szurubooru/src/Services/AuthService.php

120 lines
2.8 KiB
PHP
Raw Normal View History

<?php
namespace Szurubooru\Services;
use Szurubooru\Config;
use Szurubooru\Entities\Token;
use Szurubooru\Entities\User;
use Szurubooru\Services\PasswordService;
use Szurubooru\Services\TimeService;
use Szurubooru\Services\TokenService;
use Szurubooru\Services\UserService;
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(
Config $config,
PasswordService $passwordService,
TimeService $timeService,
TokenService $tokenService,
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(Token $token)
{
if ($token->getPurpose() !== 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 User();
$user->setName('Anonymous user');
$user->setAccessRank(User::ACCESS_RANK_ANONYMOUS);
$user->setAvatarStyle(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(User $user)
{
return $this->tokenService->createAndSaveToken($user->getId(), 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
}
}