0

For my Master's thesis, I am trying to create a decentralized file storage system using ethereum, solidity and ipfs. I want to code the smart contract with attribute-based access control but do not have much knowledge on this. Most of the links I have seen so far are of role-based access control. Could someone please guide me with a few links or a simple code explanation?

0

Maybe something like this:

A library:

library Roles {
    enum Flag {
        Role1,
        Role2,
        Role3
    }

    function isAuthorizedToRole1(uint256 _flags) internal pure returns (bool) {return isAuthorized(_flags, Flag.Role1);}
    function isAuthorizedToRole2(uint256 _flags) internal pure returns (bool) {return isAuthorized(_flags, Flag.Role2);}
    function isAuthorizedToRole3(uint256 _flags) internal pure returns (bool) {return isAuthorized(_flags, Flag.Role3);}
    function isAuthorized(uint256 _flags, Flag _flag) private pure returns (bool) {return ((_flags >> uint256(_flag)) & 1) == 1;}
}

A contract:

contract Users {
    address public owner;
    mapping(address => uint256) public roles;

    constructor() {
        owner = msg.sender;
    }

    function getRole(address _user) external view returns (uint256) {
        return roles[_user];
    }

    function setRole(address _user, uint256 _role) external {
        require(msg.sender == owner, "illegal caller");
        roles[_user] = _role;
    }
}

Then, any other contract can utilize the above in order to manage access-control; for example:

contract Manager {
    using Roles for uint256;

    Users public users;

    constructor(Users _users) public {
        users = _users;
    }

    function isAuthorizedToRole1(address _user) external view returns (bool) {
        return users.getRole(_user).isAuthorizedToRole1();
    }

    function isAuthorizedToRole2(address _user) external view returns (bool) {
        return users.getRole(_user).isAuthorizedToRole2();
    }

    function isAuthorizedToRole3(address _user) external view returns (bool) {
        return users.getRole(_user).isAuthorizedToRole3();
    }
}

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.