AccessControl.sol 6.9 KB

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