Skip to content

Commit

Permalink
Add checkOwnerOrRole in EnumerableRoles
Browse files Browse the repository at this point in the history
  • Loading branch information
Vectorized committed Dec 9, 2024
1 parent 9339f25 commit 65e17b2
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/auth/EnumerableRoles.sol
Original file line number Diff line number Diff line change
Expand Up @@ -244,17 +244,23 @@ abstract contract EnumerableRoles {
}
}

/// @dev Throws if the sender does not have `role`.
/// @dev Reverts if `msg.sender` does not have `role`.
function _checkRole(uint256 role) internal view virtual {
if (!_hasRole(msg.sender, role)) _revertEnumerableRolesUnauthorized();
}

/// @dev Throws if the sender does not have any roles in `encodedRoles`.
/// @dev Reverts if `msg.sender` does not have any role in `encodedRoles`.
function _checkRoles(bytes memory encodedRoles) internal view virtual {
if (!_hasAnyRoles(msg.sender, encodedRoles)) _revertEnumerableRolesUnauthorized();
}

/// @dev Throws if the sender does not have any roles in `encodedRoles`.
/// @dev Reverts if `msg.sender` is not the contract owner and does not have `role`.
function _checkOwnerOrRole(uint256 role) internal view virtual {
if (!_senderIsContractOwner()) _checkRole(role);
}

/// @dev Reverts if `msg.sender` is not the contract owner and
/// does not have any role in `encodedRoles`.
function _checkOwnerOrRoles(bytes memory encodedRoles) internal view virtual {
if (!_senderIsContractOwner()) _checkRoles(encodedRoles);
}
Expand All @@ -276,6 +282,12 @@ abstract contract EnumerableRoles {
_;
}

/// @dev Marks a function as only callable by the owner or by an account with `role`.
modifier onlyOwnerOrRole(uint256 role) virtual {
_checkOwnerOrRole(role);
_;
}

/// @dev Marks a function as only callable by the owner or
/// by an account with any role in `encodedRoles`.
/// Checks for ownership first, then checks for roles.
Expand Down

0 comments on commit 65e17b2

Please sign in to comment.