1+ // SPDX-License-Identifier: MIT
2+ pragma solidity >= 0.6.12 < 0.9.0 ;
3+
4+ contract ACL {
5+
6+ // Role definition
7+ enum Role { None, User, Admin }
8+
9+ // Mapping of addresses to roles
10+ mapping (address => Role) private roles;
11+
12+ // Event emitted when a role is granted
13+ event RoleGranted (address indexed addr , Role role );
14+
15+ // Event emitted when a role is revoked
16+ event RoleRevoked (address indexed addr , Role role );
17+
18+ // Modifier to restrict function access to specific roles
19+ modifier onlyRole (Role role ) {
20+ require (hasRole (msg .sender , role), "No permission to perform this action " );
21+ _;
22+ }
23+
24+ constructor (address _admin ) {
25+ roles[_admin] = Role.Admin; // Assign admin role to the deployer address
26+ emit RoleGranted (_admin, Role.Admin);
27+ }
28+
29+ // Grant a role to an address
30+ function grantRole (address _addr , Role _role ) public onlyRole (Role.Admin) {
31+ roles[_addr] = _role;
32+ emit RoleGranted (_addr, _role);
33+ }
34+
35+ // Revoke a role from an address
36+ function revokeRole (address _addr , Role _role ) public onlyRole (Role.Admin) {
37+ roles[_addr] = Role.None;
38+ emit RoleRevoked (_addr, _role);
39+ }
40+
41+ // Check if an address has a specific role
42+ function hasRole (address _addr , Role _role ) public view returns (bool ) {
43+ return roles[_addr] == _role;
44+ }
45+ }
46+
47+ contract DocumentRegistry is ACL
48+ {
49+ struct Document
50+ {
51+ address owner;
52+ string content;
53+ }
54+
55+ mapping (uint => Document) private documents;
56+ uint private nextDocumentId = 1 ;
57+
58+ constructor (address _admin ) ACL (_admin) {}
59+
60+ function createDocument (string memory _IPFSHash ) public onlyRole (Role.User) returns (uint )
61+ {
62+ uint documentId = nextDocumentId++ ;
63+ documents[documentId] = Document (msg .sender , _IPFSHash);
64+ return documentId;
65+ }
66+
67+ function updateDocument (uint _documentId , string memory _newIPFSHash ) public onlyRole (Role.User)
68+ {
69+ require (msg .sender == documents[_documentId].owner, "Only the document owner can update the document " );
70+ documents[_documentId].content = _newIPFSHash;
71+ }
72+
73+ function getDocumentContent (uint _documentId ) public view returns (string memory ) {
74+ return documents[_documentId].content;
75+ }
76+ }
0 commit comments