#include "ChangePasswordFormDialog.h" #include #include #include #include "components/ULineEdit.h" #include "models/User.h" #include "log/UserOperationLog.h" ChangePasswordFormDialog::ChangePasswordFormDialog(QWidget* parent, Qt::WindowFlags f) : GUIFormBaseDialog(parent, f) , mLEPasswd(new ULineEdit(this)) , mLENewPasswd(new ULineEdit(this)) , mLEConfirmPasswd(new ULineEdit(this)) , mLblError(new QLabel(this)) { initLayout(); } void ChangePasswordFormDialog::initLayout() { auto layout = new QVBoxLayout(mFormWidget); layout->setSpacing(10); // add title auto lblTitle = new QLabel(this); lblTitle->setAlignment(Qt::AlignCenter); lblTitle->setText(tr("Change Password")); lblTitle->setObjectName("title"); layout->addWidget(lblTitle); //add old password auto lblOld = new QLabel(this); lblOld->setText(tr("Current Password")); mLEPasswd->setEchoMode(QLineEdit::Password); mLEPasswd->setValidator(new QRegExpValidator(QRegExp("[a-zA-z0-9]+$"), mLEPasswd)); mLEPasswd->setMaxLength(30); layout->addWidget(lblOld); layout->addWidget(mLEPasswd); auto lblEndline1 = new QLabel(this); lblEndline1->setObjectName("endline"); lblEndline1->setFixedHeight(3); layout->addWidget(lblEndline1); //add new password auto lblNewPasswd = new QLabel(this); lblNewPasswd->setText(tr("New Password")); mLENewPasswd->setEchoMode(QLineEdit::Password); mLENewPasswd->setValidator(new QRegExpValidator(QRegExp("[a-zA-z0-9]+$"), mLENewPasswd)); mLENewPasswd->setMaxLength(30); layout->addWidget(lblNewPasswd); layout->addWidget(mLENewPasswd); auto lblEndline2 = new QLabel(this); lblEndline2->setFixedHeight(3); lblEndline2->setObjectName("endline"); layout->addWidget(lblEndline2); //add confirm password auto lblConfirm = new QLabel(this); lblConfirm->setText(tr("Confirm Password")); mLEConfirmPasswd->setEchoMode(QLineEdit::Password); mLEConfirmPasswd->setValidator(new QRegExpValidator(QRegExp("[a-zA-z0-9]+$"), mLEConfirmPasswd)); mLEConfirmPasswd->setMaxLength(30); layout->addWidget(lblConfirm); layout->addWidget(mLEConfirmPasswd); auto lblEndline3 = new QLabel(this); lblEndline3->setFixedHeight(3); lblEndline3->setObjectName("endline"); layout->addWidget(lblEndline3); mLblError->setObjectName("warn"); layout->addWidget(mLblError); } bool ChangePasswordFormDialog::updateReferenceData() { if (mLEPasswd->text().isEmpty()) { mLblError->setText(tr("Please enter your old password!")); return false; } if (mLENewPasswd->text().length() < 6) { mLblError->setText(tr("New password should at least 6 characters!")); return false; } QString encryptPwd = User::getEncryptedPassword(mLEPasswd->text()); if (encryptPwd != User::Current()->getPassword()) { mLblError->setText(tr("Wrong password!")); return false; } if (mLENewPasswd->text() != mLEConfirmPasswd->text()) { mLblError->setText(tr("Your new password does not match!")); return false; } User::Current()->setPassword(User::getEncryptedPassword(mLENewPasswd->text())); if (!User::Current()->submitChange()) { mLblError->setText(tr("Database update error!")); User::Current()->restorePassword(encryptPwd); return false; } LOG_USER_OPERATION("Change Password"); return true; }