This repository has been archived on 2025-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
szurubooru/src/Controllers/AuthController.php

60 lines
1.7 KiB
PHP
Raw Normal View History

<?php
namespace Szurubooru\Controllers;
final class AuthController extends AbstractController
{
private $authService;
private $tokenService;
2014-09-06 10:00:26 +02:00
private $privilegeService;
private $inputReader;
2014-09-05 19:18:49 +02:00
private $userViewProxy;
private $tokenViewProxy;
public function __construct(
\Szurubooru\Services\AuthService $authService,
\Szurubooru\Services\TokenService $tokenService,
2014-09-06 10:00:26 +02:00
\Szurubooru\Services\PrivilegeService $privilegeService,
2014-09-05 19:18:49 +02:00
\Szurubooru\Helpers\InputReader $inputReader,
\Szurubooru\Controllers\ViewProxies\UserViewProxy $userViewProxy,
\Szurubooru\Controllers\ViewProxies\TokenViewProxy $tokenViewProxy)
{
$this->authService = $authService;
$this->tokenService = $tokenService;
2014-09-06 10:00:26 +02:00
$this->privilegeService = $privilegeService;
$this->inputReader = $inputReader;
2014-09-05 19:18:49 +02:00
$this->userViewProxy = $userViewProxy;
$this->tokenViewProxy = $tokenViewProxy;
}
2014-08-30 15:04:33 +02:00
public function registerRoutes(\Szurubooru\Router $router)
{
$router->post('/api/login', [$this, 'login']);
2014-08-30 23:17:54 +02:00
$router->put('/api/login', [$this, 'login']);
}
public function login()
{
if (isset($this->inputReader->userName) and isset($this->inputReader->password))
{
$formData = new \Szurubooru\FormData\LoginFormData($this->inputReader);
$this->authService->loginFromCredentials($formData);
}
elseif (isset($this->inputReader->token))
{
$token = $this->tokenService->getByName($this->inputReader->token);
$this->authService->loginFromToken($token);
}
2014-08-30 23:17:54 +02:00
else
{
$this->authService->loginAnonymous();
}
2014-08-30 23:17:54 +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-06 10:00:26 +02:00
'privileges' => $this->privilegeService->getCurrentPrivileges(),
2014-08-30 23:17:54 +02:00
];
}
}