AccessManaged.sol 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.1.0) (access/manager/AccessManaged.sol)
  3. pragma solidity ^0.8.20;
  4. import {IAuthority} from "./IAuthority.sol";
  5. import {AuthorityUtils} from "./AuthorityUtils.sol";
  6. import {IAccessManager} from "./IAccessManager.sol";
  7. import {IAccessManaged} from "./IAccessManaged.sol";
  8. import {Context} from "../../utils/Context.sol";
  9. /**
  10. * @dev This contract module makes available a {restricted} modifier. Functions decorated with this modifier will be
  11. * permissioned according to an "authority": a contract like {AccessManager} that follows the {IAuthority} interface,
  12. * implementing a policy that allows certain callers to access certain functions.
  13. *
  14. * IMPORTANT: The `restricted` modifier should never be used on `internal` functions, judiciously used in `public`
  15. * functions, and ideally only used in `external` functions. See {restricted}.
  16. */
  17. abstract contract AccessManaged is Context, IAccessManaged {
  18. address private _authority;
  19. bool private _consumingSchedule;
  20. /**
  21. * @dev Initializes the contract connected to an initial authority.
  22. */
  23. constructor(address initialAuthority) {
  24. _setAuthority(initialAuthority);
  25. }
  26. /**
  27. * @dev Restricts access to a function as defined by the connected Authority for this contract and the
  28. * caller and selector of the function that entered the contract.
  29. *
  30. * [IMPORTANT]
  31. * ====
  32. * In general, this modifier should only be used on `external` functions. It is okay to use it on `public`
  33. * functions that are used as external entry points and are not called internally. Unless you know what you're
  34. * doing, it should never be used on `internal` functions. Failure to follow these rules can have critical security
  35. * implications! This is because the permissions are determined by the function that entered the contract, i.e. the
  36. * function at the bottom of the call stack, and not the function where the modifier is visible in the source code.
  37. * ====
  38. *
  39. * [WARNING]
  40. * ====
  41. * Avoid adding this modifier to the https://docs.soliditylang.org/en/v0.8.20/contracts.html#receive-ether-function[`receive()`]
  42. * function or the https://docs.soliditylang.org/en/v0.8.20/contracts.html#fallback-function[`fallback()`]. These
  43. * functions are the only execution paths where a function selector cannot be unambiguously determined from the calldata
  44. * since the selector defaults to `0x00000000` in the `receive()` function and similarly in the `fallback()` function
  45. * if no calldata is provided. (See {_checkCanCall}).
  46. *
  47. * The `receive()` function will always panic whereas the `fallback()` may panic depending on the calldata length.
  48. * ====
  49. */
  50. modifier restricted() {
  51. _checkCanCall(_msgSender(), _msgData());
  52. _;
  53. }
  54. /// @inheritdoc IAccessManaged
  55. function authority() public view virtual returns (address) {
  56. return _authority;
  57. }
  58. /// @inheritdoc IAccessManaged
  59. function setAuthority(address newAuthority) public virtual {
  60. address caller = _msgSender();
  61. if (caller != authority()) {
  62. revert AccessManagedUnauthorized(caller);
  63. }
  64. if (newAuthority.code.length == 0) {
  65. revert AccessManagedInvalidAuthority(newAuthority);
  66. }
  67. _setAuthority(newAuthority);
  68. }
  69. /// @inheritdoc IAccessManaged
  70. function isConsumingScheduledOp() public view returns (bytes4) {
  71. return _consumingSchedule ? this.isConsumingScheduledOp.selector : bytes4(0);
  72. }
  73. /**
  74. * @dev Transfers control to a new authority. Internal function with no access restriction. Allows bypassing the
  75. * permissions set by the current authority.
  76. */
  77. function _setAuthority(address newAuthority) internal virtual {
  78. _authority = newAuthority;
  79. emit AuthorityUpdated(newAuthority);
  80. }
  81. /**
  82. * @dev Reverts if the caller is not allowed to call the function identified by a selector. Panics if the calldata
  83. * is less than 4 bytes long.
  84. */
  85. function _checkCanCall(address caller, bytes calldata data) internal virtual {
  86. (bool immediate, uint32 delay) = AuthorityUtils.canCallWithDelay(
  87. authority(),
  88. caller,
  89. address(this),
  90. bytes4(data[0:4])
  91. );
  92. if (!immediate) {
  93. if (delay > 0) {
  94. _consumingSchedule = true;
  95. IAccessManager(authority()).consumeScheduledOp(caller, data);
  96. _consumingSchedule = false;
  97. } else {
  98. revert AccessManagedUnauthorized(caller);
  99. }
  100. }
  101. }
  102. }