szurubooru/src/Services/AuthService.php

113 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace Szurubooru\Services;
2014-09-04 19:21:18 +02:00
class AuthService
{
private $loggedInUser = null;
private $loginToken = null;
private $config;
private $passwordService;
2014-08-31 17:42:48 +02:00
private $timeService;
2014-09-08 08:20:31 +02:00
private $userService;
private $tokenService;
public function __construct(
\Szurubooru\Config $config,
\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)
{
$this->config = $config;
$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();
}
public function isLoggedIn()
{
return $this->loginToken !== null;
}
public function getLoggedInUser()
{
return $this->loggedInUser;
}
public function getLoginToken()
{
return $this->loginToken;
}
public function loginFromCredentials($formData)
{
$user = $this->userService->getByNameOrEmail($formData->userNameOrEmail);
$this->doFinalChecksOnUser($user);
2014-10-05 22:26:56 +02:00
$hashValid = $this->passwordService->isHashValid(
$formData->password,
$user->getPasswordSalt(),
$user->getPasswordHash());
if (!$hashValid)
throw new \InvalidArgumentException('Specified password is invalid.');
$this->loginToken = $this->createAndSaveLoginToken($user);
2014-08-31 17:42:48 +02:00
$this->loggedInUser = $user;
}
public function loginFromToken(\Szurubooru\Entities\Token $token)
{
if ($token->getPurpose() !== \Szurubooru\Entities\Token::PURPOSE_LOGIN)
throw new \Exception('This token is not a login token.');
$user = $this->userService->getById($token->getAdditionalData());
$this->doFinalChecksOnUser($user);
2014-09-08 08:20:31 +02:00
$this->loginToken = $token;
2014-09-08 08:20:31 +02:00
$this->loggedInUser = $user;
}
2014-08-31 17:42:48 +02:00
public function getAnonymousUser()
{
$user = new \Szurubooru\Entities\User();
$user->setName('Anonymous user');
$user->setAccessRank(\Szurubooru\Entities\User::ACCESS_RANK_ANONYMOUS);
2014-09-14 17:11:21 +02:00
$user->setAvatarStyle(\Szurubooru\Entities\User::AVATAR_STYLE_BLANK);
2014-08-31 17:42:48 +02:00
return $user;
}
public function loginAnonymous()
{
$this->loginToken = null;
2014-08-31 17:42:48 +02:00
$this->loggedInUser = $this->getAnonymousUser();
}
public function logout()
{
if (!$this->isLoggedIn())
throw new \Exception('Not logged in.');
$this->tokenService->invalidateByName($this->loginToken);
$this->loginToken = null;
}
private function createAndSaveLoginToken(\Szurubooru\Entities\User $user)
{
return $this->tokenService->createAndSaveToken($user->getId(), \Szurubooru\Entities\Token::PURPOSE_LOGIN);
}
private function doFinalChecksOnUser($user)
{
if (!$user->isAccountConfirmed() and $this->config->security->needEmailActivationToRegister)
throw new \DomainException('User didn\'t confirm account yet.');
2014-09-30 13:22:11 +02:00
if ($user->isBanned())
throw new \DomainException('Banned!');
2014-08-31 17:42:48 +02:00
}
}