From baf9b1d31a348665ac6178bedf6ab2afb55b7931 Mon Sep 17 00:00:00 2001 From: rr- Date: Sun, 3 Apr 2016 15:12:15 +0200 Subject: [PATCH] server/users: track login time --- client/js/api.js | 2 +- server/szurubooru/middleware/authenticator.py | 8 ++++++-- server/szurubooru/services/user_service.py | 3 +++ 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/client/js/api.js b/client/js/api.js index 0bb6c80..a881b56 100644 --- a/client/js/api.js +++ b/client/js/api.js @@ -66,7 +66,7 @@ class Api { return new Promise((resolve, reject) => { this.userName = userName; this.userPassword = userPassword; - this.get('/user/' + userName) + this.get('/user/' + userName + '?bump-login=true') .then(response => { this.user = response.user; resolve(); diff --git a/server/szurubooru/middleware/authenticator.py b/server/szurubooru/middleware/authenticator.py index 6fc9328..37f743d 100644 --- a/server/szurubooru/middleware/authenticator.py +++ b/server/szurubooru/middleware/authenticator.py @@ -18,6 +18,10 @@ class Authenticator(object): def process_request(self, request, response): ''' Executed before passing the request to the API. ''' request.context.user = self._get_user(request) + if request.get_param_as_bool('bump-login') \ + and request.context.user.user_id: + self._user_service.bump_login_time(request.context.user) + request.context.session.commit() def _get_user(self, request): if not request.auth: @@ -34,8 +38,8 @@ class Authenticator(object): username, password = base64.decodebytes( user_and_password.encode('ascii')).decode('utf8').split(':') - session = request.context.session - return self._authenticate(session, username, password) + return self._authenticate( + request.context.session, username, password) except ValueError as err: msg = 'Basic authentication header value not properly formed. ' \ + 'Supplied header {0}. Got error: {1}' diff --git a/server/szurubooru/services/user_service.py b/server/szurubooru/services/user_service.py index 47e5346..189a027 100644 --- a/server/szurubooru/services/user_service.py +++ b/server/szurubooru/services/user_service.py @@ -43,6 +43,9 @@ class UserService(object): session.add(user) return user + def bump_login_time(self, user): + user.last_login_time = datetime.now() + def get_by_name(self, session, name): ''' Retrieves an user by its name. ''' return session.query(User).filter_by(name=name).first()