-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: move module access controller #56
Changes from all commits
f4dde9e
c80cc1e
e0def92
a1830e3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
import {IOracle} from '../../interfaces/IOracle.sol'; | ||
import {IModuleAccessController} from '../../interfaces/access/IModuleAccessController.sol'; | ||
import {Module} from '../Module.sol'; | ||
import {CommonAccessController} from './CommonAccessController.sol'; | ||
|
||
abstract contract ModuleAccessController is IModuleAccessController, CommonAccessController, Module { | ||
constructor(IOracle _oracle) Module(_oracle) {} | ||
Comment on lines
+9
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does
|
||
|
||
/** | ||
* @notice Returns an access control object using the contract address as user and the given data | ||
* @dev should only be used by modules as the self-access-control object | ||
* @param _data Arbitrary data | ||
* @return _accessControl The self access control for this contract. | ||
*/ | ||
function _defaultAccessControl(bytes memory _data) internal view returns (AccessControl memory _accessControl) { | ||
_accessControl = AccessControl({user: address(this), data: _data}); | ||
} | ||
|
||
/** | ||
* @notice Returns an access control object using the contract address as user, and empty data | ||
* | ||
* @dev should only be used by modules as the self-access-control object | ||
* @return _accessControl The self access control for this contract. | ||
*/ | ||
function _defaultAccessControl() internal view returns (AccessControl memory _accessControl) { | ||
_accessControl = _defaultAccessControl(bytes('')); | ||
} | ||
|
||
modifier hasAccess( | ||
address _accessModule, | ||
bytes32 _typehash, | ||
bytes memory _params, | ||
AccessControl memory _accessControl | ||
) { | ||
if (_accessControl.user != msg.sender) { | ||
if (_accessModule == address(0)) { | ||
revert AccessController_NoAccess(); | ||
} else { | ||
if (!ORACLE.isAccessModuleApproved(_accessControl.user, _accessModule)) { | ||
revert ModuleAccessController_AccessModuleNotApproved(); | ||
} | ||
_hasAccess(_accessModule, _typehash, _params, _accessControl); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #55 will impact here. Similarly in |
||
} | ||
} | ||
_; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
import {IAccessController} from './IAccessController.sol'; | ||
|
||
/** | ||
* @title Module Access Controller Interface | ||
* @notice Interface for the module access controller | ||
*/ | ||
interface IModuleAccessController is IAccessController { | ||
/** | ||
* @notice Thrown when user has not approved the access module | ||
*/ | ||
error ModuleAccessController_AccessModuleNotApproved(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.19; | ||
|
||
import {IModuleAccessController, ModuleAccessController} from '../../contracts/access/ModuleAccessController.sol'; | ||
import {IOracle} from '../../interfaces/IOracle.sol'; | ||
|
||
import {IOracleAccessController} from '../../interfaces/access/IOracleAccessController.sol'; | ||
import {IAccessController, IAccessModule} from '../../interfaces/modules/access/IAccessModule.sol'; | ||
import {Helpers} from '../utils/Helpers.sol'; | ||
import 'forge-std/Test.sol'; | ||
|
||
contract MockModuleAccessController is ModuleAccessController { | ||
constructor(IOracle _oracle) ModuleAccessController(_oracle) {} | ||
|
||
function hasAccessForTest( | ||
address _accessModule, | ||
bytes32 _typehash, | ||
bytes memory _params, | ||
AccessControl memory _accessControl | ||
) public hasAccess(_accessModule, _typehash, _params, _accessControl) {} | ||
|
||
function moduleName() external pure returns (string memory _moduleName) { | ||
_moduleName = 'MockModuleAccessController'; | ||
} | ||
} | ||
|
||
/** | ||
* @title Module Abstract Unit tests | ||
*/ | ||
contract BaseTest is Test, Helpers { | ||
// A mock access controller | ||
MockModuleAccessController public moduleAccessController; | ||
// The oracle | ||
IOracle public oracle; | ||
// Mock caller | ||
address public caller; | ||
|
||
// Events | ||
event AccessModuleSet(address indexed user, address indexed accessModule, bool approved); | ||
Check warning on line 39 in solidity/test/unit/ModuleAccessController.t.sol GitHub Actions / Run Linters (20.x)
Check warning on line 39 in solidity/test/unit/ModuleAccessController.t.sol GitHub Actions / Run Linters (20.x)
|
||
|
||
/** | ||
* @notice Deploy the access controller | ||
*/ | ||
function setUp() public { | ||
oracle = IOracle(makeAddr('Oracle')); | ||
moduleAccessController = new MockModuleAccessController(oracle); | ||
caller = makeAddr('Caller'); | ||
} | ||
|
||
function _setAccessModuleForTest(address _user, address _accessModule, bool _approved) public { | ||
vm.mockCall( | ||
address(oracle), | ||
abi.encodeWithSelector(IOracleAccessController.isAccessModuleApproved.selector, _user, _accessModule), | ||
abi.encode(_approved) | ||
); | ||
} | ||
} | ||
|
||
contract ModuleAccessController_Unit_HasAccess is BaseTest { | ||
modifier happyPath( | ||
address _accessModule, | ||
bytes32 _typehash, | ||
bytes memory _params, | ||
IAccessController.AccessControl memory _accessControl | ||
) { | ||
vm.assume(_accessControl.user != caller); | ||
vm.assume(_accessModule != address(0) && _accessModule != caller); | ||
vm.assume(_params.length < 32); | ||
|
||
_setAccessModuleForTest(_accessControl.user, _accessModule, true); | ||
|
||
vm.startPrank(caller); | ||
_; | ||
} | ||
|
||
function test_revertIfNoAccessModuleAndSenderIsNotUser( | ||
address _accessModule, | ||
bytes32 _typehash, | ||
bytes memory _params, | ||
IAccessController.AccessControl memory _accessControl | ||
) public happyPath(_accessModule, _typehash, _params, _accessControl) { | ||
// Expect the revert | ||
vm.expectRevert(abi.encodeWithSelector(IAccessController.AccessController_NoAccess.selector)); | ||
|
||
// Call the hasAccess function | ||
moduleAccessController.hasAccessForTest(address(0), _typehash, _params, _accessControl); | ||
} | ||
|
||
function test_revertIfAccessModuleNotApproved( | ||
address _accessModule, | ||
bytes32 _typehash, | ||
bytes memory _params, | ||
IAccessController.AccessControl memory _accessControl | ||
) public happyPath(_accessModule, _typehash, _params, _accessControl) { | ||
// Ensure the access module is not approved | ||
_setAccessModuleForTest(_accessControl.user, _accessModule, false); | ||
|
||
// Expect the revert | ||
vm.expectRevert( | ||
abi.encodeWithSelector(IModuleAccessController.ModuleAccessController_AccessModuleNotApproved.selector) | ||
); | ||
|
||
// Call the hasAccess function | ||
moduleAccessController.hasAccessForTest(_accessModule, _typehash, _params, _accessControl); | ||
} | ||
|
||
function test_revertIfNoHasAccess( | ||
address _accessModule, | ||
bytes32 _typehash, | ||
bytes memory _params, | ||
IAccessController.AccessControl memory _accessControl | ||
) public happyPath(_accessModule, _typehash, _params, _accessControl) { | ||
IAccessModule.AccessControlParameters memory _expectedParams = IAccessModule.AccessControlParameters({ | ||
sender: caller, | ||
accessControl: _accessControl, | ||
typehash: _typehash, | ||
typehashParams: _params | ||
}); | ||
|
||
// Expect the hasAccess function to not be called | ||
_mockAndExpect( | ||
_accessModule, | ||
abi.encodeWithSelector(IAccessModule.hasAccess.selector, abi.encode(_expectedParams)), | ||
abi.encode(false) | ||
); | ||
|
||
// Expect the revert | ||
vm.expectRevert(abi.encodeWithSelector(IAccessController.AccessController_NoAccess.selector)); | ||
|
||
// Call the hasAccess function | ||
moduleAccessController.hasAccessForTest(_accessModule, _typehash, _params, _accessControl); | ||
} | ||
|
||
function test_hasAccessSenderIsNotUser( | ||
address _accessModule, | ||
bytes32 _typehash, | ||
bytes memory _params, | ||
IAccessController.AccessControl memory _accessControl | ||
) public happyPath(_accessModule, _typehash, _params, _accessControl) { | ||
IAccessModule.AccessControlParameters memory _expectedParams = IAccessModule.AccessControlParameters({ | ||
sender: caller, | ||
accessControl: _accessControl, | ||
typehash: _typehash, | ||
typehashParams: _params | ||
}); | ||
|
||
// Expect the hasAccess function to be called | ||
_mockAndExpect( | ||
_accessModule, | ||
abi.encodeWithSelector(IAccessModule.hasAccess.selector, abi.encode(_expectedParams)), | ||
abi.encode(true) | ||
); | ||
|
||
// Call the hasAccess function | ||
moduleAccessController.hasAccessForTest(_accessModule, _typehash, _params, _accessControl); | ||
} | ||
|
||
function test_hasAccessSenderIsUser( | ||
address _accessModule, | ||
bytes32 _typehash, | ||
bytes memory _params, | ||
IAccessController.AccessControl memory _accessControl | ||
) public happyPath(_accessModule, _typehash, _params, _accessControl) { | ||
_setAccessModuleForTest(caller, _accessModule, true); | ||
_accessControl.user = caller; | ||
// We expect the hasAccess function to not be called | ||
vm.expectCall( | ||
_accessModule, | ||
abi.encodeWithSelector( | ||
IAccessModule.hasAccess.selector, | ||
abi.encode( | ||
IAccessModule.AccessControlParameters({ | ||
sender: caller, | ||
accessControl: _accessControl, | ||
typehash: _typehash, | ||
typehashParams: _params | ||
}) | ||
) | ||
), | ||
0 | ||
); | ||
moduleAccessController.hasAccessForTest(_accessModule, _typehash, _params, _accessControl); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I didn't like the files thrown in
contracts/