AccessControlFacet
Role-based access control for diamonds
- Manages roles and account permissions within a diamond.
- Supports role hierarchy via
setRoleAdmin. - Provides
grantRole,revokeRole,grantRoleBatch, andrevokeRoleBatchfor flexible permission management. - Includes
hasRoleandrequireRolefor permission checks.
Overview
This facet implements role-based access control for Compose diamonds. It exposes functions to manage roles, grant and revoke permissions, and check account access. Developers integrate this facet to enforce authorization policies within their diamond architecture, ensuring only authorized accounts can perform specific actions.
Storage
AccessControlStorage
State Variables
| Property | Type | Description |
|---|---|---|
STORAGE_POSITION | bytes32 | Diamond storage slot position for this module (Value: keccak256("compose.accesscontrol")) |
DEFAULT_ADMIN_ROLE | bytes32 | Default administrative role identifier (bytes32(0)) (Value: 0x00) |
Functions
hasRole
Returns if an account has a role.
Parameters:
| Property | Type | Description |
|---|---|---|
_role | bytes32 | The role to check. |
_account | address | The account to check the role for. |
Returns:
| Property | Type | Description |
|---|---|---|
- | bool | True if the account has the role, false otherwise. |
requireRole
Checks if an account has a required role. Reverts with AccessControlUnauthorizedAccount If the account does not have the role.
Parameters:
| Property | Type | Description |
|---|---|---|
_role | bytes32 | The role to check. |
_account | address | The account to check the role for. |
getRoleAdmin
Returns the admin role for a role.
Parameters:
| Property | Type | Description |
|---|---|---|
_role | bytes32 | The role to get the admin for. |
Returns:
| Property | Type | Description |
|---|---|---|
- | bytes32 | The admin role for the role. |
setRoleAdmin
Sets the admin role for a role. Emits a RoleAdminChanged event. Reverts with AccessControlUnauthorizedAccount If the caller is not the current admin of the role.
Parameters:
| Property | Type | Description |
|---|---|---|
_role | bytes32 | The role to set the admin for. |
_adminRole | bytes32 | The new admin role to set. |
grantRole
Grants a role to an account. Emits a RoleGranted event. Reverts with AccessControlUnauthorizedAccount If the caller is not the admin of the role.
Parameters:
| Property | Type | Description |
|---|---|---|
_role | bytes32 | The role to grant. |
_account | address | The account to grant the role to. |
revokeRole
Revokes a role from an account. Emits a RoleRevoked event. Reverts with AccessControlUnauthorizedAccount If the caller is not the admin of the role.
Parameters:
| Property | Type | Description |
|---|---|---|
_role | bytes32 | The role to revoke. |
_account | address | The account to revoke the role from. |
grantRoleBatch
Grants a role to multiple accounts in a single transaction. Emits a RoleGranted event for each newly granted account. Reverts with AccessControlUnauthorizedAccount If the caller is not the admin of the role.
Parameters:
| Property | Type | Description |
|---|---|---|
_role | bytes32 | The role to grant. |
_accounts | address[] | The accounts to grant the role to. |
revokeRoleBatch
Revokes a role from multiple accounts in a single transaction. Emits a RoleRevoked event for each account the role is revoked from. Reverts with AccessControlUnauthorizedAccount If the caller is not the admin of the role.
Parameters:
| Property | Type | Description |
|---|---|---|
_role | bytes32 | The role to revoke. |
_accounts | address[] | The accounts to revoke the role from. |
renounceRole
Renounces a role from the caller. Emits a RoleRevoked event. Reverts with AccessControlUnauthorizedSender If the caller is not the account to renounce the role from.
Parameters:
| Property | Type | Description |
|---|---|---|
_role | bytes32 | The role to renounce. |
_account | address | The account to renounce the role from. |
Events
Errors
Best Practices
- Initialize roles and their admin roles during diamond deployment.
- Use
grantRoleandrevokeRolefor individual permission changes. - Leverage
grantRoleBatchandrevokeRoleBatchfor efficient bulk operations. - Ensure the caller has the necessary admin role before granting or revoking roles.
Security Considerations
All state-changing functions (setRoleAdmin, grantRole, revokeRole, grantRoleBatch, revokeRoleBatch, renounceRole) must be protected by appropriate access control mechanisms, typically enforced by the caller's role. The renounceRole function should only be callable by the account whose role is being renounced. Input validation for account addresses and role bytes is critical.