szurubooru/src/Services/AuthService.php

108 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($userNameOrEmail, $password)
{
$user = $this->userService->getByNameOrEmail($userNameOrEmail);
$this->validateUser($user);
$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);
}
public function loginFromToken($loginTokenName)
{
2014-09-08 08:20:31 +02:00
$loginToken = $this->tokenService->getByName($loginTokenName);
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);
$this->validateUser($user);
2014-09-08 08:20:31 +02:00
$this->loginToken = $loginToken;
2014-09-08 08:20:31 +02:00
$this->loggedInUser = $user;
$this->userService->updateUserLastLoginTime($this->loggedInUser);
}
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;
}
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->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
}
}