-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathreset_password_dialog.cpp
More file actions
114 lines (94 loc) · 3.52 KB
/
reset_password_dialog.cpp
File metadata and controls
114 lines (94 loc) · 3.52 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
#include "reset_password_dialog.h"
#include "ui_reset_password_dialog.h"
#include <QString>
#include <QMessageBox>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QDateTime>
//Hashing
#include <QCryptographicHash>
#include <QUuid>
Reset_Password_Dialog::Reset_Password_Dialog(const QString role,const QString id,const QString token,QWidget *parent)
: QDialog(parent)
, ui(new Ui::Reset_Password_Dialog)
, userRole(role)
, userID(id)
, resetToken(token)
{
ui->setupUi(this);
qDebug() << "role : "+role;
qDebug() << "id : "+id;
qDebug() << "Token " +resetToken;
if (userRole == "admin") {
// Setup admin reset UI
ui->status_label->setText("Admin");
} else if (userRole == "employee") {
// Setup employee reset UI
ui->status_label->setText("Employee");
}
}
Reset_Password_Dialog::~Reset_Password_Dialog()
{
delete ui;
}
void Reset_Password_Dialog::on_reset_btn_clicked()
{
QString newPassword = ui->New_Password_lineEdit->text();
QString confirmPassword = ui->Confirm_Password_lineEdit->text();
if (newPassword != confirmPassword) {
QMessageBox::warning(this, "Mismatch", "Passwords do not match.");
return;
}
// 🔐 Step 1: Validate token before password update
QSqlQuery validateQuery;
if (userRole == "admin") {
validateQuery.prepare("SELECT password_reset_expiry FROM users WHERE id = :id AND password_reset_token = :token");
} else if (userRole == "employee") {
//validateQuery.prepare("SELECT password_reset_expiry FROM employees WHERE id = :id AND password_reset_token = :token");
} else {
QMessageBox::critical(this, "Error", "Unknown user role.");
return;
}
validateQuery.bindValue(":id", userID);
validateQuery.bindValue(":token", resetToken);
if (userRole == "admin"){
if (!validateQuery.exec() || !validateQuery.next()) {
QMessageBox::critical(this, "Invalid", "Invalid or expired reset token.");
return;
}
QDateTime expiry = QDateTime::fromString(validateQuery.value(0).toString(), Qt::ISODate);
if (QDateTime::currentDateTime() > expiry) {
QMessageBox::warning(this, "Expired", "Reset token has expired.");
return;
}
}
// 🔐 Step 2: Generate salt and hash
QString password_salt = QUuid::createUuid().toString().remove("{").remove("}").remove("-");
QByteArray saltedPassword = (newPassword + password_salt).toUtf8();
QString password_hash = QString(QCryptographicHash::hash(saltedPassword, QCryptographicHash::Sha256).toHex());
// 🔐 Step 3: Update password
QSqlQuery query;
if (userRole == "admin") {
query.prepare("UPDATE users SET password_hash = :hash, password_salt = :salt, password_reset_token = NULL, password_reset_expiry = NULL WHERE id = :id");
} else if (userRole == "employee") {
query.prepare(R"(
UPDATE employees
SET password_hash = :hash,
password_salt = :salt,
password_reset_token = NULL,
password_reset_expiry = NULL,
force_password_change = 0
WHERE employee_id = :id
)");
}
query.bindValue(":hash", password_hash);
query.bindValue(":salt", password_salt);
query.bindValue(":id", userID);
if (query.exec()) {
QMessageBox::information(this, "Success", "Password updated successfully.");
this->close();
} else {
QMessageBox::critical(this, "Error", "Failed to update password: " + query.lastError().text());
}
}