-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into feat/OPT-538-rename-has-access
- Loading branch information
Showing
15 changed files
with
260 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
...dity/contracts/CommonAccessController.sol → ...ntracts/access/CommonAccessController.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) {} | ||
|
||
/** | ||
* @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); | ||
} | ||
} | ||
_; | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...dity/contracts/OracleAccessController.sol → ...ntracts/access/OracleAccessController.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters