AccessControl.sol 8.2 KB

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