[users] Give password-checks a weight, to sort them later

This commit is contained in:
Adriaan de Groot 2019-11-08 13:16:19 +01:00
parent b3e7c3f294
commit dec0cfb7d3
3 changed files with 34 additions and 23 deletions

View File

@ -31,19 +31,15 @@
#include <memory>
PasswordCheck::PasswordCheck()
: m_message()
: m_weight( 0 )
, m_message()
, m_accept( []( const QString& ) { return true; } )
{
}
PasswordCheck::PasswordCheck( const QString& m, AcceptFunc a )
: m_message( [m]() { return m; } )
, m_accept( a )
{
}
PasswordCheck::PasswordCheck( MessageFunc m, AcceptFunc a )
: m_message( m )
PasswordCheck::PasswordCheck( MessageFunc m, AcceptFunc a, Weight weight )
: m_weight( weight )
, m_message( m )
, m_accept( a )
{
}
@ -59,7 +55,8 @@ DEFINE_CHECK_FUNC( minLength )
{
cDebug() << Logger::SubEntry << "minLength set to" << minLength;
checks.push_back( PasswordCheck( []() { return QCoreApplication::translate( "PWQ", "Password is too short" ); },
[minLength]( const QString& s ) { return s.length() >= minLength; } ) );
[minLength]( const QString& s ) { return s.length() >= minLength; },
PasswordCheck::Weight( 10 ) ) );
}
}
@ -74,7 +71,8 @@ DEFINE_CHECK_FUNC( maxLength )
{
cDebug() << Logger::SubEntry << "maxLength set to" << maxLength;
checks.push_back( PasswordCheck( []() { return QCoreApplication::translate( "PWQ", "Password is too long" ); },
[maxLength]( const QString& s ) { return s.length() <= maxLength; } ) );
[maxLength]( const QString& s ) { return s.length() <= maxLength; },
PasswordCheck::Weight( 10 ) ) );
}
}
@ -363,7 +361,8 @@ DEFINE_CHECK_FUNC( libpwquality )
cDebug() << "Password strength" << r << "too low";
}
return r >= settings->arbitrary_minimum_strength;
} ) );
},
PasswordCheck::Weight( 100 ) ) );
}
}
#endif

View File

@ -38,11 +38,18 @@ public:
using AcceptFunc = std::function< bool( const QString& ) >;
using MessageFunc = std::function< QString() >;
/** Generate a @p message if @p filter returns true */
PasswordCheck( MessageFunc message, AcceptFunc filter );
/** Yields @p message if @p filter returns true */
PasswordCheck( const QString& message, AcceptFunc filter );
/** Null check, always returns empty */
using Weight = size_t;
/** @brief Generate a @p message if @p filter returns true
*
* When @p filter returns true on the proposed password, the
* password is accepted (by this check). If false, then the
* @p message will be shown to the user.
*
* @p weight is used to order the checks (low-weight goes first).
*/
PasswordCheck( MessageFunc message, AcceptFunc filter, Weight weight = 1000 );
/** @brief Null check, always accepts, no message */
PasswordCheck();
/** Applies this check to the given password string @p s
@ -52,7 +59,10 @@ public:
*/
QString filter( const QString& s ) const { return m_accept( s ) ? QString() : m_message(); }
Weight weight() const { return m_weight; }
private:
Weight m_weight;
MessageFunc m_message;
AcceptFunc m_accept;
};

View File

@ -147,8 +147,8 @@ UsersPage::retranslate()
// Re-do password checks (with output messages) as well.
// .. the password-checking methods get their values from the text boxes,
// not from their parameters.
onPasswordTextChanged(QString());
onRootPasswordTextChanged(QString());
onPasswordTextChanged( QString() );
onRootPasswordTextChanged( QString() );
}
@ -227,8 +227,8 @@ void
UsersPage::onActivate()
{
ui->textBoxFullName->setFocus();
onPasswordTextChanged(QString());
onRootPasswordTextChanged(QString());
onPasswordTextChanged( QString() );
onRootPasswordTextChanged( QString() );
}
@ -514,8 +514,10 @@ UsersPage::addPasswordCheck( const QString& key, const QVariant& value )
{
if ( value.toBool() )
{
m_passwordChecks.push_back( PasswordCheck( []() { return QCoreApplication::translate( "EMP", "Password is empty" ); },
[]( const QString& s ) { return ((cDebug() << "Checking pwd" << s << "for empty"), !s.isEmpty()); } ) );
m_passwordChecks.push_back( PasswordCheck(
[]() { return QCoreApplication::translate( "EMP", "Password is empty" ); },
[]( const QString& s ) { return ( ( cDebug() << "Checking pwd" << s << "for empty" ), !s.isEmpty() ); },
PasswordCheck::Weight( 1 ) ) );
}
}
#ifdef CHECK_PWQUALITY