From 9fa817a244b327636f2e30042e49595dd6a12a48 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Fri, 1 Nov 2019 13:38:14 +0100 Subject: [PATCH] [users] Refactor password-validation - add a method that does the work and sets UI strings for a generic double-field password entry - use that for user and root passwords --- src/modules/users/UsersPage.cpp | 80 ++++++++++----------------------- src/modules/users/UsersPage.h | 10 +++++ 2 files changed, 34 insertions(+), 56 deletions(-) diff --git a/src/modules/users/UsersPage.cpp b/src/modules/users/UsersPage.cpp index 287a771ea..9f4f31f9d 100644 --- a/src/modules/users/UsersPage.cpp +++ b/src/modules/users/UsersPage.cpp @@ -393,23 +393,19 @@ UsersPage::validateHostnameText( const QString& textRef ) emit checkReady( isReady() ); } -void -UsersPage::onPasswordTextChanged( const QString& ) +bool +UsersPage::checkPasswordAcceptance( const QString& pw1, const QString& pw2, QLabel* badge, QLabel* message ) { - QString pw1 = ui->textBoxUserPassword->text(); - QString pw2 = ui->textBoxUserVerifiedPassword->text(); - m_readyPassword = true; - if ( pw1.isEmpty() && pw2.isEmpty() ) { - ui->labelUserPasswordError->clear(); - ui->labelUserPassword->clear(); - m_readyPassword = false; + badge->clear(); + message->clear(); + return false; } else if ( pw1 != pw2 ) { - labelError( ui->labelUserPassword, ui->labelUserPasswordError, tr( "Your passwords do not match!" ) ); - m_readyPassword = false; + labelError( badge, message, tr( "Your passwords do not match!" ) ); + return false; } else { @@ -421,18 +417,24 @@ UsersPage::onPasswordTextChanged( const QString& ) if ( !s.isEmpty() ) { - labelError( ui->labelUserPassword, ui->labelUserPasswordError, s ); - m_readyPassword = false; - break; + labelError( badge, message, s ); + return false; } } } - if ( m_readyPassword ) - { - labelOk( ui->labelUserPassword, ui->labelUserPasswordError ); - } + labelOk( badge, message ); + return true; } +} + +void +UsersPage::onPasswordTextChanged( const QString& ) +{ + m_readyPassword = checkPasswordAcceptance( ui->textBoxUserPassword->text(), + ui->textBoxUserVerifiedPassword->text(), + ui->labelUserPassword, + ui->labelUserPasswordError ); emit checkReady( isReady() ); } @@ -440,44 +442,10 @@ UsersPage::onPasswordTextChanged( const QString& ) void UsersPage::onRootPasswordTextChanged( const QString& ) { - QString pw1 = ui->textBoxRootPassword->text(); - QString pw2 = ui->textBoxVerifiedRootPassword->text(); - m_readyRootPassword = true; - - if ( pw1.isEmpty() && pw2.isEmpty() ) - { - ui->labelRootPasswordError->clear(); - ui->labelRootPassword->clear(); - m_readyRootPassword = false; - } - else if ( pw1 != pw2 ) - { - labelError( ui->labelRootPassword, ui->labelRootPasswordError, tr( "Your passwords do not match!" ) ); - m_readyRootPassword = false; - } - else - { - if ( ui->checkBoxValidatePassword->isChecked() ) - { - for ( auto pc : m_passwordChecks ) - { - QString s = pc.filter( pw1 ); - - if ( !s.isEmpty() ) - { - labelError( ui->labelRootPassword, ui->labelRootPasswordError, s ); - m_readyRootPassword = false; - break; - } - } - } - - if ( m_readyRootPassword ) - { - labelOk( ui->labelRootPassword, ui->labelRootPasswordError ); - } - } - + m_readyRootPassword = checkPasswordAcceptance( ui->textBoxRootPassword->text(), + ui->textBoxVerifiedRootPassword->text(), + ui->labelRootPassword, + ui->labelRootPasswordError ); emit checkReady( isReady() ); } diff --git a/src/modules/users/UsersPage.h b/src/modules/users/UsersPage.h index d8aed07fc..520ce64a8 100644 --- a/src/modules/users/UsersPage.h +++ b/src/modules/users/UsersPage.h @@ -29,6 +29,8 @@ #include +class QLabel; + namespace Ui { class Page_UserSetup; @@ -75,6 +77,14 @@ signals: void checkReady( bool ); private: + /** @brief Is the password acceptable? + * + * Checks the two copies of the password and places error messages in the + * given QLabels. Returns true (and clears the error messages) if the + * password is acceptable. + */ + bool checkPasswordAcceptance( const QString& pw1, const QString& pw2, QLabel* badge, QLabel* message ); + Ui::Page_UserSetup* ui; PasswordCheckList m_passwordChecks;