AccessManaged.sol 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. import "../../utils/Context.sol";
  4. import "./IAuthority.sol";
  5. /**
  6. * @dev This contract module makes available a {restricted} modifier. Functions decorated with this modifier will be
  7. * permissioned according to an "authority": a contract like {AccessManager} that follows the {IAuthority} interface,
  8. * implementing a policy that allows certain callers to access certain functions.
  9. *
  10. * IMPORTANT: The `restricted` modifier should never be used on `internal` functions, judiciously used in `public`
  11. * functions, and ideally only used in `external` functions. See {restricted}.
  12. */
  13. contract AccessManaged is Context {
  14. event AuthorityUpdated(address indexed sender, IAuthority indexed newAuthority);
  15. IAuthority private _authority;
  16. /**
  17. * @dev Restricts access to a function as defined by the connected Authority for this contract and the
  18. * caller and selector of the function that entered the contract.
  19. *
  20. * [IMPORTANT]
  21. * ====
  22. * In general, this modifier should only be used on `external` functions. It is okay to use it on `public` functions
  23. * that are used as external entry points and are not called internally. Unless you know what you're doing, it
  24. * should never be used on `internal` functions. Failure to follow these rules can have critical security
  25. * implications! This is because the permissions are determined by the function that entered the contract, i.e. the
  26. * function at the bottom of the call stack, and not the function where the modifier is visible in the source code.
  27. * ====
  28. *
  29. * [NOTE]
  30. * ====
  31. * Selector collisions are mitigated by scoping permissions per contract, but some edge cases must be considered:
  32. *
  33. * * If the https://docs.soliditylang.org/en/latest/contracts.html#receive-ether-function[`receive()`] function is restricted,
  34. * any other function with a `0x00000000` selector will share permissions with `receive()`.
  35. * * Similarly, if there's no `receive()` function but a `fallback()` instead, the fallback might be called with empty `calldata`,
  36. * sharing the `0x00000000` selector permissions as well.
  37. * * For any other selector, if the restricted function is set on an upgradeable contract, an upgrade may remove the restricted
  38. * function and replace it with a new method whose selector replaces the last one, keeping the previous permissions.
  39. * ====
  40. */
  41. modifier restricted() {
  42. _checkCanCall(_msgSender(), msg.sig);
  43. _;
  44. }
  45. /**
  46. * @dev Initializes the contract connected to an initial authority.
  47. */
  48. constructor(IAuthority initialAuthority) {
  49. _setAuthority(initialAuthority);
  50. }
  51. /**
  52. * @dev Returns the current authority.
  53. */
  54. function authority() public view virtual returns (IAuthority) {
  55. return _authority;
  56. }
  57. /**
  58. * @dev Transfers control to a new authority. The caller must be the current authority.
  59. */
  60. function setAuthority(IAuthority newAuthority) public virtual {
  61. require(_msgSender() == address(_authority), "AccessManaged: not current authority");
  62. _setAuthority(newAuthority);
  63. }
  64. /**
  65. * @dev Transfers control to a new authority. Internal function with no access restriction.
  66. */
  67. function _setAuthority(IAuthority newAuthority) internal virtual {
  68. _authority = newAuthority;
  69. emit AuthorityUpdated(_msgSender(), newAuthority);
  70. }
  71. /**
  72. * @dev Reverts if the caller is not allowed to call the function identified by a selector.
  73. */
  74. function _checkCanCall(address caller, bytes4 selector) internal view virtual {
  75. require(_authority.canCall(caller, address(this), selector), "AccessManaged: authority rejected");
  76. }
  77. }