AccessControl.sol 7.2 KB

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