From de74857e1f18bff2ad881b7ac11be9a0f72e033d Mon Sep 17 00:00:00 2001 From: KOSASIH Date: Wed, 28 Aug 2024 10:57:31 +0700 Subject: [PATCH] Create AccessControl.sol --- .../security-and-integrity/AccessControl.sol | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 blockchain_integration/pi_network/features/security-and-integrity/AccessControl.sol diff --git a/blockchain_integration/pi_network/features/security-and-integrity/AccessControl.sol b/blockchain_integration/pi_network/features/security-and-integrity/AccessControl.sol new file mode 100644 index 000000000..96bdd15d0 --- /dev/null +++ b/blockchain_integration/pi_network/features/security-and-integrity/AccessControl.sol @@ -0,0 +1,44 @@ +pragma solidity ^0.8.0; + +contract AccessControl { + // Mapping of access control roles + mapping (address => Role) public roles; + + // Struct to represent an access control role + struct Role { + address owner; + bytes32 role; + bool authorized; + } + + // Event emitted when a new role is created + event NewRole(address indexed owner, bytes32 role); + + // Event emitted when a role is updated + event UpdateRole(address indexed owner, bytes32 role); + + // Function to create a new role + function createRole(bytes32 _role) public { + address owner = msg.sender; + Role storage role = roles[owner]; + role.owner = owner; + role.role = _role; + role.authorized = true; + emit NewRole(owner, _role); + } + + // Function to update a role + function updateRole(bytes32 _role) public { + address owner = msg.sender; + Role storage role = roles[owner]; + require(role.owner == owner, "Unauthorized access"); + role.role = _role; + emit UpdateRole(owner, _role); + } + + // Function to check if a user has a specific role + function hasRole(address _owner, bytes32 _role) internal returns (bool) { + Role storage role = roles[_owner]; + return role.role == _role && role.authorized; + } +}