1

im trying to do multiple admin system in my solidity and here is example

//SPDX-License-Identif`ier: MIT

pragma solidity 0.8.15;
import "@openzeppelin/contracts/access/Ownable.sol";

contract MultipleAdmin{

  modifier onlyAdmin {
    require(isAdmin[msg.sender] == true, 'This method can call only admin');
    _;
  }

  mapping(address => bool) public isAdmin;

  function addAdmin(address _admin) public onlyOwner{
      require(isAdmin[_admin] == false, 'This address already admin');
      isAdmin[_admin] = true;
  }

  function removeAdmin(address _admin) public onlyOwner{
      require(isAdmin[_admin] == true, 'This address is not in admin list');
      isAdmin[_admin] = false;
  }

  function doSometh() public onlyAdmin{
      doSomething
  }
}

is it secure to use like this?

1 Answer 1

0

If your goal is to allow the contract owner access to control who is an admin and who is not, and then allow the admins to have special permission to use functions marked onlyAdmin, then your example implementation looks correct.

Also, if you want to use an already existing contract that may fit your needs, you could try using Open Zeppelin's accessControl contract:

abstract contract AccessControl is Context, IAccessControl, ERC165 {
    struct RoleData {
        mapping(address account => bool) hasRole;
        bytes32 adminRole;
    }

    mapping(bytes32 role => RoleData) private _roles;

    bytes32 public constant DEFAULT_ADMIN_ROLE = 0x00;

    modifier onlyRole(bytes32 role) {
        _checkRole(role);
        _;
    }

    function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
        return interfaceId == type(IAccessControl).interfaceId || super.supportsInterface(interfaceId);
    }

    function hasRole(bytes32 role, address account) public view virtual returns (bool) {
        return _roles[role].hasRole[account];
    }

    function _checkRole(bytes32 role) internal view virtual {
        _checkRole(role, _msgSender());
    }

    function _checkRole(bytes32 role, address account) internal view virtual {
        if (!hasRole(role, account)) {
            revert AccessControlUnauthorizedAccount(account, role);
        }
    }

    function getRoleAdmin(bytes32 role) public view virtual returns (bytes32) {
        return _roles[role].adminRole;
    }

    function grantRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) {
        _grantRole(role, account);
    }

    function revokeRole(bytes32 role, address account) public virtual onlyRole(getRoleAdmin(role)) {
        _revokeRole(role, account);
    }

    function renounceRole(bytes32 role, address callerConfirmation) public virtual {
        if (callerConfirmation != _msgSender()) {
            revert AccessControlBadConfirmation();
        }

        _revokeRole(role, callerConfirmation);
    }

    function _setRoleAdmin(bytes32 role, bytes32 adminRole) internal virtual {
        bytes32 previousAdminRole = getRoleAdmin(role);
        _roles[role].adminRole = adminRole;
        emit RoleAdminChanged(role, previousAdminRole, adminRole);
    }

    function _grantRole(bytes32 role, address account) internal virtual returns (bool) {
        if (!hasRole(role, account)) {
            _roles[role].hasRole[account] = true;
            emit RoleGranted(role, account, _msgSender());
            return true;
        } else {
            return false;
        }
    }

    function _revokeRole(bytes32 role, address account) internal virtual returns (bool) {
        if (hasRole(role, account)) {
            _roles[role].hasRole[account] = false;
            emit RoleRevoked(role, account, _msgSender());
            return true;
        } else {
            return false;
        }
    }
}

Restricting a function to only a certain role would look like this:

function foo() public {
    require(hasRole(MY_ROLE, msg.sender));
      ...
  }

or with the modifier:

function foo() public onlyRole(MY_ROLE) {
      ...
  }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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