OwnableAccessControl

Git Source

Inherits: Ownable

Author: transientlabs.xyz

Single owner, flexible access control mechanics

Can easily be extended by inheriting and applying additional roles

By default, only the owner can grant roles but by inheriting, but you may allow other roles to grant roles by using the internal helper.

State Variables

_c

uint256 private _c;

_roleStatus

mapping(uint256 => mapping(bytes32 => mapping(address => bool))) private _roleStatus;

_roleMembers

mapping(uint256 => mapping(bytes32 => EnumerableSet.AddressSet)) private _roleMembers;

Functions

onlyRole

modifier onlyRole(bytes32 role);

onlyRoleOrOwner

modifier onlyRoleOrOwner(bytes32 role);

constructor

constructor() Ownable(msg.sender);

revokeAllRoles

Function to revoke all roles currently present

Increments the _c variables

Requires owner privileges

function revokeAllRoles() external onlyOwner;

renounceRole

Function to renounce role

function renounceRole(bytes32 role) external;

Parameters

NameTypeDescription
rolebytes32Bytes32 role created in inheriting contracts

setRole

Function to grant/revoke a role to an address

Requires owner to call this function but this may be further extended using the internal helper function in inheriting contracts

function setRole(bytes32 role, address[] memory roleMembers, bool status) external onlyOwner;

Parameters

NameTypeDescription
rolebytes32Bytes32 role created in inheriting contracts
roleMembersaddress[]List of addresses that should have roles attached to them based on status
statusboolBool whether to remove or add roleMembers to the role

hasRole

Function to see if an address is the owner

function hasRole(bytes32 role, address potentialRoleMember) public view returns (bool);

Parameters

NameTypeDescription
rolebytes32Bytes32 role created in inheriting contracts
potentialRoleMemberaddressAddress to check for role membership

getRoleMembers

Function to get role members

function getRoleMembers(bytes32 role) public view returns (address[] memory);

Parameters

NameTypeDescription
rolebytes32Bytes32 role created in inheriting contracts

_setRole

Helper function to set addresses for a role

function _setRole(bytes32 role, address[] memory roleMembers, bool status) internal;

Parameters

NameTypeDescription
rolebytes32Bytes32 role created in inheriting contracts
roleMembersaddress[]List of addresses that should have roles attached to them based on status
statusboolBool whether to remove or add roleMembers to the role

Events

RoleChange

event RoleChange(address indexed from, address indexed user, bool indexed approved, bytes32 role);

AllRolesRevoked

event AllRolesRevoked(address indexed from);

Errors

NotSpecifiedRole

Does not have specified role

error NotSpecifiedRole(bytes32 role);

NotRoleOrOwner

Is not specified role or owner

error NotRoleOrOwner(bytes32 role);