-
Notifications
You must be signed in to change notification settings - Fork 11.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove enumerable from AccessControl and add AccessControlEnumerable …
…extension (#2512) Co-authored-by: Francisco Giordano <[email protected]>
- Loading branch information
Showing
11 changed files
with
302 additions
and
217 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,63 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "./AccessControl.sol"; | ||
import "../utils/EnumerableSet.sol"; | ||
|
||
/** | ||
* @dev Extension of {AccessControl} that allows enumerating the members of each role. | ||
*/ | ||
abstract contract AccessControlEnumerable is AccessControl { | ||
using EnumerableSet for EnumerableSet.AddressSet; | ||
|
||
mapping (bytes32 => EnumerableSet.AddressSet) private _roleMembers; | ||
|
||
/** | ||
* @dev Returns one of the accounts that have `role`. `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(bytes32 role, uint256 index) public view returns (address) { | ||
return _roleMembers[role].at(index); | ||
} | ||
|
||
/** | ||
* @dev Returns the number of accounts that have `role`. Can be used | ||
* together with {getRoleMember} to enumerate all bearers of a role. | ||
*/ | ||
function getRoleMemberCount(bytes32 role) public view returns (uint256) { | ||
return _roleMembers[role].length(); | ||
} | ||
|
||
/** | ||
* @dev Overload {grantRole} to track enumerable memberships | ||
*/ | ||
function grantRole(bytes32 role, address account) public virtual override { | ||
super.grantRole(role, account); | ||
_roleMembers[role].add(account); | ||
} | ||
|
||
/** | ||
* @dev Overload {revokeRole} to track enumerable memberships | ||
*/ | ||
function revokeRole(bytes32 role, address account) public virtual override { | ||
super.revokeRole(role, account); | ||
_roleMembers[role].remove(account); | ||
} | ||
|
||
/** | ||
* @dev Overload {_setupRole} to track enumerable memberships | ||
*/ | ||
function _setupRole(bytes32 role, address account) internal virtual override { | ||
super._setupRole(role, account); | ||
_roleMembers[role].add(account); | ||
} | ||
} |
This file contains 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
This file contains 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,15 @@ | ||
// SPDX-License-Identifier: MIT | ||
|
||
pragma solidity ^0.8.0; | ||
|
||
import "../access/AccessControlEnumerable.sol"; | ||
|
||
contract AccessControlEnumerableMock is AccessControlEnumerable { | ||
constructor() { | ||
_setupRole(DEFAULT_ADMIN_ROLE, _msgSender()); | ||
} | ||
|
||
function setRoleAdmin(bytes32 roleId, bytes32 adminRoleId) public { | ||
_setRoleAdmin(roleId, adminRoleId); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.