AccessControl.sol 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.9.0) (access/AccessControl.sol)
  3. pragma solidity ^0.8.19;
  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. * ```solidity
  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. * ```solidity
  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. We recommend using {AccessControlDefaultAdminRules}
  45. * to enforce additional security measures for this role.
  46. */
  47. abstract contract AccessControl is Context, IAccessControl, ERC165 {
  48. struct RoleData {
  49. mapping(address => bool) members;
  50. bytes32 adminRole;
  51. }
  52. mapping(bytes32 => RoleData) private _roles;
  53. bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
  54. /**
  55. * @dev Modifier that checks that an account has a specific role. Reverts
  56. * with a standardized message including the required role.
  57. *
  58. * The format of the revert reason is given by the following regular expression:
  59. *
  60. * /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
  61. *
  62. * _Available since v4.1._
  63. */
  64. modifier onlyRole(bytes32 role) {
  65. _checkRole(role);
  66. _;
  67. }
  68. /**
  69. * @dev See {IERC165-supportsInterface}.
  70. */
  71. function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
  72. return interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId);
  73. }
  74. /**
  75. * @dev Returns `true` if `account` has been granted `role`.
  76. */
  77. function hasRole(bytes32 role, address account) public view virtual returns (bool) {
  78. return _roles[role].members[account];
  79. }
  80. /**
  81. * @dev Revert with a standard message if `_msgSender()` is missing `role`.
  82. * Overriding this function changes the behavior of the {onlyRole} modifier.
  83. *
  84. * Format of the revert message is described in {_checkRole}.
  85. *
  86. * _Available since v4.6._
  87. */
  88. function _checkRole(bytes32 role) internal view virtual {
  89. _checkRole(role, _msgSender());
  90. }
  91. /**
  92. * @dev Revert with a standard message if `account` is missing `role`.
  93. *
  94. * The format of the revert reason is given by the following regular expression:
  95. *
  96. * /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
  97. */
  98. function _checkRole(bytes32 role, address account) internal view virtual {
  99. if (!hasRole(role, account)) {
  100. revert AccessControlUnauthorizedAccount(account, role);
  101. }
  102. }
  103. /**
  104. * @dev Returns the admin role that controls `role`. See {grantRole} and
  105. * {revokeRole}.
  106. *
  107. * To change a role's admin, use {_setRoleAdmin}.
  108. */
  109. function getRoleAdmin(bytes32 role) public view virtual returns (bytes32) {
  110. return _roles[role].adminRole;
  111. }
  112. /**
  113. * @dev Grants `role` to `account`.
  114. *
  115. * If `account` had not been already granted `role`, emits a {RoleGranted}
  116. * event.
  117. *
  118. * Requirements:
  119. *
  120. * - the caller must have ``role``'s admin role.
  121. *
  122. * May emit a {RoleGranted} event.
  123. */
  124. function grantRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) {
  125. _grantRole(role, account);
  126. }
  127. /**
  128. * @dev Revokes `role` from `account`.
  129. *
  130. * If `account` had been granted `role`, emits a {RoleRevoked} event.
  131. *
  132. * Requirements:
  133. *
  134. * - the caller must have ``role``'s admin role.
  135. *
  136. * May emit a {RoleRevoked} event.
  137. */
  138. function revokeRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) {
  139. _revokeRole(role, account);
  140. }
  141. /**
  142. * @dev Revokes `role` from the calling account.
  143. *
  144. * Roles are often managed via {grantRole} and {revokeRole}: this function's
  145. * purpose is to provide a mechanism for accounts to lose their privileges
  146. * if they are compromised (such as when a trusted device is misplaced).
  147. *
  148. * If the calling account had been revoked `role`, emits a {RoleRevoked}
  149. * event.
  150. *
  151. * Requirements:
  152. *
  153. * - the caller must be `callerConfirmation`.
  154. *
  155. * May emit a {RoleRevoked} event.
  156. */
  157. function renounceRole(bytes32 role, address callerConfirmation) public virtual {
  158. if (callerConfirmation != _msgSender()) {
  159. revert AccessControlBadConfirmation();
  160. }
  161. _revokeRole(role, callerConfirmation);
  162. }
  163. /**
  164. * @dev Sets `adminRole` as ``role``'s admin role.
  165. *
  166. * Emits a {RoleAdminChanged} event.
  167. */
  168. function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
  169. bytes32 previousAdminRole = getRoleAdmin(role);
  170. _roles[role].adminRole = adminRole;
  171. emit RoleAdminChanged(role, previousAdminRole, adminRole);
  172. }
  173. /**
  174. * @dev Grants `role` to `account`.
  175. *
  176. * Internal function without access restriction.
  177. *
  178. * May emit a {RoleGranted} event.
  179. */
  180. function _grantRole(bytes32 role, address account) internal virtual {
  181. if (!hasRole(role, account)) {
  182. _roles[role].members[account] = true;
  183. emit RoleGranted(role, account, _msgSender());
  184. }
  185. }
  186. /**
  187. * @dev Revokes `role` from `account`.
  188. *
  189. * Internal function without access restriction.
  190. *
  191. * May emit a {RoleRevoked} event.
  192. */
  193. function _revokeRole(bytes32 role, address account) internal virtual {
  194. if (hasRole(role, account)) {
  195. _roles[role].members[account] = false;
  196. emit RoleRevoked(role, account, _msgSender());
  197. }
  198. }
  199. }