2 Star 3 Fork 4

GKing/Solidity8_perfect

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
S_44_AccessControl.sol 1.44 KB
一键复制 编辑 原始数据 按行查看 历史
GKing 提交于 2022-11-12 12:05 +08:00 . Access Control 权限控制
// 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);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/jacky2code/solidity8_perfect.git
[email protected]:jacky2code/solidity8_perfect.git
jacky2code
solidity8_perfect
Solidity8_perfect
master

搜索帮助