|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +// OpenZeppelin Contracts (last updated v5.0.0) (access/AccessControl.sol) |
| 3 | + |
| 4 | +pragma solidity ^0.8.20; |
| 5 | + |
| 6 | +import {IAccessControl} from "@openzeppelin-v5/contracts/access/IAccessControl.sol"; |
| 7 | +import {ContextUpgradeable} from "../utils/ContextUpgradeable.sol"; |
| 8 | +import {ERC165Upgradeable} from "../utils/introspection/ERC165Upgradeable.sol"; |
| 9 | +import {Initializable} from "../proxy/utils/Initializable.sol"; |
| 10 | + |
| 11 | +/** |
| 12 | + * @dev Contract module that allows children to implement role-based access |
| 13 | + * control mechanisms. This is a lightweight version that doesn't allow enumerating role |
| 14 | + * members except through off-chain means by accessing the contract event logs. Some |
| 15 | + * applications may benefit from on-chain enumerability, for those cases see |
| 16 | + * {AccessControlEnumerable}. |
| 17 | + * |
| 18 | + * Roles are referred to by their `bytes32` identifier. These should be exposed |
| 19 | + * in the external API and be unique. The best way to achieve this is by |
| 20 | + * using `public constant` hash digests: |
| 21 | + * |
| 22 | + * ```solidity |
| 23 | + * bytes32 public constant MY_ROLE = keccak256("MY_ROLE"); |
| 24 | + * ``` |
| 25 | + * |
| 26 | + * Roles can be used to represent a set of permissions. To restrict access to a |
| 27 | + * function call, use {hasRole}: |
| 28 | + * |
| 29 | + * ```solidity |
| 30 | + * function foo() public { |
| 31 | + * require(hasRole(MY_ROLE, msg.sender)); |
| 32 | + * ... |
| 33 | + * } |
| 34 | + * ``` |
| 35 | + * |
| 36 | + * Roles can be granted and revoked dynamically via the {grantRole} and |
| 37 | + * {revokeRole} functions. Each role has an associated admin role, and only |
| 38 | + * accounts that have a role's admin role can call {grantRole} and {revokeRole}. |
| 39 | + * |
| 40 | + * By default, the admin role for all roles is `DEFAULT_ADMIN_ROLE`, which means |
| 41 | + * that only accounts with this role will be able to grant or revoke other |
| 42 | + * roles. More complex role relationships can be created by using |
| 43 | + * {_setRoleAdmin}. |
| 44 | + * |
| 45 | + * WARNING: The `DEFAULT_ADMIN_ROLE` is also its own admin: it has permission to |
| 46 | + * grant and revoke this role. Extra precautions should be taken to secure |
| 47 | + * accounts that have been granted it. We recommend using {AccessControlDefaultAdminRules} |
| 48 | + * to enforce additional security measures for this role. |
| 49 | + */ |
| 50 | +abstract contract AccessControlUpgradeable is |
| 51 | + Initializable, |
| 52 | + ContextUpgradeable, |
| 53 | + IAccessControl, |
| 54 | + ERC165Upgradeable |
| 55 | +{ |
| 56 | + struct RoleData { |
| 57 | + mapping(address account => bool) hasRole; |
| 58 | + bytes32 adminRole; |
| 59 | + } |
| 60 | + |
| 61 | + bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00; |
| 62 | + |
| 63 | + /// @custom:storage-location erc7201:openzeppelin.storage.AccessControl |
| 64 | + struct AccessControlStorage { |
| 65 | + mapping(bytes32 role => RoleData) _roles; |
| 66 | + } |
| 67 | + |
| 68 | + // keccak256(abi.encode(uint256(keccak256("openzeppelin.storage.AccessControl")) - 1)) & ~bytes32(uint256(0xff)) |
| 69 | + bytes32 private constant AccessControlStorageLocation = |
| 70 | + 0x02dd7bc7dec4dceedda775e58dd541e08a116c6c53815c0bd028192f7b626800; |
| 71 | + |
| 72 | + function _getAccessControlStorage() private pure returns (AccessControlStorage storage $) { |
| 73 | + assembly { |
| 74 | + $.slot := AccessControlStorageLocation |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * @dev Modifier that checks that an account has a specific role. Reverts |
| 80 | + * with an {AccessControlUnauthorizedAccount} error including the required role. |
| 81 | + */ |
| 82 | + modifier onlyRole(bytes32 role) { |
| 83 | + _checkRole(role); |
| 84 | + _; |
| 85 | + } |
| 86 | + |
| 87 | + function __AccessControl_init() internal onlyInitializing {} |
| 88 | + |
| 89 | + function __AccessControl_init_unchained() internal onlyInitializing {} |
| 90 | + /** |
| 91 | + * @dev See {IERC165-supportsInterface}. |
| 92 | + */ |
| 93 | + |
| 94 | + function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) { |
| 95 | + return |
| 96 | + interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * @dev Returns `true` if `account` has been granted `role`. |
| 101 | + */ |
| 102 | + function hasRole(bytes32 role, address account) public view virtual returns (bool) { |
| 103 | + AccessControlStorage storage $ = _getAccessControlStorage(); |
| 104 | + return $._roles[role].hasRole[account]; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @dev Reverts with an {AccessControlUnauthorizedAccount} error if `_msgSender()` |
| 109 | + * is missing `role`. Overriding this function changes the behavior of the {onlyRole} modifier. |
| 110 | + */ |
| 111 | + function _checkRole(bytes32 role) internal view virtual { |
| 112 | + _checkRole(role, _msgSender()); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * @dev Reverts with an {AccessControlUnauthorizedAccount} error if `account` |
| 117 | + * is missing `role`. |
| 118 | + */ |
| 119 | + function _checkRole(bytes32 role, address account) internal view virtual { |
| 120 | + if (!hasRole(role, account)) { |
| 121 | + revert AccessControlUnauthorizedAccount(account, role); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * @dev Returns the admin role that controls `role`. See {grantRole} and |
| 127 | + * {revokeRole}. |
| 128 | + * |
| 129 | + * To change a role's admin, use {_setRoleAdmin}. |
| 130 | + */ |
| 131 | + function getRoleAdmin(bytes32 role) public view virtual returns (bytes32) { |
| 132 | + AccessControlStorage storage $ = _getAccessControlStorage(); |
| 133 | + return $._roles[role].adminRole; |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * @dev Grants `role` to `account`. |
| 138 | + * |
| 139 | + * If `account` had not been already granted `role`, emits a {RoleGranted} |
| 140 | + * event. |
| 141 | + * |
| 142 | + * Requirements: |
| 143 | + * |
| 144 | + * - the caller must have ``role``'s admin role. |
| 145 | + * |
| 146 | + * May emit a {RoleGranted} event. |
| 147 | + */ |
| 148 | + function grantRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) { |
| 149 | + _grantRole(role, account); |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * @dev Revokes `role` from `account`. |
| 154 | + * |
| 155 | + * If `account` had been granted `role`, emits a {RoleRevoked} event. |
| 156 | + * |
| 157 | + * Requirements: |
| 158 | + * |
| 159 | + * - the caller must have ``role``'s admin role. |
| 160 | + * |
| 161 | + * May emit a {RoleRevoked} event. |
| 162 | + */ |
| 163 | + function revokeRole(bytes32 role, address account) |
| 164 | + public |
| 165 | + virtual |
| 166 | + onlyRole(getRoleAdmin(role)) |
| 167 | + { |
| 168 | + _revokeRole(role, account); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * @dev Revokes `role` from the calling account. |
| 173 | + * |
| 174 | + * Roles are often managed via {grantRole} and {revokeRole}: this function's |
| 175 | + * purpose is to provide a mechanism for accounts to lose their privileges |
| 176 | + * if they are compromised (such as when a trusted device is misplaced). |
| 177 | + * |
| 178 | + * If the calling account had been revoked `role`, emits a {RoleRevoked} |
| 179 | + * event. |
| 180 | + * |
| 181 | + * Requirements: |
| 182 | + * |
| 183 | + * - the caller must be `callerConfirmation`. |
| 184 | + * |
| 185 | + * May emit a {RoleRevoked} event. |
| 186 | + */ |
| 187 | + function renounceRole(bytes32 role, address callerConfirmation) public virtual { |
| 188 | + if (callerConfirmation != _msgSender()) { |
| 189 | + revert AccessControlBadConfirmation(); |
| 190 | + } |
| 191 | + |
| 192 | + _revokeRole(role, callerConfirmation); |
| 193 | + } |
| 194 | + |
| 195 | + /** |
| 196 | + * @dev Sets `adminRole` as ``role``'s admin role. |
| 197 | + * |
| 198 | + * Emits a {RoleAdminChanged} event. |
| 199 | + */ |
| 200 | + function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual { |
| 201 | + AccessControlStorage storage $ = _getAccessControlStorage(); |
| 202 | + bytes32 previousAdminRole = getRoleAdmin(role); |
| 203 | + $._roles[role].adminRole = adminRole; |
| 204 | + emit RoleAdminChanged(role, previousAdminRole, adminRole); |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * @dev Attempts to grant `role` to `account` and returns a boolean indicating if `role` was granted. |
| 209 | + * |
| 210 | + * Internal function without access restriction. |
| 211 | + * |
| 212 | + * May emit a {RoleGranted} event. |
| 213 | + */ |
| 214 | + function _grantRole(bytes32 role, address account) internal virtual returns (bool) { |
| 215 | + AccessControlStorage storage $ = _getAccessControlStorage(); |
| 216 | + if (!hasRole(role, account)) { |
| 217 | + $._roles[role].hasRole[account] = true; |
| 218 | + emit RoleGranted(role, account, _msgSender()); |
| 219 | + return true; |
| 220 | + } else { |
| 221 | + return false; |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + /** |
| 226 | + * @dev Attempts to revoke `role` to `account` and returns a boolean indicating if `role` was revoked. |
| 227 | + * |
| 228 | + * Internal function without access restriction. |
| 229 | + * |
| 230 | + * May emit a {RoleRevoked} event. |
| 231 | + */ |
| 232 | + function _revokeRole(bytes32 role, address account) internal virtual returns (bool) { |
| 233 | + AccessControlStorage storage $ = _getAccessControlStorage(); |
| 234 | + if (hasRole(role, account)) { |
| 235 | + $._roles[role].hasRole[account] = false; |
| 236 | + emit RoleRevoked(role, account, _msgSender()); |
| 237 | + return true; |
| 238 | + } else { |
| 239 | + return false; |
| 240 | + } |
| 241 | + } |
| 242 | +} |
0 commit comments