RBAC.sol 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. pragma solidity ^0.4.18;
  2. import "./Roles.sol";
  3. /**
  4. * @title RBAC (Role-Based Access Control)
  5. * @author Matt Condon (@Shrugs)
  6. * @dev Stores and provides setters and getters for roles and addresses.
  7. * Supports unlimited numbers of roles and addresses.
  8. * See //contracts/examples/RBACExample.sol for an example of usage.
  9. * This RBAC method uses strings to key roles. It may be beneficial
  10. * for you to write your own implementation of this interface using Enums or similar.
  11. * It's also recommended that you define constants in the contract, like ROLE_ADMIN below,
  12. * to avoid typos.
  13. */
  14. contract RBAC {
  15. using Roles for Roles.Role;
  16. mapping (string => Roles.Role) private roles;
  17. event RoleAdded(address addr, string roleName);
  18. event RoleRemoved(address addr, string roleName);
  19. /**
  20. * A constant role name for indicating admins.
  21. */
  22. string public constant ROLE_ADMIN = "admin";
  23. /**
  24. * @dev constructor. Sets msg.sender as admin by default
  25. */
  26. function RBAC()
  27. public
  28. {
  29. addRole(msg.sender, ROLE_ADMIN);
  30. }
  31. /**
  32. * @dev reverts if addr does not have role
  33. * @param addr address
  34. * @param roleName the name of the role
  35. * // reverts
  36. */
  37. function checkRole(address addr, string roleName)
  38. view
  39. public
  40. {
  41. roles[roleName].check(addr);
  42. }
  43. /**
  44. * @dev determine if addr has role
  45. * @param addr address
  46. * @param roleName the name of the role
  47. * @return bool
  48. */
  49. function hasRole(address addr, string roleName)
  50. view
  51. public
  52. returns (bool)
  53. {
  54. return roles[roleName].has(addr);
  55. }
  56. /**
  57. * @dev add a role to an address
  58. * @param addr address
  59. * @param roleName the name of the role
  60. */
  61. function adminAddRole(address addr, string roleName)
  62. onlyAdmin
  63. public
  64. {
  65. addRole(addr, roleName);
  66. }
  67. /**
  68. * @dev remove a role from an address
  69. * @param addr address
  70. * @param roleName the name of the role
  71. */
  72. function adminRemoveRole(address addr, string roleName)
  73. onlyAdmin
  74. public
  75. {
  76. removeRole(addr, roleName);
  77. }
  78. /**
  79. * @dev add a role to an address
  80. * @param addr address
  81. * @param roleName the name of the role
  82. */
  83. function addRole(address addr, string roleName)
  84. internal
  85. {
  86. roles[roleName].add(addr);
  87. RoleAdded(addr, roleName);
  88. }
  89. /**
  90. * @dev remove a role from an address
  91. * @param addr address
  92. * @param roleName the name of the role
  93. */
  94. function removeRole(address addr, string roleName)
  95. internal
  96. {
  97. roles[roleName].remove(addr);
  98. RoleRemoved(addr, roleName);
  99. }
  100. /**
  101. * @dev modifier to scope access to a single role (uses msg.sender as addr)
  102. * @param roleName the name of the role
  103. * // reverts
  104. */
  105. modifier onlyRole(string roleName)
  106. {
  107. checkRole(msg.sender, roleName);
  108. _;
  109. }
  110. /**
  111. * @dev modifier to scope access to admins
  112. * // reverts
  113. */
  114. modifier onlyAdmin()
  115. {
  116. checkRole(msg.sender, ROLE_ADMIN);
  117. _;
  118. }
  119. /**
  120. * @dev modifier to scope access to a set of roles (uses msg.sender as addr)
  121. * @param roleNames the names of the roles to scope access to
  122. * // reverts
  123. *
  124. * @TODO - when solidity supports dynamic arrays as arguments to modifiers, provide this
  125. * see: https://github.com/ethereum/solidity/issues/2467
  126. */
  127. // modifier onlyRoles(string[] roleNames) {
  128. // bool hasAnyRole = false;
  129. // for (uint8 i = 0; i < roleNames.length; i++) {
  130. // if (hasRole(msg.sender, roleNames[i])) {
  131. // hasAnyRole = true;
  132. // break;
  133. // }
  134. // }
  135. // require(hasAnyRole);
  136. // _;
  137. // }
  138. }