-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.cpp
156 lines (130 loc) · 4.71 KB
/
user.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
#include <QFile>
#include <QJsonDocument>
#include <QJsonObject>
#include "headers/user.h"
#include "headers/filehandler.h"
User::User(QObject *parent)
: QObject{parent}
{
}
bool User::authenticate(QString email, QString password)
{
QFile file(QStringLiteral("./data.json"));
if (!file.open(QIODevice::ReadOnly)) {
qWarning("Couldn't open data file.");
return false;
}
QByteArray saveData = file.readAll();
file.close();
QJsonDocument loadDoc(QJsonDocument::fromJson(saveData));
QJsonArray users = loadDoc["users"].toArray();
QTextStream(stdout) << "Login attempt with credentials:" << email << password << "\n";
for (int i = 0; i < users.size(); i++) {
QJsonObject user = users[i].toObject();
if (user.contains("email") && user["email"].isString()
&& user.contains("password") && user["password"].isString()) {
if (user["email"].toString() == email && user["password"].toString() == password) {
QTextStream(stdout) << "Authenticated user with email:" << user["email"].toString() << "\n";
QTextStream(stdout) << "Password:" << user["password"].toString() << "\n";
QJsonArray blogs = loadDoc["blogs"].toArray();
for (int i = 0; i < blogs.size(); i++) {
QJsonObject blog = blogs[i].toObject();
if (blog.contains("owner") && blog["owner"].isString()
&& blog["owner"].toString() == email && blog.contains("blogName")
&& blog["blogName"].isString()) {
QString blogName = blog["blogName"].toString();
this->set(true, email, blogName);
return true;
}
}
}
}
}
return false;
}
bool User::isAuthenticated()
{
return this->authenticated;
}
QString User::getAuthenticatedEmail() const
{
if (this->authenticated) {
QTextStream(stdout) << "getAuthenticatedEmail():" << this->email << "\n";
return this->email;
} else {
QTextStream(stdout) << "getAuthenticatedEmail(): failed, this->authenticated=false, this->email=" << this->email << "\n";
return "Logged out";
}
}
QString User::getBlogName()
{
QTextStream(stdout) << "getBlogName():" << this->blogName << "\n";
return this->blogName;
}
void User::logout()
{
this->set(false);
}
bool User::createAccount(QString email, QString blogName, QString password)
{
qWarning("createAccount() invoked");
QFile file(QStringLiteral("./data.json"));
if (!file.open(QIODeviceBase::ReadWrite)) {
qWarning("createAccount(): Couldn't open data file.");
return false;
}
QByteArray data = file.readAll();
QJsonDocument loadDoc(QJsonDocument::fromJson(data));
QJsonArray users = loadDoc["users"].toArray();
for (int i = 0; i < users.size(); i++) {
QJsonObject user = users[i].toObject();
if (user.contains("email") && user["email"].isString()
&& user.contains("password") && user["password"].isString()) {
if (user["email"].toString() == email) {
QTextStream(stdout) << "User with such an email already exists:" << user["email"].toString() << "\n";
file.close();
return false;
}
}
}
QJsonArray blogs = loadDoc["blogs"].toArray();
for (int i = 0; i < blogs.size(); i++) {
QJsonObject blog = blogs[i].toObject();
if (blog.contains("blogName") && blog["blogName"].isString()
&& blog.contains("owner") && blog["owner"].isString()) {
if (blog["blogName"].toString() == blogName) {
QTextStream(stdout) << "Blog with such a name already exists:" << blog["blogName"].toString() << "\n";
file.close();
return false;
}
}
}
QJsonObject newUser {
{"email", email},
{"password", password}
};
users.append(newUser);
QJsonObject newBlog {
{"blogName", blogName},
{"owner", email}
};
blogs.append(newBlog);
QJsonObject docObject = loadDoc.object();
docObject.insert("users", users);
docObject.insert("blogs", blogs);
file.resize(0);
file.write(QJsonDocument(docObject).toJson());
file.close();
this->set(true, email, blogName);
return true;
}
// private setter function
void User::set(bool authenticated, QString email, QString blogName)
{
if (this->authenticated != authenticated) {
this->authenticated = authenticated;
this->email= email;
this->blogName = blogName;
emit this->dataChanged(this->email, this->blogName);
}
}