Whitelist.sol 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. pragma solidity ^0.4.24;
  2. import "../ownership/Ownable.sol";
  3. import "../ownership/rbac/RBAC.sol";
  4. /**
  5. * @title Whitelist
  6. * @dev The Whitelist contract has a whitelist of addresses, and provides basic authorization control functions.
  7. * @dev This simplifies the implementation of "user permissions".
  8. */
  9. contract Whitelist is Ownable, RBAC {
  10. event WhitelistedAddressAdded(address addr);
  11. event WhitelistedAddressRemoved(address addr);
  12. string public constant ROLE_WHITELISTED = "whitelist";
  13. /**
  14. * @dev Throws if called by any account that's not whitelisted.
  15. */
  16. modifier onlyWhitelisted() {
  17. checkRole(msg.sender, ROLE_WHITELISTED);
  18. _;
  19. }
  20. /**
  21. * @dev add an address to the whitelist
  22. * @param addr 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 addr)
  26. onlyOwner
  27. public
  28. {
  29. addRole(addr, ROLE_WHITELISTED);
  30. emit WhitelistedAddressAdded(addr);
  31. }
  32. /**
  33. * @dev getter to determine if address is in whitelist
  34. */
  35. function whitelist(address addr)
  36. public
  37. view
  38. returns (bool)
  39. {
  40. return hasRole(addr, ROLE_WHITELISTED);
  41. }
  42. /**
  43. * @dev add addresses to the whitelist
  44. * @param addrs 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[] addrs)
  49. onlyOwner
  50. public
  51. {
  52. for (uint256 i = 0; i < addrs.length; i++) {
  53. addAddressToWhitelist(addrs[i]);
  54. }
  55. }
  56. /**
  57. * @dev remove an address from the whitelist
  58. * @param addr 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 addr)
  63. onlyOwner
  64. public
  65. {
  66. removeRole(addr, ROLE_WHITELISTED);
  67. emit WhitelistedAddressRemoved(addr);
  68. }
  69. /**
  70. * @dev remove addresses from the whitelist
  71. * @param addrs addresses
  72. * @return true if at least one address was removed from the whitelist,
  73. * false if all addresses weren't in the whitelist in the first place
  74. */
  75. function removeAddressesFromWhitelist(address[] addrs)
  76. onlyOwner
  77. public
  78. {
  79. for (uint256 i = 0; i < addrs.length; i++) {
  80. removeAddressFromWhitelist(addrs[i]);
  81. }
  82. }
  83. }