AccessManager.sol 13 KB

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