publish username rules in registration failure

This commit is contained in:
Fabio Bas 2015-06-29 23:35:27 +02:00
parent 9947af7be9
commit 4c27304047
5 changed files with 57 additions and 12 deletions

View file

@ -30,6 +30,10 @@
#include <QDateTime> #include <QDateTime>
#include <QSystemTrayIcon> #include <QSystemTrayIcon>
#include <QApplication> #include <QApplication>
#if QT_VERSION < 0x050000
// for Qt::escape()
#include <QtGui/qtextdocument.h>
#endif
#include "main.h" #include "main.h"
#include "window_main.h" #include "window_main.h"
@ -359,8 +363,37 @@ void MainWindow::registerError(Response::ResponseCode r, QString reasonStr, quin
break; break;
} }
case Response::RespUsernameInvalid: case Response::RespUsernameInvalid:
QMessageBox::critical(this, tr("Error"), tr("Invalid username.\nYou may only use A-Z, a-z, 0-9, _, ., and - in your username.")); {
QString error = tr("Invalid username.") + "<br/>";
QStringList rules = reasonStr.split(QChar('|'));
if (rules.size() == 7)
{
error += tr("The username must respect these rules:") + "<br/><ul>"
+ "<li>" + tr("length between %1 and %2 characters").arg(rules.at(0)).arg(rules.at(1)) + "</li>";
if(rules.at(2).toInt() > 0)
error += "<li>" + tr("it can contain lowercase characters") + "</li>";
if(rules.at(3).toInt() > 0)
error += "<li>" + tr("it can contain uppercase characters") + "</li>";
if(rules.at(4).toInt() > 0)
error += "<li>" + tr("it can contain numeric characters") + "</li>";
if(rules.at(6).size() > 0)
error += "<li>" + tr("it can contain the following punctuation: %1").arg(
#if QT_VERSION < 0x050000
Qt::escape(rules.at(6))
#else
rules.at(6).toHtmlEscaped()
#endif
) + "</li>";
if(rules.at(5).toInt() > 0)
error += "<li>" + tr("the first character can't be a punctuation") + "</li>";
error += "</ul>";
} else {
error += tr("You may only use A-Z, a-z, 0-9, _, ., and - in your username.");
}
QMessageBox::critical(this, tr("Error"), error);
break; break;
}
case Response::RespRegistrationFailed: case Response::RespRegistrationFailed:
QMessageBox::critical(this, tr("Error"), tr("Registration failed for a technical problem on the server.")); QMessageBox::critical(this, tr("Error"), tr("Registration failed for a technical problem on the server."));
break; break;

View file

@ -25,7 +25,7 @@ public:
virtual DeckList *getDeckFromDatabase(int /* deckId */, int /* userId */) { return 0; } virtual DeckList *getDeckFromDatabase(int /* deckId */, int /* userId */) { return 0; }
virtual qint64 startSession(const QString & /* userName */, const QString & /* address */) { return 0; } virtual qint64 startSession(const QString & /* userName */, const QString & /* address */) { return 0; }
virtual bool usernameIsValid(const QString & /*userName */) { return true; }; virtual bool usernameIsValid(const QString & /*userName */, QString & /* error */) { return true; };
public slots: public slots:
virtual void endSession(qint64 /* sessionId */ ) { } virtual void endSession(qint64 /* sessionId */ ) { }
public: public:

View file

@ -118,24 +118,29 @@ bool Servatrice_DatabaseInterface::execSqlQuery(QSqlQuery *query)
return false; return false;
} }
bool Servatrice_DatabaseInterface::usernameIsValid(const QString &user) bool Servatrice_DatabaseInterface::usernameIsValid(const QString &user, QString & error)
{ {
int maxNameLength = settingsCache->value("users/maxnamelength", 12).toInt();
int minNameLength = settingsCache->value("users/minnamelength", 6).toInt(); int minNameLength = settingsCache->value("users/minnamelength", 6).toInt();
int maxNameLength = settingsCache->value("users/maxnamelength", 12).toInt();
bool allowLowercase = settingsCache->value("users/allowlowercase", true).toBool();
bool allowUppercase = settingsCache->value("users/allowuppercase", true).toBool();
bool allowNumerics = settingsCache->value("users/allownumerics", true).toBool();
bool allowPunctuationPrefix = settingsCache->value("users/allowpunctuationprefix", false).toBool();
QString allowedPunctuation = settingsCache->value("users/allowedpunctuation", "_").toString();
error = QString("%1|%2|%3|%4|%5|%6|%7").arg(minNameLength).arg(maxNameLength).arg(allowLowercase).arg(allowUppercase).arg(allowNumerics).arg(allowPunctuationPrefix).arg(allowedPunctuation);
if (user.length() < minNameLength || user.length() > maxNameLength) if (user.length() < minNameLength || user.length() > maxNameLength)
return false; return false;
bool allowPunctuationPrefix = settingsCache->value("users/allowpunctuationprefix", false).toBool();
QString allowedPunctuation = settingsCache->value("users/allowedpunctuation", "_").toString();
if (!allowPunctuationPrefix && allowedPunctuation.contains(user.at(0))) if (!allowPunctuationPrefix && allowedPunctuation.contains(user.at(0)))
return false; return false;
QString regEx("["); QString regEx("[");
if (settingsCache->value("users/allowlowercase", true).toBool()) if (allowLowercase)
regEx.append("a-z"); regEx.append("a-z");
if (settingsCache->value("users/allowuppercase", true).toBool()) if (allowUppercase)
regEx.append("A-Z"); regEx.append("A-Z");
if(settingsCache->value("users/allownumerics", true).toBool()) if(allowNumerics)
regEx.append("0-9"); regEx.append("0-9");
regEx.append(QRegExp::escape(allowedPunctuation)); regEx.append(QRegExp::escape(allowedPunctuation));
regEx.append("]+"); regEx.append("]+");
@ -242,7 +247,8 @@ AuthenticationResult Servatrice_DatabaseInterface::checkUserPassword(Server_Prot
if (!checkSql()) if (!checkSql())
return UnknownUser; return UnknownUser;
if (!usernameIsValid(user)) QString error;
if (!usernameIsValid(user, error))
return UsernameInvalid; return UsernameInvalid;
if (checkUserIsBanned(handler->getAddress(), user, reasonStr, banSecondsLeft)) if (checkUserIsBanned(handler->getAddress(), user, reasonStr, banSecondsLeft))

View file

@ -62,7 +62,7 @@ public:
void lockSessionTables(); void lockSessionTables();
void unlockSessionTables(); void unlockSessionTables();
bool userSessionExists(const QString &userName); bool userSessionExists(const QString &userName);
bool usernameIsValid(const QString &user); bool usernameIsValid(const QString &user, QString & error);
bool checkUserIsBanned(const QString &ipAddress, const QString &userName, QString &banReason, int &banSecondsRemaining); bool checkUserIsBanned(const QString &ipAddress, const QString &userName, QString &banReason, int &banSecondsRemaining);
bool getRequireRegistration(); bool getRequireRegistration();

View file

@ -788,8 +788,14 @@ Response::ResponseCode ServerSocketInterface::cmdRegisterAccount(const Command_R
} }
// TODO: Move this method outside of the db interface // TODO: Move this method outside of the db interface
if (!sqlInterface->usernameIsValid(userName)) QString errorString;
if (!sqlInterface->usernameIsValid(userName, errorString))
{
Response_Register *re = new Response_Register;
re->set_denied_reason_str(errorString.toStdString());
rc.setResponseExtension(re);
return Response::RespUsernameInvalid; return Response::RespUsernameInvalid;
}
if(sqlInterface->userExists(userName)) if(sqlInterface->userExists(userName))
return Response::RespUserAlreadyExists; return Response::RespUserAlreadyExists;