Whitelist.sol 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. pragma solidity ^0.4.24;
  2. import "../ownership/Ownable.sol";
  3. import "../access/rbac/RBAC.sol";
  4. /**
  5. * @title Whitelist
  6. * @dev The Whitelist contract has a whitelist of addresses, and provides basic authorization control functions.
  7. * This simplifies the implementation of "user permissions".
  8. */
  9. contract Whitelist is Ownable, RBAC {
  10. // Name of the whitelisted role.
  11. string private constant ROLE_WHITELISTED = "whitelist";
  12. /**
  13. * @dev Throws if operator is not whitelisted.
  14. * @param _operator address
  15. */
  16. modifier onlyIfWhitelisted(address _operator) {
  17. checkRole(_operator, ROLE_WHITELISTED);
  18. _;
  19. }
  20. /**
  21. * @dev add an address to the whitelist
  22. * @param _operator address
  23. * @return true if the address was added to the whitelist, false if the address was already in the whitelist
  24. */
  25. function addAddressToWhitelist(address _operator)
  26. public
  27. onlyOwner
  28. {
  29. _addRole(_operator, ROLE_WHITELISTED);
  30. }
  31. /**
  32. * @dev Determine if an account is whitelisted.
  33. * @return true if the account is whitelisted, false otherwise.
  34. */
  35. function isWhitelisted(address _operator)
  36. public
  37. view
  38. returns (bool)
  39. {
  40. return hasRole(_operator, ROLE_WHITELISTED);
  41. }
  42. /**
  43. * @dev add addresses to the whitelist
  44. * @param _operators addresses
  45. * @return true if at least one address was added to the whitelist,
  46. * false if all addresses were already in the whitelist
  47. */
  48. function addAddressesToWhitelist(address[] _operators)
  49. public
  50. onlyOwner
  51. {
  52. for (uint256 i = 0; i < _operators.length; i++) {
  53. addAddressToWhitelist(_operators[i]);
  54. }
  55. }
  56. /**
  57. * @dev remove an address from the whitelist
  58. * @param _operator address
  59. * @return true if the address was removed from the whitelist,
  60. * false if the address wasn't in the whitelist in the first place
  61. */
  62. function removeAddressFromWhitelist(address _operator)
  63. public
  64. onlyOwner
  65. {
  66. _removeRole(_operator, ROLE_WHITELISTED);
  67. }
  68. /**
  69. * @dev remove addresses from the whitelist
  70. * @param _operators addresses
  71. * @return true if at least one address was removed from the whitelist,
  72. * false if all addresses weren't in the whitelist in the first place
  73. */
  74. function removeAddressesFromWhitelist(address[] _operators)
  75. public
  76. onlyOwner
  77. {
  78. for (uint256 i = 0; i < _operators.length; i++) {
  79. removeAddressFromWhitelist(_operators[i]);
  80. }
  81. }
  82. }