-
Notifications
You must be signed in to change notification settings - Fork 0
/
CRUD-access.sol
140 lines (121 loc) · 3.83 KB
/
CRUD-access.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// SPDX-License-Identifier: None
pragma solidity 0.8.21;
contract UserCrudAccess {
address public owner;
mapping (address => bool) public whiteList;
struct UserStruct {
string userEmail;
uint userAge;
uint index;
}
mapping(address => UserStruct) private userStructs;
address[] private userIndex;
event LogNewUser (address indexed userAddress, uint index, string userEmail, uint userAge);
event LogUpdateUser(address indexed userAddress, uint index, string userEmail, uint userAge);
event LogDeleteUser(address indexed userAddress, uint index);
constructor() {
owner = msg.sender;
whiteList[msg.sender] = true;
}
function isUser(address userAddress) public view returns(bool isIndeed)
{
if(userIndex.length == 0) return false;
return (userIndex[userStructs[userAddress].index] == userAddress);
}
modifier onlyOwner {
require(msg.sender == owner,"Only owner");
_;
}
modifier onlyWhitelist {
require(whiteList[msg.sender] == true, "Only whitelist");
_;
}
function insertUser(
address userAddress,
string memory userEmail,
uint userAge)
external
returns(uint index)
{
require(!isUser(userAddress), "Already exists");
userStructs[userAddress].userEmail = userEmail;
userStructs[userAddress].userAge = userAge;
userIndex.push(userAddress);
userStructs[userAddress].index = userIndex.length-1;
emit LogNewUser(
userAddress,
userStructs[userAddress].index,
userEmail,
userAge);
return userIndex.length-1;
}
function getUser(address userAddress) external view
returns(string memory userEmail, uint userAge, uint index)
{
require(isUser(userAddress), "User not found");
return(
userStructs[userAddress].userEmail,
userStructs[userAddress].userAge,
userStructs[userAddress].index);
}
function updateUserEmail(address userAddress, string memory userEmail)
onlyWhitelist
external
returns(bool success)
{
require(isUser(userAddress), "User not found");
userStructs[userAddress].userEmail = userEmail;
emit LogUpdateUser(
userAddress,
userStructs[userAddress].index,
userEmail,
userStructs[userAddress].userAge);
success = true;
return success;
}
function updateUserAge(address userAddress, uint userAge)
onlyWhitelist
external
returns(bool success)
{
require(isUser(userAddress), "User not found");
userStructs[userAddress].userAge = userAge;
emit LogUpdateUser(
userAddress,
userStructs[userAddress].index,
userStructs[userAddress].userEmail,
userAge);
return true;
}
function deleteUser(address userAddress)
onlyWhitelist
external
returns(uint index)
{
require(isUser(userAddress), "User not found");
uint rowToDelete = userStructs[userAddress].index;
address keyToMove = userIndex[userIndex.length-1];
userIndex[rowToDelete] = keyToMove;
userStructs[keyToMove].index = rowToDelete;
userIndex.pop();
emit LogDeleteUser(userAddress, rowToDelete);
emit LogUpdateUser(keyToMove, rowToDelete,
userStructs[keyToMove].userEmail,
userStructs[keyToMove].userAge);
return rowToDelete;
}
function getUserCount() external view returns(uint count)
{
return userIndex.length;
}
function getUserAtIndex(uint index) external view returns(address userAddress)
{
return userIndex[index];
}
function addMember (address _member) public onlyOwner {
whiteList[_member] = true;
}
function delMember (address _member) public onlyOwner {
whiteList[_member] = false;
}
}