Whitelist.sol 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. pragma solidity ^0.4.21;
  2. import "./Ownable.sol";
  3. /**
  4. * @title Whitelist
  5. * @dev The Whitelist contract has a whitelist of addresses, and provides basic authorization control functions.
  6. * @dev This simplifies the implementation of "user permissions".
  7. */
  8. contract Whitelist is Ownable {
  9. mapping(address => bool) public whitelist;
  10. event WhitelistedAddressAdded(address addr);
  11. event WhitelistedAddressRemoved(address addr);
  12. /**
  13. * @dev Throws if called by any account that's not whitelisted.
  14. */
  15. modifier onlyWhitelisted() {
  16. require(whitelist[msg.sender]);
  17. _;
  18. }
  19. /**
  20. * @dev add an address to the whitelist
  21. * @param addr address
  22. * @return true if the address was added to the whitelist, false if the address was already in the whitelist
  23. */
  24. function addAddressToWhitelist(address addr) onlyOwner public returns(bool success) {
  25. if (!whitelist[addr]) {
  26. whitelist[addr] = true;
  27. emit WhitelistedAddressAdded(addr);
  28. success = true;
  29. }
  30. }
  31. /**
  32. * @dev add addresses to the whitelist
  33. * @param addrs addresses
  34. * @return true if at least one address was added to the whitelist,
  35. * false if all addresses were already in the whitelist
  36. */
  37. function addAddressesToWhitelist(address[] addrs) onlyOwner public returns(bool success) {
  38. for (uint256 i = 0; i < addrs.length; i++) {
  39. if (addAddressToWhitelist(addrs[i])) {
  40. success = true;
  41. }
  42. }
  43. }
  44. /**
  45. * @dev remove an address from the whitelist
  46. * @param addr address
  47. * @return true if the address was removed from the whitelist,
  48. * false if the address wasn't in the whitelist in the first place
  49. */
  50. function removeAddressFromWhitelist(address addr) onlyOwner public returns(bool success) {
  51. if (whitelist[addr]) {
  52. whitelist[addr] = false;
  53. emit WhitelistedAddressRemoved(addr);
  54. success = true;
  55. }
  56. }
  57. /**
  58. * @dev remove addresses from the whitelist
  59. * @param addrs addresses
  60. * @return true if at least one address was removed from the whitelist,
  61. * false if all addresses weren't in the whitelist in the first place
  62. */
  63. function removeAddressesFromWhitelist(address[] addrs) onlyOwner public returns(bool success) {
  64. for (uint256 i = 0; i < addrs.length; i++) {
  65. if (removeAddressFromWhitelist(addrs[i])) {
  66. success = true;
  67. }
  68. }
  69. }
  70. }