AccessManager.sol 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.13;
  3. import "../AccessControlDefaultAdminRules.sol";
  4. import "./IAuthority.sol";
  5. import "./AccessManaged.sol";
  6. interface IAccessManager is IAuthority, IAccessControlDefaultAdminRules {
  7. enum AccessMode {
  8. Custom,
  9. Closed,
  10. Open
  11. }
  12. event GroupUpdated(uint8 indexed group, string name);
  13. event GroupAllowed(address indexed target, bytes4 indexed selector, uint8 indexed group, bool allowed);
  14. event AccessModeUpdated(address indexed target, AccessMode previousMode, AccessMode indexed mode);
  15. function createGroup(uint8 group, string calldata name) external;
  16. function updateGroupName(uint8 group, string calldata name) external;
  17. function hasGroup(uint8 group) external view returns (bool);
  18. function getUserGroups(address user) external view returns (bytes32 groups);
  19. function grantGroup(uint8 group, address user) external;
  20. function revokeGroup(uint8 group, address user) external;
  21. function renounceGroup(uint8 group, address user) external;
  22. function getFunctionAllowedGroups(address target, bytes4 selector) external view returns (bytes32 groups);
  23. function setFunctionAllowedGroup(address target, bytes4[] calldata selectors, uint8 group, bool allowed) external;
  24. function getContractMode(address target) external view returns (AccessMode);
  25. function setContractModeCustom(address target) external;
  26. function setContractModeOpen(address target) external;
  27. function setContractModeClosed(address target) external;
  28. function transferContractAuthority(address target, address newAuthority) external;
  29. }
  30. /**
  31. * @dev AccessManager is a central contract to store the permissions of a system.
  32. *
  33. * The smart contracts under the control of an AccessManager instance will have a set of "restricted" functions, and the
  34. * exact details of how access is restricted for each of those functions is configurable by the admins of the instance.
  35. * These restrictions are expressed in terms of "groups".
  36. *
  37. * An AccessManager instance will define a set of groups. Each of them must be created before they can be granted, with
  38. * a maximum of 255 created groups. Users can be added into any number of these groups. Each of them defines an
  39. * AccessControl role, and may confer access to some of the restricted functions in the system, as configured by admins
  40. * through the use of {setFunctionAllowedGroup}.
  41. *
  42. * Note that a function in a target contract may become permissioned in this way only when: 1) said contract is
  43. * {AccessManaged} and is connected to this contract as its manager, and 2) said function is decorated with the
  44. * `restricted` modifier.
  45. *
  46. * There is a special group defined by default named "public" which all accounts automatically have.
  47. *
  48. * Contracts can also be configured in two special modes: 1) the "open" mode, where all functions are allowed to the
  49. * "public" group, and 2) the "closed" mode, where no function is allowed to any group.
  50. *
  51. * Since all the permissions of the managed system can be modified by the admins of this instance, it is expected that
  52. * it will be highly secured (e.g., a multisig or a well-configured DAO). Additionally, {AccessControlDefaultAdminRules}
  53. * is included to enforce security rules on this account.
  54. *
  55. * NOTE: Some of the functions in this contract, such as {getUserGroups}, return a `bytes32` bitmap to succinctly
  56. * represent a set of groups. In a bitmap, bit `n` (counting from the least significant bit) will be 1 if and only if
  57. * the group with number `n` is in the set. For example, the hex value `0x05` represents the set of the two groups
  58. * numbered 0 and 2 from its binary equivalence `0b101`
  59. */
  60. contract AccessManager is IAccessManager, AccessControlDefaultAdminRules {
  61. bytes32 private _createdGroups;
  62. // user -> groups
  63. mapping(address => bytes32) private _userGroups;
  64. // target -> selector -> groups
  65. mapping(address => mapping(bytes4 => bytes32)) private _allowedGroups;
  66. // target -> mode
  67. mapping(address => AccessMode) private _contractMode;
  68. uint8 private constant _GROUP_PUBLIC = type(uint8).max;
  69. /**
  70. * @dev Initializes an AccessManager with initial default admin and transfer delay.
  71. */
  72. constructor(
  73. uint48 initialDefaultAdminDelay,
  74. address initialDefaultAdmin
  75. ) AccessControlDefaultAdminRules(initialDefaultAdminDelay, initialDefaultAdmin) {
  76. _createGroup(_GROUP_PUBLIC, "public");
  77. }
  78. /**
  79. * @dev Returns true if the caller can invoke on a target the function identified by a function selector.
  80. * Entrypoint for {AccessManaged} contracts.
  81. */
  82. function canCall(address caller, address target, bytes4 selector) public view virtual returns (bool) {
  83. bytes32 allowedGroups = getFunctionAllowedGroups(target, selector);
  84. bytes32 callerGroups = getUserGroups(caller);
  85. return callerGroups & allowedGroups != 0;
  86. }
  87. /**
  88. * @dev Creates a new group with a group number that can be chosen arbitrarily but must be unused, and gives it a
  89. * human-readable name. The caller must be the default admin.
  90. *
  91. * Group numbers are not auto-incremented in order to avoid race conditions, but administrators can safely use
  92. * sequential numbers.
  93. *
  94. * Emits {GroupUpdated}.
  95. */
  96. function createGroup(uint8 group, string memory name) public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
  97. _createGroup(group, name);
  98. }
  99. /**
  100. * @dev Updates an existing group's name. The caller must be the default admin.
  101. */
  102. function updateGroupName(uint8 group, string memory name) public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
  103. require(group != _GROUP_PUBLIC, "AccessManager: built-in group");
  104. require(hasGroup(group), "AccessManager: unknown group");
  105. emit GroupUpdated(group, name);
  106. }
  107. /**
  108. * @dev Returns true if the group has already been created via {createGroup}.
  109. */
  110. function hasGroup(uint8 group) public view virtual returns (bool) {
  111. return _getGroup(_createdGroups, group);
  112. }
  113. /**
  114. * @dev Returns a bitmap of the groups the user has. See note on bitmaps above.
  115. */
  116. function getUserGroups(address user) public view virtual returns (bytes32) {
  117. return _userGroups[user] | _groupMask(_GROUP_PUBLIC);
  118. }
  119. /**
  120. * @dev Grants a user a group.
  121. *
  122. * Emits {RoleGranted} with the role id of the group, if wasn't already held by the user.
  123. */
  124. function grantGroup(uint8 group, address user) public virtual {
  125. grantRole(_encodeGroupRole(group), user); // will check msg.sender
  126. }
  127. /**
  128. * @dev Removes a group from a user.
  129. *
  130. * Emits {RoleRevoked} with the role id of the group, if previously held by the user.
  131. */
  132. function revokeGroup(uint8 group, address user) public virtual {
  133. revokeRole(_encodeGroupRole(group), user); // will check msg.sender
  134. }
  135. /**
  136. * @dev Allows a user to renounce a group.
  137. *
  138. * Emits {RoleRevoked} with the role id of the group, if previously held by the user.
  139. */
  140. function renounceGroup(uint8 group, address user) public virtual {
  141. renounceRole(_encodeGroupRole(group), user); // will check msg.sender
  142. }
  143. /**
  144. * @dev Returns a bitmap of the groups that are allowed to call a function of a target contract. If the target
  145. * contract is in open or closed mode it will be reflected in the return value.
  146. */
  147. function getFunctionAllowedGroups(address target, bytes4 selector) public view virtual returns (bytes32) {
  148. AccessMode mode = getContractMode(target);
  149. if (mode == AccessMode.Open) {
  150. return _groupMask(_GROUP_PUBLIC);
  151. } else if (mode == AccessMode.Closed) {
  152. return 0;
  153. } else {
  154. return _allowedGroups[target][selector];
  155. }
  156. }
  157. /**
  158. * @dev Changes whether a group is allowed to call a function of a contract, according to the `allowed` argument.
  159. * The caller must be the default admin.
  160. */
  161. function setFunctionAllowedGroup(
  162. address target,
  163. bytes4[] calldata selectors,
  164. uint8 group,
  165. bool allowed
  166. ) public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
  167. require(hasGroup(group), "AccessManager: unknown group");
  168. for (uint256 i = 0; i < selectors.length; i++) {
  169. bytes4 selector = selectors[i];
  170. _allowedGroups[target][selector] = _withUpdatedGroup(_allowedGroups[target][selector], group, allowed);
  171. emit GroupAllowed(target, selector, group, allowed);
  172. }
  173. }
  174. /**
  175. * @dev Returns the mode of the target contract, which may be custom (`0`), closed (`1`), or open (`2`).
  176. */
  177. function getContractMode(address target) public view virtual returns (AccessMode) {
  178. return _contractMode[target];
  179. }
  180. /**
  181. * @dev Sets the target contract to be in custom restricted mode. All restricted functions in the target contract
  182. * will follow the group-based restrictions defined by the AccessManager. The caller must be the default admin.
  183. */
  184. function setContractModeCustom(address target) public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
  185. _setContractMode(target, AccessMode.Custom);
  186. }
  187. /**
  188. * @dev Sets the target contract to be in "open" mode. All restricted functions in the target contract will become
  189. * callable by anyone. The caller must be the default admin.
  190. */
  191. function setContractModeOpen(address target) public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
  192. _setContractMode(target, AccessMode.Open);
  193. }
  194. /**
  195. * @dev Sets the target contract to be in "closed" mode. All restricted functions in the target contract will be
  196. * closed down and disallowed to all. The caller must be the default admin.
  197. */
  198. function setContractModeClosed(address target) public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
  199. _setContractMode(target, AccessMode.Closed);
  200. }
  201. /**
  202. * @dev Transfers a target contract onto a new authority. The caller must be the default admin.
  203. */
  204. function transferContractAuthority(
  205. address target,
  206. address newAuthority
  207. ) public virtual onlyRole(DEFAULT_ADMIN_ROLE) {
  208. AccessManaged(target).setAuthority(IAuthority(newAuthority));
  209. }
  210. /**
  211. * @dev Creates a new group.
  212. *
  213. * Emits {GroupUpdated}.
  214. */
  215. function _createGroup(uint8 group, string memory name) internal virtual {
  216. require(!hasGroup(group), "AccessManager: existing group");
  217. _createdGroups = _withUpdatedGroup(_createdGroups, group, true);
  218. emit GroupUpdated(group, name);
  219. }
  220. /**
  221. * @dev Augmented version of {AccessControl-_grantRole} that keeps track of user group bitmaps.
  222. */
  223. function _grantRole(bytes32 role, address user) internal virtual override {
  224. super._grantRole(role, user);
  225. (bool isGroup, uint8 group) = _decodeGroupRole(role);
  226. if (isGroup) {
  227. require(hasGroup(group), "AccessManager: unknown group");
  228. _userGroups[user] = _withUpdatedGroup(_userGroups[user], group, true);
  229. }
  230. }
  231. /**
  232. * @dev Augmented version of {AccessControl-_revokeRole} that keeps track of user group bitmaps.
  233. */
  234. function _revokeRole(bytes32 role, address user) internal virtual override {
  235. super._revokeRole(role, user);
  236. (bool isGroup, uint8 group) = _decodeGroupRole(role);
  237. if (isGroup) {
  238. require(hasGroup(group), "AccessManager: unknown group");
  239. require(group != _GROUP_PUBLIC, "AccessManager: irrevocable group");
  240. _userGroups[user] = _withUpdatedGroup(_userGroups[user], group, false);
  241. }
  242. }
  243. /**
  244. * @dev Sets the restricted mode of a target contract.
  245. */
  246. function _setContractMode(address target, AccessMode mode) internal virtual {
  247. AccessMode previousMode = _contractMode[target];
  248. _contractMode[target] = mode;
  249. emit AccessModeUpdated(target, previousMode, mode);
  250. }
  251. /**
  252. * @dev Returns the {AccessControl} role id that corresponds to a group.
  253. *
  254. * This role id starts with the ASCII characters `group:`, followed by zeroes, and ends with the single byte
  255. * corresponding to the group number.
  256. */
  257. function _encodeGroupRole(uint8 group) internal pure virtual returns (bytes32) {
  258. return bytes32("group:") | bytes32(uint256(group));
  259. }
  260. /**
  261. * @dev Decodes a role id into a group, if it is a role id of the kind returned by {_encodeGroupRole}.
  262. */
  263. function _decodeGroupRole(bytes32 role) internal pure virtual returns (bool isGroup, uint8 group) {
  264. bytes32 tagMask = ~bytes32(uint256(0xff));
  265. bytes32 tag = role & tagMask;
  266. isGroup = tag == bytes32("group:");
  267. group = uint8(role[31]);
  268. }
  269. /**
  270. * @dev Returns a bit mask where the only non-zero bit is the group number bit.
  271. */
  272. function _groupMask(uint8 group) private pure returns (bytes32) {
  273. return bytes32(1 << group);
  274. }
  275. /**
  276. * @dev Returns the value of the group number bit in a bitmap.
  277. */
  278. function _getGroup(bytes32 bitmap, uint8 group) private pure returns (bool) {
  279. return bitmap & _groupMask(group) > 0;
  280. }
  281. /**
  282. * @dev Returns a new group bitmap where a specific group was updated.
  283. */
  284. function _withUpdatedGroup(bytes32 bitmap, uint8 group, bool value) private pure returns (bytes32) {
  285. bytes32 mask = _groupMask(group);
  286. if (value) {
  287. return bitmap | mask;
  288. } else {
  289. return bitmap & ~mask;
  290. }
  291. }
  292. }