代码拉取完成,页面将自动刷新
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;
/**
权限控制合约
*/
contract AccessControl {
event GrantRole(bytes32 indexed role, address indexed addr);
event RevokeRole(bytes32 indexed role, address indexed addr);
// role nick name => address => bool 角色映射
mapping(bytes32 => mapping(address => bool)) public roles;
// role name hash
bytes32 private constant ADMIN = keccak256(abi.encodePacked("ADMIN"));
bytes32 private constant USER = keccak256(abi.encodePacked("USER"));
// 管理员权限
modifier onlyRole(bytes32 _role) {
require(roles[_role][msg.sender], "not authorized");
_;
}
// 通过构造函数给部署合约者授予管理员权限
constructor() {
_grantRole(ADMIN, msg.sender);
}
/**
* grant role
*/
function _grantRole(bytes32 _role, address _addr) internal {
roles[_role][_addr] = true;
// 向外部抛出执行状态事件
emit GrantRole(_role, _addr);
}
function grantRole(bytes32 _role, address _addr) external onlyRole(ADMIN){
roles[_role][_addr] = true;
// 向外部抛出执行状态事件
emit GrantRole(_role, _addr);
}
/**
* revoke role
*/
function revokeRole(bytes32 _role, address _addr) external onlyRole(ADMIN) {
roles[_role][_addr] = false;
// 向外部抛出执行状态事件
emit RevokeRole(_role, _addr);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。