@@ -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