-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
382 lines (329 loc) · 17 KB
/
mainwindow.cpp
File metadata and controls
382 lines (329 loc) · 17 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QString>
#include <QMessageBox>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include "DatabaseManager.h" // Singleton database connection
#include <QCloseEvent>
#include <QThread>
#include <globalfunctions.h>
#include <QDateTime>
//animation
#include <QGraphicsOpacityEffect>
#include <QPropertyAnimation>
bool admin = false;
bool employee=true;
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->toggle_button_admin_circle->hide();
//hide create account button for security reasons
QSqlQuery checkAdmin;
checkAdmin.prepare("SELECT COUNT(*) FROM users");
if (checkAdmin.exec() && checkAdmin.next()) {
int adminCount = checkAdmin.value(0).toInt();
if (adminCount > 0) {
ui->pushButton->hide();
}
}
//hide the forget button
ui->forgot_password_btn->setVisible(false);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_checkBox_checkStateChanged(const Qt::CheckState &arg1)
{
if (arg1 == Qt::Checked) {
ui->pass_lineEdit->setEchoMode(QLineEdit::Normal);
} else {
ui->pass_lineEdit->setEchoMode(QLineEdit::Password);
}
}
void MainWindow::on_pushButton_clicked()
{
reg = new Register(this);
reg->show();
}
void MainWindow::on_login_btn_clicked()
{
if(admin==true && employee==false){
GlobalFunctions::set_admin();//setting admin
QString username = ui->username_lineEdit->text();
QString password = ui->pass_lineEdit->text();
QSqlQuery query;
query.prepare("SELECT id, username, password_hash, password_salt, account_locked, lockout_time, failed_login_attempts, is_active "
"FROM users WHERE username = :username LIMIT 1");
query.bindValue(":username", username);
if (query.exec()) {
if (query.next()) { // If a result is found
int userId = query.value("id").toInt();
QString storedPasswordHash = query.value("password_hash").toString();
QString storedPasswordSalt = query.value("password_salt").toString();
bool accountLocked = query.value("account_locked").toBool();
QDateTime lockoutTime = query.value("lockout_time").toDateTime();
int failedLoginAttempts = query.value("failed_login_attempts").toInt();
bool isActive = query.value("is_active").toBool();
// Check if the account is active
if (!isActive) {
qDebug() << "Account is deactivated.";
QMessageBox::warning(this, "Account Inactive", "Your account has been deactivated. Please contact support.");
return;
}
// Check if the account is locked
if (accountLocked) {
QDateTime currentTime = QDateTime::currentDateTime();
if (lockoutTime.isValid() && lockoutTime.addSecs(300) > currentTime) { // 5-minute lockout
qDebug() << "Account is locked. Lockout will expire at:" << lockoutTime.addSecs(300).toString();
QMessageBox::warning(this, "Account Locked", "Your account is temporarily locked due to multiple failed login attempts. Please try again later.");
return;
} else {
// Unlock account after lockout period
query.prepare("UPDATE users SET account_locked = 0, failed_login_attempts = 0 WHERE id = :id");
query.bindValue(":id", userId);
if (!query.exec()) {
qDebug() << "Failed to unlock account:" << query.lastError().text();
}
}
}
// Log login attempt details
QString logDetails = QString("User %1 login attempt.").arg(userId);
GlobalFunctions::set_user(username, userId);
GlobalFunctions::log_user_login(); // Log login attempt
// Verify the password with the stored hash and salt
if (GlobalFunctions::verifyPassword(password, storedPasswordHash, storedPasswordSalt)) {
qDebug() << "Login successful!";
QMessageBox::information(this, "Login Success", "You have successfully logged in.");
// Reset failed login attempts on successful login
query.prepare("UPDATE users SET failed_login_attempts = 0, last_login = :last_login WHERE id = :id");
query.bindValue(":last_login", QDateTime::currentDateTime());
query.bindValue(":id", userId);
if (!query.exec()) {
qDebug() << "Failed to update login details:" << query.lastError().text();
}
// Proceed to the main application window
hide();
DashBoard *dash_board = new DashBoard(nullptr);
dash_board->show();
} else {
// Increment failed login attempts
failedLoginAttempts++;
query.prepare("UPDATE users SET failed_login_attempts = :failed_attempts WHERE id = :id");
query.bindValue(":failed_attempts", failedLoginAttempts);
query.bindValue(":id", userId);
if (!query.exec()) {
qDebug() << "Failed to update failed login attempts:" << query.lastError().text();
}
// Lock account after 3 failed attempts
if (failedLoginAttempts >= 3) {
query.prepare("UPDATE users SET account_locked = 1, lockout_time = :lockout_time WHERE id = :id");
query.bindValue(":lockout_time", QDateTime::currentDateTime());
query.bindValue(":id", userId);
if (!query.exec()) {
qDebug() << "Failed to lock account:" << query.lastError().text();
}
QMessageBox::warning(this, "Account Locked", "Your account has been locked due to multiple failed login attempts. Please try again later.");
} else {
QMessageBox::warning(this, "Login Failed", "Invalid password. Please try again.");
}
}
} else {
qDebug() << "Invalid username.";
QMessageBox::warning(this, "Login Failed", "Invalid username.");
}
} else {
qDebug() << "Login query error:" << query.lastError().text();
QMessageBox::critical(this, "Login Error", "Failed to execute login query.");
}
}else if(admin==false && employee==true){
QString email = ui->username_lineEdit->text();
QString password = ui->pass_lineEdit->text();
QSqlQuery query;
query.prepare("SELECT employee_id, email, password_hash, password_salt, account_locked, lockout_time, failed_login_attempts, is_active "
"FROM employees WHERE email = :email LIMIT 1");
query.bindValue(":email", email);
if (query.exec()) {
if (query.next()) { // If a result is found
int employeeId = query.value("employee_id").toInt();
QString storedPasswordHash = query.value("password_hash").toString();
QString storedPasswordSalt = query.value("password_salt").toString();
bool accountLocked = query.value("account_locked").toBool();
QDateTime lockoutTime = query.value("lockout_time").toDateTime();
int failedLoginAttempts = query.value("failed_login_attempts").toInt();
bool isActive = query.value("is_active").toBool();
// Check if the account is active
if (!isActive) {
qDebug() << "Account is deactivated.";
QMessageBox::warning(this, "Account Inactive", "Your account has been deactivated. Please contact support.");
return;
}
// Check if the account is locked
if (accountLocked) {
QDateTime currentTime = QDateTime::currentDateTime();
if (lockoutTime.isValid() && lockoutTime.addSecs(300) > currentTime) { // 5-minute lockout
qDebug() << "Account is locked. Lockout will expire at:" << lockoutTime.addSecs(300).toString();
QMessageBox::warning(this, "Account Locked", "Your account is temporarily locked due to multiple failed login attempts. Please try again later.");
return;
} else {
// Unlock account after lockout period
query.prepare("UPDATE employees SET account_locked = 0, failed_login_attempts = 0 WHERE employee_id = :id");
query.bindValue(":id", employeeId);
if (!query.exec()) {
qDebug() << "Failed to unlock account:" << query.lastError().text();
}
}
}
// Log login attempt
QString logDetails = QString("User %1 login attempt.").arg(email);
GlobalFunctions::set_user(email, employeeId);
GlobalFunctions::log_user_login(); // Log login attempt
// Verify the password with the stored hash and salt
if (GlobalFunctions::verifyPassword(password, storedPasswordHash, storedPasswordSalt)) {
qDebug() << "Login successful!";
QMessageBox::information(this, "Login Success", "You have successfully logged in.");
// Reset failed login attempts on successful login
query.prepare("UPDATE employees SET failed_login_attempts = 0, last_login = :last_login WHERE employee_id = :id");
query.bindValue(":last_login", QDateTime::currentDateTime());
query.bindValue(":id", employeeId);
if (!query.exec()) {
qDebug() << "Failed to update login details:" << query.lastError().text();
}
// Proceed to the main application window
hide();
DashBoard *dash_board = new DashBoard(nullptr);
dash_board->show();
} else {
// Increment failed login attempts
failedLoginAttempts++;
query.prepare("UPDATE employees SET failed_login_attempts = :failed_attempts WHERE employee_id = :id");
query.bindValue(":failed_attempts", failedLoginAttempts);
query.bindValue(":id", employeeId);
if (!query.exec()) {
qDebug() << "Failed to update failed login attempts:" << query.lastError().text();
}
// Lock account after 3 failed attempts
if (failedLoginAttempts >= 3) {
query.prepare("UPDATE employees SET account_locked = 1, lockout_time = :lockout_time WHERE employee_id = :id");
query.bindValue(":lockout_time", QDateTime::currentDateTime());
query.bindValue(":id", employeeId);
if (!query.exec()) {
qDebug() << "Failed to lock account:" << query.lastError().text();
}
QMessageBox::warning(this, "Account Locked", "Your account has been locked due to multiple failed login attempts. Please try again later.");
} else {
QMessageBox::warning(this, "Login Failed", "Invalid password. Please try again.");
}
}
} else {
qDebug() << "Invalid email.";
QMessageBox::warning(this, "Login Failed", "Invalid email.");
}
} else {
qDebug() << "Login query error:" << query.lastError().text();
QMessageBox::critical(this, "Login Error", "Failed to execute login query.");
}
}else{
QMessageBox::warning(this,"Report","Something went wrong");
}
}
void MainWindow::closeEvent(QCloseEvent *event)
{
if (QMessageBox::question(this, "Close IMS", "Are you sure you want to exit and end all processes?") == QMessageBox::Yes) {
//logs
GlobalFunctions::log_user_logout();
// Close the database connection
DatabaseManager &dbManager = DatabaseManager::instance();
if (dbManager.getDatabase().isOpen()) {
dbManager.getDatabase().close();
qDebug() << "Database connection closed.";
}
// Perform any additional cleanup if required
QApplication::quit(); // Terminate the application
event->accept();
} else {
event->ignore(); // Keep the window open
}
}
void MainWindow::on_toggle_button_main_circle_clicked()
{
// Fade out effect for the main circle button
QGraphicsOpacityEffect *opacityEffectMain = new QGraphicsOpacityEffect(this);
ui->toggle_button_main_circle->setGraphicsEffect(opacityEffectMain);
QPropertyAnimation *fadeOutMain = new QPropertyAnimation(opacityEffectMain, "opacity");
fadeOutMain->setDuration(300); // 300 ms for fade out
fadeOutMain->setStartValue(1.0); // Fully visible
fadeOutMain->setEndValue(0.0); // Fully transparent
// Fade in effect for the admin circle button
QGraphicsOpacityEffect *opacityEffectAdmin = new QGraphicsOpacityEffect(this);
ui->toggle_button_admin_circle->setGraphicsEffect(opacityEffectAdmin);
QPropertyAnimation *fadeInAdmin = new QPropertyAnimation(opacityEffectAdmin, "opacity");
fadeInAdmin->setDuration(300); // 300 ms for fade in
fadeInAdmin->setStartValue(0.0); // Fully transparent
fadeInAdmin->setEndValue(1.0); // Fully visible
// Chain animations
connect(fadeOutMain, &QPropertyAnimation::finished, [=]() {
ui->toggle_button_main_circle->hide(); // Hide the main button after fade-out
ui->toggle_button_admin_circle->show(); // Show the admin button
ui->label_input_uername_email->setText("Username");
ui->username_lineEdit->setPlaceholderText("Enter your username");
ui->toogle_status_label->setText("Admin");
ui->title_job_label->setText("Login as Admin");
admin = true;
employee = false;
//visible the forget button
ui->forgot_password_btn->setVisible(true);
fadeInAdmin->start(QAbstractAnimation::DeleteWhenStopped);
});
fadeOutMain->start(QAbstractAnimation::DeleteWhenStopped);
}
void MainWindow::on_toggle_button_admin_circle_clicked()
{
// Fade out effect for the admin circle button
QGraphicsOpacityEffect *opacityEffectAdmin = new QGraphicsOpacityEffect(this);
ui->toggle_button_admin_circle->setGraphicsEffect(opacityEffectAdmin);
QPropertyAnimation *fadeOutAdmin = new QPropertyAnimation(opacityEffectAdmin, "opacity");
fadeOutAdmin->setDuration(300); // 300 ms for fade out
fadeOutAdmin->setStartValue(1.0); // Fully visible
fadeOutAdmin->setEndValue(0.0); // Fully transparent
// Fade in effect for the main circle button
QGraphicsOpacityEffect *opacityEffectMain = new QGraphicsOpacityEffect(this);
ui->toggle_button_main_circle->setGraphicsEffect(opacityEffectMain);
QPropertyAnimation *fadeInMain = new QPropertyAnimation(opacityEffectMain, "opacity");
fadeInMain->setDuration(300); // 300 ms for fade in
fadeInMain->setStartValue(0.0); // Fully transparent
fadeInMain->setEndValue(1.0); // Fully visible
// Chain animations
connect(fadeOutAdmin, &QPropertyAnimation::finished, [=]() {
ui->toggle_button_admin_circle->hide(); // Hide the admin button after fade-out
ui->toggle_button_main_circle->show(); // Show the main button
ui->label_input_uername_email->setText("Email");
ui->username_lineEdit->setPlaceholderText("Enter your email");
ui->toogle_status_label->setText("Employee");
ui->title_job_label->setText("Login as Employee");
admin = false;
employee=true;
//hide the forget button
ui->forgot_password_btn->setVisible(false);
fadeInMain->start(QAbstractAnimation::DeleteWhenStopped);
});
fadeOutAdmin->start(QAbstractAnimation::DeleteWhenStopped);
}
void MainWindow::on_forgot_password_btn_clicked()
{
if (admin && !employee) {
// Admin is logged in
Forgot_Password = new Forgot_Password_Dialog("admin");
Forgot_Password->exec();
//ui->forgot_password_btn->setVisible(true);
} else if (!admin && employee) {
//ui->forgot_password_btn->setVisible(false);
} else {
// Optional: Handle case where neither or both are true (invalid state)
QMessageBox::warning(this, "Error", "Invalid user role detected.");
}
}