forked from GaliLeo03/FakeDos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserManagement.cpp
More file actions
178 lines (141 loc) · 6.57 KB
/
Copy pathuserManagement.cpp
File metadata and controls
178 lines (141 loc) · 6.57 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
#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 "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 = "users\\"+username;
user_route[username] = userRoute;
_mkdir((fakeDosFolderPath + "\\users\\" + username).c_str());
// Debug
// cout << (fakeDosFolderPath + "\\users\\" + username).c_str() << endl;
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) {
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);
}
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) {
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) {
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) {
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);
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;
}
}