AccessControl.sol 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../utils/Context.sol";
  4. import "../utils/Strings.sol";
  5. import "../utils/introspection/ERC165.sol";
  6. /**
  7. * @dev External interface of AccessControl declared to support ERC165 detection.
  8. */
  9. interface IAccessControl {
  10. function hasRole(bytes32 role, address account) external view returns (bool);
  11. function getRoleAdmin(bytes32 role) external view returns (bytes32);
  12. function grantRole(bytes32 role, address account) external;
  13. function revokeRole(bytes32 role, address account) external;
  14. function renounceRole(bytes32 role, address account) external;
  15. }
  16. /**
  17. * @dev Contract module that allows children to implement role-based access
  18. * control mechanisms. This is a lightweight version that doesn't allow enumerating role
  19. * members except through off-chain means by accessing the contract event logs. Some
  20. * applications may benefit from on-chain enumerability, for those cases see
  21. * {AccessControlEnumerable}.
  22. *
  23. * Roles are referred to by their `bytes32` identifier. These should be exposed
  24. * in the external API and be unique. The best way to achieve this is by
  25. * using `public constant` hash digests:
  26. *
  27. * ```
  28. * bytes32 public constant MY_ROLE = keccak256("MY_ROLE");
  29. * ```
  30. *
  31. * Roles can be used to represent a set of permissions. To restrict access to a
  32. * function call, use {hasRole}:
  33. *
  34. * ```
  35. * function foo() public {
  36. * require(hasRole(MY_ROLE, msg.sender));
  37. * ...
  38. * }
  39. * ```
  40. *
  41. * Roles can be granted and revoked dynamically via the {grantRole} and
  42. * {revokeRole} functions. Each role has an associated admin role, and only
  43. * accounts that have a role's admin role can call {grantRole} and {revokeRole}.
  44. *
  45. * By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means
  46. * that only accounts with this role will be able to grant or revoke other
  47. * roles. More complex role relationships can be created by using
  48. * {_setRoleAdmin}.
  49. *
  50. * WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to
  51. * grant and revoke this role. Extra precautions should be taken to secure
  52. * accounts that have been granted it.
  53. */
  54. abstract contract AccessControl is Context, IAccessControl, ERC165 {
  55. struct RoleData {
  56. mapping(address => bool) members;
  57. bytes32 adminRole;
  58. }
  59. mapping(bytes32 => RoleData) private _roles;
  60. bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
  61. /**
  62. * @dev Emitted when `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole`
  63. *
  64. * `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite
  65. * {RoleAdminChanged} not being emitted signaling this.
  66. *
  67. * _Available since v3.1._
  68. */
  69. event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);
  70. /**
  71. * @dev Emitted when `account` is granted `role`.
  72. *
  73. * `sender` is the account that originated the contract call, an admin role
  74. * bearer except when using {_setupRole}.
  75. */
  76. event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
  77. /**
  78. * @dev Emitted when `account` is revoked `role`.
  79. *
  80. * `sender` is the account that originated the contract call:
  81. * - if using `revokeRole`, it is the admin role bearer
  82. * - if using `renounceRole`, it is the role bearer (i.e. `account`)
  83. */
  84. event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
  85. /**
  86. * @dev Modifier that checks that an account has a specific role. Reverts
  87. * with a standardized message including the required role.
  88. *
  89. * The format of the revert reason is given by the following regular expression:
  90. *
  91. * /^AccessControl: account (0x[0-9a-f]{20}) is missing role (0x[0-9a-f]{32})$/
  92. *
  93. * _Available since v4.1._
  94. */
  95. modifier onlyRole(bytes32 role) {
  96. _checkRole(role, _msgSender());
  97. _;
  98. }
  99. /**
  100. * @dev See {IERC165-supportsInterface}.
  101. */
  102. function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
  103. return interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId);
  104. }
  105. /**
  106. * @dev Returns `true` if `account` has been granted `role`.
  107. */
  108. function hasRole(bytes32 role, address account) public view override returns (bool) {
  109. return _roles[role].members[account];
  110. }
  111. /**
  112. * @dev Revert with a standard message if `account` is missing `role`.
  113. *
  114. * The format of the revert reason is given by the following regular expression:
  115. *
  116. * /^AccessControl: account (0x[0-9a-f]{20}) is missing role (0x[0-9a-f]{32})$/
  117. */
  118. function _checkRole(bytes32 role, address account) internal view {
  119. if (!hasRole(role, account)) {
  120. revert(
  121. string(
  122. abi.encodePacked(
  123. "AccessControl: account ",
  124. Strings.toHexString(uint160(account), 20),
  125. " is missing role ",
  126. Strings.toHexString(uint256(role), 32)
  127. )
  128. )
  129. );
  130. }
  131. }
  132. /**
  133. * @dev Returns the admin role that controls `role`. See {grantRole} and
  134. * {revokeRole}.
  135. *
  136. * To change a role's admin, use {_setRoleAdmin}.
  137. */
  138. function getRoleAdmin(bytes32 role) public view override returns (bytes32) {
  139. return _roles[role].adminRole;
  140. }
  141. /**
  142. * @dev Grants `role` to `account`.
  143. *
  144. * If `account` had not been already granted `role`, emits a {RoleGranted}
  145. * event.
  146. *
  147. * Requirements:
  148. *
  149. * - the caller must have ``role``'s admin role.
  150. */
  151. function grantRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
  152. _grantRole(role, account);
  153. }
  154. /**
  155. * @dev Revokes `role` from `account`.
  156. *
  157. * If `account` had been granted `role`, emits a {RoleRevoked} event.
  158. *
  159. * Requirements:
  160. *
  161. * - the caller must have ``role``'s admin role.
  162. */
  163. function revokeRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
  164. _revokeRole(role, account);
  165. }
  166. /**
  167. * @dev Revokes `role` from the calling account.
  168. *
  169. * Roles are often managed via {grantRole} and {revokeRole}: this function's
  170. * purpose is to provide a mechanism for accounts to lose their privileges
  171. * if they are compromised (such as when a trusted device is misplaced).
  172. *
  173. * If the calling account had been granted `role`, emits a {RoleRevoked}
  174. * event.
  175. *
  176. * Requirements:
  177. *
  178. * - the caller must be `account`.
  179. */
  180. function renounceRole(bytes32 role, address account) public virtual override {
  181. require(account == _msgSender(), "AccessControl: can only renounce roles for self");
  182. _revokeRole(role, account);
  183. }
  184. /**
  185. * @dev Grants `role` to `account`.
  186. *
  187. * If `account` had not been already granted `role`, emits a {RoleGranted}
  188. * event. Note that unlike {grantRole}, this function doesn't perform any
  189. * checks on the calling account.
  190. *
  191. * [WARNING]
  192. * ====
  193. * This function should only be called from the constructor when setting
  194. * up the initial roles for the system.
  195. *
  196. * Using this function in any other way is effectively circumventing the admin
  197. * system imposed by {AccessControl}.
  198. * ====
  199. */
  200. function _setupRole(bytes32 role, address account) internal virtual {
  201. _grantRole(role, account);
  202. }
  203. /**
  204. * @dev Sets `adminRole` as ``role``'s admin role.
  205. *
  206. * Emits a {RoleAdminChanged} event.
  207. */
  208. function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
  209. emit RoleAdminChanged(role, getRoleAdmin(role), adminRole);
  210. _roles[role].adminRole = adminRole;
  211. }
  212. function _grantRole(bytes32 role, address account) private {
  213. if (!hasRole(role, account)) {
  214. _roles[role].members[account] = true;
  215. emit RoleGranted(role, account, _msgSender());
  216. }
  217. }
  218. function _revokeRole(bytes32 role, address account) private {
  219. if (hasRole(role, account)) {
  220. _roles[role].members[account] = false;
  221. emit RoleRevoked(role, account, _msgSender());
  222. }
  223. }
  224. }