AccessControl.sol 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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
  104. || super.supportsInterface(interfaceId);
  105. }
  106. /**
  107. * @dev Returns `true` if `account` has been granted `role`.
  108. */
  109. function hasRole(bytes32 role, address account) public view override returns (bool) {
  110. return _roles[role].members[account];
  111. }
  112. /**
  113. * @dev Revert with a standard message if `account` is missing `role`.
  114. *
  115. * The format of the revert reason is given by the following regular expression:
  116. *
  117. * /^AccessControl: account (0x[0-9a-f]{20}) is missing role (0x[0-9a-f]{32})$/
  118. */
  119. function _checkRole(bytes32 role, address account) internal view {
  120. if(!hasRole(role, account)) {
  121. revert(string(abi.encodePacked(
  122. "AccessControl: account ",
  123. Strings.toHexString(uint160(account), 20),
  124. " is missing role ",
  125. Strings.toHexString(uint256(role), 32)
  126. )));
  127. }
  128. }
  129. /**
  130. * @dev Returns the admin role that controls `role`. See {grantRole} and
  131. * {revokeRole}.
  132. *
  133. * To change a role's admin, use {_setRoleAdmin}.
  134. */
  135. function getRoleAdmin(bytes32 role) public view override returns (bytes32) {
  136. return _roles[role].adminRole;
  137. }
  138. /**
  139. * @dev Grants `role` to `account`.
  140. *
  141. * If `account` had not been already granted `role`, emits a {RoleGranted}
  142. * event.
  143. *
  144. * Requirements:
  145. *
  146. * - the caller must have ``role``'s admin role.
  147. */
  148. function grantRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
  149. _grantRole(role, account);
  150. }
  151. /**
  152. * @dev Revokes `role` from `account`.
  153. *
  154. * If `account` had been granted `role`, emits a {RoleRevoked} event.
  155. *
  156. * Requirements:
  157. *
  158. * - the caller must have ``role``'s admin role.
  159. */
  160. function revokeRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
  161. _revokeRole(role, account);
  162. }
  163. /**
  164. * @dev Revokes `role` from the calling account.
  165. *
  166. * Roles are often managed via {grantRole} and {revokeRole}: this function's
  167. * purpose is to provide a mechanism for accounts to lose their privileges
  168. * if they are compromised (such as when a trusted device is misplaced).
  169. *
  170. * If the calling account had been granted `role`, emits a {RoleRevoked}
  171. * event.
  172. *
  173. * Requirements:
  174. *
  175. * - the caller must be `account`.
  176. */
  177. function renounceRole(bytes32 role, address account) public virtual override {
  178. require(account == _msgSender(), "AccessControl: can only renounce roles for self");
  179. _revokeRole(role, account);
  180. }
  181. /**
  182. * @dev Grants `role` to `account`.
  183. *
  184. * If `account` had not been already granted `role`, emits a {RoleGranted}
  185. * event. Note that unlike {grantRole}, this function doesn't perform any
  186. * checks on the calling account.
  187. *
  188. * [WARNING]
  189. * ====
  190. * This function should only be called from the constructor when setting
  191. * up the initial roles for the system.
  192. *
  193. * Using this function in any other way is effectively circumventing the admin
  194. * system imposed by {AccessControl}.
  195. * ====
  196. */
  197. function _setupRole(bytes32 role, address account) internal virtual {
  198. _grantRole(role, account);
  199. }
  200. /**
  201. * @dev Sets `adminRole` as ``role``'s admin role.
  202. *
  203. * Emits a {RoleAdminChanged} event.
  204. */
  205. function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
  206. emit RoleAdminChanged(role, getRoleAdmin(role), adminRole);
  207. _roles[role].adminRole = adminRole;
  208. }
  209. function _grantRole(bytes32 role, address account) private {
  210. if (!hasRole(role, account)) {
  211. _roles[role].members[account] = true;
  212. emit RoleGranted(role, account, _msgSender());
  213. }
  214. }
  215. function _revokeRole(bytes32 role, address account) private {
  216. if (hasRole(role, account)) {
  217. _roles[role].members[account] = false;
  218. emit RoleRevoked(role, account, _msgSender());
  219. }
  220. }
  221. }