-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwindow_changepassmember.cpp
More file actions
135 lines (117 loc) · 4.91 KB
/
window_changepassmember.cpp
File metadata and controls
135 lines (117 loc) · 4.91 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
#include "window_changepassmember.h"
#include "ui_window_changepassmember.h"
#include "window_updateaccount.h"
#include "ui_window_updateaccount.h"
#include <QFile>
#include <QTextStream>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <bits/stdc++.h>
#include <QDate>
#include <QString>
#include <QPair>
#include<QMessageBox>
#include<QCryptographicHash>
#include<globalvaribals.h>
#include<QByteArray>
#include<QDir>
#include "globalvaribals.h"
Window_ChangePassMember::Window_ChangePassMember(QWidget *parent) :
QDialog(parent),
ui(new Ui::Window_ChangePassMember)
{
ui->setupUi(this);
}
Window_ChangePassMember::~Window_ChangePassMember()
{
delete ui;
}
void Window_ChangePassMember::on_pushButton_ChangePass_clicked()
{
QDir account;
account.cd("..");
account.cd("Login_MY_FINAL");
account.cd("RowData");
QFile AccountsFile (account.path()+"/accounts.json");
AccountsFile.open(QIODevice::ReadWrite);
QJsonDocument jsonDocAcc = QJsonDocument::fromJson( AccountsFile.readAll() );
QJsonObject Accounts_Obj=jsonDocAcc.object();
AccountsFile.close();
QString MemberId = QString::number(GlobalVaribals::CurrentId);
if(Accounts_Obj.find(MemberId)==Accounts_Obj.end()){
QMessageBox::warning(this," ","Member Account not found!");
this->close();
return;
}
QJsonValueRef Account_ref = Accounts_Obj.find(MemberId).value();
QJsonObject Account_Obj= Account_ref.toObject();
QString RealHashedPassword= Account_Obj["Password"].toString();
QString EnteredOLDPassword = ui->lineEdit_OldPass->text();
QString EnteredNEWPassword = ui->lineEdit_NewPass->text();
QString EnteredConfirmPassword =ui->lineEdit_ConfirmPass->text();
if(EnteredNEWPassword.size()<8){
QMessageBox::warning(this,"","Password can not be less than 8 characters.");
this->close();
return;
}
if(EnteredNEWPassword==""||EnteredConfirmPassword==""){
QMessageBox::warning(this,"","All sections haven't filled out");
this->close();
}
else{
if(EnteredConfirmPassword!=EnteredNEWPassword){
QMessageBox::warning(this,"","New Password and confirmed password did not match");
this->close();
return;
}
char* ch;
QByteArray ba = EnteredOLDPassword.toLatin1();
ch=ba.data ();
QString HashedOLDEnteredPassword = QString(QCryptographicHash::hash((ch),QCryptographicHash::Keccak_512).toHex());
ba = EnteredNEWPassword.toLatin1();
ch=ba.data ();
QString HashedNEWEnteredPassword = QString(QCryptographicHash::hash((ch),QCryptographicHash::Keccak_512).toHex());
if(RealHashedPassword==HashedNEWEnteredPassword){
QMessageBox::warning(this,"","Entered Password is equal to the previous one");
this->close();
return;
}
if(RealHashedPassword==HashedOLDEnteredPassword&&EnteredConfirmPassword==EnteredNEWPassword){
QString newPassword= ui->lineEdit_NewPass->text();
QString MemberName= Account_Obj["Name"].toString();
QString Username= Account_Obj["Username"].toString();
QString ExpireDate= Account_Obj["ExpireDate"].toString();
QString date_added= Account_Obj["date_added"].toString();
int AccountType= Account_Obj["AccountType"].toInt();
QJsonObject RentedBooks= Account_Obj["RentedBooks"].toObject();
QJsonObject EditedAccount = { {"Name", MemberName},
{"Username", Username},
{"id", MemberId},
{"date_added", date_added},
{"ExpireDate", ExpireDate},
{"AccountType", AccountType},
{"Password", HashedNEWEnteredPassword},
{"RentedBooks",RentedBooks}
};
Accounts_Obj.remove(MemberId);
Accounts_Obj[MemberId]=EditedAccount;
QJsonDocument doc( Accounts_Obj );
if( !AccountsFile.open( QIODevice::WriteOnly ) ) //write json content to file.
{
qDebug()<<"error opening file for write.\n";
return;
}
AccountsFile.write(doc.toJson());
AccountsFile.close();
QMessageBox::information(this,"","Password updated successfully");
return;
}
if(RealHashedPassword!=HashedOLDEnteredPassword){
QMessageBox::warning(this,"","Incorrect old password");
this->close();
return;
}
}
}