Publish username rules in login failure, too
This commit is contained in:
parent
4c27304047
commit
02dcaff356
4 changed files with 48 additions and 36 deletions
|
@ -83,7 +83,7 @@ void MainWindow::processConnectionClosedEvent(const Event_ConnectionClosed &even
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Event_ConnectionClosed::SERVER_SHUTDOWN: reasonStr = tr("Scheduled server shutdown."); break;
|
case Event_ConnectionClosed::SERVER_SHUTDOWN: reasonStr = tr("Scheduled server shutdown."); break;
|
||||||
case Event_ConnectionClosed::USERNAMEINVALID: reasonStr = tr("Invalid username.\nYou may only use A-Z, a-z, 0-9, _, ., and - in your username."); break;
|
case Event_ConnectionClosed::USERNAMEINVALID: reasonStr = tr("Invalid username."); break;
|
||||||
default: reasonStr = QString::fromStdString(event.reason_str());
|
default: reasonStr = QString::fromStdString(event.reason_str());
|
||||||
}
|
}
|
||||||
QMessageBox::critical(this, tr("Connection closed"), tr("The server has terminated your connection.\nReason: %1").arg(reasonStr));
|
QMessageBox::critical(this, tr("Connection closed"), tr("The server has terminated your connection.\nReason: %1").arg(reasonStr));
|
||||||
|
@ -306,9 +306,12 @@ void MainWindow::loginError(Response::ResponseCode r, QString reasonStr, quint32
|
||||||
QMessageBox::critical(this, tr("Error"), bannedStr);
|
QMessageBox::critical(this, tr("Error"), bannedStr);
|
||||||
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 errorStr;
|
||||||
|
extractInvalidUsernameMessage(reasonStr, errorStr);
|
||||||
|
QMessageBox::critical(this, tr("Error"), errorStr);
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
case Response::RespRegistrationRequired:
|
case Response::RespRegistrationRequired:
|
||||||
if (QMessageBox::question(this, tr("Error"), tr("This server requires user registration. Do you want to register now?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
|
if (QMessageBox::question(this, tr("Error"), tr("This server requires user registration. Do you want to register now?"), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
|
||||||
actRegister();
|
actRegister();
|
||||||
|
@ -332,6 +335,36 @@ void MainWindow::loginError(Response::ResponseCode r, QString reasonStr, quint32
|
||||||
actConnect();
|
actConnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void MainWindow::extractInvalidUsernameMessage(QString & in, QString & out)
|
||||||
|
{
|
||||||
|
out = tr("Invalid username.") + "<br/>";
|
||||||
|
QStringList rules = in.split(QChar('|'));
|
||||||
|
if (rules.size() == 7)
|
||||||
|
{
|
||||||
|
out += 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)
|
||||||
|
out += "<li>" + tr("it can contain lowercase characters") + "</li>";
|
||||||
|
if(rules.at(3).toInt() > 0)
|
||||||
|
out += "<li>" + tr("it can contain uppercase characters") + "</li>";
|
||||||
|
if(rules.at(4).toInt() > 0)
|
||||||
|
out += "<li>" + tr("it can contain numeric characters") + "</li>";
|
||||||
|
if(rules.at(6).size() > 0)
|
||||||
|
out += "<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)
|
||||||
|
out += "<li>" + tr("the first character can't be a punctuation") + "</li>";
|
||||||
|
out += "</ul>";
|
||||||
|
} else {
|
||||||
|
out += tr("You may only use A-Z, a-z, 0-9, _, ., and - in your username.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void MainWindow::registerError(Response::ResponseCode r, QString reasonStr, quint32 endTime)
|
void MainWindow::registerError(Response::ResponseCode r, QString reasonStr, quint32 endTime)
|
||||||
{
|
{
|
||||||
switch (r) {
|
switch (r) {
|
||||||
|
@ -362,36 +395,10 @@ void MainWindow::registerError(Response::ResponseCode r, QString reasonStr, quin
|
||||||
QMessageBox::critical(this, tr("Error"), bannedStr);
|
QMessageBox::critical(this, tr("Error"), bannedStr);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Response::RespUsernameInvalid:
|
case Response::RespUsernameInvalid: {
|
||||||
{
|
QString errorStr;
|
||||||
QString error = tr("Invalid username.") + "<br/>";
|
extractInvalidUsernameMessage(reasonStr, errorStr);
|
||||||
QStringList rules = reasonStr.split(QChar('|'));
|
QMessageBox::critical(this, tr("Error"), errorStr);
|
||||||
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:
|
||||||
|
|
|
@ -109,6 +109,7 @@ public:
|
||||||
protected:
|
protected:
|
||||||
void closeEvent(QCloseEvent *event);
|
void closeEvent(QCloseEvent *event);
|
||||||
void changeEvent(QEvent *event);
|
void changeEvent(QEvent *event);
|
||||||
|
void extractInvalidUsernameMessage(QString & in, QString & out);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -388,7 +388,12 @@ Response::ResponseCode Server_ProtocolHandler::cmdLogin(const Command_Login &cmd
|
||||||
}
|
}
|
||||||
case NotLoggedIn: return Response::RespWrongPassword;
|
case NotLoggedIn: return Response::RespWrongPassword;
|
||||||
case WouldOverwriteOldSession: return Response::RespWouldOverwriteOldSession;
|
case WouldOverwriteOldSession: return Response::RespWouldOverwriteOldSession;
|
||||||
case UsernameInvalid: return Response::RespUsernameInvalid;
|
case UsernameInvalid: {
|
||||||
|
Response_Login *re = new Response_Login;
|
||||||
|
re->set_denied_reason_str(reasonStr.toStdString());
|
||||||
|
rc.setResponseExtension(re);
|
||||||
|
return Response::RespUsernameInvalid;
|
||||||
|
}
|
||||||
case RegistrationRequired: return Response::RespRegistrationRequired;
|
case RegistrationRequired: return Response::RespRegistrationRequired;
|
||||||
case UserIsInactive: return Response::RespAccountNotActivated;
|
case UserIsInactive: return Response::RespAccountNotActivated;
|
||||||
default: authState = res;
|
default: authState = res;
|
||||||
|
|
|
@ -247,8 +247,7 @@ AuthenticationResult Servatrice_DatabaseInterface::checkUserPassword(Server_Prot
|
||||||
if (!checkSql())
|
if (!checkSql())
|
||||||
return UnknownUser;
|
return UnknownUser;
|
||||||
|
|
||||||
QString error;
|
if (!usernameIsValid(user, reasonStr))
|
||||||
if (!usernameIsValid(user, error))
|
|
||||||
return UsernameInvalid;
|
return UsernameInvalid;
|
||||||
|
|
||||||
if (checkUserIsBanned(handler->getAddress(), user, reasonStr, banSecondsLeft))
|
if (checkUserIsBanned(handler->getAddress(), user, reasonStr, banSecondsLeft))
|
||||||
|
|
Loading…
Reference in a new issue