Added fallback anonymous user to authorization

This commit is contained in:
Marcin Kurczewski 2014-08-31 13:16:29 +02:00
parent 2335037a9c
commit 1f6017aae7
4 changed files with 46 additions and 9 deletions

View file

@ -4,10 +4,15 @@ namespace Szurubooru\Controllers;
final class AuthController extends AbstractController
{
private $authService;
private $inputReader;
public function __construct(\Szurubooru\Services\AuthService $authService)
public function __construct(
\Szurubooru\Services\AuthService $authService,
\Szurubooru\Helpers\InputReader $inputReader)
{
$this->authService = $authService;
$this->inputReader = $inputReader;
}
public function registerRoutes(\Szurubooru\Router $router)
@ -18,16 +23,28 @@ final class AuthController extends AbstractController
public function login()
{
$input = new \Szurubooru\Helpers\InputReader();
if (isset($this->inputReader->userName) and isset($this->inputReader->password))
{
if (!$this->inputReader->userName)
throw new \DomainException('User name cannot be empty.');
else if (!$this->inputReader->password)
throw new \DomainException('Password cannot be empty.');
if ($input->userName and $input->password)
$this->authService->loginFromCredentials($input->userName, $input->password);
elseif ($input->token)
$this->authService->loginFromToken($input->token);
$this->authService->loginFromCredentials($this->inputReader->userName, $this->inputReader->password);
}
elseif (isset($this->inputReader->token))
{
if (!$this->inputReader->token)
throw new \DomainException('Authentication token cannot be empty.');
$this->authService->loginFromToken($this->inputReader->token);
}
else
throw new \Szurubooru\MissingArgumentException();
{
$this->authService->loginAnonymous();
}
return [
return
[
'token' => new \Szurubooru\ViewProxies\Token($this->authService->getLoginToken()),
'user' => new \Szurubooru\ViewProxies\User($this->authService->getLoggedInUser()),
];

View file

@ -3,6 +3,13 @@ namespace Szurubooru\Entities;
final class User extends Entity
{
const ACCESS_RANK_NOBODY = 0;
const ACCESS_RANK_ANONYMOUS = 1;
const ACCESS_RANK_REGULAR_USER = 2;
const ACCESS_RANK_POWER_USER = 3;
const ACCESS_RANK_MODERATOR = 4;
const ACCESS_RANK_ADMINISTRATOR = 5;
public $name;
public $passwordHash;
}

View file

@ -34,7 +34,7 @@ final class AuthService
public function getLoginToken()
{
return $this->token;
return $this->loginToken;
}
public function loginFromCredentials($userName, $password)
@ -66,6 +66,12 @@ final class AuthService
}
}
public function loginAnonymous()
{
$this->loginToken = null;
$this->loggedInUser = $this->userService->getAnonymousUser();
}
public function logout()
{
if (!$this->isLoggedIn())

View file

@ -24,4 +24,11 @@ class UserService
{
return $this->userDao->save($user);
}
public function getAnonymousUser()
{
$user = new \Szurubooru\Entities\User();
$user->name = 'Anonymous user';
$user->accessRank = \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS;
}
}