RBAC.sol 2.6 KB

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