Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add getRoleMembers method to return all accounts that have role
  • Loading branch information
vlad-khramov committed Aug 28, 2023
commit 6869c14b8f724f083c62d12a4dae3b5a7bc42328
5 changes: 5 additions & 0 deletions .changeset/violet-moons-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`AccessControlEnumerable`: add `getRoleMembers` method to return all accounts that have `role`
12 changes: 12 additions & 0 deletions contracts/access/extensions/AccessControlEnumerable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,18 @@ abstract contract AccessControlEnumerable is IAccessControlEnumerable, AccessCon
return _roleMembers[role].length();
}

/**
* @dev Return all accounts that have `role`
*
* 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(bytes32 role) public view virtual returns (address[] memory) {
return _roleMembers[role].values();
}

/**
* @dev Overload {AccessControl-_grantRole} to track enumerable memberships
*/
Expand Down
2 changes: 2 additions & 0 deletions test/access/AccessControl.behavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,8 @@ function shouldBehaveLikeAccessControlEnumerable(admin, authorized, other, other
}

expect(bearers).to.have.members([authorized, otherAuthorized]);

expect(await this.accessControl.getRoleMembers(ROLE)).to.be.deep.equal([authorized, otherAuthorized]);
});
it('role enumeration should be in sync after renounceRole call', async function () {
expect(await this.accessControl.getRoleMemberCount(ROLE)).to.bignumber.equal('0');
Expand Down