Cockatrice/servatrice/src/settingscache.cpp
Kevin Boxhoorn 0b7f4c134c Disallow usernames that contain certain words and RegExp (#2200)
* Add `disallowedwords` setting and perform check

Check if any of the words in `disallowedwords` are contained in the username. If
so, return false like other checks.

NOTE: Needs testing for advanced bugs.

* Remove "administrator" from `disallowedwords`

"administrator" contains "admin" anyway, so it is not needed.

* Add error message if username contains a disallowed word

* Add `disallowedregexp` setting and perform check

Check if each expression in `disallowedregexp` exactly matches the username. If
so, return false.

TODO: Add specific error to dialog in `window_main.cpp`.

* Add error message for username matching RegExp

* Fix indentation

* Compile `disallowedregexp` into a QList upon initialization

Reduces system load with each registration request.

* Clean up `isUsernameValid` function

* Fix indentation

* Add backwards compatibility to client

Client can accept either 7 or 9 rules to maintain compatibility with older
versions of server.

* Add examples and warnings to `servatrice.ini`
2016-10-17 12:24:42 +02:00

41 lines
1.3 KiB
C++

#include "settingscache.h"
#include <QCoreApplication>
#include <QFile>
#include <QStandardPaths>
SettingsCache::SettingsCache(const QString & fileName, QSettings::Format format, QObject * parent)
:QSettings(fileName, format, parent)
{
QStringList disallowedRegExpStr = value("users/disallowedregexp", "").toString().split(",", QString::SkipEmptyParts);
disallowedRegExpStr.removeDuplicates();
for (const QString &regExpStr : disallowedRegExpStr) {
disallowedRegExp.append(QRegExp(regExpStr));
}
}
QString SettingsCache::guessConfigurationPath(QString & specificPath)
{
const QString fileName="servatrice.ini";
#ifdef PORTABLE_BUILD
return fileName;
#endif
QString guessFileName;
// specific path
if(!specificPath.isEmpty() && QFile::exists(specificPath))
return specificPath;
// application directory path
guessFileName = QCoreApplication::applicationDirPath() + "/" + fileName;
if(QFile::exists(guessFileName))
return guessFileName;
#ifdef Q_OS_UNIX
// /etc
guessFileName = "/etc/servatrice/" + fileName;
if(QFile::exists(guessFileName))
return guessFileName;
#endif
guessFileName = QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/" + fileName;
return guessFileName;
}