2014-08-30 13:04:52 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Controllers;
|
|
|
|
|
|
|
|
final class AuthController extends AbstractController
|
|
|
|
{
|
2014-08-30 18:11:32 +02:00
|
|
|
private $authService;
|
2014-08-31 14:07:46 +02:00
|
|
|
private $userService;
|
|
|
|
private $passwordService;
|
2014-08-31 13:16:29 +02:00
|
|
|
private $inputReader;
|
2014-09-05 19:18:49 +02:00
|
|
|
private $userViewProxy;
|
|
|
|
private $tokenViewProxy;
|
2014-08-30 18:11:32 +02:00
|
|
|
|
2014-08-31 13:16:29 +02:00
|
|
|
public function __construct(
|
|
|
|
\Szurubooru\Services\AuthService $authService,
|
2014-08-31 14:07:46 +02:00
|
|
|
\Szurubooru\Services\UserService $userService,
|
|
|
|
\Szurubooru\Services\PasswordService $passwordService,
|
2014-09-05 19:18:49 +02:00
|
|
|
\Szurubooru\Helpers\InputReader $inputReader,
|
|
|
|
\Szurubooru\Controllers\ViewProxies\UserViewProxy $userViewProxy,
|
|
|
|
\Szurubooru\Controllers\ViewProxies\TokenViewProxy $tokenViewProxy)
|
2014-08-30 18:11:32 +02:00
|
|
|
{
|
|
|
|
$this->authService = $authService;
|
2014-08-31 14:07:46 +02:00
|
|
|
$this->userService = $userService;
|
|
|
|
$this->passwordService = $passwordService;
|
2014-08-31 13:16:29 +02:00
|
|
|
$this->inputReader = $inputReader;
|
2014-09-05 19:18:49 +02:00
|
|
|
$this->userViewProxy = $userViewProxy;
|
|
|
|
$this->tokenViewProxy = $tokenViewProxy;
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|
|
|
|
|
2014-08-30 15:04:33 +02:00
|
|
|
public function registerRoutes(\Szurubooru\Router $router)
|
2014-08-30 13:04:52 +02:00
|
|
|
{
|
|
|
|
$router->post('/api/login', [$this, 'login']);
|
2014-08-30 23:17:54 +02:00
|
|
|
$router->put('/api/login', [$this, 'login']);
|
2014-08-30 13:04:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function login()
|
|
|
|
{
|
2014-08-31 13:16:29 +02:00
|
|
|
if (isset($this->inputReader->userName) and isset($this->inputReader->password))
|
|
|
|
{
|
|
|
|
$this->authService->loginFromCredentials($this->inputReader->userName, $this->inputReader->password);
|
|
|
|
}
|
|
|
|
elseif (isset($this->inputReader->token))
|
|
|
|
{
|
|
|
|
$this->authService->loginFromToken($this->inputReader->token);
|
|
|
|
}
|
2014-08-30 23:17:54 +02:00
|
|
|
else
|
2014-08-31 13:16:29 +02:00
|
|
|
{
|
|
|
|
$this->authService->loginAnonymous();
|
|
|
|
}
|
2014-08-30 23:17:54 +02:00
|
|
|
|
2014-08-31 13:16:29 +02:00
|
|
|
return
|
|
|
|
[
|
2014-09-05 19:18:49 +02:00
|
|
|
'token' => $this->tokenViewProxy->fromEntity($this->authService->getLoginToken()),
|
|
|
|
'user' => $this->userViewProxy->fromEntity($this->authService->getLoggedInUser()),
|
2014-09-04 19:57:06 +02:00
|
|
|
'privileges' => $this->authService->getCurrentPrivileges(),
|
2014-08-30 23:17:54 +02:00
|
|
|
];
|
2014-08-30 13:04:52 +02:00
|
|
|
}
|
|
|
|
}
|