Skip to main content

Access Control Data Facet

Manages roles and permissions within a diamond

Key Features
  • Exposes external view functions for role checks.
  • Utilizes diamond storage for role data.
  • Includes a custom error AccessControlUnauthorizedAccount for failed role checks.
  • Provides exportSelectors to identify its exposed functions.

Overview

This facet provides core access control data and validation functions for a diamond. It exposes external view functions to check role assignments and role hierarchies, enabling other facets or off-chain applications to query permissions. It accesses shared diamond storage to retrieve role information.

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"))

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.

exportSelectors

Exports the selectors that are exposed by the facet.

function exportSelectors() external pure returns (bytes memory);

Returns:

PropertyTypeDescription
-bytesSelectors that are exported by the facet.

Errors

Best Practices

Best Practice
  • Query role information using the external view functions exposed by the diamond.
  • Use requireRole to enforce access control within other facets before executing sensitive operations.
  • Understand the role hierarchy by calling getRoleAdmin.

Security Considerations

Security

The hasRole, requireRole, and getRoleAdmin functions are view functions and do not pose reentrancy risks. requireRole reverts with AccessControlUnauthorizedAccount if the specified account does not possess the required role, providing input validation for access control checks. Developers should ensure correct role assignments in diamond initialization.

Was this helpful?
Last updated:

Newsletter

Get notified about releases, feature announcements, and technical deep-dives on building smart contracts with Compose.

No spam. Unsubscribe anytime.