Skip to content
Permalink
master
Switch branches/tags
Go to file
 
 
Cannot retrieve contributors at this time
// 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"];
}
}