Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring Superuser contract to allow Owners to transfer ownership … #978

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 17 additions & 9 deletions contracts/ownership/Ownable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,29 @@ contract Ownable {
_;
}

/**
* @dev Allows the current owner to relinquish control of the contract.
*/
function renounceOwnership() public onlyOwner {
emit OwnershipRenounced(owner);
owner = address(0);
}

/**
* @dev Allows the current owner to transfer control of the contract to a newOwner.
* @param newOwner The address to transfer ownership to.
* @param _newOwner The address to transfer ownership to.
*/
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
function transferOwnership(address _newOwner) public onlyOwner {
_transferOwnership(_newOwner);
}

/**
* @dev Allows the current owner to relinquish control of the contract.
* @dev Transfers control of the contract to a newOwner.
* @param _newOwner The address to transfer ownership to.
*/
function renounceOwnership() public onlyOwner {
emit OwnershipRenounced(owner);
owner = address(0);
function _transferOwnership(address _newOwner) internal {
require(_newOwner != address(0));
emit OwnershipTransferred(owner, _newOwner);
owner = _newOwner;
}
}
18 changes: 9 additions & 9 deletions contracts/ownership/Superuser.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ contract Superuser is Ownable, RBAC {
_;
}

modifier onlyOwnerOrSuperuser() {
require(msg.sender == owner || isSuperuser(msg.sender));
_;
}

/**
* @dev getter to determine if address has superuser role
*/
Expand All @@ -41,22 +46,17 @@ contract Superuser is Ownable, RBAC {
* @dev Allows the current superuser to transfer his role to a newSuperuser.
* @param _newSuperuser The address to transfer ownership to.
*/
function transferSuperuser(address _newSuperuser)
onlySuperuser
public
{
function transferSuperuser(address _newSuperuser) public onlySuperuser {
require(_newSuperuser != address(0));
removeRole(msg.sender, ROLE_SUPERUSER);
addRole(_newSuperuser, ROLE_SUPERUSER);
}

/**
* @dev Allows the current superuser to transfer control of the contract to a newOwner.
* @dev Allows the current superuser or owner to transfer control of the contract to a newOwner.
* @param _newOwner The address to transfer ownership to.
*/
function transferOwnership(address _newOwner) public onlySuperuser {
require(_newOwner != address(0));
owner = _newOwner;
emit OwnershipTransferred(owner, _newOwner);
function transferOwnership(address _newOwner) public onlyOwnerOrSuperuser {
_transferOwnership(_newOwner);
}
}
15 changes: 13 additions & 2 deletions test/ownership/Superuser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ contract('Superuser', function (accounts) {
firstOwner,
newSuperuser,
newOwner,
finalOwner,
anyone,
] = accounts;

Expand All @@ -35,7 +36,7 @@ contract('Superuser', function (accounts) {
address1IsSuperuser.should.be.equal(true);
});

it('should change owner after transferring', async function () {
it('should change owner after the superuser transfers the ownership', async function () {
await expectEvent.inTransaction(
this.superuser.transferOwnership(newOwner, { from: newSuperuser }),
'OwnershipTransferred'
Expand All @@ -44,6 +45,16 @@ contract('Superuser', function (accounts) {
const currentOwner = await this.superuser.owner();
currentOwner.should.be.equal(newOwner);
});

it('should change owner after the owner transfers the ownership', async function () {
await expectEvent.inTransaction(
this.superuser.transferOwnership(finalOwner, { from: newOwner }),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was confused that newOwner was sending this tx, but I saw that you're using the same Superuser instance for all tests! You should create the Superuser instance in a beforeEach block instead of before, so as to have the tests be independent of one another.

Then it will be firstOwner sending this tx.

'OwnershipTransferred'
);

const currentOwner = await this.superuser.owner();
currentOwner.should.be.equal(finalOwner);
});
});

context('in adversarial conditions', () => {
Expand All @@ -53,7 +64,7 @@ contract('Superuser', function (accounts) {
);
});

it('should prevent non-superusers from setting a new owner', async function () {
it('should prevent users that are not superuser nor owner from setting a new owner', async function () {
await expectThrow(
this.superuser.transferOwnership(newOwner, { from: anyone })
);
Expand Down