AccessControl.sol 6.1 KB

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