Skip to main content

AccessControlFacet

Role-based access control for diamonds

Key Features
  • Manages roles and account permissions within a diamond.
  • Supports role hierarchy via setRoleAdmin.
  • Provides grantRole, revokeRole, grantRoleBatch, and revokeRoleBatch for flexible permission management.
  • Includes hasRole and requireRole for 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

Definition
struct AccessControlStorage {
mapping(address account => mapping(bytes32 role => bool hasRole)) hasRole;
mapping(bytes32 role => bytes32 adminRole) adminRole;
}

State Variables

PropertyTypeDescription
STORAGE_POSITIONbytes32Diamond storage slot position for this module (Value: keccak256("compose.accesscontrol"))
DEFAULT_ADMIN_ROLEbytes32Default administrative role identifier (bytes32(0)) (Value: 0x00)

Functions

hasRole

Returns if an account has a role.

function hasRole(bytes32 _role, address _account) external view returns (bool);

Parameters:

PropertyTypeDescription
_rolebytes32The role to check.
_accountaddressThe account to check the role for.

Returns:

PropertyTypeDescription
-boolTrue 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.

function requireRole(bytes32 _role, address _account) external view;

Parameters:

PropertyTypeDescription
_rolebytes32The role to check.
_accountaddressThe account to check the role for.

getRoleAdmin

Returns the admin role for a role.

function getRoleAdmin(bytes32 _role) external view returns (bytes32);

Parameters:

PropertyTypeDescription
_rolebytes32The role to get the admin for.

Returns:

PropertyTypeDescription
-bytes32The 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.

function setRoleAdmin(bytes32 _role, bytes32 _adminRole) external;

Parameters:

PropertyTypeDescription
_rolebytes32The role to set the admin for.
_adminRolebytes32The 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.

function grantRole(bytes32 _role, address _account) external;

Parameters:

PropertyTypeDescription
_rolebytes32The role to grant.
_accountaddressThe 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.

function revokeRole(bytes32 _role, address _account) external;

Parameters:

PropertyTypeDescription
_rolebytes32The role to revoke.
_accountaddressThe 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.

function grantRoleBatch(bytes32 _role, address[] calldata _accounts) external;

Parameters:

PropertyTypeDescription
_rolebytes32The role to grant.
_accountsaddress[]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.

function revokeRoleBatch(bytes32 _role, address[] calldata _accounts) external;

Parameters:

PropertyTypeDescription
_rolebytes32The role to revoke.
_accountsaddress[]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.

function renounceRole(bytes32 _role, address _account) external;

Parameters:

PropertyTypeDescription
_rolebytes32The role to renounce.
_accountaddressThe account to renounce the role from.

Events

Errors

Best Practices

Best Practice
  • Initialize roles and their admin roles during diamond deployment.
  • Use grantRole and revokeRole for individual permission changes.
  • Leverage grantRoleBatch and revokeRoleBatch for efficient bulk operations.
  • Ensure the caller has the necessary admin role before granting or revoking roles.

Security Considerations

Security

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.

Was this helpful?
Last updated: