AccessControlMod
Manage roles and permissions within a diamond
- All functions are
internalfor integration into custom facets. - Utilizes the diamond storage pattern for shared state management.
- Compatible with ERC-2535 diamonds.
- No external dependencies, promoting composability.
This module provides internal functions for use in your custom facets. Import it to access shared logic and storage.
Overview
This module provides internal functions for managing role-based access control within a Compose diamond. Facets can import this module to grant, revoke, and check roles using shared diamond storage. This pattern ensures consistent permission management across all facets interacting with the same storage.
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
getStorage
Returns the storage for the AccessControl.
Returns:
| Property | Type | Description |
|---|---|---|
_s | AccessControlStorage | The storage for the AccessControl. |
grantRole
function to grant a role to an account.
Parameters:
| Property | Type | Description |
|---|---|---|
_role | bytes32 | The role to grant. |
_account | address | The account to grant the role to. |
Returns:
| Property | Type | Description |
|---|---|---|
- | bool | True if the role was granted, false otherwise. |
hasRole
function to check 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
function to check 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 assert. |
_account | address | The account to assert the role for. |
revokeRole
function to revoke a role from an account.
Parameters:
| Property | Type | Description |
|---|---|---|
_role | bytes32 | The role to revoke. |
_account | address | The account to revoke the role from. |
Returns:
| Property | Type | Description |
|---|---|---|
- | bool | True if the role was revoked, false otherwise. |
setRoleAdmin
function to set the admin role for a role.
Parameters:
| Property | Type | Description |
|---|---|---|
_role | bytes32 | The role to set the admin for. |
_adminRole | bytes32 | The admin role to set. |
Events
Errors
Best Practices
- Call
requireRoleto enforce access control checks before executing sensitive functions. - Ensure that your facet's storage layout is compatible with
AccessControlStorageto prevent collisions. - Handle the
AccessControlUnauthorizedAccounterror for predictable revert behavior.
Integration Notes
This module uses diamond storage at the STORAGE_POSITION defined by keccak256("compose.accesscontrol"). All state modifications and reads are performed against the AccessControlStorage struct within this shared storage slot. Changes made by any facet using this module are immediately visible to all other facets accessing the same storage position.