-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuserstorage.cpp
More file actions
152 lines (112 loc) · 3.74 KB
/
userstorage.cpp
File metadata and controls
152 lines (112 loc) · 3.74 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
#include "userstorage.h"
#include <QFile>
#include <QTextStream>
#include <QCryptographicHash>
QString hashPassword(const QString& password) {
QByteArray hashed = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Sha256);
return QString(hashed.toHex());
}
bool UserStorage::saveUser(const QString& name, const QString& lastname,
const QString& phone, const QString& email,
const QString& username, const QString& password,
const QString& filename) {
QFile file(filename);
if (!file.open(QIODevice::Append | QIODevice::Text)) {
return false;
}
QTextStream out(&file);
out << username << "|" << hashPassword(password) << "|" << name << "|"
<< lastname << "|" << phone << "|" << email << "\n";
file.close();
return true;
}
bool UserStorage::loginUser(const QString& username, const QString& password) {
QFile file("users.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
return false;
}
QTextStream in(&file);
QString hashedInputPassword = hashPassword(password);
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
QStringList parts = line.split('|');
if (parts.size() < 6) continue;
QString fileUsername = parts[0];
QString fileHashedPassword = parts[1];
if (fileUsername == username && fileHashedPassword == hashedInputPassword) {
return true;
}
}
return false;
}
bool UserStorage::phoneExists(const QString& phone) {
QFile file("users.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
QStringList parts = line.split('|');
if (parts.size() < 6) continue;
QString filePhone = parts[4];
if (filePhone == phone) {
return true;
}
}
return false;
}
bool UserStorage::loadUserProfile(const QString& username, QStringList& profileFields) {
QFile file("users.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
QStringList parts = line.split('|');
if (parts.size() < 6) continue;
if (parts[0] == username) {
profileFields = parts;
return true;
}
}
return false;
}
bool UserStorage::updateUserProfile(const QString& username, const QStringList& newFields) {
QFile file("users.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return false;
QStringList lines;
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
QStringList parts = line.split('|');
if (parts.size() < 6) continue;
if (parts[0] == username) {
lines << newFields.join("|");
} else {
lines << line;
}
}
file.close();
if (!file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate))
return false;
QTextStream out(&file);
for (const QString& line : lines)
out << line << "\n";
return true;
}
QString UserStorage::findUsernameByPhone(const QString& phone) {
QFile file("users.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return "";
QTextStream in(&file);
while (!in.atEnd()) {
QString line = in.readLine().trimmed();
QStringList parts = line.split('|');
if (parts.size() < 6) continue;
if (parts[4] == phone) {
return parts[0]; // username
}
}
return "";
}