feat: added isInAnyRole, isInAllRoles

This commit is contained in:
The3D 2021-05-20 11:38:22 +02:00
parent 1ad9275cbd
commit 088d2733cf
2 changed files with 50 additions and 34 deletions

View File

@ -52,4 +52,19 @@ interface IPermissionManager {
**/ **/
function isPermissionsAdmin(address account) external view returns (bool); function isPermissionsAdmin(address account) external view returns (bool);
/**
* @dev Used to query if a certain account satisfies certain roles
* @param account The address of the user
* @param roles The roles to check
* @return True if the account has all the roles, false otherwise
**/
function isInAllRoles(address account, uint256[] calldata roles) external view returns (bool);
/**
* @dev Used to query if a certain account is in at least one of the roles specified
* @param account The address of the user
* @return True if the account has all the roles, false otherwise
**/
function isInAnyRole(address account, uint256[] calldata roles) external view returns (bool);
} }

View File

@ -20,10 +20,8 @@ contract PermissionManager is IPermissionManager, Ownable {
_; _;
} }
/** ///@inheritdoc IPermissionManager
* @dev Allows owner to add new permission admins
* @param users The addresses of the users to promote to permission admin
**/
function addPermissionAdmins(address[] calldata users) external override onlyOwner { function addPermissionAdmins(address[] calldata users) external override onlyOwner {
for (uint256 i = 0; i < users.length; i++) { for (uint256 i = 0; i < users.length; i++) {
_permissionsAdmins[users[i]] = 1; _permissionsAdmins[users[i]] = 1;
@ -32,10 +30,8 @@ contract PermissionManager is IPermissionManager, Ownable {
} }
} }
/** ///@inheritdoc IPermissionManager
* @dev Allows owner to remove permission admins
* @param users The addresses of the users to demote as permission admin
**/
function removePermissionAdmins(address[] calldata users) external override onlyOwner { function removePermissionAdmins(address[] calldata users) external override onlyOwner {
for (uint256 i = 0; i < users.length; i++) { for (uint256 i = 0; i < users.length; i++) {
_permissionsAdmins[users[i]] = 0; _permissionsAdmins[users[i]] = 0;
@ -44,11 +40,8 @@ contract PermissionManager is IPermissionManager, Ownable {
} }
} }
/** ///@inheritdoc IPermissionManager
* @dev Allows permission admins to whitelist a set of addresses for multiple roles
* @param roles The list of roles to assign to the different users
* @param users The addresses of the users to assign to the corresponding role
**/
function addPermissions(uint256[] calldata roles, address[] calldata users) function addPermissions(uint256[] calldata roles, address[] calldata users)
external external
override override
@ -68,11 +61,8 @@ contract PermissionManager is IPermissionManager, Ownable {
} }
} }
/** ///@inheritdoc IPermissionManager
* @dev Allows owner to remove permissions on a set of addresses for multiple roles
* @param roles The list of roles
* @param users The addresses of the users
**/
function removePermissions(uint256[] calldata roles, address[] calldata users) function removePermissions(uint256[] calldata roles, address[] calldata users)
external external
override override
@ -91,11 +81,8 @@ contract PermissionManager is IPermissionManager, Ownable {
} }
} }
/** ///@inheritdoc IPermissionManager
* @dev Returns the permissions configuration for a specific account
* @param account The address of the user
* @return the set of permissions states for the account
**/
function getAccountPermissions(address account) function getAccountPermissions(address account)
external external
view view
@ -116,20 +103,34 @@ contract PermissionManager is IPermissionManager, Ownable {
return (roles, rolesCount); return (roles, rolesCount);
} }
/** ///@inheritdoc IPermissionManager
* @dev Used to query if a certain account is a depositor function isInRole(address account, uint256 role) external view override returns (bool) {
* @param account The address of the user
* @return True if the account is a depositor, false otherwise
**/
function isInRole(address account, uint256 role) public view override returns (bool) {
return (_permissions[account] >> role) & 1 > 0; return (_permissions[account] >> role) & 1 > 0;
} }
/** ///@inheritdoc IPermissionManager
* @dev Used to query if a certain account is a depositor function isInAllRoles(address account, uint256[] calldata roles) external view override returns (bool) {
* @param account The address of the user
* @return True if the account is a depositor, false otherwise for(uint256 i=0; i<roles.length; i++){
**/ if((_permissions[account] >> roles[i]) & 1 == 0){
return false;
}
}
return true;
}
///@inheritdoc IPermissionManager
function isInAnyRole(address account, uint256[] calldata roles) public view override returns (bool) {
for(uint256 i=0; i<roles.length; i++){
if((_permissions[account] >> roles[i]) & 1 > 0){
return true;
}
}
return false;
}
///@inheritdoc IPermissionManager
function isPermissionsAdmin(address account) public view override returns (bool) { function isPermissionsAdmin(address account) public view override returns (bool) {
return _permissionsAdmins[account] > 0; return _permissionsAdmins[account] > 0;
} }