access_control
Deployed program (coming soon)
Access control, that is, "who is allowed to do this thing", is incredibly important in the world of decentralized programs. The access control of your program may govern who can mint tokens, vote on proposals, freeze transfers, and many other things. It is therefore critical to understand how you implement it, lest someone else steals your whole system. We implement access control in a way that is both secure and flexible, which is called role-based access control (RBAC).
Role-based access control is a policy-neutral access control mechanism defined around roles and privileges. In essence, we will be defining multiple roles, each allowed to perform different sets of actions. An account may have, for example, 'moderator', 'minter' or 'admin' roles, which you will then check for. This check can be enforced through the only_role
or check_role
transition. Separately, you will be able to define rules for how accounts can be granted a role, have it revoked, and more.
Every role has an associated admin role, which grants permission to call the grant_role
and revoke_role
transitions. A role can be granted or revoked by using these if the calling account has the corresponding admin role. Multiple roles may have the same admin role to make management easier. A role’s admin can even be the same role itself, which would cause accounts with that role to be able to also grant and revoke it.
This mechanism can be used to create complex permissioning structures resembling organizational charts, but it also provides an easy way to manage simpler applications. The program includes a special role, called DEFAULT_ADMIN_ROLE
(with value 0u8
), which acts as the default admin role for all roles. An account with this role will be able to manage any other role, unless set_role_admin
is used to select a new admin role.
Mappings, Structs and Records
All the mappings can be read publicly, either by the user or by the programs.
grants
Returns whether the account has the role. The key is the BHP256 hash of the grant
struct instance.
role_admins
Returns the admin role of the role. Role is represented by a u8
number.
initialized
Returns whether the mapping has been initialized. The only valid key is 0u8
.
Transitions
only_role
Check whether the caller has the role. If not, the transition will fail.
check_role
Check whether the account has the role. If not, the transition will fail.
check_role_admin
Check whether the account
account has the admin role of the role
role.
set_role_admin
Set admin_role
as the admin role of the role
role. The caller must have the DEFAULT_ADMIN_ROLE
role.
grant_role
Grant the role
role to the account
account.
revoke_role
Revoke the role
role from the account
account.
renounce_role
Renounce the role
role from the account
account. The account
must be the caller itself. That is, only the caller can renounce its own role.
Admin Transitions
The following transitions can only be called by the admin. The admin should be an administrator program, which is managed by the Spectre Governance.
initialize
Initializes the access control program. It will grant the DEFAULT_ADMIN_ROLE
role to the caller. Actually, the caller address must be the same as the constant INITIAL_ADMIN
written in the program before deployment.
Last updated