AccessControl.sol 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 `newAdminRole` is set as ``role``'s admin role, replacing `previousAdminRole`
  52. *
  53. * `DEFAULT_ADMIN_ROLE` is the starting admin for all roles, despite
  54. * {RoleAdminChanged} not being emitted signaling this.
  55. *
  56. * _Available since v3.1._
  57. */
  58. event RoleAdminChanged(bytes32 indexed role, bytes32 indexed previousAdminRole, bytes32 indexed newAdminRole);
  59. /**
  60. * @dev Emitted when `account` is granted `role`.
  61. *
  62. * `sender` is the account that originated the contract call, an admin role
  63. * bearer except when using {_setupRole}.
  64. */
  65. event RoleGranted(bytes32 indexed role, address indexed account, address indexed sender);
  66. /**
  67. * @dev Emitted when `account` is revoked `role`.
  68. *
  69. * `sender` is the account that originated the contract call:
  70. * - if using `revokeRole`, it is the admin role bearer
  71. * - if using `renounceRole`, it is the role bearer (i.e. `account`)
  72. */
  73. event RoleRevoked(bytes32 indexed role, address indexed account, address indexed sender);
  74. /**
  75. * @dev Returns `true` if `account` has been granted `role`.
  76. */
  77. function hasRole(bytes32 role, address account) public view returns (bool) {
  78. return _roles[role].members.contains(account);
  79. }
  80. /**
  81. * @dev Returns the number of accounts that have `role`. Can be used
  82. * together with {getRoleMember} to enumerate all bearers of a role.
  83. */
  84. function getRoleMemberCount(bytes32 role) public view returns (uint256) {
  85. return _roles[role].members.length();
  86. }
  87. /**
  88. * @dev Returns one of the accounts that have `role`. `index` must be a
  89. * value between 0 and {getRoleMemberCount}, non-inclusive.
  90. *
  91. * Role bearers are not sorted in any particular way, and their ordering may
  92. * change at any point.
  93. *
  94. * WARNING: When using {getRoleMember} and {getRoleMemberCount}, make sure
  95. * you perform all queries on the same block. See the following
  96. * https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296[forum post]
  97. * for more information.
  98. */
  99. function getRoleMember(bytes32 role, uint256 index) public view returns (address) {
  100. return _roles[role].members.at(index);
  101. }
  102. /**
  103. * @dev Returns the admin role that controls `role`. See {grantRole} and
  104. * {revokeRole}.
  105. *
  106. * To change a role's admin, use {_setRoleAdmin}.
  107. */
  108. function getRoleAdmin(bytes32 role) public view returns (bytes32) {
  109. return _roles[role].adminRole;
  110. }
  111. /**
  112. * @dev Grants `role` to `account`.
  113. *
  114. * If `account` had not been already granted `role`, emits a {RoleGranted}
  115. * event.
  116. *
  117. * Requirements:
  118. *
  119. * - the caller must have ``role``'s admin role.
  120. */
  121. function grantRole(bytes32 role, address account) public virtual {
  122. require(hasRole(_roles[role].adminRole, _msgSender()), "AccessControl: sender must be an admin to grant");
  123. _grantRole(role, account);
  124. }
  125. /**
  126. * @dev Revokes `role` from `account`.
  127. *
  128. * If `account` had been granted `role`, emits a {RoleRevoked} event.
  129. *
  130. * Requirements:
  131. *
  132. * - the caller must have ``role``'s admin role.
  133. */
  134. function revokeRole(bytes32 role, address account) public virtual {
  135. require(hasRole(_roles[role].adminRole, _msgSender()), "AccessControl: sender must be an admin to revoke");
  136. _revokeRole(role, account);
  137. }
  138. /**
  139. * @dev Revokes `role` from the calling account.
  140. *
  141. * Roles are often managed via {grantRole} and {revokeRole}: this function's
  142. * purpose is to provide a mechanism for accounts to lose their privileges
  143. * if they are compromised (such as when a trusted device is misplaced).
  144. *
  145. * If the calling account had been granted `role`, emits a {RoleRevoked}
  146. * event.
  147. *
  148. * Requirements:
  149. *
  150. * - the caller must be `account`.
  151. */
  152. function renounceRole(bytes32 role, address account) public virtual {
  153. require(account == _msgSender(), "AccessControl: can only renounce roles for self");
  154. _revokeRole(role, account);
  155. }
  156. /**
  157. * @dev Grants `role` to `account`.
  158. *
  159. * If `account` had not been already granted `role`, emits a {RoleGranted}
  160. * event. Note that unlike {grantRole}, this function doesn't perform any
  161. * checks on the calling account.
  162. *
  163. * [WARNING]
  164. * ====
  165. * This function should only be called from the constructor when setting
  166. * up the initial roles for the system.
  167. *
  168. * Using this function in any other way is effectively circumventing the admin
  169. * system imposed by {AccessControl}.
  170. * ====
  171. */
  172. function _setupRole(bytes32 role, address account) internal virtual {
  173. _grantRole(role, account);
  174. }
  175. /**
  176. * @dev Sets `adminRole` as ``role``'s admin role.
  177. *
  178. * Emits a {RoleAdminChanged} event.
  179. */
  180. function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
  181. emit RoleAdminChanged(role, _roles[role].adminRole, adminRole);
  182. _roles[role].adminRole = adminRole;
  183. }
  184. function _grantRole(bytes32 role, address account) private {
  185. if (_roles[role].members.add(account)) {
  186. emit RoleGranted(role, account, _msgSender());
  187. }
  188. }
  189. function _revokeRole(bytes32 role, address account) private {
  190. if (_roles[role].members.remove(account)) {
  191. emit RoleRevoked(role, account, _msgSender());
  192. }
  193. }
  194. }