-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserList.cpp
More file actions
191 lines (178 loc) · 4.53 KB
/
UserList.cpp
File metadata and controls
191 lines (178 loc) · 4.53 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "UserList.hpp"
UserList::UserList() {
}
UserList::~UserList() {
}
void UserList::add_user() {
User user;
int cp;
std::string key, arg;
bool fail = false;
while (std::cin >> key >> arg) {
if (key == "-c") {
if (!loggedIn(arg) && !list.empty()) fail = true;
else cp = list.find(arg)->second.privilege;
}
else if (key == "-u") {
if (found(arg)) fail = true;
user.username = arg;
}
else if (key == "-p") {
user.password = arg;
}
else if (key == "-n") {
user.name = arg;
}
else if (key == "-m") {
user.mailAddr = arg;
}
else if (key == "-g") {
user.privilege = atoi(arg.c_str());
}
char c = std::cin.get();
if (c == '\n' || c == '\r') break;
}
if (list.empty()) {
cp = 11;
user.privilege = 10;
}
if (cp <= user.privilege) fail = true;
if (fail) {
std::cout << -1;
return;
}
list[user.username] = user;
std::cout << 0;
}
void UserList::login() {
std::string key, arg;
mString username, password;
bool fail = false;
while (std::cin >> key >> arg) {
if (key == "-u") {
if (!found(arg)) fail = true;
if (loggedIn(arg)) fail = true;
username = arg;
}
if (key == "-p") {
password = arg;
}
char c = std::cin.get();
if (c == '\n' || c == '\r') break;
}
if (list.find(username)->second.password != password) fail = true;
if (fail) {
std::cout << -1;
return;
}
loginList.insert(username);
std::cout << 0;
}
void UserList::logout() {
std::string key, arg;
std::cin >> key >> arg;
if (!loggedIn(arg)) {
std::cout << -1;
return;
}
loginList.erase(arg);
std::cout << 0;
}
void UserList::query_profile() {
User user;
int cp;
mString cu;
std::string key, arg;
bool fail = false;
while (std::cin >> key >> arg) {
if (key == "-c") {
if (!loggedIn(arg)) fail = true;
else {
cp = list.find(arg)->second.privilege;
cu = list.find(arg)->second.username;
}
}
if (key == "-u") {
if (!found(arg)) fail = true;
else {
user = list.find(arg)->second;
}
}
char c = std::cin.get();
if (c == '\n' || c == '\r') break;
}
if (cp <= user.privilege && cu != user.username) fail = true;
if (fail) {
std::cout << -1;
return;
}
print(user);
}
void UserList::modify_profile() {
User user;
int cp;
mString cu;
std::string key, arg;
mString password, name, mailAddr;
int privilege = 0;
bool p = false, n = false, m = false, g = false, fail = false;
while (std::cin >> key >> arg) {
if (key == "-c") {
if (!loggedIn(arg)) fail = true;
else {
cp = list.find(arg)->second.privilege;
cu = list.find(arg)->second.username;
}
}
if (key == "-u") {
if (!found(arg)) fail = true;
else {
user = list.find(arg)->second;
}
}
if (key == "-p") {
password = arg;
p = true;
}
if (key == "-n") {
name = arg;
n = true;
}
if (key == "-m") {
mailAddr = arg;
m = true;
}
if (key == "-g") {
privilege = atoi(arg.c_str());
g = true;
}
char c = std::cin.get();
if (c == '\n' || c == '\r') break;
}
if ((cp <= user.privilege && cu != user.username) || cp <= privilege) fail = true;
if (fail) {
std::cout << -1;
return;
}
if (p) user.password = password;
if (n) user.name = name;
if (m) user.mailAddr = mailAddr;
if (g) user.privilege = privilege;
mString username = user.username;
list.erase(list.find(username));
list[username] = user;
print(user);
}
void UserList::exit() {
loginList.clear();
std::cout << "bye";
}
bool UserList::found(mString username) {
return list.find(username) != list.end();
}
bool UserList::loggedIn(mString username) {
return loginList.find(username) != loginList.end();
}
void UserList::print(User user) {
std::cout << user.username << " " << user.name << " " << user.mailAddr << " " << user.privilege;
}