Implemented InputReader

This commit is contained in:
Marcin Kurczewski 2014-08-30 23:17:54 +02:00
parent 4202ae2ec7
commit 45e32c4e50
3 changed files with 23 additions and 4 deletions

View file

@ -13,13 +13,23 @@ final class AuthController extends AbstractController
public function registerRoutes(\Szurubooru\Router $router)
{
$router->post('/api/login', [$this, 'login']);
$router->get('/api/login', [$this, 'login']);
$router->put('/api/login', [$this, 'login']);
}
public function login()
{
$input = new \Szurubooru\Helpers\InputReader();
if ($input->userName and $input->password)
$this->authService->loginFromCredentials($input->userName, $input->password);
return $this->authService->getLoginToken();
elseif ($input->token)
$this->authService->loginFromToken($input->token);
else
throw new \Szurubooru\MissingArgumentException();
return [
'token' => $this->authService->getLoginToken(),
'user' => $this->authService->getLoggedInUser()
];
}
}

View file

@ -5,6 +5,15 @@ final class InputReader
{
public function __construct()
{
$_PUT = [];
if ($_SERVER['REQUEST_METHOD'] == 'PUT')
parse_str(file_get_contents('php://input'), $_PUT);
foreach ([$_GET, $_POST, $_PUT] as $source)
{
foreach ($source as $key => $value)
$this->$key = $value;
}
}
public function __get($key)

View file

@ -55,7 +55,7 @@ final class AuthService
{
$loginToken = $this->tokenDao->getByName($loginTokenName);
if (!$loginToken)
throw new \Exception('Error while logging in (invalid token.)');
throw new \Exception('Invalid login token.');
$this->loginToken = $loginToken;
$this->loggedInUser = $this->userDao->getById($loginToken->additionalData);