Optimized API operations

Every operation updated user last login time, which was inefficient.
Changed it to update only after logins from credentials or cookies.
This commit is contained in:
Marcin Kurczewski 2014-09-23 20:35:41 +02:00
parent a140e04ca3
commit c33817e4ab
4 changed files with 20 additions and 8 deletions

2
TODO
View file

@ -145,8 +145,6 @@ miscellaneous:
- I don't think browsing online logs is very necessary
- add help, api documentation
- add version on homepage that reads version from package.json and git hash
- make script faster, right now every request to backend takes 200ms
(current suspect: dependency injector and sqlite)
- add tool for migrating szurubooru database
- endless pager should include information about page number
- add README - apache2 has good README:

View file

@ -44,9 +44,13 @@ App.Auth = function(_, jQuery, util, api, appState, promise) {
});
}
function loginFromToken(token) {
function loginFromToken(token, isFromCookie) {
return promise.make(function(resolve, reject) {
promise.wait(api.post('/login', {token: token}))
var fd = {
token: token,
isFromCookie: isFromCookie
};
promise.wait(api.post('/login', fd))
.then(function(response) {
updateAppState(response);
resolve(response);
@ -89,7 +93,7 @@ App.Auth = function(_, jQuery, util, api, appState, promise) {
return;
}
promise.wait(loginFromToken(authCookie))
promise.wait(loginFromToken(authCookie, true))
.then(function(response) {
resolve();
}).fail(function(response) {

View file

@ -4,6 +4,7 @@ namespace Szurubooru\Controllers;
final class AuthController extends AbstractController
{
private $authService;
private $userService;
private $tokenService;
private $privilegeService;
private $inputReader;
@ -12,6 +13,7 @@ final class AuthController extends AbstractController
public function __construct(
\Szurubooru\Services\AuthService $authService,
\Szurubooru\Services\UserService $userService,
\Szurubooru\Services\TokenService $tokenService,
\Szurubooru\Services\PrivilegeService $privilegeService,
\Szurubooru\Helpers\InputReader $inputReader,
@ -19,6 +21,7 @@ final class AuthController extends AbstractController
\Szurubooru\Controllers\ViewProxies\TokenViewProxy $tokenViewProxy)
{
$this->authService = $authService;
$this->userService = $userService;
$this->tokenService = $tokenService;
$this->privilegeService = $privilegeService;
$this->inputReader = $inputReader;
@ -38,21 +41,30 @@ final class AuthController extends AbstractController
{
$formData = new \Szurubooru\FormData\LoginFormData($this->inputReader);
$this->authService->loginFromCredentials($formData);
$user = $this->authService->getLoggedInUser();
$this->userService->updateUserLastLoginTime($user);
}
elseif (isset($this->inputReader->token))
{
$token = $this->tokenService->getByName($this->inputReader->token);
$this->authService->loginFromToken($token);
$user = $this->authService->getLoggedInUser();
$isFromCookie = boolval($this->inputReader->isFromCookie);
if ($isFromCookie)
$this->userService->updateUserLastLoginTime($user);
}
else
{
$this->authService->loginAnonymous();
$user = $this->authService->getLoggedInUser();
}
return
[
'token' => $this->tokenViewProxy->fromEntity($this->authService->getLoginToken()),
'user' => $this->userViewProxy->fromEntity($this->authService->getLoggedInUser()),
'user' => $this->userViewProxy->fromEntity($user),
'privileges' => $this->privilegeService->getCurrentPrivileges(),
];
}

View file

@ -54,7 +54,6 @@ class AuthService
$this->loginToken = $this->createAndSaveLoginToken($user);
$this->loggedInUser = $user;
$this->userService->updateUserLastLoginTime($user);
}
public function loginFromToken(\Szurubooru\Entities\Token $token)
@ -67,7 +66,6 @@ class AuthService
$this->loginToken = $token;
$this->loggedInUser = $user;
$this->userService->updateUserLastLoginTime($this->loggedInUser);
}
public function getAnonymousUser()