feat: add adminAddRole, adminRemoveRole, and make hasRole/checkRole public
This commit is contained in:
32
test/RBAC.js
32
test/RBAC.js
@ -10,39 +10,39 @@ contract('RBAC', function(accounts) {
|
||||
let mock
|
||||
|
||||
const [
|
||||
owner,
|
||||
admin,
|
||||
anyone,
|
||||
...advisors
|
||||
] = accounts
|
||||
|
||||
before(async () => {
|
||||
mock = await RBACMock.new(advisors, { from: owner })
|
||||
mock = await RBACMock.new(advisors, { from: admin })
|
||||
})
|
||||
|
||||
context('in normal conditions', () => {
|
||||
it('allows owner to call #onlyOwnersCanDoThis', async () => {
|
||||
await mock.onlyOwnersCanDoThis({ from: owner })
|
||||
it('allows admin to call #onlyAdminsCanDoThis', async () => {
|
||||
await mock.onlyAdminsCanDoThis({ from: admin })
|
||||
.should.be.fulfilled
|
||||
})
|
||||
it('allows owner to call #onlyAdvisorsCanDoThis', async () => {
|
||||
await mock.onlyAdvisorsCanDoThis({ from: owner })
|
||||
it('allows admin to call #onlyAdvisorsCanDoThis', async () => {
|
||||
await mock.onlyAdvisorsCanDoThis({ from: admin })
|
||||
.should.be.fulfilled
|
||||
})
|
||||
it('allows advisors to call #onlyAdvisorsCanDoThis', async () => {
|
||||
await mock.onlyAdvisorsCanDoThis({ from: advisors[0] })
|
||||
.should.be.fulfilled
|
||||
})
|
||||
it('allows owner to call #eitherOwnerOrAdvisorCanDoThis', async () => {
|
||||
await mock.eitherOwnerOrAdvisorCanDoThis({ from: owner })
|
||||
it('allows admin to call #eitherAdminOrAdvisorCanDoThis', async () => {
|
||||
await mock.eitherAdminOrAdvisorCanDoThis({ from: admin })
|
||||
.should.be.fulfilled
|
||||
})
|
||||
it('allows advisors to call #eitherOwnerOrAdvisorCanDoThis', async () => {
|
||||
await mock.eitherOwnerOrAdvisorCanDoThis({ from: advisors[0] })
|
||||
it('allows advisors to call #eitherAdminOrAdvisorCanDoThis', async () => {
|
||||
await mock.eitherAdminOrAdvisorCanDoThis({ from: advisors[0] })
|
||||
.should.be.fulfilled
|
||||
})
|
||||
it('does not allow owners to call #nobodyCanDoThis', async () => {
|
||||
it('does not allow admins to call #nobodyCanDoThis', async () => {
|
||||
expectThrow(
|
||||
mock.nobodyCanDoThis({ from: owner })
|
||||
mock.nobodyCanDoThis({ from: admin })
|
||||
)
|
||||
})
|
||||
it('does not allow advisors to call #nobodyCanDoThis', async () => {
|
||||
@ -55,8 +55,12 @@ contract('RBAC', function(accounts) {
|
||||
mock.nobodyCanDoThis({ from: anyone })
|
||||
)
|
||||
})
|
||||
it('allows an owner to remove an advisor\'s role', async () => {
|
||||
await mock.removeAdvisor(advisors[0], { from: owner })
|
||||
it('allows an admin to remove an advisor\'s role', async () => {
|
||||
await mock.removeAdvisor(advisors[0], { from: admin })
|
||||
.should.be.fulfilled
|
||||
})
|
||||
it('allows admins to #adminRemoveRole', async () => {
|
||||
await mock.adminRemoveRole(advisors[3], 'advisor', { from: admin })
|
||||
.should.be.fulfilled
|
||||
})
|
||||
})
|
||||
|
||||
@ -5,10 +5,10 @@ import '../../contracts/ownership/rbac/RBAC.sol';
|
||||
|
||||
contract RBACMock is RBAC {
|
||||
|
||||
modifier onlyOwnerOrAdvisor()
|
||||
modifier onlyAdminOrAdvisor()
|
||||
{
|
||||
require(
|
||||
hasRole(msg.sender, "owner") ||
|
||||
hasRole(msg.sender, "admin") ||
|
||||
hasRole(msg.sender, "advisor")
|
||||
);
|
||||
_;
|
||||
@ -17,7 +17,7 @@ contract RBACMock is RBAC {
|
||||
function RBACMock(address[] _advisors)
|
||||
public
|
||||
{
|
||||
addRole(msg.sender, "owner");
|
||||
addRole(msg.sender, "admin");
|
||||
addRole(msg.sender, "advisor");
|
||||
|
||||
for (uint256 i = 0; i < _advisors.length; i++) {
|
||||
@ -25,8 +25,8 @@ contract RBACMock is RBAC {
|
||||
}
|
||||
}
|
||||
|
||||
function onlyOwnersCanDoThis()
|
||||
onlyRole("owner")
|
||||
function onlyAdminsCanDoThis()
|
||||
onlyRole("admin")
|
||||
view
|
||||
external
|
||||
{
|
||||
@ -39,8 +39,8 @@ contract RBACMock is RBAC {
|
||||
{
|
||||
}
|
||||
|
||||
function eitherOwnerOrAdvisorCanDoThis()
|
||||
onlyOwnerOrAdvisor
|
||||
function eitherAdminOrAdvisorCanDoThis()
|
||||
onlyAdminOrAdvisor
|
||||
view
|
||||
external
|
||||
{
|
||||
@ -53,9 +53,9 @@ contract RBACMock is RBAC {
|
||||
{
|
||||
}
|
||||
|
||||
// owners can remove advisor's role
|
||||
// admins can remove advisor's role
|
||||
function removeAdvisor(address _addr)
|
||||
onlyRole("owner")
|
||||
onlyAdmin
|
||||
public
|
||||
{
|
||||
// revert if the user isn't an advisor
|
||||
|
||||
Reference in New Issue
Block a user