Skip to content

Commit 5b571ed

Browse files
aritkulovaKyrylR
andauthored
Added missing storage backets for access folder (#141)
* added missing storage backets for access folder * typo * order of layout consistency * bump version to 3.0.2 --------- Co-authored-by: Kyryl R <kyryl.ryabov@gmail.com>
1 parent ee0ab79 commit 5b571ed

File tree

7 files changed

+156
-48
lines changed

7 files changed

+156
-48
lines changed

contracts/access/AMerkleWhitelisted.sol

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ import {MerkleProof} from "@openzeppelin/contracts/utils/cryptography/MerkleProo
2020
abstract contract AMerkleWhitelisted {
2121
using MerkleProof for bytes32[];
2222

23-
bytes32 private _merkleRoot;
23+
struct AMerkleWhitelistedStorage {
24+
bytes32 merkleRoot;
25+
}
26+
27+
// bytes32(uint256(keccak256("solarity.contract.AMerkleWhitelisted")) - 1)
28+
bytes32 private constant A_MERKLE_WHITELISTED_STORAGE =
29+
0x655e174042e5ffc37fc1cb8b514e651c61ae9bb4dd4f6ce06d8f229ee9767b24;
2430

2531
error LeafNotWhitelisted(bytes data);
2632
error UserNotWhitelisted(address user);
@@ -40,7 +46,9 @@ abstract contract AMerkleWhitelisted {
4046
* @return the current Merkle root or zero bytes if it has not been set
4147
*/
4248
function getMerkleRoot() public view returns (bytes32) {
43-
return _merkleRoot;
49+
AMerkleWhitelistedStorage storage $ = _getAMerkleWhitelistedStorage();
50+
51+
return $.merkleRoot;
4452
}
4553

4654
/**
@@ -53,7 +61,9 @@ abstract contract AMerkleWhitelisted {
5361
bytes32 leaf_,
5462
bytes32[] memory merkleProof_
5563
) internal view returns (bool) {
56-
return merkleProof_.verify(_merkleRoot, leaf_);
64+
AMerkleWhitelistedStorage storage $ = _getAMerkleWhitelistedStorage();
65+
66+
return merkleProof_.verify($.merkleRoot, leaf_);
5767
}
5868

5969
/**
@@ -74,6 +84,21 @@ abstract contract AMerkleWhitelisted {
7484
* @param merkleRoot_ the Merkle root to be set
7585
*/
7686
function _setMerkleRoot(bytes32 merkleRoot_) internal {
77-
_merkleRoot = merkleRoot_;
87+
AMerkleWhitelistedStorage storage $ = _getAMerkleWhitelistedStorage();
88+
89+
$.merkleRoot = merkleRoot_;
90+
}
91+
92+
/**
93+
* @dev Returns a pointer to the storage namespace
94+
*/
95+
function _getAMerkleWhitelistedStorage()
96+
private
97+
pure
98+
returns (AMerkleWhitelistedStorage storage $)
99+
{
100+
assembly {
101+
$.slot := A_MERKLE_WHITELISTED_STORAGE
102+
}
78103
}
79104
}

contracts/access/AMultiOwnable.sol

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,13 @@ abstract contract AMultiOwnable is IMultiOwnable, Initializable {
2626
using TypeCaster for address;
2727
using SetHelper for EnumerableSet.AddressSet;
2828

29-
EnumerableSet.AddressSet private _owners;
29+
struct AMultiOwnableStorage {
30+
EnumerableSet.AddressSet owners;
31+
}
32+
33+
// bytes32(uint256(keccak256("solarity.contract.AMultiOwnable")) - 1)
34+
bytes32 private constant A_MULTI_OWNABLE_STORAGE =
35+
0x54985b7dba18117ef28d5d113b6eab9fb186b92b1987f5efdadbc365eb2a5cba;
3036

3137
error InvalidOwner();
3238
error UnauthorizedAccount(address account);
@@ -83,7 +89,9 @@ abstract contract AMultiOwnable is IMultiOwnable, Initializable {
8389
* @return the list of current owners
8490
*/
8591
function getOwners() public view override returns (address[] memory) {
86-
return _owners.values();
92+
AMultiOwnableStorage storage $ = _getAMultiOwnableStorage();
93+
94+
return $.owners.values();
8795
}
8896

8997
/**
@@ -92,7 +100,9 @@ abstract contract AMultiOwnable is IMultiOwnable, Initializable {
92100
* @return true if address_ is owner, false otherwise
93101
*/
94102
function isOwner(address address_) public view override returns (bool) {
95-
return _owners.contains(address_);
103+
AMultiOwnableStorage storage $ = _getAMultiOwnableStorage();
104+
105+
return $.owners.contains(address_);
96106
}
97107

98108
/**
@@ -101,9 +111,11 @@ abstract contract AMultiOwnable is IMultiOwnable, Initializable {
101111
* @param newOwners_ the array of addresses to add to _owners
102112
*/
103113
function _addOwners(address[] memory newOwners_) private {
104-
_owners.add(newOwners_);
114+
AMultiOwnableStorage storage $ = _getAMultiOwnableStorage();
115+
116+
$.owners.add(newOwners_);
105117

106-
if (_owners.contains(address(0))) revert InvalidOwner();
118+
if ($.owners.contains(address(0))) revert InvalidOwner();
107119

108120
emit OwnersAdded(newOwners_);
109121
}
@@ -118,7 +130,9 @@ abstract contract AMultiOwnable is IMultiOwnable, Initializable {
118130
* @param oldOwners_ the array of addresses to remove from _owners
119131
*/
120132
function _removeOwners(address[] memory oldOwners_) private {
121-
_owners.remove(oldOwners_);
133+
AMultiOwnableStorage storage $ = _getAMultiOwnableStorage();
134+
135+
$.owners.remove(oldOwners_);
122136

123137
emit OwnersRemoved(oldOwners_);
124138
}
@@ -129,4 +143,13 @@ abstract contract AMultiOwnable is IMultiOwnable, Initializable {
129143
function _checkOwner() private view {
130144
if (!isOwner(msg.sender)) revert UnauthorizedAccount(msg.sender);
131145
}
146+
147+
/**
148+
* @dev Returns a pointer to the storage namespace
149+
*/
150+
function _getAMultiOwnableStorage() private pure returns (AMultiOwnableStorage storage $) {
151+
assembly {
152+
$.slot := A_MULTI_OWNABLE_STORAGE
153+
}
154+
}
132155
}

contracts/access/ARBAC.sol

Lines changed: 49 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ abstract contract ARBAC is IRBAC, Initializable {
3535
using SetHelper for DynamicSet.StringSet;
3636
using TypeCaster for string;
3737

38+
struct ARBACStorage {
39+
mapping(string => mapping(bool => mapping(string => DynamicSet.StringSet))) rolePermissions;
40+
mapping(string => mapping(bool => DynamicSet.StringSet)) roleResources;
41+
mapping(address => DynamicSet.StringSet) userRoles;
42+
}
43+
3844
string public constant MASTER_ROLE = "MASTER";
3945

4046
string public constant ALL_RESOURCE = "*";
@@ -47,11 +53,9 @@ abstract contract ARBAC is IRBAC, Initializable {
4753

4854
string public constant RBAC_RESOURCE = "RBAC_RESOURCE";
4955

50-
mapping(string => mapping(bool => mapping(string => DynamicSet.StringSet)))
51-
private _rolePermissions;
52-
mapping(string => mapping(bool => DynamicSet.StringSet)) private _roleResources;
53-
54-
mapping(address => DynamicSet.StringSet) private _userRoles;
56+
// bytes32(uint256(keccak256("solarity.contract.ARBAC")) - 1)
57+
bytes32 private constant A_RBAC_STORAGE =
58+
0xf2ad3663acdafb41a6feebdd394e5d1d04767a13f9432491d7491e61819b106e;
5559

5660
error EmptyRoles();
5761
error NoPermissionForResource(address account, string permission, string resource);
@@ -145,7 +149,9 @@ abstract contract ARBAC is IRBAC, Initializable {
145149
* @return roles_ the roles of the user
146150
*/
147151
function getUserRoles(address who_) public view override returns (string[] memory roles_) {
148-
return _userRoles[who_].values();
152+
ARBACStorage storage $ = _getARBACStorage();
153+
154+
return $.userRoles[who_].values();
149155
}
150156

151157
/**
@@ -165,13 +171,15 @@ abstract contract ARBAC is IRBAC, Initializable {
165171
ResourceWithPermissions[] memory disallowed_
166172
)
167173
{
168-
DynamicSet.StringSet storage _allowedResources = _roleResources[role_][true];
169-
DynamicSet.StringSet storage _disallowedResources = _roleResources[role_][false];
174+
ARBACStorage storage $ = _getARBACStorage();
175+
176+
DynamicSet.StringSet storage _allowedResources = $.roleResources[role_][true];
177+
DynamicSet.StringSet storage _disallowedResources = $.roleResources[role_][false];
170178

171-
mapping(string => DynamicSet.StringSet) storage _allowedPermissions = _rolePermissions[
179+
mapping(string => DynamicSet.StringSet) storage _allowedPermissions = $.rolePermissions[
172180
role_
173181
][true];
174-
mapping(string => DynamicSet.StringSet) storage _disallowedPermissions = _rolePermissions[
182+
mapping(string => DynamicSet.StringSet) storage _disallowedPermissions = $.rolePermissions[
175183
role_
176184
][false];
177185

@@ -224,7 +232,9 @@ abstract contract ARBAC is IRBAC, Initializable {
224232
* @param rolesToGrant_ the roles to grant
225233
*/
226234
function _grantRoles(address to_, string[] memory rolesToGrant_) internal {
227-
_userRoles[to_].add(rolesToGrant_);
235+
ARBACStorage storage $ = _getARBACStorage();
236+
237+
$.userRoles[to_].add(rolesToGrant_);
228238

229239
emit GrantedRoles(to_, rolesToGrant_);
230240
}
@@ -235,7 +245,9 @@ abstract contract ARBAC is IRBAC, Initializable {
235245
* @param rolesToRevoke_ the roles to revoke
236246
*/
237247
function _revokeRoles(address from_, string[] memory rolesToRevoke_) internal {
238-
_userRoles[from_].remove(rolesToRevoke_);
248+
ARBACStorage storage $ = _getARBACStorage();
249+
250+
$.userRoles[from_].remove(rolesToRevoke_);
239251

240252
emit RevokedRoles(from_, rolesToRevoke_);
241253
}
@@ -253,8 +265,10 @@ abstract contract ARBAC is IRBAC, Initializable {
253265
string[] memory permissionsToAdd_,
254266
bool allowed_
255267
) internal {
256-
DynamicSet.StringSet storage _resources = _roleResources[role_][allowed_];
257-
DynamicSet.StringSet storage _permissions = _rolePermissions[role_][allowed_][
268+
ARBACStorage storage $ = _getARBACStorage();
269+
270+
DynamicSet.StringSet storage _resources = $.roleResources[role_][allowed_];
271+
DynamicSet.StringSet storage _permissions = $.rolePermissions[role_][allowed_][
258272
resourceToAdd_
259273
];
260274

@@ -277,8 +291,10 @@ abstract contract ARBAC is IRBAC, Initializable {
277291
string[] memory permissionsToRemove_,
278292
bool allowed_
279293
) internal {
280-
DynamicSet.StringSet storage _resources = _roleResources[role_][allowed_];
281-
DynamicSet.StringSet storage _permissions = _rolePermissions[role_][allowed_][
294+
ARBACStorage storage $ = _getARBACStorage();
295+
296+
DynamicSet.StringSet storage _resources = $.roleResources[role_][allowed_];
297+
DynamicSet.StringSet storage _permissions = $.rolePermissions[role_][allowed_][
282298
resourceToRemove_
283299
];
284300

@@ -303,7 +319,11 @@ abstract contract ARBAC is IRBAC, Initializable {
303319
string memory resource_,
304320
string memory permission_
305321
) internal view returns (bool) {
306-
mapping(string => DynamicSet.StringSet) storage _resources = _rolePermissions[role_][true];
322+
ARBACStorage storage $ = _getARBACStorage();
323+
324+
mapping(string => DynamicSet.StringSet) storage _resources = $.rolePermissions[role_][
325+
true
326+
];
307327

308328
DynamicSet.StringSet storage _allAllowed = _resources[ALL_RESOURCE];
309329
DynamicSet.StringSet storage _allowed = _resources[resource_];
@@ -326,7 +346,9 @@ abstract contract ARBAC is IRBAC, Initializable {
326346
string memory resource_,
327347
string memory permission_
328348
) internal view returns (bool) {
329-
mapping(string => DynamicSet.StringSet) storage _resources = _rolePermissions[role_][
349+
ARBACStorage storage $ = _getARBACStorage();
350+
351+
mapping(string => DynamicSet.StringSet) storage _resources = $.rolePermissions[role_][
330352
false
331353
];
332354

@@ -338,4 +360,13 @@ abstract contract ARBAC is IRBAC, Initializable {
338360
_disallowed.contains(ALL_PERMISSION) ||
339361
_disallowed.contains(permission_));
340362
}
363+
364+
/**
365+
* @dev Returns a pointer to the storage namespace
366+
*/
367+
function _getARBACStorage() private pure returns (ARBACStorage storage $) {
368+
assembly {
369+
$.slot := A_RBAC_STORAGE
370+
}
371+
}
341372
}

0 commit comments

Comments
 (0)