AccessControl.sol 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. import "../utils/EnumerableSet.sol";
  4. import "../utils/Address.sol";
  5. import "../GSN/Context.sol";
  6. /**
  7. * @dev Contract module that allows children to implement role-based access
  8. * control mechanisms.
  9. *
  10. * Roles are referred to by their `bytes32` identifier. These should be exposed
  11. * in the external API and be unique. The best way to achieve this is by
  12. * using `public constant` hash digests:
  13. *
  14. * ```
  15. * bytes32 public constant MY_ROLE = keccak256("MY_ROLE");
  16. * ```
  17. *
  18. * Roles can be used to represent a set of permissions. To restrict access to a
  19. * function call, use {hasRole}:
  20. *
  21. * ```
  22. * function foo() public {
  23. * require(hasRole(MY_ROLE, msg.sender));
  24. * ...
  25. * }
  26. * ```
  27. *
  28. * Roles can be granted and revoked dynamically via the {grantRole} and
  29. * {revokeRole} functions. Each role has an associated admin role, and only
  30. * accounts that have a role's admin role can call {grantRole} and {revokeRole}.
  31. *
  32. * By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means
  33. * that only accounts with this role will be able to grant or revoke other
  34. * roles. More complex role relationships can be created by using
  35. * {_setRoleAdmin}.
  36. *
  37. * WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to
  38. * grant and revoke this role. Extra precautions should be taken to secure
  39. * accounts that have been granted it.
  40. */
  41. abstract contract AccessControl is Context {
  42. using EnumerableSet for EnumerableSet.AddressSet;
  43. using Address for address;
  44. struct RoleData {
  45. EnumerableSet.AddressSet members;
  46. bytes32 adminRole;
  47. }
  48. mapping (bytes32 => RoleData) private _roles;
  49. bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;
  50. /**
  51. * @dev Emitted when `account` is granted `role`.
  52. *
  53. * `sender` is the account that originated the contract call, an admin role
  54. * bearer except when using {_setupRole}.
  55. */
  56. event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
  57. /**
  58. * @dev Emitted when `account` is revoked `role`.
  59. *
  60. * `sender` is the account that originated the contract call:
  61. * - if using `revokeRole`, it is the admin role bearer
  62. * - if using `renounceRole`, it is the role bearer (i.e. `account`)
  63. */
  64. event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
  65. /**
  66. * @dev Returns `true` if `account` has been granted `role`.
  67. */
  68. function hasRole(bytes32 role, address account) public view returns (bool) {
  69. return _roles[role].members.contains(account);
  70. }
  71. /**
  72. * @dev Returns the number of accounts that have `role`. Can be used
  73. * together with {getRoleMember} to enumerate all bearers of a role.
  74. */
  75. function getRoleMemberCount(bytes32 role) public view returns (uint256) {
  76. return _roles[role].members.length();
  77. }
  78. /**
  79. * @dev Returns one of the accounts that have `role`. `index` must be a
  80. * value between 0 and {getRoleMemberCount}, non-inclusive.
  81. *
  82. * Role bearers are not sorted in any particular way, and their ordering may
  83. * change at any point.
  84. *
  85. * WARNING: When using {getRoleMember} and {getRoleMemberCount}, make sure
  86. * you perform all queries on the same block. See the following
  87. * https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296[forum post]
  88. * for more information.
  89. */
  90. function getRoleMember(bytes32 role, uint256 index) public view returns (address) {
  91. return _roles[role].members.at(index);
  92. }
  93. /**
  94. * @dev Returns the admin role that controls `role`. See {grantRole} and
  95. * {revokeRole}.
  96. *
  97. * To change a role's admin, use {_setRoleAdmin}.
  98. */
  99. function getRoleAdmin(bytes32 role) public view returns (bytes32) {
  100. return _roles[role].adminRole;
  101. }
  102. /**
  103. * @dev Grants `role` to `account`.
  104. *
  105. * If `account` had not been already granted `role`, emits a {RoleGranted}
  106. * event.
  107. *
  108. * Requirements:
  109. *
  110. * - the caller must have ``role``'s admin role.
  111. */
  112. function grantRole(bytes32 role, address account) public virtual {
  113. require(hasRole(_roles[role].adminRole, _msgSender()), "AccessControl: sender must be an admin to grant");
  114. _grantRole(role, account);
  115. }
  116. /**
  117. * @dev Revokes `role` from `account`.
  118. *
  119. * If `account` had been granted `role`, emits a {RoleRevoked} event.
  120. *
  121. * Requirements:
  122. *
  123. * - the caller must have ``role``'s admin role.
  124. */
  125. function revokeRole(bytes32 role, address account) public virtual {
  126. require(hasRole(_roles[role].adminRole, _msgSender()), "AccessControl: sender must be an admin to revoke");
  127. _revokeRole(role, account);
  128. }
  129. /**
  130. * @dev Revokes `role` from the calling account.
  131. *
  132. * Roles are often managed via {grantRole} and {revokeRole}: this function's
  133. * purpose is to provide a mechanism for accounts to lose their privileges
  134. * if they are compromised (such as when a trusted device is misplaced).
  135. *
  136. * If the calling account had been granted `role`, emits a {RoleRevoked}
  137. * event.
  138. *
  139. * Requirements:
  140. *
  141. * - the caller must be `account`.
  142. */
  143. function renounceRole(bytes32 role, address account) public virtual {
  144. require(account == _msgSender(), "AccessControl: can only renounce roles for self");
  145. _revokeRole(role, account);
  146. }
  147. /**
  148. * @dev Grants `role` to `account`.
  149. *
  150. * If `account` had not been already granted `role`, emits a {RoleGranted}
  151. * event. Note that unlike {grantRole}, this function doesn't perform any
  152. * checks on the calling account.
  153. *
  154. * [WARNING]
  155. * ====
  156. * This function should only be called from the constructor when setting
  157. * up the initial roles for the system.
  158. *
  159. * Using this function in any other way is effectively circumventing the admin
  160. * system imposed by {AccessControl}.
  161. * ====
  162. */
  163. function _setupRole(bytes32 role, address account) internal virtual {
  164. _grantRole(role, account);
  165. }
  166. /**
  167. * @dev Sets `adminRole` as ``role``'s admin role.
  168. */
  169. function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
  170. _roles[role].adminRole = adminRole;
  171. }
  172. function _grantRole(bytes32 role, address account) private {
  173. if (_roles[role].members.add(account)) {
  174. emit RoleGranted(role, account, _msgSender());
  175. }
  176. }
  177. function _revokeRole(bytes32 role, address account) private {
  178. if (_roles[role].members.remove(account)) {
  179. emit RoleRevoked(role, account, _msgSender());
  180. }
  181. }
  182. }