diff --git a/cockatrice/src/localserver.h b/cockatrice/src/localserver.h index db28b639..3409b1a4 100644 --- a/cockatrice/src/localserver.h +++ b/cockatrice/src/localserver.h @@ -19,6 +19,7 @@ public: LocalServerInterface *newConnection(); protected: + bool userExists(const QString & /*name*/) { return false; } ServerInfo_User *getUserData(const QString &name); QList getBuddyList(const QString & /*name*/) { return QList(); } QList getIgnoreList(const QString & /*name*/) { return QList(); } diff --git a/common/server.cpp b/common/server.cpp index 4890f04f..ac4b5e63 100644 --- a/common/server.cpp +++ b/common/server.cpp @@ -38,6 +38,8 @@ Server::~Server() AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString &name, const QString &password) { + if (name.size() > 35) + name = name.left(35); AuthenticationResult authState = checkUserPassword(name, password); if (authState == PasswordWrong) return authState; @@ -47,10 +49,11 @@ AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString if (oldSession) delete oldSession; // ~Server_ProtocolHandler() will call Server::removeClient } 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. QString tempName = name; int i = 0; - while (users.contains(tempName)) + while (users.contains(tempName) || userExists(tempName)) tempName = name + "_" + QString::number(++i); name = tempName; } diff --git a/common/server.h b/common/server.h index aa0e5dbc..6a7ed157 100644 --- a/common/server.h +++ b/common/server.h @@ -47,6 +47,7 @@ protected: QMap users; QMap rooms; + virtual bool userExists(const QString &user) = 0; virtual AuthenticationResult checkUserPassword(const QString &user, const QString &password) = 0; virtual ServerInfo_User *getUserData(const QString &name) = 0; int nextGameId; diff --git a/servatrice/src/servatrice.cpp b/servatrice/src/servatrice.cpp index b3d532cc..476c3868 100644 --- a/servatrice/src/servatrice.cpp +++ b/servatrice/src/servatrice.cpp @@ -170,6 +170,25 @@ AuthenticationResult Servatrice::checkUserPassword(const QString &user, const QS return UnknownUser; } +bool Servatrice::userExists(const QString &user) +{ + const QString method = settings->value("authentication/method").toString(); + if (method == "sql") { + checkSql(); + + QSqlQuery query; + query.prepare("select 1 from " + dbPrefix + "_users where name = :name"); + query.bindValue(":name", user); + qDebug() << "a"; + if (!execSqlQuery(query)) + return false; + qDebug() << "b"; + bool res = query.next(); + qDebug() << res << user; + return res; + } else return false; +} + ServerInfo_User *Servatrice::evalUserQueryResult(const QSqlQuery &query, bool complete) { QString name = query.value(0).toString(); diff --git a/servatrice/src/servatrice.h b/servatrice/src/servatrice.h index 9bc9ab00..0ee0b032 100644 --- a/servatrice/src/servatrice.h +++ b/servatrice/src/servatrice.h @@ -48,6 +48,7 @@ public: QString getDbPrefix() const { return dbPrefix; } void updateLoginMessage(); protected: + bool userExists(const QString &user); AuthenticationResult checkUserPassword(const QString &user, const QString &password); ServerInfo_User *getUserData(const QString &name); QList getBuddyList(const QString &name);