Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added can and can_any methods to dpp::permission class #776

Merged
merged 13 commits into from
Aug 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions include/dpp/permissions.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,44 @@ class DPP_EXPORT permission {
*/
operator nlohmann::json() const;

/**
* @brief Check for certain permissions, taking into account administrator privileges. It uses the Bitwise AND operator
* @tparam T one or more uint64_t permission bits
* @param values The permissions (from dpp::permissions) to check for
*
* **Example:**
*
* ```cpp
* bool is_mod = permission.can(dpp::p_kick_members, dpp::p_ban_members);
* // Returns true if it has permission to p_kick_members and p_ban_members
* ```
*
* @return bool True if it has **all** the given permissions or dpp::p_administrator
*/
template <typename... T>
constexpr bool can(T... values) const noexcept {
return has(values...) || (value & p_administrator);
}

/**
* @brief Check for certain permissions, taking into account administrator privileges. It uses the Bitwise AND operator
Mishura4 marked this conversation as resolved.
Show resolved Hide resolved
* @tparam T one or more uint64_t permission bits
* @param values The permissions (from dpp::permissions) to check for
*
* **Example:**
*
* ```cpp
* bool is_mod = permission.can_any(dpp::p_kick_members, dpp::p_ban_members);
* // Returns true if it has permission to p_kick_members or p_ban_members
* ```
*
* @return bool True if it has **any** of the given permissions or dpp::p_administrator
*/
template <typename... T>
constexpr bool can_any(T... values) const noexcept {
return has_any(values...) || (value & p_administrator);
}

/**
* @brief Check for permission flags set. It uses the Bitwise AND operator
* @tparam T one or more uint64_t permission bits
Expand All @@ -139,7 +177,7 @@ class DPP_EXPORT permission {
* // Returns true if the permission bitmask contains p_kick_members and p_ban_members
* ```
*
* @return bool True if it has all the given permissions
* @return bool True if it has **all** the given permissions
*/
template <typename... T>
constexpr bool has(T... values) const noexcept {
Expand All @@ -158,7 +196,7 @@ class DPP_EXPORT permission {
* // Returns true if the permission bitmask contains p_administrator or p_ban_members
* ```
*
* @return bool True if it has any the given permissions
* @return bool True if it has **any** of the given permissions
*/
template <typename... T>
constexpr bool has_any(T... values) const noexcept {
Expand Down Expand Up @@ -187,7 +225,7 @@ class DPP_EXPORT permission {
}

/**
* @brief Assign a permission. This will reset the bitmask to the new value.
* @brief Assign permissions. This will reset the bitmask to the new value.
* @tparam T one or more uint64_t permission bits
* @param values The permissions (from dpp::permissions) to set
*
Expand Down
6 changes: 6 additions & 0 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,8 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
p.set(0).add(~uint64_t{0}).remove(dpp::p_speak).set(dpp::p_administrator);
success = !p.has(dpp::p_administrator, dpp::p_ban_members) && success; // must return false because they're not both set
success = !p.has(dpp::p_administrator | dpp::p_ban_members) && success;
success = p.can(dpp::p_ban_members) && success;
success = p.can(dpp::p_speak) && success;

constexpr auto permission_test = [](dpp::permission p) constexpr noexcept {
bool success{true};
Expand All @@ -328,6 +330,10 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
success = p.has(dpp::p_administrator | dpp::p_ban_members) && success;
success = p.add(dpp::p_speak).has(dpp::p_administrator, dpp::p_speak) && success;
success = !p.remove(dpp::p_speak).has(dpp::p_administrator, dpp::p_speak) && success;
p.remove(dpp::p_administrator);
success = p.can(dpp::p_ban_members) && success;
success = !p.can(dpp::p_speak, dpp::p_ban_members) && success;
success = p.can_any(dpp::p_speak, dpp::p_ban_members) && success;
return success;
};
constexpr auto constexpr_success = permission_test({~uint64_t{0}}); // test in constant evaluated
Expand Down
Loading