-
Notifications
You must be signed in to change notification settings - Fork 12.4k
Add AccessManagerEnumerable #6053
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Amxx
merged 27 commits into
OpenZeppelin:master
from
ernestognw:feat/access-manager-enumerable
Dec 15, 2025
+208
−5
Merged
Changes from all commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
4374b53
Add AccessManagerEnumerable
ernestognw 841e6bb
Add interface, docs and tests
ernestognw b4af192
Add changeset
ernestognw b9e7965
up
ernestognw 2a0e900
Update list of functions by targets
ernestognw ab194a7
Add docs
ernestognw ba5a754
Merge branch 'master' into feat/access-manager-enumerable
ernestognw dad105c
Update doc references
ernestognw ece797a
up
ernestognw f199d64
Merge remote-tracking branch 'upstream/master' into feat/access-manag…
james-toussaint 05379c1
Store target functions in `Bytes4Set`
james-toussaint b0ff8f2
Add _updateRoleTargetFunction
ernestognw 6d54710
up
ernestognw c1e1c7e
Update .changeset/crazy-bears-flash.md
Amxx 2da8182
update behavior
Amxx 0605209
Remove IAccessManagerEnumerable
ernestognw a133fc0
up
ernestognw 698f948
Run AccessManager tests for AccessManagerEnumerable
ernestognw a4c7b89
remove IAccessManagerEnumerable interface
Amxx 903a7da
Move AccessControlEnumerable to mocks/docs
Amxx 32fe6f4
remove tests, and update documentation
Amxx bf4a284
reset
Amxx 0b981fd
re-enable tests for the enumerable variant of AccessManager
Amxx e0ca256
Merge branch 'master' into feat/access-manager-enumerable
Amxx b1c9eb9
revert changes to AccessManager tests
Amxx 13f08cf
reset mock
Amxx d39400a
up
ernestognw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| pragma solidity ^0.8.24; | ||
|
|
||
| import {AccessManager} from "../../access/manager/AccessManager.sol"; | ||
| import {EnumerableSet} from "../../utils/structs/EnumerableSet.sol"; | ||
|
|
||
| /** | ||
| * @dev Extension of {AccessManager} that allows enumerating the members of each role | ||
| * and the target functions each role is allowed to call. | ||
| * | ||
| * NOTE: Given {ADMIN_ROLE} is the default role for every restricted function, the | ||
| * {getRoleTargetFunctions} and {getRoleTargetFunctionCount} functions will return an empty array | ||
| * and 0 respectively. | ||
| */ | ||
| abstract contract AccessManagerEnumerable is AccessManager { | ||
| using EnumerableSet for EnumerableSet.AddressSet; | ||
| using EnumerableSet for EnumerableSet.Bytes4Set; | ||
|
|
||
| mapping(uint64 roleId => EnumerableSet.AddressSet) private _roleMembers; | ||
| mapping(uint64 roleId => mapping(address target => EnumerableSet.Bytes4Set)) private _roleTargetFunctions; | ||
|
|
||
| /** | ||
| * @dev Returns the number of accounts that have `roleId`. Can be used | ||
| * together with {getRoleMember} to enumerate all bearers of a role. | ||
| */ | ||
| function getRoleMemberCount(uint64 roleId) public view virtual returns (uint256) { | ||
| return _roleMembers[roleId].length(); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns one of the accounts that have `roleId`. `index` must be a | ||
| * value between 0 and {getRoleMemberCount}, non-inclusive. | ||
| * | ||
| * Role bearers are not sorted in any particular way, and their ordering may change at any point. | ||
| * | ||
| * WARNING: When using {getRoleMember} and {getRoleMemberCount}, make sure | ||
| * you perform all queries on the same block. See the following | ||
| * https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296[forum post] | ||
| * for more information. | ||
| */ | ||
| function getRoleMember(uint64 roleId, uint256 index) public view virtual returns (address) { | ||
| return _roleMembers[roleId].at(index); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns a range of accounts that have `roleId`. `start` and `end` define the range bounds. | ||
| * `start` is inclusive and `end` is exclusive. | ||
| * | ||
| * Role bearers are not sorted in any particular way, and their ordering may change at any point. | ||
| * | ||
| * It is not necessary to call {getRoleMemberCount} before calling this function. Using `start = 0` and | ||
| * `end = type(uint256).max` will return every member of `roleId`. | ||
| * | ||
| * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed | ||
| * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that | ||
| * this function has an unbounded cost, and using it as part of a state-changing function may render the function | ||
| * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. | ||
| */ | ||
| function getRoleMembers(uint64 roleId, uint256 start, uint256 end) public view virtual returns (address[] memory) { | ||
| return _roleMembers[roleId].values(start, end); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns the number of target function selectors that require `roleId` for the given `target`. | ||
| * Can be used together with {getRoleTargetFunction} to enumerate all target functions for a role on a specific target. | ||
| * | ||
| * NOTE: Given {ADMIN_ROLE} is the default role for every restricted function, passing {ADMIN_ROLE} as `roleId` will | ||
| * return 0. See {_updateRoleTargetFunction} for more details. | ||
| */ | ||
| function getRoleTargetFunctionCount(uint64 roleId, address target) public view virtual returns (uint256) { | ||
| return _roleTargetFunctions[roleId][target].length(); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns one of the target function selectors that require `roleId` for the given `target`. | ||
| * `index` must be a value between 0 and {getRoleTargetFunctionCount}, non-inclusive. | ||
| * | ||
| * Target function selectors are not sorted in any particular way, and their ordering may change at any point. | ||
| * | ||
| * WARNING: When using {getRoleTargetFunction} and {getRoleTargetFunctionCount}, make sure | ||
| * you perform all queries on the same block. See the following | ||
| * https://forum.openzeppelin.com/t/iterating-over-elements-on-enumerableset-in-openzeppelin-contracts/2296[forum post] | ||
| * for more information. | ||
| */ | ||
| function getRoleTargetFunction(uint64 roleId, address target, uint256 index) public view virtual returns (bytes4) { | ||
| return _roleTargetFunctions[roleId][target].at(index); | ||
| } | ||
|
|
||
| /** | ||
| * @dev Returns a range of target function selectors that require `roleId` for the given `target`. | ||
| * `start` and `end` define the range bounds. `start` is inclusive and `end` is exclusive. | ||
| * | ||
| * Target function selectors are not sorted in any particular way, and their ordering may change at any point. | ||
| * | ||
| * It is not necessary to call {getRoleTargetFunctionCount} before calling this function. Using `start = 0` and | ||
| * `end = type(uint256).max` will return every function selector that `roleId` is allowed to call on `target`. | ||
| * | ||
| * WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed | ||
| * to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that | ||
| * this function has an unbounded cost, and using it as part of a state-changing function may render the function | ||
| * uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block. | ||
| * | ||
| * NOTE: Given {ADMIN_ROLE} is the default role for every restricted function, passing {ADMIN_ROLE} as `roleId` will | ||
| * return an empty array. See {_updateRoleTargetFunction} for more details. | ||
| */ | ||
| function getRoleTargetFunctions( | ||
| uint64 roleId, | ||
| address target, | ||
| uint256 start, | ||
| uint256 end | ||
| ) public view virtual returns (bytes4[] memory) { | ||
| return _roleTargetFunctions[roleId][target].values(start, end); | ||
| } | ||
|
|
||
| /// @dev See {AccessManager-_grantRole}. Adds the account to the role members set. | ||
| function _grantRole( | ||
| uint64 roleId, | ||
| address account, | ||
| uint32 grantDelay, | ||
| uint32 executionDelay | ||
| ) internal virtual override returns (bool) { | ||
| bool granted = super._grantRole(roleId, account, grantDelay, executionDelay); | ||
| if (granted) { | ||
| _roleMembers[roleId].add(account); | ||
| } | ||
| return granted; | ||
| } | ||
|
|
||
| /// @dev See {AccessManager-_revokeRole}. Removes the account from the role members set. | ||
| function _revokeRole(uint64 roleId, address account) internal virtual override returns (bool) { | ||
| bool revoked = super._revokeRole(roleId, account); | ||
| if (revoked) { | ||
| _roleMembers[roleId].remove(account); | ||
| } | ||
| return revoked; | ||
| } | ||
|
|
||
| /** | ||
| * @dev See {AccessManager-_setTargetFunctionRole}. Adds the selector to the role target functions set. | ||
| * | ||
| * NOTE: This function does not track function selectors for the {ADMIN_ROLE}, since exhaustively tracking | ||
| * all restricted/admin functions is impractical (by default, all restricted functions are assigned to {ADMIN_ROLE}). | ||
| * Therefore, roles assigned as {ADMIN_ROLE} will not have their selectors included in this extension's tracking. | ||
| */ | ||
| function _setTargetFunctionRole(address target, bytes4 selector, uint64 roleId) internal virtual override { | ||
| // cache old role ID | ||
| uint64 oldRoleId = getTargetFunctionRole(target, selector); | ||
|
|
||
| // call super | ||
| super._setTargetFunctionRole(target, selector, roleId); | ||
|
|
||
| // update enumerable sets | ||
| if (oldRoleId != ADMIN_ROLE) { | ||
| _roleTargetFunctions[oldRoleId][target].remove(selector); | ||
| } | ||
| if (roleId != ADMIN_ROLE) { | ||
| _roleTargetFunctions[roleId][target].add(selector); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.