show ban duration in client

This commit is contained in:
Max-Wilhelm Bruker 2012-03-31 12:07:25 +02:00
parent 78d188c462
commit 13b992cf12
15 changed files with 159 additions and 91 deletions

View file

@ -297,7 +297,7 @@ QList<ServerProperties> Servatrice::getServerList() const
return result;
}
AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr)
AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr, int &banSecondsLeft)
{
QMutexLocker locker(&dbMutex);
const QString method = settings->value("authentication/method").toString();
@ -308,7 +308,7 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
return UnknownUser;
QSqlQuery ipBanQuery;
ipBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))) < 0, b.minutes <=> 0, b.visible_reason from " + dbPrefix + "_bans b where b.time_from = (select max(c.time_from) from " + dbPrefix + "_bans c where c.ip_address = :address) and b.ip_address = :address2");
ipBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))), b.minutes <=> 0, b.visible_reason from " + dbPrefix + "_bans b where b.time_from = (select max(c.time_from) from " + dbPrefix + "_bans c where c.ip_address = :address) and b.ip_address = :address2");
ipBanQuery.bindValue(":address", static_cast<ServerSocketInterface *>(handler)->getPeerAddress().toString());
ipBanQuery.bindValue(":address2", static_cast<ServerSocketInterface *>(handler)->getPeerAddress().toString());
if (!execSqlQuery(ipBanQuery)) {
@ -316,15 +316,19 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
return NotLoggedIn;
}
if (ipBanQuery.next())
if (ipBanQuery.value(0).toInt() || ipBanQuery.value(1).toInt()) {
if (ipBanQuery.next()) {
const int secondsLeft = -ipBanQuery.value(0).toInt();
const bool permanentBan = ipBanQuery.value(1).toInt();
if ((secondsLeft > 0) || permanentBan) {
reasonStr = ipBanQuery.value(2).toString();
banSecondsLeft = permanentBan ? 0 : secondsLeft;
qDebug("Login denied: banned by address");
return UserIsBanned;
}
}
QSqlQuery nameBanQuery;
nameBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))) < 0, b.minutes <=> 0, b.visible_reason from " + dbPrefix + "_bans b where b.time_from = (select max(c.time_from) from " + dbPrefix + "_bans c where c.user_name = :name2) and b.user_name = :name1");
nameBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))), b.minutes <=> 0, b.visible_reason from " + dbPrefix + "_bans b where b.time_from = (select max(c.time_from) from " + dbPrefix + "_bans c where c.user_name = :name2) and b.user_name = :name1");
nameBanQuery.bindValue(":name1", user);
nameBanQuery.bindValue(":name2", user);
if (!execSqlQuery(nameBanQuery)) {
@ -332,12 +336,16 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
return NotLoggedIn;
}
if (nameBanQuery.next())
if (nameBanQuery.value(0).toInt() || nameBanQuery.value(1).toInt()) {
if (nameBanQuery.next()) {
const int secondsLeft = -nameBanQuery.value(0).toInt();
const bool permanentBan = nameBanQuery.value(1).toInt();
if ((secondsLeft > 0) || permanentBan) {
reasonStr = nameBanQuery.value(2).toString();
banSecondsLeft = permanentBan ? 0 : secondsLeft;
qDebug("Login denied: banned by name");
return UserIsBanned;
}
}
QSqlQuery passwordQuery;
passwordQuery.prepare("select password_sha512 from " + dbPrefix + "_users where name = :name and active = 1");

View file

@ -130,7 +130,7 @@ protected:
qint64 startSession(const QString &userName, const QString &address);
void endSession(qint64 sessionId);
bool userExists(const QString &user);
AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr);
AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr, int &secondsLeft);
void clearSessionTables();
void lockSessionTables();

View file

@ -587,8 +587,8 @@ Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_Ban
query.bindValue(":ip_address", address);
query.bindValue(":id_admin", userInfo->id());
query.bindValue(":minutes", minutes);
query.bindValue(":reason", QString::fromStdString(cmd.reason()) + "\n");
query.bindValue(":visible_reason", QString::fromStdString(cmd.visible_reason()) + "\n");
query.bindValue(":reason", QString::fromStdString(cmd.reason()));
query.bindValue(":visible_reason", QString::fromStdString(cmd.visible_reason()));
servatrice->execSqlQuery(query);
servatrice->dbMutex.unlock();
@ -601,6 +601,8 @@ Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_Ban
event.set_reason(Event_ConnectionClosed::BANNED);
if (cmd.has_visible_reason())
event.set_reason_str(cmd.visible_reason());
if (minutes)
event.set_end_time(QDateTime::currentDateTime().addSecs(60 * minutes).toTime_t());
for (int i = 0; i < userList.size(); ++i) {
SessionEvent *se = userList[i]->prepareSessionEvent(event);
userList[i]->sendProtocolItem(*se);