source: calamares/trunk/fuentes/src/modules/users/CheckPWQuality.h @ 7538

Last change on this file since 7538 was 7538, checked in by kbut, 17 months ago

sync with github

File size: 2.7 KB
Line 
1/* === This file is part of Calamares - <https://github.com/calamares> ===
2 *
3 *   Copyright 2018, Adriaan de Groot <groot@kde.org>
4 *
5 *   Calamares is free software: you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation, either version 3 of the License, or
8 *   (at your option) any later version.
9 *
10 *   Calamares is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 *   GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with Calamares. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#ifndef CHECKPWQUALITY_H
20#define CHECKPWQUALITY_H
21
22#include <QString>
23#include <QVariant>
24#include <QVector>
25
26#include <functional>
27
28/**
29 * Support for (dynamic) checks on the password's validity.
30 * This can be used to implement password requirements like
31 * "at least 6 characters". Function addPasswordCheck()
32 * instantiates these and adds them to the list of checks.
33 */
34class PasswordCheck
35{
36public:
37    /** Return true if the string is acceptable. */
38    using AcceptFunc = std::function<bool( const QString& )>;
39    using MessageFunc = std::function<QString()>;
40
41    /** Generate a @p message if @p filter returns true */
42    PasswordCheck( MessageFunc message, AcceptFunc filter );
43    /** Yields @p message if @p filter returns true */
44    PasswordCheck( const QString& message, AcceptFunc filter );
45    /** Null check, always returns empty */
46    PasswordCheck();
47
48    /** Applies this check to the given password string @p s
49        *  and returns an empty string if the password is ok
50        *  according to this filter. Returns a message describing
51        *  what is wrong if not.
52        */
53    QString filter( const QString& s ) const
54    {
55        return m_accept( s ) ? QString() : m_message();
56    }
57
58private:
59    MessageFunc m_message;
60    AcceptFunc m_accept;
61} ;
62
63using PasswordCheckList = QVector<PasswordCheck>;
64
65/* Each of these functions adds a check (if possible) to the list
66 * of checks; they use the configuration value(s) from the
67 * variant. If the value doesn't make sense, each function
68 * may skip adding a check, and do nothing (it should log
69 * an error, though).
70 */
71#define _xDEFINE_CHECK_FUNC(x) \
72    add_check_##x( PasswordCheckList& checks, const QVariant& value )
73#define DEFINE_CHECK_FUNC(x) void _xDEFINE_CHECK_FUNC(x)
74#define DECLARE_CHECK_FUNC(x) void _xDEFINE_CHECK_FUNC(x);
75
76DECLARE_CHECK_FUNC(minLength)
77DECLARE_CHECK_FUNC(maxLength)
78#ifdef HAVE_LIBPWQUALITY
79DECLARE_CHECK_FUNC(libpwquality)
80#endif
81
82#endif
83
Note: See TracBrowser for help on using the repository browser.