-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathupdateemployee.cpp
More file actions
122 lines (98 loc) · 4.34 KB
/
updateemployee.cpp
File metadata and controls
122 lines (98 loc) · 4.34 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
#include "updateemployee.h"
#include "ui_updateemployee.h"
#include <QApplication>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QSqlQuery>
#include <QSqlError>
#include <QSqlDatabase>
#include <QDate>
#include <QDebug>
//hashing
#include <QUuid>
#include <QCryptographicHash>
UpdateEmployee::UpdateEmployee(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::UpdateEmployee)
, tableView(new QTableView(this)),
model(new QSqlTableModel(this))
{
ui->setupUi(this);
// Set up the QTableView
model->setTable("employees");
model->setEditStrategy(QSqlTableModel::OnManualSubmit); // Manual commit
model->select(); // Load data lazily
tableView->setModel(model);
tableView->setSelectionMode(QAbstractItemView::SingleSelection);
tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->table_horizontalLayout->addWidget(tableView);
// Adjust column sizes based on the content
tableView->resizeColumnsToContents();
// Set a minimum width for each column to ensure the table looks good
for (int column = 0; column < model->columnCount(); ++column) {
// Set a minimum width for the column, you can adjust this value as needed
tableView->setColumnWidth(column, qMax(tableView->columnWidth(column), 100)); // Minimum width is 100px
}
}
UpdateEmployee::~UpdateEmployee()
{
delete ui;
}
void UpdateEmployee::on_Update_btn_clicked()
{
// Commit the changes made in the table back to the database
if (model->submitAll()) {
QMessageBox::information(this, "Success", "Product(s) updated successfully.");
refreshTable(); // Refresh the table view to reflect the changes
} else {
QMessageBox::critical(this, "Error", "Failed to update product(s).");
}
}
void UpdateEmployee::refreshTable(){
model->select(); // Reload data from the database into the model
tableView->resizeColumnsToContents(); // Adjust column sizes based on content
// Adjust column sizes based on the content
tableView->resizeColumnsToContents();
// Set a minimum width for each column to ensure the table looks good
for (int column = 0; column < model->columnCount(); ++column) {
// Set a minimum width for the column, you can adjust this value as needed
tableView->setColumnWidth(column, qMax(tableView->columnWidth(column), 100)); // Minimum width is 100px
}
}
void UpdateEmployee::on_reset_btn_clicked()
{
QModelIndexList selectedRows = tableView->selectionModel()->selectedRows();
if (selectedRows.isEmpty()) {
QMessageBox::warning(this, "No Selection", "Please select an employee.");
return;
}
int row = selectedRows.first().row();
QString employeeId = model->data(model->index(row, model->fieldIndex("employee_id"))).toString();
QString email = model->data(model->index(row, model->fieldIndex("email"))).toString();
QString uuidPassword = QUuid::createUuid().toString(QUuid::WithoutBraces).remove("-");
// Desired length
int desiredLength = 8;
// Ensure it's not longer than the UUID itself
if (desiredLength < uuidPassword.length()) {
uuidPassword = uuidPassword.left(desiredLength);
}
qDebug() << "Generated UUID Password (length" << desiredLength << "):" << uuidPassword;
// Generate temporary password (e.g., using employee ID)
QString tempPassword = uuidPassword; // You can customize this pattern
QString salt = QUuid::createUuid().toString().remove("{").remove("}").remove("-");
QByteArray salted = (tempPassword + salt).toUtf8();
QString hash = QString(QCryptographicHash::hash(salted, QCryptographicHash::Sha256).toHex());
// Update password_hash, password_salt, and force_password_change
QSqlQuery query;
query.prepare("UPDATE employees SET password_hash = :hash, password_salt = :salt, force_password_change = 1 WHERE employee_id = :id");
query.bindValue(":hash", hash);
query.bindValue(":salt", salt);
query.bindValue(":id", employeeId);
if (query.exec()) {
QMessageBox::information(this, "Password Reset",
QString("Password has been reset to: %1\nPlease ask the employee to log in and change it.")
.arg(tempPassword));
} else {
QMessageBox::critical(this, "Error", "Failed to reset password:\n" + query.lastError().text());
}
}