AccessControl.sol 7.2 KB

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