-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathusershandler.cpp
More file actions
835 lines (692 loc) · 30.5 KB
/
Copy pathusershandler.cpp
File metadata and controls
835 lines (692 loc) · 30.5 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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
#include "usershandler.h"
#include <QTableWidget>
#include <QObject>
#include <QTableView>
#include <QHeaderView>
#include <QDialog>
#include <QComboBox>
#include <QLineEdit>
#include <QPushButton>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QFormLayout>
#include <QFile>
#include <QTextStream>
#include <QDir>
#include <QDateTime>
#include <QList>
#include <QStringList>
#include <QLabel>
UsersHandler::UsersHandler(QObject *parent)
: QObject(parent)
,full_user{"Comum","Produção"}
, password("----")
{
QString filePath = QDir::currentPath() + "/user_credentials.csv";
QFile file(filePath);
if (!file.exists()) {
// Arquivo não existe, cria com cabeçalho e insere o admin
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream stream(&file);
stream << "user_type,username,password,changed_date" << Qt::endl;
stream << "Administrador,admin,admin_password,------" << Qt::endl;
stream << "Administrador,lucas_luchiari,lucas_luchiari_senha,-----" << Qt::endl;
file.close();
} else {
QMessageBox::critical(nullptr, "Erro", "Erro ao criar o arquivo de credenciais.");
// Lidar com o erro, talvez sair do programa ou usar valores padrão.
full_user.type = "Produção";
full_user.username = "Comum";
password = "----";
return; // Importante: sair do construtor em caso de erro na criação do arquivo.
}
} else {
// Arquivo existe, tenta ler o primeiro usuário (ou valores padrão)
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QTextStream stream(&file);
stream.readLine(); // Ignora o cabeçalho
if (!stream.atEnd()) { // Verifica se há pelo menos uma linha de dados
QString line = stream.readLine();
QStringList fields = line.split(',');
if (fields.size() >= 3) { // Verifica se há pelo menos tipo, nome e senha
full_user.type = fields[0];
full_user.username = fields[1];
password = fields[2];
} else {
// Arquivo corrompido ou com formato incorreto, usa valores padrão
QMessageBox::warning(nullptr, "Aviso", "Arquivo de credenciais corrompido. Usando valores padrão.");
full_user.type = "Produção";
full_user.username = "Comum";
password = "----";
}
} else {
// Arquivo vazio (após o cabeçalho), usa valores padrão
full_user.type = "Produção";
full_user.username = "Comum";
password = "----";
}
file.close();
} else {
QMessageBox::critical(nullptr, "Erro", "Erro ao abrir o arquivo de credenciais.");
full_user.type = "Produção";
full_user.username = "Comum";
password = "----";
return; // Importante: sair do construtor em caso de erro na abertura do arquivo.
}
}
}
UsersHandler::~UsersHandler()
{
}
// Abrir a janela de adicionar um usuário
void UsersHandler::showAddUserDialog(UserType myUser)
{
QDialog *dialog = new QDialog();
dialog->setWindowTitle("Adicionar Usuário");
dialog->resize(400, 200);
QComboBox *typeComboBox = new QComboBox;
typeComboBox->addItem("Administrador");
typeComboBox->addItem("Engenharia");
typeComboBox->addItem("Produção");
typeComboBox->addItem("Qualidade");
QLineEdit *userNameEdit = new QLineEdit;
userNameEdit->setPlaceholderText("Nome de Usuário");
QLineEdit *passwordEdit = new QLineEdit;
passwordEdit->setPlaceholderText("Senha");
passwordEdit->setEchoMode(QLineEdit::Password);
QPushButton *okButton = new QPushButton("OK");
QPushButton *cancelButton = new QPushButton("Cancel");
QVBoxLayout *mainLayout = new QVBoxLayout(dialog);
mainLayout->setSpacing(15);
QFormLayout *formLayout = new QFormLayout();
mainLayout->setSpacing(15);
formLayout->addRow("Tipo:", typeComboBox);
formLayout->addRow(userNameEdit);
formLayout->addRow(passwordEdit);
QHBoxLayout *buttonLayout = new QHBoxLayout;
mainLayout->setSpacing(15);
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
mainLayout->addLayout(formLayout);
mainLayout->addLayout(buttonLayout);
dialog->setLayout(mainLayout);
connect(okButton, &QPushButton::clicked, dialog, &QDialog::accept);
connect(cancelButton, &QPushButton::clicked, dialog, &QDialog::reject);
while (true) {
if (dialog->exec() == QDialog::Accepted) {
QString type = typeComboBox->currentText();
QString userName = userNameEdit->text().trimmed();
QString password = passwordEdit->text();
// Validações
if (password.length() < 4) {
QMessageBox::critical(dialog, "Erro", "A senha deve ter no mínimo 4 dígitos.");
continue;
}
if (userName.contains(" ")) {
QMessageBox::critical(dialog, "Erro", "O nome de usuário não pode ter espaços internos.");
continue;
}
if (userName.isEmpty()) {
QMessageBox::critical(dialog, "Erro", "O nome de usuário não pode estar vazio.");
continue;
}
if (addUser(type, userName, password)) {
QMessageBox::information(dialog, "Sucesso", "Usuário adicionado com sucesso.");
break;
}
} else {
break;
}
}
dialog->close();
}
// Criar o cabeçlho
void UsersHandler::createFileWithHeader(const QString& filePath) {
QFile file(filePath);
if (!file.exists()) {
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream stream(&file);
stream << "type,username,password,changed_date" << Qt::endl;
file.close();
} else {
QMessageBox::critical(nullptr, "Erro", "Erro ao criar o arquivo.");
}
}
}
// Função acessora de inserir o usuário previamente filtrado no banco de dados
bool UsersHandler::addUser(const QString &type, const QString &userName, const QString &password)
{
QString filePath = QDir::currentPath() + "/user_credentials.csv";
QString tempFilePath = filePath + ".temp";
createFileWithHeader(filePath);
QFile file(filePath);
QFile tempFile(tempFilePath);
QList<QStringList> users;
bool userExists = false;
try {
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
throw std::runtime_error("Erro ao abrir o arquivo para leitura.");
}
QTextStream inStream(&file);
QString header = inStream.readLine();
while (!inStream.atEnd()) {
QString line = inStream.readLine();
QStringList fields = line.split(',');
users.append(fields);
if (fields.size() >= 2 && fields[1] == userName) {
userExists = true;
break;
}
}
file.close();
if (userExists) {
QMessageBox::critical(nullptr, "Erro", "Este nome de usuário já existe.");
return false;
}
QStringList newUser;
newUser << type << userName << password << QDateTime::currentDateTime().toString("dd-MM-yyyy-hh:mm:ss");
users.append(newUser);
std::sort(users.begin(), users.end(), [](const QStringList &a, const QStringList &b) {
if (a[0] == b[0]) {
return a[1] < b[1];
}
return a[0] < b[0];
});
if (!tempFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
throw std::runtime_error("Erro ao abrir o arquivo temporário para escrita.");
}
QTextStream outStream(&tempFile);
outStream << header << Qt::endl;
for (const auto &user : users) {
outStream << user.join(',') << Qt::endl;
}
tempFile.close();
if (!QFile::remove(filePath)) {
throw std::runtime_error("Erro ao remover o arquivo original.");
}
if (!QFile::rename(tempFilePath, filePath)) {
throw std::runtime_error("Erro ao renomear o arquivo temporário.");
}
return true;
} catch (const std::runtime_error& error) {
QMessageBox::critical(nullptr, "Erro", error.what());
if (file.isOpen()) file.close();
if (tempFile.isOpen()) tempFile.close();
QFile::remove(tempFilePath);
return false;
} catch (const std::exception& error) {
QMessageBox::critical(nullptr, "Erro", error.what());
if (file.isOpen()) file.close();
if (tempFile.isOpen()) tempFile.close();
QFile::remove(tempFilePath);
return false;
} catch (...) {
QMessageBox::critical(nullptr, "Erro", "Ocorreu um erro desconhecido.");
if (file.isOpen()) file.close();
if (tempFile.isOpen()) tempFile.close();
QFile::remove(tempFilePath);
return false;
}
}
// Função para visualização dos usuários cadastrados
void UsersHandler::showViewUsersDialog(UserType myUser) {
// Criando a janela de diálogo
QDialog *dialog = new QDialog();
dialog->setWindowTitle("Lista de Usuários");
dialog->resize(600, 400);
// Criando a tabela
QTableWidget *tableWidget = new QTableWidget(dialog);
if(myUser.type== "Administrador"){
tableWidget->setColumnCount(4);// Exibir apenas tipo, nome de usuário, senha e data de modificação
tableWidget->setHorizontalHeaderLabels({"Tipo", "Nome de Usuário", "Senha","Data de Modificação"});
}
else{
tableWidget->setColumnCount(3); // Exibir apenas tipo, nome de usuário e data de modificação
tableWidget->setHorizontalHeaderLabels({"Tipo", "Nome de Usuário", "Data de Modificação"});
}
tableWidget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers); // Impede edição da tabela
// Criando botão de fechar
QPushButton *closeButton = new QPushButton("Fechar", dialog);
// Layout
QVBoxLayout *layout = new QVBoxLayout(dialog);
layout->addWidget(tableWidget);
layout->addWidget(closeButton);
dialog->setLayout(layout);
try {
// Carregar os dados do CSV
QString filePath = QDir::currentPath() + "/user_credentials.csv";
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::critical(dialog, "Erro", "Erro ao abrir o arquivo de usuários.");
return;
}
QTextStream stream(&file);
QString header = stream.readLine(); // Ignorar o cabeçalho
QList<QStringList> users;
while (!stream.atEnd()) {
QString line = stream.readLine();
QStringList fields = line.split(',');
if (fields.size() >= 4) {
QStringList userData;
if(myUser.type == "Administrador")
userData << fields[0] << fields[1] << fields[2] << fields[3]; // Tipo, Nome, senha, Data de Modificação
else
userData << fields[0] << fields[1] << fields[3]; // Tipo, Nome, Data de Modificação
users.append(userData);
}
}
file.close();
// Ordenar os usuários primeiro por tipo, depois por nome
std::sort(users.begin(), users.end(), [](const QStringList &a, const QStringList &b) {
return a[0] == b[0] ? a[1] < b[1] : a[0] < b[0];
});
// Preencher a tabela
tableWidget->setRowCount(users.size());
for (int i = 0; i < users.size(); ++i) {
if (myUser.type == "Administrador"){
for (int j = 0; j < 4; ++j) {
tableWidget->setItem(i, j, new QTableWidgetItem(users[i][j]));
}
}else {
for (int j = 0; j < 3; ++j) {
tableWidget->setItem(i, j, new QTableWidgetItem(users[i][j]));
}
}
}
}
catch (const std::runtime_error& error) {
QMessageBox::critical(dialog, "Erro", error.what());
return; // Importante: sair da função em caso de erro
} catch (const std::exception& error) {
QMessageBox::critical(dialog, "Erro", error.what());
return; // Importante: sair da função em caso de erro
} catch (...) {
QMessageBox::critical(dialog, "Erro", "Ocorreu um erro desconhecido.");
return; // Importante: sair da função em caso de erro
}
// Conectar botão de fechar
connect(closeButton, &QPushButton::clicked, dialog, &QDialog::accept);
// Exibir a janela
dialog->exec();
}
// Remove usuário
void UsersHandler::showRemoveUserDialog() {
QString filePath = "user_credentials.csv"; // Caminho do arquivo CSV
// Tenta abrir o arquivo CSV
QVector<QStringList> userData;
try {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(nullptr, "Erro", "Não foi possível abrir o arquivo CSV.");
return; // Sai da função se não conseguir abrir o arquivo
}
QTextStream in(&file);
in.readLine(); // Pula o cabeçalho
while (!in.atEnd()) {
userData.append(in.readLine().split(",")); // Lê os dados do CSV
}
file.close();
} catch (const std::exception& error) {
QMessageBox::critical(nullptr, "Erro", QString("Erro ao ler o arquivo: %1").arg(error.what()));
return; // Sai da função em caso de erro
} catch (...) {
QMessageBox::critical(nullptr, "Erro", "Ocorreu um erro desconhecido ao tentar abrir o arquivo.");
return; // Sai da função em caso de erro desconhecido
}
// Cria a interface do diálogo
QDialog *dialog = new QDialog();
dialog->setWindowTitle("Remover Usuário");
dialog->resize(300,150);
QComboBox *userComboBox = new QComboBox(dialog);
QPushButton *removeButton = new QPushButton("Remover", dialog);
QPushButton *cancelButton = new QPushButton("Cancelar", dialog);
// layout principal
QVBoxLayout *mainLayout = new QVBoxLayout(dialog);
// Label e combo box
QHBoxLayout *selectionLayout = new QHBoxLayout();
selectionLayout->addWidget(new QLabel("Selecione o usuário:"));
selectionLayout->addWidget(userComboBox);
mainLayout->addLayout(selectionLayout);
// bottons abaixo
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->addWidget(removeButton);
buttonLayout->addWidget(cancelButton);
mainLayout->addLayout(buttonLayout);
// Preenche o ComboBox com os usuários
for (const QStringList& user : userData) {
if (user.size() > 1) { // Verifica se há pelo menos um nome de usuário
userComboBox->addItem(user[1]); // Adiciona o nome do usuário ao ComboBox
}
}
// Conectar os sinais dos botões
connect(cancelButton, &QPushButton::clicked, dialog, &QDialog::reject);
connect(removeButton, &QPushButton::clicked, dialog, &QDialog::accept);
while (true){
if(dialog->exec() == QDialog::Accepted){
QString user_to_remove = userComboBox->currentText();
// Verifica se um usuário foi selecionado
if (user_to_remove.isEmpty()) {
QMessageBox::warning(dialog, "Erro", "Selecione um usuário para remover.");
continue;
}
// Confirmação de remoção
if (QMessageBox::question(dialog, "Confirmação"
, QString("Deseja realmente remover o usuário %1?").arg(user_to_remove)
, QMessageBox::Yes | QMessageBox::No
, QMessageBox::No)
!= QMessageBox::Yes)
{
return; // Aborta a operação se o usuário não confirmar
}
if(removeUser(user_to_remove,userData)){
QMessageBox::information(dialog, "Sucesso", "Usuário removido com sucesso.");
break;
}
} else
break;
}
dialog->accept(); // Fecha o diálogo após a remoção bem-sucedida
}
// Função acessora para remoção de um usuário
bool UsersHandler::removeUser(const QString &userName,QVector<QStringList> userData){
QString filePath = "user_credentials.csv"; // Caminho do arquivo CSV
// Remove o usuário selecionado do QVector
userData.erase(std::remove_if(userData.begin(), userData.end(),
[userName](const QStringList& row) {
return row.size() > 1 && row[1] == userName;
}), userData.end());
// Tenta reescrever o arquivo CSV
try {
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly)) {
throw std::runtime_error("Erro ao abrir o arquivo para leitura.");
return false;
}
QTextStream out(&file);
out << "type,username,password,changed_date\n"; // Escreve o cabeçalho
for (const QStringList& row : userData) {
out << row.join(",") << "\n"; // Escreve os dados atualizados
}
file.close();
return true;
} catch (const std::exception& error) {
QMessageBox::critical(nullptr, "Erro", QString("Erro ao salvar o arquivo: %1").arg(error.what()));
} catch (...) {
QMessageBox::critical(nullptr, "Erro", "Ocorreu um erro desconhecido ao tentar salvar o arquivo.");
}
return true;
}
// Atualizar usuário
void UsersHandler::showUpdateUserDialog(UserType myUser) {
if(full_user.username =="Comum"){
QMessageBox::information(nullptr, "Erro", "Login não foi feito para atualizar perfil.");
return;
}
QString filePath = "user_credentials.csv";
QVector<QStringList> userData;
try {
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly)) {
QMessageBox::critical(nullptr, "Erro", "Não foi possível abrir o arquivo CSV.");
return;
}
QTextStream in(&file);
in.readLine(); // Pular o cabeçalho
while (!in.atEnd()) {
userData.append(in.readLine().split(","));
}
file.close();
} catch (const std::exception& error) {
QMessageBox::critical(nullptr, "Erro", QString("Erro ao ler o arquivo: %1").arg(error.what()));
return;
} catch (...) {
QMessageBox::critical(nullptr, "Erro", "Ocorreu um erro desconhecido ao tentar abrir o arquivo.");
return;
}
if (myUser.type != "Administrador") {
QString selectedUser = myUser.username;
QString _userType = myUser.type;
QDialog *updateDialog = new QDialog();
updateDialog->setWindowTitle("Atualizar Usuário");
QLineEdit *userNameEdit = new QLineEdit(selectedUser);
QLineEdit *passwordEdit = new QLineEdit();
QPushButton *updateButton = new QPushButton("Atualizar", updateDialog);
QPushButton *cancelUpdateButton = new QPushButton("Cancelar", updateDialog);
QVBoxLayout *mainLayout = new QVBoxLayout(updateDialog);
QFormLayout *formLayout = new QFormLayout();
formLayout->addRow("Nome de Usuário:", userNameEdit);
formLayout->addRow("Senha:", passwordEdit);
mainLayout->addLayout(formLayout);
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->addWidget(updateButton);
buttonLayout->addWidget(cancelUpdateButton);
mainLayout->addLayout(buttonLayout);
int selectedIndex = -1;
for (int i = 0; i < userData.size(); ++i) {
if (userData[i][1] == selectedUser) {
selectedIndex = i;
break;
}
}
connect(cancelUpdateButton, &QPushButton::clicked, updateDialog, &QDialog::reject);
connect(updateButton, &QPushButton::clicked, [&, this, updateDialog, userData, selectedIndex,_userType]() mutable {
QString newUsername = userNameEdit->text().trimmed();
QString newPassword = passwordEdit->text();
if (newUsername.isEmpty()) {
QMessageBox::critical(updateDialog, "Erro", "O nome de usuário não pode estar vazio.");
return;
}
if (newUsername.contains(" ")) {
QMessageBox::critical(updateDialog, "Erro", "O nome de usuário não pode ter espaços internos.");
return;
}
userData[selectedIndex][0] = _userType;
userData[selectedIndex][1] = newUsername;
userData[selectedIndex][2] = newPassword;
userData[selectedIndex][3] = QDateTime::currentDateTime().toString("dd-MM-yyyy-hh:mm:ss");
try {
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::critical(updateDialog, "Erro", "Não foi possível salvar as alterações no arquivo.");
return;
}
QTextStream out(&file);
out << "type,username,password,changed_date\n";
for (const QStringList& row : userData) {
out << row.join(",") << "\n";
}
file.close();
QMessageBox::information(updateDialog, "Sucesso", "Usuário atualizado com sucesso.");
updateDialog->accept();
} catch (const std::exception& error) {
QMessageBox::critical(updateDialog, "Erro", QString("Erro ao salvar o arquivo: %1").arg(error.what()));
} catch (...) {
QMessageBox::critical(updateDialog, "Erro", "Ocorreu um erro desconhecido ao tentar salvar o arquivo.");
}
});
updateDialog->exec();
return;
} else { // Is Administrator
QDialog* selectDialog = new QDialog();
selectDialog->setWindowTitle("Selecionar Usuário para update");
QComboBox *userComboBox = new QComboBox(selectDialog);
QPushButton *selectButton = new QPushButton("Selecionar", selectDialog);
QPushButton *cancelButton = new QPushButton("Cancelar", selectDialog);
QVBoxLayout *selectLayout = new QVBoxLayout(selectDialog);
selectLayout->addWidget(userComboBox);
QHBoxLayout *selectionLayout = new QHBoxLayout();
selectionLayout->addWidget(new QLabel("Selecione o usuário para update:"));
selectionLayout->addWidget(userComboBox);
selectLayout->addLayout(selectionLayout);
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->addWidget(selectButton);
buttonLayout->addWidget(cancelButton);
selectLayout->addLayout(buttonLayout);
for (const QStringList& user : userData) {
if (user.size() > 1) {
userComboBox->addItem(user[1]);
}
}
connect(cancelButton, &QPushButton::clicked, selectDialog, &QDialog::reject);
connect(selectButton, &QPushButton::clicked, [&, this, selectDialog, userData]() {
QString selectedUser = userComboBox->currentText();
if (selectedUser.isEmpty()) {
QMessageBox::warning(selectDialog, "Erro", "Selecione um usuário.");
return;
}
QDialog *updateDialog = new QDialog(selectDialog);
updateDialog->setWindowTitle("Atualizar Usuário");
QComboBox *typeComboBox = new QComboBox;
typeComboBox->addItem("Administrador");
typeComboBox->addItem("Engenharia");
typeComboBox->addItem("Produção");
typeComboBox->addItem("Qualidade");
QLineEdit *userNameEdit = new QLineEdit(selectedUser);
QLineEdit *passwordEdit = new QLineEdit();
QPushButton *updateButton = new QPushButton("Atualizar", updateDialog);
QPushButton *cancelUpdateButton = new QPushButton("Cancelar", updateDialog);
QVBoxLayout *mainLayout = new QVBoxLayout(updateDialog); // Corrected: Main layout added
QFormLayout *formLayout = new QFormLayout();
formLayout->addRow("Tipo:", typeComboBox);
formLayout->addRow("Nome de Usuário:", userNameEdit);
formLayout->addRow("Senha:", passwordEdit);
mainLayout->addLayout(formLayout);
QHBoxLayout *buttonLayout = new QHBoxLayout();
buttonLayout->addWidget(updateButton);
buttonLayout->addWidget(cancelUpdateButton);
mainLayout->addLayout(buttonLayout);
int selectedIndex = -1;
for (int i = 0; i < userData.size(); ++i) {
if (userData[i][1] == selectedUser) {
selectedIndex = i;
break;
}
}
if (selectedIndex != -1) {
typeComboBox->setCurrentText(userData[selectedIndex][0]);
}
connect(cancelUpdateButton, &QPushButton::clicked, updateDialog, &QDialog::reject);
connect(updateButton, &QPushButton::clicked, [&, this, updateDialog, userData, selectedIndex]() mutable {
QString newType = typeComboBox->currentText();
QString newUsername = userNameEdit->text().trimmed();
QString newPassword = passwordEdit->text();
if (newUsername.isEmpty()) {
QMessageBox::critical(updateDialog, "Erro", "O nome de usuário não pode estar vazio.");
return;
}
if (newUsername.contains(" ")) {
QMessageBox::critical(updateDialog, "Erro", "O nome de usuário não pode ter espaços internos.");
return;
}
userData[selectedIndex][0] = newType;
userData[selectedIndex][1] = newUsername;
userData[selectedIndex][2] = newPassword;
userData[selectedIndex][3] = QDateTime::currentDateTime().toString("dd-MM-yyyy-hh:mm:ss");
try {
QFile file(filePath);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
QMessageBox::critical(updateDialog, "Erro", "Não foi possível salvar as alterações no arquivo.");
return;
}
QTextStream out(&file);
out << "type,username,password,changed_date\n";
for (const QStringList& row : userData) {
out << row.join(",") << "\n";
}
file.close();
QMessageBox::information(updateDialog, "Sucesso", "Usuário atualizado com sucesso.");
updateDialog->accept(); // Fecha o diálogo após a atualização bem-sucedida
} catch (const std::exception& error) {
QMessageBox::critical(updateDialog, "Erro", QString("Erro ao salvar o arquivo: %1").arg(error.what()));
} catch (...) {
QMessageBox::critical(updateDialog, "Erro", "Ocorreu um erro desconhecido ao tentar salvar o arquivo.");
}
});
updateDialog->exec();
});
selectDialog->exec();
}
}
// login de usuário
UserType UsersHandler::loginAccess() {
full_user.username = "Comum"; // usuário base caso falha de login
full_user.type = "Produção";
QDialog dialog;
dialog.setWindowTitle("Login de Usuário");
QLineEdit *usernameEdit = new QLineEdit;
usernameEdit->setPlaceholderText("Nome de Usuário");
QLineEdit *passwordEdit = new QLineEdit;
passwordEdit->setPlaceholderText("Senha");
passwordEdit->setEchoMode(QLineEdit::Password);
QPushButton *okButton = new QPushButton("OK");
QPushButton *cancelButton = new QPushButton("Cancelar");
QVBoxLayout *mainLayout = new QVBoxLayout(&dialog);
QFormLayout *formLayout = new QFormLayout();
formLayout->addRow("Usuário:", usernameEdit);
formLayout->addRow("Senha:", passwordEdit);
QHBoxLayout *buttonLayout = new QHBoxLayout;
buttonLayout->addWidget(okButton);
buttonLayout->addWidget(cancelButton);
mainLayout->addLayout(formLayout);
mainLayout->addLayout(buttonLayout);
dialog.setLayout(mainLayout);
connect(cancelButton, &QPushButton::clicked, &dialog, &QDialog::reject);
connect(okButton, &QPushButton::clicked, [&]() {
QString enteredUsername = usernameEdit->text().trimmed();
QString enteredPassword = passwordEdit->text();
QString filePath = QDir::currentPath() + "/user_credentials.csv";
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
QMessageBox::critical(&dialog, "Erro", "Erro ao abrir o arquivo de credenciais.");
return;
}
QTextStream stream(&file);
stream.readLine(); // Ignora o cabeçalho
bool userFound = false;
bool passwordCorrect = false;
while (!stream.atEnd()) {
QString line = stream.readLine();
QStringList fields = line.split(',');
if (fields.size() >= 4 && fields[1] == enteredUsername) {
userFound = true;
if (fields[1] == enteredUsername && fields[2] == enteredPassword) {
passwordCorrect = true;
full_user.type = fields[0];
full_user.username = enteredUsername;
break; // Sai do loop após encontrar o usuário e senha corretos
}
}
}
file.close();
if (!userFound) {
QMessageBox::warning(&dialog, "Erro", "Usuário não cadastrado.");
} else if (!passwordCorrect) {
QMessageBox::warning(&dialog, "Erro", "Credenciais incorretas. Tente novamente.");
} else {
QMessageBox::information(&dialog, "Sucesso", QString("Login realizado com sucesso!\nUsuário: %1\nTipo: %2").arg(full_user.username, full_user.type));
dialog.accept(); // Aceita o diálogo apenas em caso de sucesso
}
});
if (dialog.exec() == QDialog::Accepted) {
return full_user;
} else {
return {"Comum", "Produção"}; // Retorna um usuário padrão se o login for cancelado
}
}
UserType UsersHandler::getFullUser() const
{
return full_user;
}
QString UsersHandler::getUserType() const
{
return full_user.type;
}
QString UsersHandler::getUsername() const
{
return full_user.username;
}
QString UsersHandler::getPassword() const
{
return password;
}