mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-04-27 07:48:01 -07:00
display reason for ban to banned user
This commit is contained in:
parent
6344b987de
commit
ff3eb9b5f4
19 changed files with 98 additions and 59 deletions
|
|
@ -167,7 +167,7 @@ bool Servatrice::execSqlQuery(QSqlQuery &query)
|
|||
return false;
|
||||
}
|
||||
|
||||
AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password)
|
||||
AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr)
|
||||
{
|
||||
QMutexLocker locker(&dbMutex);
|
||||
const QString method = settings->value("authentication/method").toString();
|
||||
|
|
@ -177,33 +177,35 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
|
|||
checkSql();
|
||||
|
||||
QSqlQuery ipBanQuery;
|
||||
ipBanQuery.prepare("select time_to_sec(timediff(now(), date_add(b.time_from, interval b.minutes minute))) < 0, b.minutes <=> 0 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))) < 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.bindValue(":address", static_cast<ServerSocketInterface *>(handler)->getPeerAddress().toString());
|
||||
ipBanQuery.bindValue(":address2", static_cast<ServerSocketInterface *>(handler)->getPeerAddress().toString());
|
||||
if (!execSqlQuery(ipBanQuery)) {
|
||||
qDebug("Login denied: SQL error");
|
||||
return PasswordWrong;
|
||||
return NotLoggedIn;
|
||||
}
|
||||
|
||||
if (ipBanQuery.next())
|
||||
if (ipBanQuery.value(0).toInt() || ipBanQuery.value(1).toInt()) {
|
||||
reasonStr = ipBanQuery.value(2).toString();
|
||||
qDebug("Login denied: banned by address");
|
||||
return PasswordWrong;
|
||||
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 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))) < 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.bindValue(":name1", user);
|
||||
nameBanQuery.bindValue(":name2", user);
|
||||
if (!execSqlQuery(nameBanQuery)) {
|
||||
qDebug("Login denied: SQL error");
|
||||
return PasswordWrong;
|
||||
return NotLoggedIn;
|
||||
}
|
||||
|
||||
if (nameBanQuery.next())
|
||||
if (nameBanQuery.value(0).toInt() || nameBanQuery.value(1).toInt()) {
|
||||
reasonStr = nameBanQuery.value(2).toString();
|
||||
qDebug("Login denied: banned by name");
|
||||
return PasswordWrong;
|
||||
return UserIsBanned;
|
||||
}
|
||||
|
||||
QSqlQuery passwordQuery;
|
||||
|
|
@ -211,7 +213,7 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
|
|||
passwordQuery.bindValue(":name", user);
|
||||
if (!execSqlQuery(passwordQuery)) {
|
||||
qDebug("Login denied: SQL error");
|
||||
return PasswordWrong;
|
||||
return NotLoggedIn;
|
||||
}
|
||||
|
||||
if (passwordQuery.next()) {
|
||||
|
|
@ -221,7 +223,7 @@ AuthenticationResult Servatrice::checkUserPassword(Server_ProtocolHandler *handl
|
|||
return PasswordRight;
|
||||
} else {
|
||||
qDebug("Login denied: password wrong");
|
||||
return PasswordWrong;
|
||||
return NotLoggedIn;
|
||||
}
|
||||
} else {
|
||||
qDebug("Login accepted: unknown user");
|
||||
|
|
@ -527,7 +529,7 @@ void Servatrice::shutdownTimeout()
|
|||
se = Server_ProtocolHandler::prepareSessionEvent(event);
|
||||
} else {
|
||||
Event_ConnectionClosed event;
|
||||
event.set_reason("server_shutdown");
|
||||
event.set_reason(Event_ConnectionClosed::SERVER_SHUTDOWN);
|
||||
se = Server_ProtocolHandler::prepareSessionEvent(event);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -85,7 +85,7 @@ protected:
|
|||
int startSession(const QString &userName, const QString &address);
|
||||
void endSession(int sessionId);
|
||||
bool userExists(const QString &user);
|
||||
AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password);
|
||||
AuthenticationResult checkUserPassword(Server_ProtocolHandler *handler, const QString &user, const QString &password, QString &reasonStr);
|
||||
private:
|
||||
QTimer *pingClock, *statusUpdateClock;
|
||||
QTcpServer *tcpServer;
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ ServerSocketInterface::ServerSocketInterface(Servatrice *_server, QTcpSocket *_s
|
|||
int maxUsers = _server->getMaxUsersPerAddress();
|
||||
if ((maxUsers > 0) && (_server->getUsersWithAddress(socket->peerAddress()) >= maxUsers)) {
|
||||
Event_ConnectionClosed event;
|
||||
event.set_reason("too_many_connections");
|
||||
event.set_reason(Event_ConnectionClosed::TOO_MANY_CONNECTIONS);
|
||||
SessionEvent *se = prepareSessionEvent(event);
|
||||
sendProtocolItem(*se);
|
||||
delete se;
|
||||
|
|
@ -482,19 +482,22 @@ Response::ResponseCode ServerSocketInterface::cmdBanFromServer(const Command_Ban
|
|||
|
||||
servatrice->dbMutex.lock();
|
||||
QSqlQuery query;
|
||||
query.prepare("insert into " + servatrice->getDbPrefix() + "_bans (user_name, ip_address, id_admin, time_from, minutes, reason) values(:user_name, :ip_address, :id_admin, NOW(), :minutes, :reason)");
|
||||
query.prepare("insert into " + servatrice->getDbPrefix() + "_bans (user_name, ip_address, id_admin, time_from, minutes, reason, visible_reason) values(:user_name, :ip_address, :id_admin, NOW(), :minutes, :reason, :visible_reason)");
|
||||
query.bindValue(":user_name", userName);
|
||||
query.bindValue(":ip_address", address);
|
||||
query.bindValue(":id_admin", servatrice->getUserIdInDB(QString::fromStdString(userInfo->name())));
|
||||
query.bindValue(":minutes", minutes);
|
||||
query.bindValue(":reason", QString::fromStdString(cmd.reason()) + "\n");
|
||||
query.bindValue(":visible_reason", QString::fromStdString(cmd.visible_reason()) + "\n");
|
||||
servatrice->execSqlQuery(query);
|
||||
servatrice->dbMutex.unlock();
|
||||
|
||||
ServerSocketInterface *user = static_cast<ServerSocketInterface *>(server->getUsers().value(userName));
|
||||
if (user) {
|
||||
Event_ConnectionClosed event;
|
||||
event.set_reason("banned");
|
||||
event.set_reason(Event_ConnectionClosed::BANNED);
|
||||
if (cmd.has_visible_reason())
|
||||
event.set_reason_str(cmd.visible_reason());
|
||||
SessionEvent *se = user->prepareSessionEvent(event);
|
||||
user->sendProtocolItem(*se);
|
||||
delete se;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue