Refresh only last login time when necessary (avoid hammering the db, ref #420)
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
|
||||
#include "AuthServiceBase.hpp"
|
||||
|
||||
#include <cstdlib>
|
||||
#include "database/Db.hpp"
|
||||
#include "database/Session.hpp"
|
||||
#include "database/User.hpp"
|
||||
@@ -32,8 +33,7 @@ namespace Auth
|
||||
: _db{ db }
|
||||
{}
|
||||
|
||||
UserId
|
||||
AuthServiceBase::getOrCreateUser(std::string_view loginName)
|
||||
UserId AuthServiceBase::getOrCreateUser(std::string_view loginName)
|
||||
{
|
||||
Session& session{ getDbSession() };
|
||||
auto transaction{ session.createWriteTransaction() };
|
||||
@@ -52,19 +52,31 @@ namespace Auth
|
||||
return user->getId();
|
||||
}
|
||||
|
||||
void
|
||||
AuthServiceBase::onUserAuthenticated(UserId userId)
|
||||
void AuthServiceBase::onUserAuthenticated(UserId userId)
|
||||
{
|
||||
Session& session{ getDbSession() };
|
||||
auto transaction {session.createWriteTransaction()};
|
||||
|
||||
User::pointer user {User::find(session, userId)};
|
||||
if (user)
|
||||
user.modify()->setLastLogin(Wt::WDateTime::currentDateTime());
|
||||
// Update last login only if relevant (avoid hammering write accesses to the database)
|
||||
{
|
||||
auto transaction{ session.createReadTransaction() };
|
||||
|
||||
const User::pointer user{ User::find(session, userId) };
|
||||
if (!user)
|
||||
return;
|
||||
|
||||
if (std::abs(Wt::WDateTime::currentDateTime().secsTo(user->getLastLogin())) < 60)
|
||||
return;
|
||||
}
|
||||
|
||||
Session&
|
||||
AuthServiceBase::getDbSession()
|
||||
{
|
||||
auto transaction{ session.createWriteTransaction() };
|
||||
|
||||
if (User::pointer user{ User::find(session, userId) })
|
||||
user.modify()->setLastLogin(Wt::WDateTime::currentDateTime());
|
||||
}
|
||||
}
|
||||
|
||||
Session& AuthServiceBase::getDbSession()
|
||||
{
|
||||
return _db.getTLSSession();
|
||||
}
|
||||
|
||||
@@ -41,8 +41,7 @@ namespace Auth
|
||||
_validator.setMinimumMatchLength(3);
|
||||
}
|
||||
|
||||
bool
|
||||
InternalPasswordService::checkUserPassword(std::string_view loginName, std::string_view password)
|
||||
bool InternalPasswordService::checkUserPassword(std::string_view loginName, std::string_view password)
|
||||
{
|
||||
LMS_LOG(AUTH, DEBUG, "Checking internal password for user '" << loginName << "'");
|
||||
|
||||
@@ -73,14 +72,12 @@ namespace Auth
|
||||
return _hashFunc.verify(std::string{ password }, std::string{ passwordHash.salt }, std::string{ passwordHash.hash });
|
||||
}
|
||||
|
||||
bool
|
||||
InternalPasswordService::canSetPasswords() const
|
||||
bool InternalPasswordService::canSetPasswords() const
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
IPasswordService::PasswordAcceptabilityResult
|
||||
InternalPasswordService::checkPasswordAcceptability(std::string_view password, const PasswordValidationContext& context) const
|
||||
IPasswordService::PasswordAcceptabilityResult InternalPasswordService::checkPasswordAcceptability(std::string_view password, const PasswordValidationContext& context) const
|
||||
{
|
||||
switch (context.userType)
|
||||
{
|
||||
@@ -94,8 +91,7 @@ namespace Auth
|
||||
throw NotImplementedException{};
|
||||
}
|
||||
|
||||
void
|
||||
InternalPasswordService::setPassword(Database::UserId userId, std::string_view newPassword)
|
||||
void InternalPasswordService::setPassword(Database::UserId userId, std::string_view newPassword)
|
||||
{
|
||||
const Database::User::PasswordHash passwordHash{ hashPassword(newPassword) };
|
||||
|
||||
@@ -120,8 +116,7 @@ namespace Auth
|
||||
getAuthTokenService().clearAuthTokens(userId);
|
||||
}
|
||||
|
||||
Database::User::PasswordHash
|
||||
InternalPasswordService::hashPassword(std::string_view password) const
|
||||
Database::User::PasswordHash InternalPasswordService::hashPassword(std::string_view password) const
|
||||
{
|
||||
const std::string salt{ Wt::WRandom::generateId(32) };
|
||||
|
||||
|
||||
Reference in New Issue
Block a user