AccessControlEnumerable.sol 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0-rc.1) (access/extensions/AccessControlEnumerable.sol)
  3. pragma solidity ^0.8.20;
  4. import {IAccessControlEnumerable} from "./IAccessControlEnumerable.sol";
  5. import {AccessControl} from "../AccessControl.sol";
  6. import {EnumerableSet} from "../../utils/structs/EnumerableSet.sol";
  7. /**
  8. * @dev Extension of {AccessControl} that allows enumerating the members of each role.
  9. */
  10. abstract contract AccessControlEnumerable is IAccessControlEnumerable, AccessControl {
  11. using EnumerableSet for EnumerableSet.AddressSet;
  12. mapping(bytes32 role => EnumerableSet.AddressSet) private _roleMembers;
  13. /**
  14. * @dev See {IERC165-supportsInterface}.
  15. */
  16. function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
  17. return interfaceId == type(IAccessControlEnumerable).interfaceId || super.supportsInterface(interfaceId);
  18. }
  19. /**
  20. * @dev Returns one of the accounts that have `role`. `index` must be a
  21. * value between 0 and {getRoleMemberCount}, non-inclusive.
  22. *
  23. * Role bearers are not sorted in any particular way, and their ordering may
  24. * change at any point.
  25. *
  26. * WARNING: When using {getRoleMember} and {getRoleMemberCount}, make sure
  27. * you perform all queries on the same block. See the following
  28. * https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296[forum post]
  29. * for more information.
  30. */
  31. function getRoleMember(bytes32 role, uint256 index) public view virtual returns (address) {
  32. return _roleMembers[role].at(index);
  33. }
  34. /**
  35. * @dev Returns the number of accounts that have `role`. Can be used
  36. * together with {getRoleMember} to enumerate all bearers of a role.
  37. */
  38. function getRoleMemberCount(bytes32 role) public view virtual returns (uint256) {
  39. return _roleMembers[role].length();
  40. }
  41. /**
  42. * @dev Return all accounts that have `role`
  43. *
  44. * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
  45. * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
  46. * this function has an unbounded cost, and using it as part of a state-changing function may render the function
  47. * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
  48. */
  49. function getRoleMembers(bytes32 role) public view virtual returns (address[] memory) {
  50. return _roleMembers[role].values();
  51. }
  52. /**
  53. * @dev Overload {AccessControl-_grantRole} to track enumerable memberships
  54. */
  55. function _grantRole(bytes32 role, address account) internal virtual override returns (bool) {
  56. bool granted = super._grantRole(role, account);
  57. if (granted) {
  58. _roleMembers[role].add(account);
  59. }
  60. return granted;
  61. }
  62. /**
  63. * @dev Overload {AccessControl-_revokeRole} to track enumerable memberships
  64. */
  65. function _revokeRole(bytes32 role, address account) internal virtual override returns (bool) {
  66. bool revoked = super._revokeRole(role, account);
  67. if (revoked) {
  68. _roleMembers[role].remove(account);
  69. }
  70. return revoked;
  71. }
  72. }