Skip to content
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

Add a bool return to _grantRole and _revokeRole #4241

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
5 changes: 5 additions & 0 deletions .changeset/two-wasps-punch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'openzeppelin-solidity': minor
---

`AccessControl`: Add a boolean return value to the internal `_grantRole` and `_revokeRole` functions indicating whether the role was granted or revoked.
10 changes: 8 additions & 2 deletions contracts/access/AccessControl.sol
Original file line number Diff line number Diff line change
Expand Up @@ -190,10 +190,13 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 {
*
* May emit a {RoleGranted} event.
*/
function _grantRole(bytes32 role, address account) internal virtual {
function _grantRole(bytes32 role, address account) internal virtual returns (bool) {
Amxx marked this conversation as resolved.
Show resolved Hide resolved
if (!hasRole(role, account)) {
_roles[role].members[account] = true;
emit RoleGranted(role, account, _msgSender());
return true;
} else {
return false;
}
}

Expand All @@ -204,10 +207,13 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 {
*
* May emit a {RoleRevoked} event.
*/
function _revokeRole(bytes32 role, address account) internal virtual {
function _revokeRole(bytes32 role, address account) internal virtual returns (bool) {
Amxx marked this conversation as resolved.
Show resolved Hide resolved
if (hasRole(role, account)) {
_roles[role].members[account] = false;
emit RoleRevoked(role, account, _msgSender());
return true;
} else {
return false;
}
}
}
8 changes: 4 additions & 4 deletions contracts/access/AccessControlDefaultAdminRules.sol
Original file line number Diff line number Diff line change
Expand Up @@ -130,24 +130,24 @@ abstract contract AccessControlDefaultAdminRules is IAccessControlDefaultAdminRu
* NOTE: Exposing this function through another mechanism may make the `DEFAULT_ADMIN_ROLE`
* assignable again. Make sure to guarantee this is the expected behavior in your implementation.
*/
function _grantRole(bytes32 role, address account) internal virtual override {
function _grantRole(bytes32 role, address account) internal virtual override returns (bool) {
if (role == DEFAULT_ADMIN_ROLE) {
if (defaultAdmin() != address(0)) {
revert AccessControlEnforcedDefaultAdminRules();
}
_currentDefaultAdmin = account;
}
super._grantRole(role, account);
return super._grantRole(role, account);
}

/**
* @dev See {AccessControl-_revokeRole}.
*/
function _revokeRole(bytes32 role, address account) internal virtual override {
function _revokeRole(bytes32 role, address account) internal virtual override returns (bool) {
if (role == DEFAULT_ADMIN_ROLE && account == defaultAdmin()) {
delete _currentDefaultAdmin;
}
super._revokeRole(role, account);
return super._revokeRole(role, account);
}

/**
Expand Down
18 changes: 12 additions & 6 deletions contracts/access/AccessControlEnumerable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,22 @@ abstract contract AccessControlEnumerable is IAccessControlEnumerable, AccessCon
/**
* @dev Overload {_grantRole} to track enumerable memberships
Amxx marked this conversation as resolved.
Show resolved Hide resolved
*/
function _grantRole(bytes32 role, address account) internal virtual override {
super._grantRole(role, account);
_roleMembers[role].add(account);
function _grantRole(bytes32 role, address account) internal virtual override returns (bool) {
bool granted = super._grantRole(role, account);
if (granted) {
_roleMembers[role].add(account);
}
return granted;
}

/**
* @dev Overload {_revokeRole} to track enumerable memberships
Amxx marked this conversation as resolved.
Show resolved Hide resolved
*/
function _revokeRole(bytes32 role, address account) internal virtual override {
super._revokeRole(role, account);
_roleMembers[role].remove(account);
function _revokeRole(bytes32 role, address account) internal virtual override returns (bool) {
bool revoked = super._revokeRole(role, account);
if (revoked) {
_roleMembers[role].remove(account);
}
return revoked;
}
}