RBAC.sol 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 add a role to an address
  33. * @param addr address
  34. * @param roleName the name of the role
  35. */
  36. function addRole(address addr, string roleName)
  37. internal
  38. {
  39. roles[roleName].add(addr);
  40. RoleAdded(addr, roleName);
  41. }
  42. /**
  43. * @dev remove a role from an address
  44. * @param addr address
  45. * @param roleName the name of the role
  46. */
  47. function removeRole(address addr, string roleName)
  48. internal
  49. {
  50. roles[roleName].remove(addr);
  51. RoleRemoved(addr, roleName);
  52. }
  53. /**
  54. * @dev reverts if addr does not have role
  55. * @param addr address
  56. * @param roleName the name of the role
  57. * // reverts
  58. */
  59. function checkRole(address addr, string roleName)
  60. view
  61. public
  62. {
  63. roles[roleName].check(addr);
  64. }
  65. /**
  66. * @dev determine if addr has role
  67. * @param addr address
  68. * @param roleName the name of the role
  69. * @return bool
  70. */
  71. function hasRole(address addr, string roleName)
  72. view
  73. public
  74. returns (bool)
  75. {
  76. return roles[roleName].has(addr);
  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 adminAddRole(address addr, string roleName)
  84. onlyAdmin
  85. public
  86. {
  87. addRole(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 adminRemoveRole(address addr, string roleName)
  95. onlyAdmin
  96. public
  97. {
  98. removeRole(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. }