Merge pull request #2 from Cockatrice/admin_adjustmods

Address PR feedback
This commit is contained in:
woogerboy21 2015-08-21 23:08:29 -04:00
commit 2a0197a7ab
2 changed files with 23 additions and 34 deletions

View file

@ -231,23 +231,13 @@ void UserContextMenu::showContextMenu(const QPoint &pos, const QString &userName
PendingCommand *pend = client->prepareSessionCommand(cmd); PendingCommand *pend = client->prepareSessionCommand(cmd);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(banUser_processUserInfoResponse(Response))); connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(banUser_processUserInfoResponse(Response)));
client->sendCommand(pend); client->sendCommand(pend);
} else if (actionClicked == aPromoteToMod) { } else if (actionClicked == aPromoteToMod || actionClicked == aDemoteFromMod) {
Command_AdjustMod cmd; Command_AdjustMod cmd;
cmd.set_user_name(userName.toStdString()); cmd.set_user_name(userName.toStdString());
cmd.set_should_be_mod(true); cmd.set_should_be_mod(actionClicked == aPromoteToMod);
// client->sendCommand(client->prepareAdminCommand(cmd));
PendingCommand *pend = client->prepareAdminCommand(cmd); PendingCommand *pend = client->prepareAdminCommand(cmd);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(adjustMod_processUserResponse(Response,CommandContainer))); connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(adjustMod_processUserResponse(Response, CommandContainer)));
client->sendCommand(pend);
} else if (actionClicked == aDemoteFromMod) {
Command_AdjustMod cmd;
cmd.set_user_name(userName.toStdString());
cmd.set_should_be_mod(false);
// client->sendCommand(client->prepareAdminCommand(cmd));
PendingCommand *pend = client->prepareAdminCommand(cmd);
connect(pend, SIGNAL(finished(Response, CommandContainer, QVariant)), this, SLOT(adjustMod_processUserResponse(Response,CommandContainer)));
client->sendCommand(pend); client->sendCommand(pend);
} }

View file

@ -19,6 +19,7 @@
***************************************************************************/ ***************************************************************************/
#include <iostream> #include <iostream>
#include <QSqlError>
#include <QSqlQuery> #include <QSqlQuery>
#include <QHostAddress> #include <QHostAddress>
#include <QDebug> #include <QDebug>
@ -1008,47 +1009,45 @@ Response::ResponseCode ServerSocketInterface::cmdAdjustMod(const Command_AdjustM
QString userName = QString::fromStdString(cmd.user_name()); QString userName = QString::fromStdString(cmd.user_name());
if (cmd.should_be_mod()) { if (cmd.should_be_mod()) {
logDebugMessage("Received admin command: promote user");
QSqlQuery *query = sqlInterface->prepareQuery( QSqlQuery *query = sqlInterface->prepareQuery(
"update {prefix}_users set admin = :adminlevel where name = :username"); "update {prefix}_users set admin = :adminlevel where name = :username");
query->bindValue(":adminlevel", 2); query->bindValue(":adminlevel", 2);
query->bindValue(":username", userName); query->bindValue(":username", userName);
if (!sqlInterface->execSqlQuery(query)) if (!sqlInterface->execSqlQuery(query)){
logger->logMessage(QString::fromStdString("Failed to promote user %1: %2").arg(userName).arg(query->lastError().text()));
return Response::RespInternalError; return Response::RespInternalError;
}
ServerSocketInterface *user = static_cast<ServerSocketInterface *>(server->getUsers().value(userName)); ServerSocketInterface *user = static_cast<ServerSocketInterface *>(server->getUsers().value(userName));
Event_NotifyUser event; if (user) {
event.set_type(Event_NotifyUser::PROMOTED); Event_NotifyUser event;
SessionEvent *se = user->prepareSessionEvent(event); event.set_type(Event_NotifyUser::PROMOTED);
user->sendProtocolItem(*se); SessionEvent *se = user->prepareSessionEvent(event);
delete se; user->sendProtocolItem(*se);
delete se;
}
} else { } else {
logDebugMessage("Received admin command: demote user");
QSqlQuery *query = sqlInterface->prepareQuery("update {prefix}_users set admin = :adminlevel where name = :username"); QSqlQuery *query = sqlInterface->prepareQuery("update {prefix}_users set admin = :adminlevel where name = :username");
query->bindValue(":adminlevel", 0); query->bindValue(":adminlevel", 0);
query->bindValue(":username", userName); query->bindValue(":username", userName);
if (!sqlInterface->execSqlQuery(query)) if (!sqlInterface->execSqlQuery(query)){
logger->logMessage(QString::fromStdString("Failed to demote user %1: %2").arg(userName).arg(query->lastError().text()));
return Response::RespInternalError; return Response::RespInternalError;
}
QList<ServerSocketInterface *> userList;
ServerSocketInterface *user = static_cast<ServerSocketInterface *>(server->getUsers().value(userName)); ServerSocketInterface *user = static_cast<ServerSocketInterface *>(server->getUsers().value(userName));
userList.append(user); if (user) {
if (!userList.isEmpty()) {
Event_ConnectionClosed event; Event_ConnectionClosed event;
event.set_reason(Event_ConnectionClosed::DEMOTED); event.set_reason(Event_ConnectionClosed::DEMOTED);
event.set_reason_str("Your moderator status has been revoked."); event.set_reason_str("Your moderator status has been revoked.");
event.set_end_time(QDateTime::currentDateTime().toTime_t()); event.set_end_time(QDateTime::currentDateTime().toTime_t());
for (int i = 0; i < userList.size(); ++i) {
SessionEvent *se = userList[i]->prepareSessionEvent(event); SessionEvent *se = user->prepareSessionEvent(event);
userList[i]->sendProtocolItem(*se); user->sendProtocolItem(*se);
delete se; delete se;
QMetaObject::invokeMethod(userList[i], "prepareDestroy", Qt::QueuedConnection);
}
userList.clear();
} }
QMetaObject::invokeMethod(user, "prepareDestroy", Qt::QueuedConnection);
} }
return Response::RespOk; return Response::RespOk;