From 1f6017aae727f306a248a2d296a9a8bba6690f2f Mon Sep 17 00:00:00 2001 From: Marcin Kurczewski Date: Sun, 31 Aug 2014 13:16:29 +0200 Subject: [PATCH] Added fallback anonymous user to authorization --- src/Controllers/AuthController.php | 33 ++++++++++++++++++++++-------- src/Entities/User.php | 7 +++++++ src/Services/AuthService.php | 8 +++++++- src/Services/UserService.php | 7 +++++++ 4 files changed, 46 insertions(+), 9 deletions(-) diff --git a/src/Controllers/AuthController.php b/src/Controllers/AuthController.php index 21b01146..33ccb785 100644 --- a/src/Controllers/AuthController.php +++ b/src/Controllers/AuthController.php @@ -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()), ]; diff --git a/src/Entities/User.php b/src/Entities/User.php index 0e70b596..35174187 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -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; } diff --git a/src/Services/AuthService.php b/src/Services/AuthService.php index 2424261b..9ddcfc50 100644 --- a/src/Services/AuthService.php +++ b/src/Services/AuthService.php @@ -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()) diff --git a/src/Services/UserService.php b/src/Services/UserService.php index f718cd1d..d235aaf9 100644 --- a/src/Services/UserService.php +++ b/src/Services/UserService.php @@ -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; + } }