This repository has been archived by the owner on May 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
AccessControl.sol
104 lines (92 loc) · 3.19 KB
/
AccessControl.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.9;
import "./Address.sol";
import "./EternalStorage.sol";
import "./ISettings.sol";
import "./IACL.sol";
import "./IAccessControl.sol";
import "./IACLConstants.sol";
/**
* @dev Base contract for interacting with the ACL.
*/
contract AccessControl is EternalStorage, IAccessControl, IACLConstants {
using Address for address;
/**
* @dev Constructor.
* @param _settings Address of Settings.
*/
constructor(address _settings) {
dataAddress["settings"] = _settings;
dataBytes32["aclContext"] = acl().generateContextFromAddress(address(this));
}
/**
* @dev Check that sender is an admin.
*/
modifier assertIsAdmin() {
require(isAdmin(msg.sender), "must be admin");
_;
}
modifier assertBelongsToEntityWithRole(address _user, bytes32 _role) {
require(_belongsToEntityWithRole(_user, _role), "not a rep of associated entity");
_;
}
function _belongsToEntityWithRole(address _user, bytes32 _role) internal view returns (bool) {
address entity = _getEntityWithRole(_role);
return _isRepOfEntity(_user, entity);
}
function _getEntityWithRole(bytes32 _role) internal view returns (address) {
address[] memory entities = acl().getUsersForRole(aclContext(), _role);
require(entities.length > 0, "no entity with role");
return entities[0];
}
function _isRepOfEntity(address _user, address _entity) internal view returns (bool) {
// check they are a rep
bytes32 ctx = AccessControl(_entity).aclContext();
return inRoleGroupWithContext(ctx, _user, ROLEGROUP_ENTITY_REPS);
}
/**
* @dev Check if given address has admin privileges.
* @param _addr Address to check.
* @return true if so
*/
function isAdmin(address _addr) public view override returns (bool) {
return acl().isAdmin(_addr);
}
/**
* @dev Check if given address has a role in the given role group in the current context.
* @param _addr Address to check.
* @param _roleGroup Rolegroup to check against.
* @return true if so
*/
function inRoleGroup(address _addr, bytes32 _roleGroup) public view override returns (bool) {
return inRoleGroupWithContext(aclContext(), _addr, _roleGroup);
}
/**
* @dev Check if given address has a role in the given rolegroup in the given context.
* @param _ctx Context to check against.
* @param _addr Address to check.
* @param _roleGroup Role group to check against.
* @return true if so
*/
function inRoleGroupWithContext(
bytes32 _ctx,
address _addr,
bytes32 _roleGroup
) public view override returns (bool) {
return acl().hasRoleInGroup(_ctx, _addr, _roleGroup);
}
/**
* @dev Get ACL reference.
* @return ACL reference.
*/
function acl() public view override returns (IACL) {
return ISettings(dataAddress["settings"]).acl();
}
/**
* @dev Get current ACL context.
* @return the context.
*/
function aclContext() public view override returns (bytes32) {
return dataBytes32["aclContext"];
}
}