AccessControlUpgradeable.sol 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. __Context_init_unchained();
  50. __ERC165_init_unchained();
  51. __AccessControl_init_unchained();
  52. }
  53. function __AccessControl_init_unchained() internal onlyInitializing {
  54. }
  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 Modifier that checks that an account has a specific role. Reverts
  63. * with a standardized message including the required role.
  64. *
  65. * The format of the revert reason is given by the following regular expression:
  66. *
  67. * /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
  68. *
  69. * _Available since v4.1._
  70. */
  71. modifier onlyRole(bytes32 role) {
  72. _checkRole(role, _msgSender());
  73. _;
  74. }
  75. /**
  76. * @dev See {IERC165-supportsInterface}.
  77. */
  78. function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
  79. return interfaceId == type(IAccessControlUpgradeable).interfaceId || super.supportsInterface(interfaceId);
  80. }
  81. /**
  82. * @dev Returns `true` if `account` has been granted `role`.
  83. */
  84. function hasRole(bytes32 role, address account) public view virtual override returns (bool) {
  85. return _roles[role].members[account];
  86. }
  87. /**
  88. * @dev Revert with a standard message if `account` is missing `role`.
  89. *
  90. * The format of the revert reason is given by the following regular expression:
  91. *
  92. * /^AccessControl: account (0x[0-9a-f]{40}) is missing role (0x[0-9a-f]{64})$/
  93. */
  94. function _checkRole(bytes32 role, address account) internal view virtual {
  95. if (!hasRole(role, account)) {
  96. revert(
  97. string(
  98. abi.encodePacked(
  99. "AccessControl: account ",
  100. StringsUpgradeable.toHexString(uint160(account), 20),
  101. " is missing role ",
  102. StringsUpgradeable.toHexString(uint256(role), 32)
  103. )
  104. )
  105. );
  106. }
  107. }
  108. /**
  109. * @dev Returns the admin role that controls `role`. See {grantRole} and
  110. * {revokeRole}.
  111. *
  112. * To change a role's admin, use {_setRoleAdmin}.
  113. */
  114. function getRoleAdmin(bytes32 role) public view virtual override returns (bytes32) {
  115. return _roles[role].adminRole;
  116. }
  117. /**
  118. * @dev Grants `role` to `account`.
  119. *
  120. * If `account` had not been already granted `role`, emits a {RoleGranted}
  121. * event.
  122. *
  123. * Requirements:
  124. *
  125. * - the caller must have ``role``'s admin role.
  126. */
  127. function grantRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
  128. _grantRole(role, account);
  129. }
  130. /**
  131. * @dev Revokes `role` from `account`.
  132. *
  133. * If `account` had been granted `role`, emits a {RoleRevoked} event.
  134. *
  135. * Requirements:
  136. *
  137. * - the caller must have ``role``'s admin role.
  138. */
  139. function revokeRole(bytes32 role, address account) public virtual override onlyRole(getRoleAdmin(role)) {
  140. _revokeRole(role, account);
  141. }
  142. /**
  143. * @dev Revokes `role` from the calling account.
  144. *
  145. * Roles are often managed via {grantRole} and {revokeRole}: this function's
  146. * purpose is to provide a mechanism for accounts to lose their privileges
  147. * if they are compromised (such as when a trusted device is misplaced).
  148. *
  149. * If the calling account had been revoked `role`, emits a {RoleRevoked}
  150. * event.
  151. *
  152. * Requirements:
  153. *
  154. * - the caller must be `account`.
  155. */
  156. function renounceRole(bytes32 role, address account) public virtual override {
  157. require(account == _msgSender(), "AccessControl: can only renounce roles for self");
  158. _revokeRole(role, account);
  159. }
  160. /**
  161. * @dev Grants `role` to `account`.
  162. *
  163. * If `account` had not been already granted `role`, emits a {RoleGranted}
  164. * event. Note that unlike {grantRole}, this function doesn't perform any
  165. * checks on the calling account.
  166. *
  167. * [WARNING]
  168. * ====
  169. * This function should only be called from the constructor when setting
  170. * up the initial roles for the system.
  171. *
  172. * Using this function in any other way is effectively circumventing the admin
  173. * system imposed by {AccessControl}.
  174. * ====
  175. *
  176. * NOTE: This function is deprecated in favor of {_grantRole}.
  177. */
  178. function _setupRole(bytes32 role, address account) internal virtual {
  179. _grantRole(role, account);
  180. }
  181. /**
  182. * @dev Sets `adminRole` as ``role``'s admin role.
  183. *
  184. * Emits a {RoleAdminChanged} event.
  185. */
  186. function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
  187. bytes32 previousAdminRole = getRoleAdmin(role);
  188. _roles[role].adminRole = adminRole;
  189. emit RoleAdminChanged(role, previousAdminRole, adminRole);
  190. }
  191. /**
  192. * @dev Grants `role` to `account`.
  193. *
  194. * Internal function without access restriction.
  195. */
  196. function _grantRole(bytes32 role, address account) internal virtual {
  197. if (!hasRole(role, account)) {
  198. _roles[role].members[account] = true;
  199. emit RoleGranted(role, account, _msgSender());
  200. }
  201. }
  202. /**
  203. * @dev Revokes `role` from `account`.
  204. *
  205. * Internal function without access restriction.
  206. */
  207. function _revokeRole(bytes32 role, address account) internal virtual {
  208. if (hasRole(role, account)) {
  209. _roles[role].members[account] = false;
  210. emit RoleRevoked(role, account, _msgSender());
  211. }
  212. }
  213. uint256[49] private __gap;
  214. }