-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser.cpp
More file actions
80 lines (64 loc) · 1.62 KB
/
Copy pathuser.cpp
File metadata and controls
80 lines (64 loc) · 1.62 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
#include "user.h"
User::User()
{
}
User::~User()
{
}
bool User::authenticate(const QString& username, const QString& password, QSqlDatabase db)
{
QSqlQuery query(db);
query.prepare("SELECT hashed_password, salt FROM users WHERE username = :username");
query.bindValue(":username", username);
query.exec();
query.next();
QString hashed_password = query.value(0).toString();
QString salt = query.value(1).toString();
bool success = hashed_password == QCryptographicHash::hash(password.toAscii() + salt.toAscii(),
QCryptographicHash::Sha1);
if (success)
{
return true;
}
return false;
}
User User::find(const QString& username, QSqlDatabase db)
{
QSqlQuery query(db);
query.prepare("SELECT id, username, email, hashed_password, salt, role_id FROM users WHERE username = :username");
query.bindValue(":username", username);
query.exec();
query.next();
User user;
user.m_id = query.value(0).toInt();
user.m_username = query.value(1).toString();
user.m_email = query.value(2).toString();
user.m_hashedPassword = query.value(3).toString();
user.m_salt = query.value(4).toString();
user.m_role = query.value(5).toInt();
return user;
}
QString User::username() const
{
return m_username;
}
int User::id() const
{
return m_id;
}
int User::role() const
{
return m_role;
}
QString User::email() const
{
return m_email;
}
QString User::hashedPassword() const
{
return m_hashedPassword;
}
QString User::salt() const
{
return m_salt;
}