-
Notifications
You must be signed in to change notification settings - Fork 0
/
accessmanagment.sol
46 lines (38 loc) · 1.38 KB
/
accessmanagment.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
contract Access {
event GrantRole(bytes32 indexed role, address indexed account);
event RevokeRole(bytes32 indexed role, address indexed account);
mapping(bytes32 => mapping(address => bool)) public roles;
modifier onlyRole(bytes32 _role) {
require(roles[_role][msg.sender], "it is not authorized role");
_;
}
constructor() {
//Person which deploys contract is Admin
_grantRole(Admin, msg.sender);
}
// Used for definig roles
bytes32 private constant Admin = keccak256(abi.encodePacked("Admin"));
bytes32 private constant User = keccak256(abi.encodePacked("User"));
//Function defines the role inside of SC internaly
function _grantRole(bytes32 _role, address _account) internal {
roles[_role][_account] = true;
emit GrantRole(_role, _account);
}
//Function defines the role inside of SC externaly and can be only called if msg.sender has Admin role granted
function grantRole(bytes32 _role, address _account)
external
onlyRole(Admin)
{
_grantRole(_role, _account);
}
//Only Admin can revoke contract and role
function revokeRole(bytes32 _role, address _account)
external
onlyRole(Admin)
{
roles[_role][_account] = false;
emit RevokeRole(_role, _account);
}
}