1

I want the owner of the smart contract to give/add authorization to the user of the modifier . and the only the users in the array will have access to the certain functions.

How to make modifier to check the list of the address.?

contract Owned {
    address owner;
    address[] listuser;
    function Owned() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    function whitelistAddress (address user) onlyOwner {
        //add users address to the listuser array
    }   

    modifier onlyusers() {
        //How can i assign access to users address in the array listuser
        require();
        _;
    }

    function accessbyonlyusers () onlyusers {
    }
}

I want the function accessonlyusers to be accessed my list of address in the listusers

1

As @ivicaa said, this is better implemented as a mapping. Here's the full implementation for that:

contract Owned {
    address owner;
    mapping(address=>bool) allowedUsers;
    function Owned() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    function whitelistAddress (address user) onlyOwner {
        allowedUsers[user]=true;
    }   

    modifier onlyusers() {
        //How can i assign access to users address in the array listuser
        require(allowedUsers[msg.sender]);
        _;
    }

    function accessbyonlyusers () onlyusers {
        //Only runs if the user is whitelisted
    }
}

However, if you absolutely can't do this because users is an array anyway, here is how this could be implemented as an array (this will be more expensive since you're iterating over a boundless arrya, which is generally bad practice in the EVM)

contract Owned {
    address owner;
    address[] listuser;
    function Owned() {
        owner = msg.sender;
    }

    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    function whitelistAddress (address user) onlyOwner {
        listuser.push(user);
    }   

    modifier onlyusers() {
        //How can i assign access to users address in the array listuser

        for(uint index = 0; index < listuser; index++){
            if(userlist[index]==msg.sender){
                _;
                return;
            }
        }
        revert(); 
    }

    function accessbyonlyusers () onlyusers {
        //Only runs if the user is whitelisted
    }
}
  • Thanks, In this code Owner can add access to different address . can he also revoke/blacklist the access to user. can this code be helpful for the same ? function blacklistAddress (address user) onlyOwner { allowedUsers[user]=false; } – Deepan Feb 25 '18 at 0:48
0

It's more handy and it's better for the performance to have a mapping.

...
mapping (address => bool) users;
...
require(users[msg.sender]);
...

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.