AccessManaged.sol 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v5.0.0-rc.1) (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 unambiguosly 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. /**
  55. * @dev Returns the current authority.
  56. */
  57. function authority() public view virtual returns (address) {
  58. return _authority;
  59. }
  60. /**
  61. * @dev Transfers control to a new authority. The caller must be the current authority.
  62. */
  63. function setAuthority(address newAuthority) public virtual {
  64. address caller = _msgSender();
  65. if (caller != authority()) {
  66. revert AccessManagedUnauthorized(caller);
  67. }
  68. if (newAuthority.code.length == 0) {
  69. revert AccessManagedInvalidAuthority(newAuthority);
  70. }
  71. _setAuthority(newAuthority);
  72. }
  73. /**
  74. * @dev Returns true only in the context of a delayed restricted call, at the moment that the scheduled operation is
  75. * being consumed. Prevents denial of service for delayed restricted calls in the case that the contract performs
  76. * attacker controlled calls.
  77. */
  78. function isConsumingScheduledOp() public view returns (bytes4) {
  79. return _consumingSchedule ? this.isConsumingScheduledOp.selector : bytes4(0);
  80. }
  81. /**
  82. * @dev Transfers control to a new authority. Internal function with no access restriction. Allows bypassing the
  83. * permissions set by the current authority.
  84. */
  85. function _setAuthority(address newAuthority) internal virtual {
  86. _authority = newAuthority;
  87. emit AuthorityUpdated(newAuthority);
  88. }
  89. /**
  90. * @dev Reverts if the caller is not allowed to call the function identified by a selector. Panics if the calldata
  91. * is less than 4 bytes long.
  92. */
  93. function _checkCanCall(address caller, bytes calldata data) internal virtual {
  94. (bool immediate, uint32 delay) = AuthorityUtils.canCallWithDelay(
  95. authority(),
  96. caller,
  97. address(this),
  98. bytes4(data[0:4])
  99. );
  100. if (!immediate) {
  101. if (delay > 0) {
  102. _consumingSchedule = true;
  103. IAccessManager(authority()).consumeScheduledOp(caller, data);
  104. _consumingSchedule = false;
  105. } else {
  106. revert AccessManagedUnauthorized(caller);
  107. }
  108. }
  109. }
  110. }