-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuserManagement.cpp
240 lines (194 loc) · 8.54 KB
/
userManagement.cpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <unistd.h>
#include <string>
#include <map>
#include <vector>
#include <io.h>
#include <direct.h>
#include <userManagement.h>
#include <taskManagement.h>
#include <mem.h>
#include "fakeDos.h"
using namespace std;
void create_user(vector<string> command_splited, vector<string> & user_name, map<string, string> & user_password, map<string, string> & user_route, string fakeDosFolderPath) {
if (command_splited.size() < 3) {
cout << "Error: Please enter a valid user name and password." << endl;
cout << "\tcreate_user (user name) (password)" << endl;
} else {
string username = command_splited[1];
string password = command_splited[2];
vector<string>::iterator it = find(user_name.begin(), user_name.end(), username);
if (it == user_name.end()) {
if (username == "NULL") {
cout << "Error: you can not use NULL as user name. Try another one." << endl;
} else {
user_name.push_back(username);
user_password[username] = password;
string userRoute = "root\\users\\"+username;
user_route[username] = userRoute;
_mkdir((fakeDosFolderPath + "\\users\\" + username).c_str());
system(("cd " + fakeDosFolderPath + "\\users\\" + username + " && " + "mklink /j files " + fakeDosFolderPath + "\\files > nul").c_str());
system(("cd " + fakeDosFolderPath + "\\users\\" + username + " && " + "mklink /j system " + fakeDosFolderPath + "\\system > nul").c_str());
cout << "Successfully created the user: " << username << endl;
cout << "\tUser information:" << endl;
cout << "\tUser name: " << username << endl;
cout << "\tPassword: " << password << endl;
write_users();
}
} else {
cout << "Error: user name " << username << " already exists. Try another one." << endl;
}
}
}
void delete_user(vector<string> command_splited,
vector<string> & user_name,
map<string, string> & user_password,
map<string, string> & user_route,
string fakeDosFolderPath,
vector<string> & logged_in_users,
string current_user,
string & jsonmem,
PCB_type (&mem)[100],
vector<PCB_type> & runningQueue,
vector<PCB_type> & blockQueue,
vector<PCB_type> & readyQueue
) {
if (command_splited.size() < 2) {
cout << "Error: Please enter a valid user name." << endl;
cout << "\tdelete_user (user name)" << endl;
} else {
string username = command_splited[1];
if (username == current_user) {
cout << "Error: you can not delete yourself while logging in." << endl;
} else {
vector<string>::iterator it = find(user_name.begin(), user_name.end(), username);
if (it != user_name.end()) {
cout << "Are you sure you want to delete " << username << " ? Enter password to continue:" << endl;
string password;
getline(cin, password);
if (password == user_password[username]) {
user_name.erase(it);
user_password.erase(username);
user_route.erase(username);
system(("rmdir /s /Q " + fakeDosFolderPath + "\\users\\" + username).c_str());
vector<string>::iterator it2 = find(logged_in_users.begin(), logged_in_users.end(), username);
if (it2 != logged_in_users.end()) {
logged_in_users.erase(it2);
user_mem_free(username, jsonmem);
for (int i = 0; i < 100; i++) {
if ((mem[i].state == RUNNING) || (mem[i].state == BLOCK)) {
if (mem[i].user_name == username) {
kill(username, mem[i].id, jsonmem, mem,
runningQueue, blockQueue, readyQueue);
}
}
}
}
cout << "Successfully deleted the user: " << username << endl;
}
else {
cout << "Error: incorrent password." << endl;
}
} else {
cout << "Error: user name " << username << " does not exist." << endl;
}
}
}
}
void change_user(bool & is_logged_in, string & current_user) {
if (!is_logged_in) {
cout << "Error: you are already in the user select panel." << endl;
} else {
is_logged_in = false;
current_user = "NULL";
}
}
void log_in(vector<string> command_splited,
vector<string> user_name,
map<string, string> user_password,
map<string, string> user_route,
bool & is_logged_in,
string & current_user,
string & current_path,
vector<string> & logged_in_users,
string & jsonmem) {
if (is_logged_in) {
cout << "Error: you have already logged in!" << endl;
} else {
if (command_splited.size() < 3) {
cout << "Error: please enter valid user name and password." << endl;
cout << "log_in (user name) (password)" << endl;
} else {
string username = command_splited[1];
string password = command_splited[2];
vector<string>::iterator it_u = find(user_name.begin(), user_name.end(), username);
if (it_u != user_name.end()) {
if (user_password[username] == password) {
vector<string>::iterator it_lu = find(logged_in_users.begin(), logged_in_users.end(), username);
if (it_lu == logged_in_users.end()) {
if (limit_check(0xFF, logged_in_users, jsonmem)) {
user_mem_alloc(username, jsonmem);
} else {
cout << "Error: insufficient memory to log in a new user." << endl;
return ;
}
}
cout << "Welcome back, " << username << "!" << endl;
is_logged_in = true;
vector<string>::iterator it_liu = find(logged_in_users.begin(), logged_in_users.end(), username);
if (it_liu == logged_in_users.end()) {
logged_in_users.push_back(username);
}
current_user = username;
current_path = user_route[username];
} else {
cout << "Error: incorrent password." << endl;
}
} else {
cout << "Error: username " << username << " does not exist." << endl;
}
}
}
}
void log_out(bool & is_logged_in,
string & current_user,
vector<string> & logged_in_users,
string & jsonmem,
PCB_type (&mem)[100],
vector<PCB_type> & runningQueue,
vector<PCB_type> & blockQueue,
vector<PCB_type> & readyQueue
) {
if (!is_logged_in) {
cout << "Error: you have not logged in yet." << endl;
} else {
is_logged_in = false;
vector<string>::iterator it_liu = find(logged_in_users.begin(), logged_in_users.end(), current_user);
logged_in_users.erase(it_liu);
user_mem_free(current_user, jsonmem);
for (int i = 0; i < 100; i++) {
if ((mem[i].state == RUNNING) || (mem[i].state == BLOCK)) {
if (mem[i].user_name == current_user) {
kill(current_user, mem[i].id, jsonmem, mem,
runningQueue, blockQueue, readyQueue);
}
}
}
current_user = "NULL";
cout << "You have logged out." << endl;
}
}
void ls_u(vector<string> user_name) {
cout << user_name.size() << " users exist." << endl;
for (int i = 0; i < int(user_name.size()); i++) {
cout << user_name[i] << endl;
}
}
void ls_lu(vector<string> logged_in_users) {
cout << logged_in_users.size() << " users have currently logged in." << endl;
for (int i = 0; i < int(logged_in_users.size()); i++) {
cout << logged_in_users[i] << endl;
}
}