-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathUserAction.cpp
164 lines (152 loc) · 5.14 KB
/
UserAction.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
#include "UserAction.h"
#include <utility>
#include "CsvFile.h"
#include "Users.h"
#include "UserData.h"
UserAction::UserAction(std::string role) {
_strRole = std::move(role);
_usersList.importUserData();
}
UserAction::UserAction() {
_strRole = "";
_usersList.importUserData();
}
void UserAction::setRole(std::string role) {
_strRole = std::move(role);
_usersList.~Users();
_usersList.importUserData();
}
bool UserAction::addUser(const UserData& user) {
if (!(_strRole == "Admin" && _usersList.addUser(user))) return false;
CsvFile csvFileEmployee("Employees.txt");
csvFileEmployee.append(
{
{user.getUserName() + "," + user.getPassword()}
}
);
CsvFile csvFileNewUser(user.getUserName() + ".txt");
csvFileNewUser.write(
{
{user.getName()},
{user.getDateOfBirth()},
{user.getAddress()},
{user.getPhoneNumber()},
{user.getEmail()}
}
);
return true;
}
bool UserAction::deleteUser(const std::string& userName) {
if (_strRole == "Admin" && _usersList.removeUser(userName)) {
CsvFile csvFileEmployee("Employees.txt");
std::vector<std::vector<std::string>> vtEmployeeAccounts = csvFileEmployee.read();
int iLine = -1;
for (int i = 0; i < vtEmployeeAccounts.size(); i++) {
if (vtEmployeeAccounts[i][0] == userName) {
iLine = i;
break;
}
}
if (iLine != -1) {
csvFileEmployee.remove(iLine);
CsvFile csvFileDelete(userName + ".txt");
csvFileDelete.del();
return true;
}
}
return false;
}
UserData UserAction::findUser(const std::string& userName) {
if (_usersList.searchUser(userName) == -1) return {};
std::vector<UserData> vtUsersDataList = _usersList.listUsers();
int iUserNum = _usersList.searchUser(userName);
return vtUsersDataList[iUserNum];
}
bool UserAction::updateUserInformation(const std::string& userName, const UserData& user) {
if (!(_usersList.editUser(userName, user))) return false;
CsvFile csvFileEmployee("Employees.txt");
std::vector<std::vector<std::string>> vtEmployeeAccounts = csvFileEmployee.read();
int iLine = -1;
for (int i = 1; i <= vtEmployeeAccounts.size(); i++) {
if (vtEmployeeAccounts[i][0] == userName) {
iLine = i;
break;
}
}
if (iLine != -1) {
csvFileEmployee.remove(iLine);
csvFileEmployee.append(
{
{user.getUserName() + "," + user.getPassword()}
}
);
CsvFile csvFileUser(userName + ".txt");
csvFileUser.write(
{
{user.getName()},
{user.getDateOfBirth()},
{user.getAddress()},
{user.getPhoneNumber()},
{user.getEmail()}
}
);
return true;
}
return false;
}
std::string UserAction::getUserInformation(const std::string& userName) {
std::string strUserData;
CsvFile csvFileUser(userName + ".txt");
std::vector<std::vector<std::string>> vtDataUser = csvFileUser.read();
for (auto& v: vtDataUser) {
for (auto& s: v) {
strUserData += s + " ";
}
}
return strUserData;
}
std::vector<std::string> UserAction::getAllUsersInformation() {
if (_strRole != "Admin") return {"None user were found"};
std::vector<std::string> strUsers;
std::vector<UserData> vtUsersDataList = _usersList.listUsers();
for (const auto& data: vtUsersDataList) {
if (data.getRole() == "Employee") {
strUsers.push_back(
data.getName() + " " + data.getDateOfBirth() + " " + data.getAddress() + " " + data.getPhoneNumber() +
" " + data.getEmail()
);
}
}
return strUsers;
}
bool UserAction::authenticateUser(const std::string& userName, const std::string& password) {
std::vector<std::vector<std::string>> vtAccountsUser;
if (_strRole == "Admin") {
CsvFile csvFileAdmin("Administrators.txt");
vtAccountsUser = csvFileAdmin.read();
for (int i = 1; i < vtAccountsUser.size(); i++) {
if (vtAccountsUser[i][0] == userName && vtAccountsUser[i][1] == password) return true;
}
} else {
CsvFile csvFileEmployee("Employees.txt");
vtAccountsUser = csvFileEmployee.read();
for (int i = 1; i < vtAccountsUser.size(); i++) {
if (vtAccountsUser[i][0] == userName && vtAccountsUser[i][1] == password) return true;
}
}
return false;
}
bool UserAction::findUserName(const std::string& userName) {
std::vector<std::vector<std::string>> vtAccountsUser;
CsvFile csvFileAdmin("Administrators.txt");
vtAccountsUser = csvFileAdmin.read();
for (int i = 1; i < vtAccountsUser.size(); i++) {
if (vtAccountsUser[i][0] == userName) return true;
}
CsvFile csvFileEmployee("Employees.txt");
vtAccountsUser = csvFileEmployee.read();
for (int i = 1; i < vtAccountsUser.size(); i++) {
if (vtAccountsUser[i][0] == userName) return true;
}
return false;
}