-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.cpp
More file actions
90 lines (78 loc) · 2.73 KB
/
User.cpp
File metadata and controls
90 lines (78 loc) · 2.73 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
#include "User.h"
#include "SHA256.h"
#include <fstream>
#include <sstream>
#include <vector>
User::User(const std::string& username, const std::string& password, double initialBalance)
: username(username), passwordHash(hashPassword(password)), balance(initialBalance) {}
std::string User::hashPassword(const std::string& password) const {
return SHA256::hash(password);
}
bool User::authenticate(const std::string& password) const {
return this->passwordHash == hashPassword(password);
}
void User::saveToFile() const {
std::ofstream file("users.txt", std::ios::app);
file << username << " " << passwordHash << " " << balance << "\n";
file.close();
}
bool User::userExists(const std::string& username) {
std::ifstream file("users.txt");
std::string storedUsername, storedHash;
double storedBalance;
while (file >> storedUsername >> storedHash >> storedBalance) {
if (storedUsername == username) {
return true;
}
}
return false;
}
User User::loadUser(const std::string& username) {
std::ifstream file("users.txt");
std::string storedUsername, storedHash;
double storedBalance;
while (file >> storedUsername >> storedHash >> storedBalance) {
if (storedUsername == username) {
User user(storedUsername, ""); // Create a user object without setting password
user.passwordHash = storedHash;
user.balance = storedBalance;
return user;
}
}
throw std::runtime_error("User not found");
}
// Updates user data in users.txt without duplicating
void User::updateUserFile(const User& updatedUser) {
std::ifstream file("users.txt");
std::vector<User> users;
std::string storedUsername, storedHash;
double storedBalance;
// Read existing users
while (file >> storedUsername >> storedHash >> storedBalance) {
User user(storedUsername, ""); // Create user without hashing new password
user.passwordHash = storedHash;
user.balance = storedBalance;
if (storedUsername == updatedUser.username) {
user.balance = updatedUser.balance; // Update balance for the specified user
}
users.push_back(user);
}
file.close();
// Rewrite file with updated users
std::ofstream outFile("users.txt", std::ios::trunc);
for (const User& user : users) {
outFile << user.username << " " << user.passwordHash << " " << user.balance << "\n";
}
outFile.close();
}
bool User::updateBalance(double amount) {
if (balance + amount < 0) return false; // Prevent negative balance
balance += amount;
return true;
}
double User::getBalance() const {
return balance;
}
std::string User::getUsername() const{
return username;
}