AccessManagerAdapter.sol 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "./AccessManager.sol";
  4. import "./AccessManaged.sol";
  5. /**
  6. * @dev This contract can be used to migrate existing {Ownable} or {AccessControl} contracts into an {AccessManager}
  7. * system.
  8. *
  9. * Ownable contracts can have their ownership transferred to an instance of this adapter. AccessControl contracts can
  10. * grant all roles to the adapter, while ideally revoking them from all other accounts. Subsequently, the permissions
  11. * for those contracts can be managed centrally and with function granularity in the {AccessManager} instance the
  12. * adapter is connected to.
  13. *
  14. * Permissioned interactions with thus migrated contracts must go through the adapter's {relay} function and will
  15. * proceed if the function is allowed for the caller in the AccessManager instance.
  16. */
  17. contract AccessManagerAdapter is AccessManaged {
  18. bytes32 private constant _DEFAULT_ADMIN_ROLE = 0;
  19. /**
  20. * @dev Initializes an adapter connected to an AccessManager instance.
  21. */
  22. constructor(AccessManager manager) AccessManaged(manager) {}
  23. /**
  24. * @dev Relays a function call to the target contract. The call will be relayed if the AccessManager allows the
  25. * caller access to this function in the target contract, i.e. if the caller is in a team that is allowed for the
  26. * function, or if the caller is the default admin for the AccessManager. The latter is meant to be used for
  27. * ad hoc operations such as asset recovery.
  28. */
  29. function relay(address target, bytes memory data) external payable {
  30. bytes4 sig = bytes4(data);
  31. AccessManager manager = AccessManager(address(authority()));
  32. require(
  33. manager.canCall(msg.sender, target, sig) || manager.hasRole(_DEFAULT_ADMIN_ROLE, msg.sender),
  34. "AccessManagerAdapter: caller not allowed"
  35. );
  36. (bool ok, bytes memory result) = target.call{value: msg.value}(data);
  37. assembly {
  38. let result_pointer := add(32, result)
  39. let result_size := mload(result)
  40. switch ok
  41. case true {
  42. return(result_pointer, result_size)
  43. }
  44. default {
  45. revert(result_pointer, result_size)
  46. }
  47. }
  48. }
  49. }