mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-11-10 12:03:25 -08:00
[Server] Clarify the guards in Server::loginUser (#7886)
This commit is contained in:
parent
105cba6a1c
commit
e97d3e5519
1 changed files with 35 additions and 27 deletions
|
|
@ -98,6 +98,37 @@ Server_DatabaseInterface *Server::getDatabaseInterface() const
|
||||||
return databaseInterfaces.value(QThread::currentThread());
|
return databaseInterfaces.value(QThread::currentThread());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
/// Logs out a session that still holds this name, or warns when only the
|
||||||
|
/// sessions table thinks it does. Either way the name cannot simply be inserted.
|
||||||
|
void disconnectExistingSession(const QString &name,
|
||||||
|
const QMap<QString, Server_ProtocolHandler *> &users,
|
||||||
|
Server_DatabaseInterface *databaseInterface)
|
||||||
|
{
|
||||||
|
if (!users.contains(name)) {
|
||||||
|
if (databaseInterface->userSessionExists(name)) {
|
||||||
|
qDebug() << "Active session and sessions table inconsistent, please validate session table information "
|
||||||
|
"for user "
|
||||||
|
<< name;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
qDebug("Session already logged in, logging old session out");
|
||||||
|
Event_ConnectionClosed event;
|
||||||
|
event.set_reason(Event_ConnectionClosed::LOGGEDINELSEWERE);
|
||||||
|
event.set_reason_str("You have been logged out due to logging in at another location.");
|
||||||
|
event.set_end_time(QDateTime::currentDateTime().toSecsSinceEpoch());
|
||||||
|
|
||||||
|
SessionEvent *se = users.value(name)->prepareSessionEvent(event);
|
||||||
|
users.value(name)->sendProtocolItem(*se);
|
||||||
|
delete se;
|
||||||
|
|
||||||
|
users.value(name)->prepareDestroy();
|
||||||
|
}
|
||||||
|
} // namespace
|
||||||
|
|
||||||
AuthenticationResult Server::loginUser(Server_ProtocolHandler *session,
|
AuthenticationResult Server::loginUser(Server_ProtocolHandler *session,
|
||||||
QString &name,
|
QString &name,
|
||||||
const QString &password,
|
const QString &password,
|
||||||
|
|
@ -108,15 +139,11 @@ AuthenticationResult Server::loginUser(Server_ProtocolHandler *session,
|
||||||
QString &clientVersion,
|
QString &clientVersion,
|
||||||
QString & /* connectionType */)
|
QString & /* connectionType */)
|
||||||
{
|
{
|
||||||
bool hasClientId = false;
|
const bool hasClientId = !clientid.isEmpty();
|
||||||
if (clientid.isEmpty()) {
|
if (!hasClientId && getClientIDRequiredEnabled()) {
|
||||||
// client id is empty, either out dated client or client has been modified
|
// client id is empty, either out dated client or client has been modified
|
||||||
if (getClientIDRequiredEnabled()) {
|
|
||||||
return ClientIdRequired;
|
return ClientIdRequired;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
hasClientId = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (name.size() > 35) {
|
if (name.size() > 35) {
|
||||||
name = name.left(35);
|
name = name.left(35);
|
||||||
|
|
@ -146,26 +173,7 @@ AuthenticationResult Server::loginUser(Server_ProtocolHandler *session,
|
||||||
name = QString::fromStdString(data.name()); // Compensate for case indifference
|
name = QString::fromStdString(data.name()); // Compensate for case indifference
|
||||||
|
|
||||||
if (authState == PasswordRight) {
|
if (authState == PasswordRight) {
|
||||||
if (users.contains(name) || databaseInterface->userSessionExists(name)) {
|
disconnectExistingSession(name, users, databaseInterface);
|
||||||
if (users.contains(name)) {
|
|
||||||
qDebug("Session already logged in, logging old session out");
|
|
||||||
Event_ConnectionClosed event;
|
|
||||||
event.set_reason(Event_ConnectionClosed::LOGGEDINELSEWERE);
|
|
||||||
event.set_reason_str("You have been logged out due to logging in at another location.");
|
|
||||||
event.set_end_time(QDateTime::currentDateTime().toSecsSinceEpoch());
|
|
||||||
|
|
||||||
SessionEvent *se = users.value(name)->prepareSessionEvent(event);
|
|
||||||
users.value(name)->sendProtocolItem(*se);
|
|
||||||
delete se;
|
|
||||||
|
|
||||||
users.value(name)->prepareDestroy();
|
|
||||||
} else {
|
|
||||||
qDebug() << "Active session and sessions table inconsistent, please validate session table information "
|
|
||||||
"for user "
|
|
||||||
<< name;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (authState == UnknownUser) {
|
} else if (authState == UnknownUser) {
|
||||||
// Change user name so that no two users have the same names,
|
// Change user name so that no two users have the same names,
|
||||||
// don't interfere with registered user names though.
|
// don't interfere with registered user names though.
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue