|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +/// @author thirdweb |
| 5 | + |
| 6 | +import "../../extension/interface/IPermissions.sol"; |
| 7 | +import "../../lib/TWStrings.sol"; |
| 8 | + |
| 9 | +/** |
| 10 | + * @title Permissions |
| 11 | + * @dev This contracts provides extending-contracts with role-based access control mechanisms |
| 12 | + */ |
| 13 | + |
| 14 | +library PermissionsStorage { |
| 15 | + bytes32 public constant PERMISSIONS_STORAGE_POSITION = keccak256("permissions.storage"); |
| 16 | + |
| 17 | + struct Data { |
| 18 | + /// @dev Map from keccak256 hash of a role => a map from address => whether address has role. |
| 19 | + mapping(bytes32 => mapping(address => bool)) _hasRole; |
| 20 | + /// @dev Map from keccak256 hash of a role to role admin. See {getRoleAdmin}. |
| 21 | + mapping(bytes32 => bytes32) _getRoleAdmin; |
| 22 | + } |
| 23 | + |
| 24 | + function permissionsStorage() internal pure returns (Data storage permissionsData) { |
| 25 | + bytes32 position = PERMISSIONS_STORAGE_POSITION; |
| 26 | + assembly { |
| 27 | + permissionsData.slot := position |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +contract Permissions is IPermissions { |
| 33 | + /// @dev Default admin role for all roles. Only accounts with this role can grant/revoke other roles. |
| 34 | + bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00; |
| 35 | + |
| 36 | + /// @dev Modifier that checks if an account has the specified role; reverts otherwise. |
| 37 | + modifier onlyRole(bytes32 role) { |
| 38 | + _checkRole(role, _msgSender()); |
| 39 | + _; |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * @notice Checks whether an account has a particular role. |
| 44 | + * @dev Returns `true` if `account` has been granted `role`. |
| 45 | + * |
| 46 | + * @param role keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE") |
| 47 | + * @param account Address of the account for which the role is being checked. |
| 48 | + */ |
| 49 | + function hasRole(bytes32 role, address account) public view override returns (bool) { |
| 50 | + PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage(); |
| 51 | + return data._hasRole[role][account]; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @notice Checks whether an account has a particular role; |
| 56 | + * role restrictions can be swtiched on and off. |
| 57 | + * |
| 58 | + * @dev Returns `true` if `account` has been granted `role`. |
| 59 | + * Role restrictions can be swtiched on and off: |
| 60 | + * - If address(0) has ROLE, then the ROLE restrictions |
| 61 | + * don't apply. |
| 62 | + * - If address(0) does not have ROLE, then the ROLE |
| 63 | + * restrictions will apply. |
| 64 | + * |
| 65 | + * @param role keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE") |
| 66 | + * @param account Address of the account for which the role is being checked. |
| 67 | + */ |
| 68 | + function hasRoleWithSwitch(bytes32 role, address account) public view returns (bool) { |
| 69 | + PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage(); |
| 70 | + if (!data._hasRole[role][address(0)]) { |
| 71 | + return data._hasRole[role][account]; |
| 72 | + } |
| 73 | + |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * @notice Returns the admin role that controls the specified role. |
| 79 | + * @dev See {grantRole} and {revokeRole}. |
| 80 | + * To change a role's admin, use {_setRoleAdmin}. |
| 81 | + * |
| 82 | + * @param role keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE") |
| 83 | + */ |
| 84 | + function getRoleAdmin(bytes32 role) external view override returns (bytes32) { |
| 85 | + PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage(); |
| 86 | + return data._getRoleAdmin[role]; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * @notice Grants a role to an account, if not previously granted. |
| 91 | + * @dev Caller must have admin role for the `role`. |
| 92 | + * Emits {RoleGranted Event}. |
| 93 | + * |
| 94 | + * @param role keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE") |
| 95 | + * @param account Address of the account to which the role is being granted. |
| 96 | + */ |
| 97 | + function grantRole(bytes32 role, address account) public virtual override { |
| 98 | + PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage(); |
| 99 | + _checkRole(data._getRoleAdmin[role], _msgSender()); |
| 100 | + if (data._hasRole[role][account]) { |
| 101 | + revert("Can only grant to non holders"); |
| 102 | + } |
| 103 | + _setupRole(role, account); |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @notice Revokes role from an account. |
| 108 | + * @dev Caller must have admin role for the `role`. |
| 109 | + * Emits {RoleRevoked Event}. |
| 110 | + * |
| 111 | + * @param role keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE") |
| 112 | + * @param account Address of the account from which the role is being revoked. |
| 113 | + */ |
| 114 | + function revokeRole(bytes32 role, address account) public virtual override { |
| 115 | + PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage(); |
| 116 | + _checkRole(data._getRoleAdmin[role], _msgSender()); |
| 117 | + _revokeRole(role, account); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @notice Revokes role from the account. |
| 122 | + * @dev Caller must have the `role`, with caller being the same as `account`. |
| 123 | + * Emits {RoleRevoked Event}. |
| 124 | + * |
| 125 | + * @param role keccak256 hash of the role. e.g. keccak256("TRANSFER_ROLE") |
| 126 | + * @param account Address of the account from which the role is being revoked. |
| 127 | + */ |
| 128 | + function renounceRole(bytes32 role, address account) public virtual override { |
| 129 | + if (_msgSender() != account) { |
| 130 | + revert("Can only renounce for self"); |
| 131 | + } |
| 132 | + _revokeRole(role, account); |
| 133 | + } |
| 134 | + |
| 135 | + /// @dev Sets `adminRole` as `role`'s admin role. |
| 136 | + function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual { |
| 137 | + PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage(); |
| 138 | + bytes32 previousAdminRole = data._getRoleAdmin[role]; |
| 139 | + data._getRoleAdmin[role] = adminRole; |
| 140 | + emit RoleAdminChanged(role, previousAdminRole, adminRole); |
| 141 | + } |
| 142 | + |
| 143 | + /// @dev Sets up `role` for `account` |
| 144 | + function _setupRole(bytes32 role, address account) internal virtual { |
| 145 | + PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage(); |
| 146 | + data._hasRole[role][account] = true; |
| 147 | + emit RoleGranted(role, account, _msgSender()); |
| 148 | + } |
| 149 | + |
| 150 | + /// @dev Revokes `role` from `account` |
| 151 | + function _revokeRole(bytes32 role, address account) internal virtual { |
| 152 | + PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage(); |
| 153 | + _checkRole(role, account); |
| 154 | + delete data._hasRole[role][account]; |
| 155 | + emit RoleRevoked(role, account, _msgSender()); |
| 156 | + } |
| 157 | + |
| 158 | + /// @dev Checks `role` for `account`. Reverts with a message including the required role. |
| 159 | + function _checkRole(bytes32 role, address account) internal view virtual { |
| 160 | + PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage(); |
| 161 | + if (!data._hasRole[role][account]) { |
| 162 | + revert( |
| 163 | + string( |
| 164 | + abi.encodePacked( |
| 165 | + "Permissions: account ", |
| 166 | + TWStrings.toHexString(uint160(account), 20), |
| 167 | + " is missing role ", |
| 168 | + TWStrings.toHexString(uint256(role), 32) |
| 169 | + ) |
| 170 | + ) |
| 171 | + ); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /// @dev Checks `role` for `account`. Reverts with a message including the required role. |
| 176 | + function _checkRoleWithSwitch(bytes32 role, address account) internal view virtual { |
| 177 | + if (!hasRoleWithSwitch(role, account)) { |
| 178 | + revert( |
| 179 | + string( |
| 180 | + abi.encodePacked( |
| 181 | + "Permissions: account ", |
| 182 | + TWStrings.toHexString(uint160(account), 20), |
| 183 | + " is missing role ", |
| 184 | + TWStrings.toHexString(uint256(role), 32) |
| 185 | + ) |
| 186 | + ) |
| 187 | + ); |
| 188 | + } |
| 189 | + } |
| 190 | + |
| 191 | + function _msgSender() internal view virtual returns (address sender) { |
| 192 | + return msg.sender; |
| 193 | + } |
| 194 | + |
| 195 | + function _msgData() internal view virtual returns (bytes calldata) { |
| 196 | + return msg.data; |
| 197 | + } |
| 198 | +} |
0 commit comments