From 869dd83e567d1d92e447e329e76a287b182c51d2 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Wed, 26 Jan 2022 16:54:39 +0100 Subject: [PATCH 1/3] add a virtual _onlyRole(bytes32) modifier --- CHANGELOG.md | 4 ++++ contracts/access/AccessControl.sol | 12 +++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b99c31689c9..1e53d592c5c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + + * `AccessControl`: add a virtual `_onlyRole(bytes32)` function that can be overriden to alter the the `onlyRole` modifier behavior. ([#xxxx](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/xxxx)) + ## Unreleased * `ERC2891`: add implementation of the royalty standard, and the respective extensions for `ERC721` and `ERC1155`. ([#3012](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3012)) diff --git a/contracts/access/AccessControl.sol b/contracts/access/AccessControl.sol index 70c81f6c268..a2242492792 100644 --- a/contracts/access/AccessControl.sol +++ b/contracts/access/AccessControl.sol @@ -67,7 +67,7 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 { * _Available since v4.1._ */ modifier onlyRole(bytes32 role) { - _checkRole(role, _msgSender()); + _onlyRole(role); _; } @@ -85,6 +85,16 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 { return _roles[role].members[account]; } + /** + * @dev Revert with a standard message if `_msgSender()` is missing `role`. + * Overriding this function changes the behavior of the {onlyRole} modifier. + * + * Format of the revert message is described in {_checkRole}. + */ + function _onlyRole(bytes32 role) internal view virtual { + _checkRole(role, _msgSender()); + } + /** * @dev Revert with a standard message if `account` is missing `role`. * From e74ae0e732333531c2181ceb7bf82d144c06e797 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Wed, 26 Jan 2022 17:29:03 +0100 Subject: [PATCH 2/3] =?UTF-8?q?=5FonlyRole(role)=20=E2=86=92=20=5FcheckRol?= =?UTF-8?q?e(role)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- contracts/access/AccessControl.sol | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e53d592c5c..994883529b2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased - * `AccessControl`: add a virtual `_onlyRole(bytes32)` function that can be overriden to alter the the `onlyRole` modifier behavior. ([#xxxx](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/xxxx)) + * `AccessControl`: add a virtual `_checkRole(bytes32)` function that can be overriden to alter the the `onlyRole` modifier behavior. ([#3137](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3137)) ## Unreleased diff --git a/contracts/access/AccessControl.sol b/contracts/access/AccessControl.sol index a2242492792..f8dd529c040 100644 --- a/contracts/access/AccessControl.sol +++ b/contracts/access/AccessControl.sol @@ -67,7 +67,7 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 { * _Available since v4.1._ */ modifier onlyRole(bytes32 role) { - _onlyRole(role); + _checkRole(role); _; } @@ -91,7 +91,7 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 { * * Format of the revert message is described in {_checkRole}. */ - function _onlyRole(bytes32 role) internal view virtual { + function _checkRole(bytes32 role) internal view virtual { _checkRole(role, _msgSender()); } From 721f1f7b4500004438405f6296f73a98006c58c5 Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Wed, 26 Jan 2022 17:33:11 +0100 Subject: [PATCH 3/3] update doc --- contracts/access/AccessControl.sol | 2 ++ 1 file changed, 2 insertions(+) diff --git a/contracts/access/AccessControl.sol b/contracts/access/AccessControl.sol index f8dd529c040..f18448770bc 100644 --- a/contracts/access/AccessControl.sol +++ b/contracts/access/AccessControl.sol @@ -90,6 +90,8 @@ abstract contract AccessControl is Context, IAccessControl, ERC165 { * Overriding this function changes the behavior of the {onlyRole} modifier. * * Format of the revert message is described in {_checkRole}. + * + * _Available since v4.6._ */ function _checkRole(bytes32 role) internal view virtual { _checkRole(role, _msgSender());