You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
2.7 KiB
108 lines
2.7 KiB
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.
|
|
* It's also recommended that you define constants in the contract, like ROLE_ADMIN below,
|
|
* to avoid typos.
|
|
*/
|
|
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)
|
|
view
|
|
public
|
|
{
|
|
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)
|
|
view
|
|
public
|
|
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);
|
|
|
|
// _;
|
|
// }
|
|
}
|
|
|