AccessControl.sol 7.7 KB

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