store sessions in database

This commit is contained in:
Max-Wilhelm Bruker 2011-12-04 13:36:53 +01:00
parent a7f3ce4050
commit 1455c093cc
8 changed files with 52 additions and 2 deletions

View file

@ -149,7 +149,7 @@ CREATE TABLE `cockatrice_buddylist` (
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `cockatrice_bans` (
`user_name` varchar(255) unsigned zerofill NOT NULL,
`user_name` varchar(255) NOT NULL,
`ip_address` varchar(255) NOT NULL,
`id_admin` int(7) unsigned zerofill NOT NULL,
`time_from` datetime NOT NULL,
@ -159,3 +159,12 @@ CREATE TABLE `cockatrice_bans` (
KEY `time_from` (`time_from`,`ip_address`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
CREATE TABLE `cockatrice_sessions` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`ip_address` char(15) COLLATE utf8_unicode_ci NOT NULL,
`start_time` datetime NOT NULL,
`end_time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `username` (`user_name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

View file

@ -300,6 +300,31 @@ int Servatrice::getUsersWithAddress(const QHostAddress &address) const
return result;
}
int Servatrice::startSession(const QString &userName, const QString &address)
{
QMutexLocker locker(&dbMutex);
checkSql();
QSqlQuery query;
query.prepare("insert into " + dbPrefix + "_sessions (user_name, ip_address, start_time) values(:user_name, :ip_address, NOW())");
query.bindValue(":user_name", userName);
query.bindValue(":ip_address", address);
if (execSqlQuery(query))
return query.lastInsertId().toInt();
return -1;
}
void Servatrice::endSession(int sessionId)
{
QMutexLocker locker(&dbMutex);
checkSql();
QSqlQuery query;
query.prepare("update " + dbPrefix + "_sessions set end_time=NOW() where id = :id_session");
query.bindValue(":id_session", sessionId);
execSqlQuery(query);
}
QMap<QString, ServerInfo_User *> Servatrice::getBuddyList(const QString &name)
{
QMutexLocker locker(&dbMutex);

View file

@ -76,6 +76,8 @@ public:
QMap<QString, ServerInfo_User *> getIgnoreList(const QString &name);
void scheduleShutdown(const QString &reason, int minutes);
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);
private: