-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserManager.cpp
More file actions
189 lines (166 loc) · 5.21 KB
/
UserManager.cpp
File metadata and controls
189 lines (166 loc) · 5.21 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
//
// Created by matin on 1/2/26.
//
#include "UserManager.h"
#include <sstream>
#include <iostream>
UserManager::UserManager(const string& filename) : nextUserId(1), dataFile(filename) {
loadFromFile();
}
UserManager::~UserManager() {
clear();
}
void UserManager::updateIdCounter() {
nextUserId = 1;
for (const User* user : users) {
if (user->getUserId() >= nextUserId) {
nextUserId = user->getUserId() + 1;
}
}
}
void UserManager::loadFromFile() {
clear();
ifstream file(dataFile);
if (!file.is_open()) {
return;
}
string line;
while (getline(file, line)) {
if (line.empty()) continue;
try {
User* user = User::deserialize(line);
if (user) {
users.push_back(user);
usernameMap[user->getUsername()] = user;
userIdMap[user->getUserId()] = user;
}
} catch (...) {
// Skip
}
}
file.close();
updateIdCounter();
}
void UserManager::saveToFile() const {
ofstream file(dataFile);
if (!file.is_open()) {
throw FileIOException("Cannot open file for writing: " + dataFile);
}
for (const User* user : users) {
file << user->serialize() << endl;
}
file.close();
}
LocalUser* UserManager::registerUser(const string& username, const string& fullName,const string& birthdate, const string& password) {
User::validateUsername(username);
User::validateFullName(fullName);
User::validatePassword(password);
User::validateBirthdate(birthdate);
if (usernameExists(username)) {
throw UsernameTakenException("Username '" + username + "' is already taken");
}
Date birth = Date::fromString(birthdate);
Date joinDate;
LocalUser* newUser = new LocalUser(nextUserId++, username, fullName, password, birth, joinDate);
users.push_back(newUser);
usernameMap[username] = newUser;
userIdMap[newUser->getUserId()] = newUser;
saveToFile();
return newUser;
}
LocalUser* UserManager::login(const string& username, const string& password) {
auto it = usernameMap.find(username);
if (it == usernameMap.end()) {
throw InvalidUserException("User '" + username + "' does not exist");
}
User* user = it->second;
if (user->getPassword() != password) {
throw WrongPasswordException("Incorrect password");
}
return dynamic_cast<LocalUser*>(user);
}
bool UserManager::usernameExists(const string& username) const {
return usernameMap.find(username) != usernameMap.end();
}
bool UserManager::userIdExists(int userId) const {
return userIdMap.find(userId) != userIdMap.end();
}
User* UserManager::findByUsername(const string& username) const {
auto it = usernameMap.find(username);
if (it != usernameMap.end()) {
return it->second;
}
return nullptr;
}
User* UserManager::findById(int userId) const {
auto it = userIdMap.find(userId);
if (it != userIdMap.end()) {
return it->second;
}
return nullptr;
}
int UserManager::getUserIdByUsername(const string& username) const {
User* user = findByUsername(username);
if (user) {
return user->getUserId();
}
throw InvalidUserException("User '" + username + "' does not exist");
}
string UserManager::getUsernameById(int userId) const {
User* user = findById(userId);
if (user) {
return user->getUsername();
}
return "Unknown";
}
vector<User*> UserManager::searchByName(const string& name) const {
vector<User*> results;
string lowerName = name;
transform(lowerName.begin(), lowerName.end(), lowerName.begin(), ::tolower);
for (User* user : users) {
string fullName = user->getFullName();
transform(fullName.begin(), fullName.end(), fullName.begin(), ::tolower);
if (fullName.find(lowerName) != string::npos) {
results.push_back(user);
}
}
return results;
}
vector<User*> UserManager::searchByUsername(const string& username) const {
vector<User*> results;
string lowerSearch = username;
transform(lowerSearch.begin(), lowerSearch.end(), lowerSearch.begin(), ::tolower);
for (User* user : users) {
string uname = user->getUsername();
transform(uname.begin(), uname.end(), uname.begin(), ::tolower);
if (uname.find(lowerSearch) != string::npos) {
results.push_back(user);
}
}
return results;
}
vector<User*> UserManager::sortByName() {
vector<User*> &sorted = users;
sort(sorted.begin(), sorted.end(), [](const User* a, const User* b) {return a->getFullName() < b->getFullName();});
return sorted;
}
vector<User*> UserManager::sortByJoinDate() {
vector<User*> &sorted = users;
sort(sorted.begin(), sorted.end(), [](const User* a, const User* b) {return a->getJoinDate() < b->getJoinDate();});
return sorted;
}
vector<User*> UserManager::sortById() {
vector<User*> &sorted = users;
sort(sorted.begin(), sorted.end(), [](const User* a, const User* b) {
return a->getUserId() < b->getUserId();
});
return sorted;
}
void UserManager::clear() {
for (User* user : users) {
delete user;
}
users.clear();
usernameMap.clear();
userIdMap.clear();
}