1

Say I want to restrict a function to only authors and editors. And later on, I want to restrict another function to authors and contributors only.

So ideally, I would add a modifier like this:

pragma solidity ^0.4.19;

contract Foo
{
    modifier ifHasOneOf(uint[] roles) {
        for (uint i=0; i < roles.length; i++) {
            if (getRoleForUser(msg.sender) == roles[i]) {
                _;
            }
        }
    }

    function A() public ifHasOneOf([UserRoles.AUTHOR, UserRoles.EDITOR]) {
        // ...
    }

    function B() public ifHasOneOf([UserRoles.AUTHOR, UserRoles.CONTRIBUTOR]) {
        // ...
    }
}

But then I get into dynamic vs fixed size array trouble. Then I was thinking if there may be a way to do it like this:

pragma solidity ^0.4.19;

contract Foo
{
    modifier ifHasRole(uint role) {
        if (getRoleForUser(msg.sender) == role) {
            _;
        }
    }

    function A() public ifHasRole(UserRoles.AUTHOR) ifHasRole(UserRoles.EDITOR) {
        // ...
    }

    function B() public ifHasRole(UserRoles.AUTHOR) ifHasRole(UserRoles.CONTRIBUTOR) {
        // ...
    }
}

But then they both have to comply, and having only one of two roles is enough.

What is the best take on this?

6
  • Can one user have multiple roles? – Jesbus May 18 '18 at 10:00
  • Yes. In my actual code, I have organisations that act in multiple sectors. – Sven Deckers May 18 '18 at 12:13
  • So how does the getRoleForUser function decide which role to return, if the user has multiple? – Jesbus May 18 '18 at 13:56
  • It's just placeholder code to simplify the example, but I see I got the opposite effect here :-) So let's say "no" then, users have only one role. – Sven Deckers May 22 '18 at 7:48
  • Was this solved? I face the same problem – forhas Aug 13 '19 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.