-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserManager.cpp
More file actions
234 lines (213 loc) · 5.2 KB
/
UserManager.cpp
File metadata and controls
234 lines (213 loc) · 5.2 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
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
#include <algorithm>
#include <iostream>
#include <optional>
#include <string>
#include "UserManager.h"
#include "Utils.h"
namespace fs
{
const std::filesystem::path UserManager::userManagerPath{ "./fs/userManager" };
UserManager::UserManager()
{
loadUsers();
}
UserManager::~UserManager()
{
saveUsers();
}
std::optional<const User*> UserManager::authenticate() const
{
while (true)
{
const auto username = getUsername();
if (!username)
{
return {};
}
if (const auto foundUser = getUser(username.value()))
{
if (foundUser.value()->getPassword() == getPassword())
{
return { foundUser.value() };
}
std::cout << "Wrong password\n\n";
}
else
{
std::cout << "User with username: " << username.value() << " could not be found.\n\n";
}
}
}
void UserManager::registerUser()
{
if (const auto user = createUser())
{
m_users.push_back(user.value());
}
}
void UserManager::saveUsers()
{
createEssentialFiles();
std::ofstream file{ userManagerPath / "users.txt" };
std::for_each(m_users.begin(), m_users.end(), [&](const User& user)
{
file << user << "\n";
});
}
bool UserManager::usernameExists(const std::string_view username) const
{
return std::any_of(m_users.begin(), m_users.end(), [&](const User& user)
{
return user.getUsername() == username;
});
}
std::optional<const User*> UserManager::getUser(const std::string_view username) const
{
const auto userIt = std::find_if(m_users.begin(), m_users.end(), [&](const User& user)
{
return user.getUsername() == username;
});
if (userIt == m_users.end())
{
return {};
}
return &*userIt;
}
bool UserManager::requestPassword(const std::string& username) const
{
if (const auto foundUser = getUser(username))
{
if (foundUser.value()->getPassword() == getPassword())
{
return true;
}
std::cout << "Wrong password\n";
}
else
{
std::cout << "User with username: " << username << " could not be found.\n";
}
return false;
}
void UserManager::loadUsers()
{
if (Utils::checkIfDirectoryExists(userManagerPath))
{
std::ifstream file{ userManagerPath / "users.txt" };
std::string line{};
while (std::getline(file, line))
{
std::istringstream iss{ line };
m_users.emplace_back(iss);
}
}
}
std::optional<User> UserManager::createUser() const
{
std::string username{};
std::string password{};
bool isValid{ false };
while (!isValid)
{
if (const auto userInput = getUsername(); !userInput)
{
return {};
}
else if (usernameExists(userInput.value()))
{
std::cout << "Invalid username.\n"
<< "Username is already in use\n\n";
}
else
{
username = userInput.value();
isValid = true;
}
}
if (const auto userInput = getPassword(); !userInput)
{
return {};
}
else
{
password = userInput.value();
}
return User{ username, password };
}
std::optional<std::string> UserManager::getUsername()
{
std::string username{};
bool isValid{ false };
while (!isValid)
{
std::cout << "Please enter username: (empty input to cancel)\n"
<< "> ";
if (auto line = Utils::getLine())
{
username = Utils::trim(line.value());
if (username.size() < minUsernameLength || username.size() >= maxUsernameLength)
{
std::cout << "\nInvalid username.\n"
<< "Username cannot have less than " << 3 << " or more than "
<< maxUsernameLength << " characters" << "\n";
}
else if (!Utils::areAllCharactersAlpha(username))
{
std::cout << "\nInvalid username.\n"
<< "Username must contain only valid characters\n";
}
else
{
isValid = true;
}
}
if (!isValid && Utils::promptToExitLoop())
{
std::cout << "\n";
return {};
}
std::cout << "\n";
}
Utils::bufferSafetyCheck();
return { username };
}
std::optional<std::string> UserManager::getPassword()
{
std::string password{};
bool isValid{ false };
while (!isValid)
{
std::cout << "Please enter password:\n"
<< "> ";
if (auto line = Utils::getLine())
{
password = Utils::trim(line.value()); // skipcq: CXX-A1004
isValid = true;
}
else if (Utils::promptToExitLoop())
{
return {};
}
}
std::cout << "\n";
Utils::bufferSafetyCheck();
return { password };
}
void UserManager::createEssentialFiles()
{
if (!Utils::checkIfDirectoryExists("./fs"))
{
if (!Utils::createDirectory("./fs"))
{
std::clog << "ERROR: failed to create fs directory\n\n";
}
}
if (!Utils::checkIfDirectoryExists(userManagerPath))
{
if (!Utils::createDirectory(userManagerPath))
{
std::clog << "ERROR: UserManager::saveUsers() failed to create directory\n\n";
}
}
}
} // namespace fs