[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
This commit is contained in:
Adriaan de Groot 2019-11-01 13:38:14 +01:00
parent 7d25173afb
commit 9fa817a244
2 changed files with 34 additions and 56 deletions

View File

@ -393,23 +393,19 @@ UsersPage::validateHostnameText( const QString& textRef )
emit checkReady( isReady() ); emit checkReady( isReady() );
} }
void bool
UsersPage::onPasswordTextChanged( const QString& ) 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() ) if ( pw1.isEmpty() && pw2.isEmpty() )
{ {
ui->labelUserPasswordError->clear(); badge->clear();
ui->labelUserPassword->clear(); message->clear();
m_readyPassword = false; return false;
} }
else if ( pw1 != pw2 ) else if ( pw1 != pw2 )
{ {
labelError( ui->labelUserPassword, ui->labelUserPasswordError, tr( "Your passwords do not match!" ) ); labelError( badge, message, tr( "Your passwords do not match!" ) );
m_readyPassword = false; return false;
} }
else else
{ {
@ -421,18 +417,24 @@ UsersPage::onPasswordTextChanged( const QString& )
if ( !s.isEmpty() ) if ( !s.isEmpty() )
{ {
labelError( ui->labelUserPassword, ui->labelUserPasswordError, s ); labelError( badge, message, s );
m_readyPassword = false; return false;
break;
} }
} }
} }
if ( m_readyPassword ) labelOk( badge, message );
return true;
}
}
void
UsersPage::onPasswordTextChanged( const QString& )
{ {
labelOk( ui->labelUserPassword, ui->labelUserPasswordError ); m_readyPassword = checkPasswordAcceptance( ui->textBoxUserPassword->text(),
} ui->textBoxUserVerifiedPassword->text(),
} ui->labelUserPassword,
ui->labelUserPasswordError );
emit checkReady( isReady() ); emit checkReady( isReady() );
} }
@ -440,44 +442,10 @@ UsersPage::onPasswordTextChanged( const QString& )
void void
UsersPage::onRootPasswordTextChanged( const QString& ) UsersPage::onRootPasswordTextChanged( const QString& )
{ {
QString pw1 = ui->textBoxRootPassword->text(); m_readyRootPassword = checkPasswordAcceptance( ui->textBoxRootPassword->text(),
QString pw2 = ui->textBoxVerifiedRootPassword->text(); ui->textBoxVerifiedRootPassword->text(),
m_readyRootPassword = true; ui->labelRootPassword,
ui->labelRootPasswordError );
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 );
}
}
emit checkReady( isReady() ); emit checkReady( isReady() );
} }

View File

@ -29,6 +29,8 @@
#include <QWidget> #include <QWidget>
class QLabel;
namespace Ui namespace Ui
{ {
class Page_UserSetup; class Page_UserSetup;
@ -75,6 +77,14 @@ signals:
void checkReady( bool ); void checkReady( bool );
private: 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; Ui::Page_UserSetup* ui;
PasswordCheckList m_passwordChecks; PasswordCheckList m_passwordChecks;