AccessControlUpgradeable.sol 7.9 KB

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