123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- pragma solidity ^0.4.24;
- import "./Roles.sol";
- /**
- * @title RBAC (Role-Based Access Control)
- * @author Matt Condon (@Shrugs)
- * @dev Stores and provides setters and getters for roles and addresses.
- * Supports unlimited numbers of roles and addresses.
- * See //contracts/mocks/RBACMock.sol for an example of usage.
- * This RBAC method uses strings to key roles. It may be beneficial
- * for you to write your own implementation of this interface using Enums or similar.
- */
- contract RBAC {
- using Roles for Roles.Role;
- mapping (string => Roles.Role) private roles;
- event RoleAdded(address indexed operator, string role);
- event RoleRemoved(address indexed operator, string role);
- /**
- * @dev reverts if addr does not have role
- * @param _operator address
- * @param _role the name of the role
- * // reverts
- */
- function checkRole(address _operator, string _role)
- public
- view
- {
- roles[_role].check(_operator);
- }
- /**
- * @dev determine if addr has role
- * @param _operator address
- * @param _role the name of the role
- * @return bool
- */
- function hasRole(address _operator, string _role)
- public
- view
- returns (bool)
- {
- return roles[_role].has(_operator);
- }
- /**
- * @dev add a role to an address
- * @param _operator address
- * @param _role the name of the role
- */
- function _addRole(address _operator, string _role)
- internal
- {
- roles[_role].add(_operator);
- emit RoleAdded(_operator, _role);
- }
- /**
- * @dev remove a role from an address
- * @param _operator address
- * @param _role the name of the role
- */
- function _removeRole(address _operator, string _role)
- internal
- {
- roles[_role].remove(_operator);
- emit RoleRemoved(_operator, _role);
- }
- /**
- * @dev modifier to scope access to a single role (uses msg.sender as addr)
- * @param _role the name of the role
- * // reverts
- */
- modifier onlyRole(string _role)
- {
- checkRole(msg.sender, _role);
- _;
- }
- /**
- * @dev modifier to scope access to a set of roles (uses msg.sender as addr)
- * @param _roles the names of the roles to scope access to
- * // reverts
- *
- * @TODO - when solidity supports dynamic arrays as arguments to modifiers, provide this
- * see: https://github.com/ethereum/solidity/issues/2467
- */
- // modifier onlyRoles(string[] _roles) {
- // bool hasAnyRole = false;
- // for (uint8 i = 0; i < _roles.length; i++) {
- // if (hasRole(msg.sender, _roles[i])) {
- // hasAnyRole = true;
- // break;
- // }
- // }
- // require(hasAnyRole);
- // _;
- // }
- }
|