Exemple #1
0
AuthenticationResult Server::loginUser(Server_ProtocolHandler *session, QString &name, const QString &password, QString &reasonStr, int &secondsLeft)
{
    if (name.size() > 35)
        name = name.left(35);
    
    Server_DatabaseInterface *databaseInterface = getDatabaseInterface();
    
    QWriteLocker locker(&clientsLock);
    
    AuthenticationResult authState = databaseInterface->checkUserPassword(session, name, password, reasonStr, secondsLeft);
    if ((authState == NotLoggedIn) || (authState == UserIsBanned || authState == UsernameInvalid))
        return authState;
    
    ServerInfo_User data = databaseInterface->getUserData(name, true);
    data.set_address(session->getAddress().toStdString());
    name = QString::fromStdString(data.name()); // Compensate for case indifference
    
    databaseInterface->lockSessionTables();
    
    if (authState == PasswordRight) {
        if (users.contains(name) || databaseInterface->userSessionExists(name)) {
            qDebug("Login denied: would overwrite old session");
            databaseInterface->unlockSessionTables();
            return WouldOverwriteOldSession;
        }
    } else if (authState == UnknownUser) {
        // Change user name so that no two users have the same names,
        // don't interfere with registered user names though.
        QSettings settings("servatrice.ini", QSettings::IniFormat);
        bool requireReg = settings.value("authentication/regonly", 0).toBool();
        if (requireReg) {
            qDebug("Login denied: registration required");
            databaseInterface->unlockSessionTables();
            return RegistrationRequired;
        }

        QString tempName = name;
        int i = 0;
        while (users.contains(tempName) || databaseInterface->userExists(tempName) || databaseInterface->userSessionExists(tempName))
            tempName = name + "_" + QString::number(++i);
        name = tempName;
        data.set_name(name.toStdString());
    }
    
    users.insert(name, session);
    qDebug() << "Server::loginUser:"******"name=" << name;
    
    data.set_session_id(databaseInterface->startSession(name, session->getAddress()));  
    databaseInterface->unlockSessionTables();
    
    usersBySessionId.insert(data.session_id(), session);
    
    qDebug() << "session id:" << data.session_id();
    session->setUserInfo(data);
    
    Event_UserJoined event;
    event.mutable_user_info()->CopyFrom(session->copyUserInfo(false));
    SessionEvent *se = Server_ProtocolHandler::prepareSessionEvent(event);
    for (int i = 0; i < clients.size(); ++i)
        if (clients[i]->getAcceptsUserListChanges())
            clients[i]->sendProtocolItem(*se);
    delete se;
    
    event.mutable_user_info()->CopyFrom(session->copyUserInfo(true, true, true));
    locker.unlock();
    
    se = Server_ProtocolHandler::prepareSessionEvent(event);
    sendIsl_SessionEvent(*se);
    delete se;
    
    return authState;
}