2014-08-30 18:11:32 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Services;
|
|
|
|
|
2014-09-04 19:21:18 +02:00
|
|
|
class AuthService
|
2014-08-30 18:11:32 +02:00
|
|
|
{
|
|
|
|
private $loggedInUser = null;
|
|
|
|
private $loginToken = null;
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
private $config;
|
2014-08-30 18:11:32 +02:00
|
|
|
private $passwordService;
|
2014-08-31 17:42:48 +02:00
|
|
|
private $timeService;
|
2014-09-08 08:20:31 +02:00
|
|
|
private $userService;
|
|
|
|
private $tokenService;
|
2014-08-30 18:11:32 +02:00
|
|
|
|
|
|
|
public function __construct(
|
2014-09-08 13:06:32 +02:00
|
|
|
\Szurubooru\Config $config,
|
2014-08-30 18:11:32 +02:00
|
|
|
\Szurubooru\Services\PasswordService $passwordService,
|
2014-08-31 17:42:48 +02:00
|
|
|
\Szurubooru\Services\TimeService $timeService,
|
2014-09-08 08:20:31 +02:00
|
|
|
\Szurubooru\Services\TokenService $tokenService,
|
|
|
|
\Szurubooru\Services\UserService $userService)
|
2014-08-30 18:11:32 +02:00
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->config = $config;
|
2014-08-30 18:11:32 +02:00
|
|
|
$this->passwordService = $passwordService;
|
2014-08-31 17:42:48 +02:00
|
|
|
$this->timeService = $timeService;
|
2014-09-08 08:20:31 +02:00
|
|
|
$this->tokenService = $tokenService;
|
|
|
|
$this->userService = $userService;
|
|
|
|
|
|
|
|
$this->loggedInUser = $this->getAnonymousUser();
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function isLoggedIn()
|
|
|
|
{
|
|
|
|
return $this->loginToken !== null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLoggedInUser()
|
|
|
|
{
|
|
|
|
return $this->loggedInUser;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getLoginToken()
|
|
|
|
{
|
2014-08-31 13:16:29 +02:00
|
|
|
return $this->loginToken;
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
public function loginFromCredentials($userNameOrEmail, $password)
|
2014-08-30 18:11:32 +02:00
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
$user = $this->userService->getByNameOrEmail($userNameOrEmail);
|
|
|
|
$this->validateUser($user);
|
2014-08-30 18:11:32 +02:00
|
|
|
|
|
|
|
$passwordHash = $this->passwordService->getHash($password);
|
|
|
|
if ($user->passwordHash != $passwordHash)
|
|
|
|
throw new \InvalidArgumentException('Specified password is invalid.');
|
|
|
|
|
|
|
|
$this->loginToken = $this->createAndSaveLoginToken($user);
|
2014-08-31 17:42:48 +02:00
|
|
|
$this->loggedInUser = $user;
|
2014-09-08 08:20:31 +02:00
|
|
|
$this->userService->updateUserLastLoginTime($user);
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function loginFromToken($loginTokenName)
|
|
|
|
{
|
2014-09-08 08:20:31 +02:00
|
|
|
$loginToken = $this->tokenService->getByName($loginTokenName);
|
2014-09-07 18:07:24 +02:00
|
|
|
if ($loginToken->purpose != \Szurubooru\Entities\Token::PURPOSE_LOGIN)
|
|
|
|
throw new \Exception('This token is not a login token.');
|
|
|
|
|
2014-09-08 08:20:31 +02:00
|
|
|
$user = $this->userService->getById($loginToken->additionalData);
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->validateUser($user);
|
2014-09-08 08:20:31 +02:00
|
|
|
|
2014-08-30 18:11:32 +02:00
|
|
|
$this->loginToken = $loginToken;
|
2014-09-08 08:20:31 +02:00
|
|
|
$this->loggedInUser = $user;
|
|
|
|
$this->userService->updateUserLastLoginTime($this->loggedInUser);
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|
|
|
|
|
2014-08-31 17:42:48 +02:00
|
|
|
public function getAnonymousUser()
|
|
|
|
{
|
|
|
|
$user = new \Szurubooru\Entities\User();
|
|
|
|
$user->name = 'Anonymous user';
|
|
|
|
$user->accessRank = \Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS;
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2014-08-31 13:16:29 +02:00
|
|
|
public function loginAnonymous()
|
|
|
|
{
|
|
|
|
$this->loginToken = null;
|
2014-08-31 17:42:48 +02:00
|
|
|
$this->loggedInUser = $this->getAnonymousUser();
|
2014-08-31 13:16:29 +02:00
|
|
|
}
|
|
|
|
|
2014-08-30 18:11:32 +02:00
|
|
|
public function logout()
|
|
|
|
{
|
|
|
|
if (!$this->isLoggedIn())
|
|
|
|
throw new \Exception('Not logged in.');
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->tokenService->invalidateByName($this->loginToken);
|
2014-08-30 18:11:32 +02:00
|
|
|
$this->loginToken = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function createAndSaveLoginToken(\Szurubooru\Entities\User $user)
|
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
return $this->tokenService->createAndSaveToken($user->id, \Szurubooru\Entities\Token::PURPOSE_LOGIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function validateUser($user)
|
|
|
|
{
|
|
|
|
if (!$user->email and $this->config->security->needEmailActivationToRegister)
|
|
|
|
throw new \DomainException('User didn\'t confirm mail yet.');
|
2014-08-31 17:42:48 +02:00
|
|
|
}
|
2014-08-30 18:11:32 +02:00
|
|
|
}
|