From 34946ecdee90584caf255fcca732c021e1dc1832 Mon Sep 17 00:00:00 2001 From: Adriaan de Groot Date: Mon, 17 Aug 2020 14:20:54 +0200 Subject: [PATCH] [users] Refactor validity-checking - provide a structured datatype for password status --- src/modules/users/Config.cpp | 41 ++++++++++++++++++++---------------- src/modules/users/Config.h | 22 ++++++++++++++----- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/src/modules/users/Config.cpp b/src/modules/users/Config.cpp index 61746275e..dc2ae5b5c 100644 --- a/src/modules/users/Config.cpp +++ b/src/modules/users/Config.cpp @@ -427,17 +427,23 @@ Config::passwordStatus( const QString& pw1, const QString& pw2 ) const } +Config::PasswordStatus +Config::userPasswordStatus() const +{ + return passwordStatus( m_userPassword, m_userPasswordSecondary ); +} + int Config::userPasswordValidity() const { - auto p = passwordStatus( m_userPassword, m_userPasswordSecondary ); + auto p = userPasswordStatus(); return p.first; } QString -Config::userPasswordStatus() const +Config::userPasswordMessage() const { - auto p = passwordStatus( m_userPassword, m_userPasswordSecondary ); + auto p = userPasswordStatus(); return p.second; } @@ -494,32 +500,31 @@ Config::rootPasswordSecondary() const return QString(); } -int -Config::rootPasswordValidity() const +Config::PasswordStatus +Config::rootPasswordStatus() const { if ( writeRootPassword() && !reuseUserPasswordForRoot() ) { - auto p = passwordStatus( m_rootPassword, m_rootPasswordSecondary ); - return p.first; + return passwordStatus( m_rootPassword, m_rootPasswordSecondary ); } else { - return userPasswordValidity(); + return userPasswordStatus(); } } +int +Config::rootPasswordValidity() const +{ + auto p = rootPasswordStatus(); + return p.first; +} + QString -Config::rootPasswordStatus() const +Config::rootPasswordMessage() const { - if ( writeRootPassword() && !reuseUserPasswordForRoot() ) - { - auto p = passwordStatus( m_rootPassword, m_rootPasswordSecondary ); - return p.second; - } - else - { - return userPasswordStatus(); - } + auto p = rootPasswordStatus(); + return p.second; } diff --git a/src/modules/users/Config.h b/src/modules/users/Config.h index a785fb3ae..0581778d4 100644 --- a/src/modules/users/Config.h +++ b/src/modules/users/Config.h @@ -63,13 +63,13 @@ class Config : public QObject Q_PROPERTY( QString userPasswordSecondary READ userPasswordSecondary WRITE setUserPasswordSecondary NOTIFY userPasswordSecondaryChanged ) Q_PROPERTY( int userPasswordValidity READ userPasswordValidity NOTIFY userPasswordStatusChanged STORED false ) - Q_PROPERTY( QString userPasswordStatus READ userPasswordStatus NOTIFY userPasswordStatusChanged STORED false ) + Q_PROPERTY( QString userPasswordMessage READ userPasswordMessage NOTIFY userPasswordStatusChanged STORED false ) Q_PROPERTY( QString rootPassword READ rootPassword WRITE setRootPassword NOTIFY rootPasswordChanged ) Q_PROPERTY( QString rootPasswordSecondary READ rootPasswordSecondary WRITE setRootPasswordSecondary NOTIFY rootPasswordSecondaryChanged ) Q_PROPERTY( int rootPasswordValidity READ rootPasswordValidity NOTIFY rootPasswordStatusChanged STORED false ) - Q_PROPERTY( QString rootPasswordStatus READ rootPasswordStatus NOTIFY rootPasswordStatusChanged STORED false ) + Q_PROPERTY( QString rootPasswordMessage READ rootPasswordMessage NOTIFY rootPasswordStatusChanged STORED false ) Q_PROPERTY( bool writeRootPassword READ writeRootPassword CONSTANT ) Q_PROPERTY( bool reuseUserPasswordForRoot READ reuseUserPasswordForRoot WRITE setReuseUserPasswordForRoot NOTIFY @@ -101,6 +101,16 @@ public: Invalid = 2 }; + /** @brief Full password status + * + * A password's status is in two parts: + * - a validity (valid, weak or invalid) + * - a message describing that validity + * The message is empty when the password is valid, but + * weak and invalid passwords have an explanatory message. + */ + using PasswordStatus = QPair< PasswordValidity, QString >; + Config( QObject* parent = nullptr ); ~Config(); @@ -149,7 +159,8 @@ public: QString userPassword() const { return m_userPassword; } QString userPasswordSecondary() const { return m_userPasswordSecondary; } int userPasswordValidity() const; - QString userPasswordStatus() const; + QString userPasswordMessage() const; + PasswordStatus userPasswordStatus() const; // The root password **may** be entered in the UI, or may be suppressed // entirely when writeRootPassword is off, or may be equal to @@ -157,7 +168,8 @@ public: QString rootPassword() const; QString rootPasswordSecondary() const; int rootPasswordValidity() const; - QString rootPasswordStatus() const; + QString rootPasswordMessage() const; + PasswordStatus rootPasswordStatus() const; static const QStringList& forbiddenLoginNames(); static const QStringList& forbiddenHostNames(); @@ -220,7 +232,7 @@ signals: private: - QPair< PasswordValidity, QString > passwordStatus( const QString&, const QString& ) const; + PasswordStatus passwordStatus( const QString&, const QString& ) const; QStringList m_defaultGroups; QString m_userShell;