Unverified Commit 4223c9d5 by Matt Condon Committed by GitHub

feat: refactor whitelist.sol to use RBAC (#893)

* feat: refactor whitelist.sol to use RBAC

* fix: remove poor backwards compat attempt
parent f0eea31b
...@@ -2,6 +2,7 @@ pragma solidity ^0.4.21; ...@@ -2,6 +2,7 @@ pragma solidity ^0.4.21;
import "./Ownable.sol"; import "./Ownable.sol";
import "./rbac/RBAC.sol";
/** /**
...@@ -9,17 +10,17 @@ import "./Ownable.sol"; ...@@ -9,17 +10,17 @@ import "./Ownable.sol";
* @dev The Whitelist contract has a whitelist of addresses, and provides basic authorization control functions. * @dev The Whitelist contract has a whitelist of addresses, and provides basic authorization control functions.
* @dev This simplifies the implementation of "user permissions". * @dev This simplifies the implementation of "user permissions".
*/ */
contract Whitelist is Ownable { contract Whitelist is Ownable, RBAC {
mapping(address => bool) public whitelist;
event WhitelistedAddressAdded(address addr); event WhitelistedAddressAdded(address addr);
event WhitelistedAddressRemoved(address addr); event WhitelistedAddressRemoved(address addr);
string public constant ROLE_WHITELISTED = "whitelist";
/** /**
* @dev Throws if called by any account that's not whitelisted. * @dev Throws if called by any account that's not whitelisted.
*/ */
modifier onlyWhitelisted() { modifier onlyWhitelisted() {
require(whitelist[msg.sender]); checkRole(msg.sender, ROLE_WHITELISTED);
_; _;
} }
...@@ -28,12 +29,23 @@ contract Whitelist is Ownable { ...@@ -28,12 +29,23 @@ contract Whitelist is Ownable {
* @param addr address * @param addr address
* @return true if the address was added to the whitelist, false if the address was already in the whitelist * @return true if the address was added to the whitelist, false if the address was already in the whitelist
*/ */
function addAddressToWhitelist(address addr) onlyOwner public returns(bool success) { function addAddressToWhitelist(address addr)
if (!whitelist[addr]) { onlyOwner
whitelist[addr] = true; public
{
addRole(addr, ROLE_WHITELISTED);
emit WhitelistedAddressAdded(addr); emit WhitelistedAddressAdded(addr);
success = true;
} }
/**
* @dev getter to determine if address is in whitelist
*/
function whitelist(address addr)
public
view
returns (bool)
{
return hasRole(addr, ROLE_WHITELISTED);
} }
/** /**
...@@ -42,11 +54,12 @@ contract Whitelist is Ownable { ...@@ -42,11 +54,12 @@ contract Whitelist is Ownable {
* @return true if at least one address was added to the whitelist, * @return true if at least one address was added to the whitelist,
* false if all addresses were already in the whitelist * false if all addresses were already in the whitelist
*/ */
function addAddressesToWhitelist(address[] addrs) onlyOwner public returns(bool success) { function addAddressesToWhitelist(address[] addrs)
onlyOwner
public
{
for (uint256 i = 0; i < addrs.length; i++) { for (uint256 i = 0; i < addrs.length; i++) {
if (addAddressToWhitelist(addrs[i])) { addAddressToWhitelist(addrs[i]);
success = true;
}
} }
} }
...@@ -56,12 +69,12 @@ contract Whitelist is Ownable { ...@@ -56,12 +69,12 @@ contract Whitelist is Ownable {
* @return true if the address was removed from the whitelist, * @return true if the address was removed from the whitelist,
* false if the address wasn't in the whitelist in the first place * false if the address wasn't in the whitelist in the first place
*/ */
function removeAddressFromWhitelist(address addr) onlyOwner public returns(bool success) { function removeAddressFromWhitelist(address addr)
if (whitelist[addr]) { onlyOwner
whitelist[addr] = false; public
{
removeRole(addr, ROLE_WHITELISTED);
emit WhitelistedAddressRemoved(addr); emit WhitelistedAddressRemoved(addr);
success = true;
}
} }
/** /**
...@@ -70,11 +83,12 @@ contract Whitelist is Ownable { ...@@ -70,11 +83,12 @@ contract Whitelist is Ownable {
* @return true if at least one address was removed from the whitelist, * @return true if at least one address was removed from the whitelist,
* false if all addresses weren't in the whitelist in the first place * false if all addresses weren't in the whitelist in the first place
*/ */
function removeAddressesFromWhitelist(address[] addrs) onlyOwner public returns(bool success) { function removeAddressesFromWhitelist(address[] addrs)
onlyOwner
public
{
for (uint256 i = 0; i < addrs.length; i++) { for (uint256 i = 0; i < addrs.length; i++) {
if (removeAddressFromWhitelist(addrs[i])) { removeAddressFromWhitelist(addrs[i]);
success = true;
}
} }
} }
......
...@@ -44,11 +44,6 @@ contract('Whitelist', function (accounts) { ...@@ -44,11 +44,6 @@ contract('Whitelist', function (accounts) {
} }
}); });
it('should not announce WhitelistedAddressAdded event if address is already in the whitelist', async function () {
const { logs } = await mock.addAddressToWhitelist(whitelistedAddress1, { from: owner });
logs.should.be.empty;
});
it('should remove address from the whitelist', async function () { it('should remove address from the whitelist', async function () {
await expectEvent.inTransaction( await expectEvent.inTransaction(
mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner }), mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner }),
...@@ -69,11 +64,6 @@ contract('Whitelist', function (accounts) { ...@@ -69,11 +64,6 @@ contract('Whitelist', function (accounts) {
} }
}); });
it('should not announce WhitelistedAddressRemoved event if address is not in the whitelist', async function () {
const { logs } = await mock.removeAddressFromWhitelist(whitelistedAddress1, { from: owner });
logs.should.be.empty;
});
it('should allow whitelisted address to call #onlyWhitelistedCanDoThis', async () => { it('should allow whitelisted address to call #onlyWhitelistedCanDoThis', async () => {
await mock.addAddressToWhitelist(whitelistedAddress1, { from: owner }); await mock.addAddressToWhitelist(whitelistedAddress1, { from: owner });
await mock.onlyWhitelistedCanDoThis({ from: whitelistedAddress1 }) await mock.onlyWhitelistedCanDoThis({ from: whitelistedAddress1 })
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment