RBAC.sol 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. pragma solidity ^0.4.24;
  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/mocks/RBACMock.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. */
  12. contract RBAC {
  13. using Roles for Roles.Role;
  14. mapping (string => Roles.Role) private roles;
  15. event RoleAdded(address indexed operator, string role);
  16. event RoleRemoved(address indexed operator, string role);
  17. /**
  18. * @dev reverts if addr does not have role
  19. * @param _operator address
  20. * @param _role the name of the role
  21. * // reverts
  22. */
  23. function checkRole(address _operator, string _role)
  24. public
  25. view
  26. {
  27. roles[_role].check(_operator);
  28. }
  29. /**
  30. * @dev determine if addr has role
  31. * @param _operator address
  32. * @param _role the name of the role
  33. * @return bool
  34. */
  35. function hasRole(address _operator, string _role)
  36. public
  37. view
  38. returns (bool)
  39. {
  40. return roles[_role].has(_operator);
  41. }
  42. /**
  43. * @dev add a role to an address
  44. * @param _operator address
  45. * @param _role the name of the role
  46. */
  47. function _addRole(address _operator, string _role)
  48. internal
  49. {
  50. roles[_role].add(_operator);
  51. emit RoleAdded(_operator, _role);
  52. }
  53. /**
  54. * @dev remove a role from an address
  55. * @param _operator address
  56. * @param _role the name of the role
  57. */
  58. function _removeRole(address _operator, string _role)
  59. internal
  60. {
  61. roles[_role].remove(_operator);
  62. emit RoleRemoved(_operator, _role);
  63. }
  64. /**
  65. * @dev modifier to scope access to a single role (uses msg.sender as addr)
  66. * @param _role the name of the role
  67. * // reverts
  68. */
  69. modifier onlyRole(string _role)
  70. {
  71. checkRole(msg.sender, _role);
  72. _;
  73. }
  74. /**
  75. * @dev modifier to scope access to a set of roles (uses msg.sender as addr)
  76. * @param _roles the names of the roles to scope access to
  77. * // reverts
  78. *
  79. * @TODO - when solidity supports dynamic arrays as arguments to modifiers, provide this
  80. * see: https://github.com/ethereum/solidity/issues/2467
  81. */
  82. // modifier onlyRoles(string[] _roles) {
  83. // bool hasAnyRole = false;
  84. // for (uint8 i = 0; i < _roles.length; i++) {
  85. // if (hasRole(msg.sender, _roles[i])) {
  86. // hasAnyRole = true;
  87. // break;
  88. // }
  89. // }
  90. // require(hasAnyRole);
  91. // _;
  92. // }
  93. }