2014-08-31 09:03:11 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Services;
|
|
|
|
|
|
|
|
class UserService
|
|
|
|
{
|
2014-09-03 19:07:53 +02:00
|
|
|
private $config;
|
2014-09-02 09:09:07 +02:00
|
|
|
private $validator;
|
2014-08-31 17:42:48 +02:00
|
|
|
private $userDao;
|
2014-09-03 19:07:53 +02:00
|
|
|
private $userSearchService;
|
2014-08-31 17:42:48 +02:00
|
|
|
private $passwordService;
|
|
|
|
private $emailService;
|
2014-09-07 00:33:46 +02:00
|
|
|
private $fileService;
|
2014-09-09 17:49:19 +02:00
|
|
|
private $thumbnailService;
|
2014-08-31 17:42:48 +02:00
|
|
|
private $timeService;
|
2014-09-08 13:06:32 +02:00
|
|
|
private $tokenService;
|
2014-08-31 09:03:11 +02:00
|
|
|
|
2014-08-31 14:07:46 +02:00
|
|
|
public function __construct(
|
2014-09-03 19:07:53 +02:00
|
|
|
\Szurubooru\Config $config,
|
2014-09-02 09:09:07 +02:00
|
|
|
\Szurubooru\Validator $validator,
|
2014-08-31 14:07:46 +02:00
|
|
|
\Szurubooru\Dao\UserDao $userDao,
|
2014-09-03 19:07:53 +02:00
|
|
|
\Szurubooru\Dao\Services\UserSearchService $userSearchService,
|
2014-08-31 17:42:48 +02:00
|
|
|
\Szurubooru\Services\PasswordService $passwordService,
|
|
|
|
\Szurubooru\Services\EmailService $emailService,
|
2014-09-07 00:33:46 +02:00
|
|
|
\Szurubooru\Services\FileService $fileService,
|
2014-09-09 17:49:19 +02:00
|
|
|
\Szurubooru\Services\ThumbnailService $thumbnailService,
|
2014-09-08 13:06:32 +02:00
|
|
|
\Szurubooru\Services\TimeService $timeService,
|
|
|
|
\Szurubooru\Services\TokenService $tokenService)
|
2014-08-31 09:03:11 +02:00
|
|
|
{
|
2014-09-03 19:07:53 +02:00
|
|
|
$this->config = $config;
|
2014-09-02 09:09:07 +02:00
|
|
|
$this->validator = $validator;
|
2014-08-31 17:42:48 +02:00
|
|
|
$this->userDao = $userDao;
|
2014-09-03 19:07:53 +02:00
|
|
|
$this->userSearchService = $userSearchService;
|
2014-08-31 17:42:48 +02:00
|
|
|
$this->passwordService = $passwordService;
|
|
|
|
$this->emailService = $emailService;
|
2014-09-07 00:33:46 +02:00
|
|
|
$this->fileService = $fileService;
|
2014-09-09 17:49:19 +02:00
|
|
|
$this->thumbnailService = $thumbnailService;
|
2014-08-31 17:42:48 +02:00
|
|
|
$this->timeService = $timeService;
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->tokenService = $tokenService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getByNameOrEmail($userNameOrEmail, $allowUnconfirmed = false)
|
|
|
|
{
|
|
|
|
$user = $this->userDao->getByName($userNameOrEmail);
|
|
|
|
if ($user)
|
|
|
|
return $user;
|
|
|
|
|
|
|
|
$user = $this->userDao->getByEmail($userNameOrEmail, $allowUnconfirmed);
|
|
|
|
if ($user)
|
|
|
|
return $user;
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException('User "' . $userNameOrEmail . '" was not found.');
|
2014-08-31 09:03:11 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 08:20:31 +02:00
|
|
|
public function getByName($userName)
|
2014-09-04 19:07:57 +02:00
|
|
|
{
|
2014-09-08 08:20:31 +02:00
|
|
|
$user = $this->userDao->getByName($userName);
|
|
|
|
if (!$user)
|
|
|
|
throw new \InvalidArgumentException('User with name "' . $userName . '" was not found.');
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getById($userId)
|
|
|
|
{
|
|
|
|
$user = $this->userDao->getById($userId);
|
|
|
|
if (!$user)
|
|
|
|
throw new \InvalidArgumentException('User with id "' . $userId . '" was not found.');
|
|
|
|
return $user;
|
2014-09-04 19:07:57 +02:00
|
|
|
}
|
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
public function getFiltered(\Szurubooru\FormData\SearchFormData $formData)
|
|
|
|
{
|
2014-09-09 19:38:16 +02:00
|
|
|
$this->validator->validate($formData);
|
|
|
|
$searchFilter = new \Szurubooru\Dao\SearchFilter($this->config->users->usersPerPage, $formData);
|
2014-09-03 19:07:53 +02:00
|
|
|
return $this->userSearchService->getFiltered($searchFilter);
|
|
|
|
}
|
|
|
|
|
2014-09-07 00:33:46 +02:00
|
|
|
public function createUser(\Szurubooru\FormData\RegistrationFormData $formData)
|
2014-08-31 09:03:11 +02:00
|
|
|
{
|
2014-09-09 19:38:16 +02:00
|
|
|
$formData->validate($this->validator);
|
2014-08-31 09:03:11 +02:00
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
if ($formData->email and $this->userDao->getByEmail($formData->email))
|
|
|
|
throw new \DomainException('User with this e-mail already exists.');
|
|
|
|
|
2014-09-07 00:33:46 +02:00
|
|
|
if ($this->userDao->getByName($formData->userName))
|
2014-08-31 17:42:48 +02:00
|
|
|
throw new \DomainException('User with this name already exists.');
|
|
|
|
|
|
|
|
$user = new \Szurubooru\Entities\User();
|
2014-09-07 00:33:46 +02:00
|
|
|
$user->name = $formData->userName;
|
2014-09-08 13:06:32 +02:00
|
|
|
$user->emailUnconfirmed = $formData->email;
|
2014-08-31 17:42:48 +02:00
|
|
|
$user->passwordHash = $this->passwordService->getHash($formData->password);
|
2014-09-01 19:43:49 +02:00
|
|
|
$user->accessRank = $this->userDao->hasAnyUsers()
|
|
|
|
? \Szurubooru\Entities\User::ACCESS_RANK_REGULAR_USER
|
|
|
|
: \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR;
|
2014-08-31 17:42:48 +02:00
|
|
|
$user->registrationTime = $this->timeService->getCurrentTime();
|
2014-09-01 19:43:49 +02:00
|
|
|
$user->lastLoginTime = null;
|
2014-09-07 00:33:46 +02:00
|
|
|
$user->avatarStyle = \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR;
|
2014-08-31 17:42:48 +02:00
|
|
|
|
2014-09-09 12:34:57 +02:00
|
|
|
$user = $this->sendActivationEmailIfNeeded($user);
|
2014-08-31 09:03:11 +02:00
|
|
|
|
|
|
|
return $this->userDao->save($user);
|
|
|
|
}
|
2014-09-05 13:50:51 +02:00
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
public function updateUser(\Szurubooru\Entities\User $user, \Szurubooru\FormData\UserEditFormData $formData)
|
2014-09-05 13:50:51 +02:00
|
|
|
{
|
2014-09-09 19:38:16 +02:00
|
|
|
$this->validator->validate($formData);
|
|
|
|
|
2014-09-07 00:33:46 +02:00
|
|
|
if ($formData->avatarStyle !== null)
|
|
|
|
{
|
|
|
|
$user->avatarStyle = \Szurubooru\Helpers\EnumHelper::avatarStyleFromString($formData->avatarStyle);
|
|
|
|
if ($formData->avatarContent)
|
2014-09-09 17:49:19 +02:00
|
|
|
{
|
|
|
|
$target = $this->getCustomAvatarSourcePath($user);
|
|
|
|
$this->fileService->saveFromBase64($formData->avatarContent, $target);
|
|
|
|
$this->thumbnailService->deleteUsedThumbnails($target);
|
|
|
|
}
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
|
|
|
|
2014-09-09 12:34:57 +02:00
|
|
|
if ($formData->userName !== null and $formData->userName !== $user->name)
|
2014-09-07 00:33:46 +02:00
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
$userWithThisEmail = $this->userDao->getByName($formData->userName);
|
2014-09-09 12:34:57 +02:00
|
|
|
if ($userWithThisEmail and $userWithThisEmail->id !== $user->id)
|
2014-09-07 00:33:46 +02:00
|
|
|
throw new \DomainException('User with this name already exists.');
|
|
|
|
|
|
|
|
$user->name = $formData->userName;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($formData->password !== null)
|
|
|
|
{
|
|
|
|
$user->passwordHash = $this->passwordService->getHash($formData->password);
|
|
|
|
}
|
|
|
|
|
2014-09-09 12:34:57 +02:00
|
|
|
if ($formData->email !== null and $formData->email !== $user->email)
|
2014-09-07 00:33:46 +02:00
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
if ($this->userDao->getByEmail($formData->email))
|
|
|
|
throw new \DomainException('User with this e-mail already exists.');
|
|
|
|
|
|
|
|
$user->emailUnconfirmed = $formData->email;
|
2014-09-09 12:34:57 +02:00
|
|
|
$user = $this->sendActivationEmailIfNeeded($user);
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($formData->accessRank !== null)
|
|
|
|
{
|
|
|
|
$user->accessRank = \Szurubooru\Helpers\EnumHelper::accessRankFromString($formData->accessRank);
|
|
|
|
}
|
|
|
|
|
2014-09-07 14:50:16 +02:00
|
|
|
if ($formData->browsingSettings !== null)
|
|
|
|
{
|
|
|
|
$user->browsingSettings = $formData->browsingSettings;
|
|
|
|
}
|
|
|
|
|
2014-09-07 00:33:46 +02:00
|
|
|
return $this->userDao->save($user);
|
|
|
|
}
|
|
|
|
|
2014-09-09 19:38:16 +02:00
|
|
|
public function updateUserLastLoginTime(\Szurubooru\Entities\User $user)
|
|
|
|
{
|
|
|
|
$user->lastLoginTime = $this->timeService->getCurrentTime();
|
|
|
|
$this->userDao->save($user);
|
|
|
|
}
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
public function deleteUser(\Szurubooru\Entities\User $user)
|
2014-09-07 00:33:46 +02:00
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->userDao->deleteById($user->id);
|
2014-09-09 17:49:19 +02:00
|
|
|
|
|
|
|
$avatarSource = $this->getCustomAvatarSourcePath($user);
|
|
|
|
$this->fileService->delete($avatarSource);
|
|
|
|
$this->thumbnailService->deleteUsedThumbnails($avatarSource);
|
2014-09-05 13:50:51 +02:00
|
|
|
}
|
2014-09-07 00:33:46 +02:00
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
public function sendPasswordResetEmail(\Szurubooru\Entities\User $user)
|
|
|
|
{
|
|
|
|
$token = $this->tokenService->createAndSaveToken($user->name, \Szurubooru\Entities\Token::PURPOSE_PASSWORD_RESET);
|
|
|
|
$this->emailService->sendPasswordResetEmail($user, $token);
|
|
|
|
}
|
|
|
|
|
2014-09-09 19:38:16 +02:00
|
|
|
public function finishPasswordReset(\Szurubooru\Entities\Token $token)
|
2014-09-08 13:06:32 +02:00
|
|
|
{
|
2014-09-09 12:34:57 +02:00
|
|
|
if ($token->purpose !== \Szurubooru\Entities\Token::PURPOSE_PASSWORD_RESET)
|
2014-09-08 13:06:32 +02:00
|
|
|
throw new \Exception('This token is not a password reset token.');
|
|
|
|
|
|
|
|
$user = $this->getByName($token->additionalData);
|
|
|
|
$newPassword = $this->passwordService->getRandomPassword();
|
|
|
|
$user->passwordHash = $this->passwordService->getHash($newPassword);
|
|
|
|
$this->userDao->save($user);
|
|
|
|
$this->tokenService->invalidateByName($token->name);
|
|
|
|
return $newPassword;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function sendActivationEmail(\Szurubooru\Entities\User $user)
|
|
|
|
{
|
|
|
|
$token = $this->tokenService->createAndSaveToken($user->name, \Szurubooru\Entities\Token::PURPOSE_ACTIVATE);
|
|
|
|
$this->emailService->sendActivationEmail($user, $token);
|
|
|
|
}
|
|
|
|
|
2014-09-09 19:38:16 +02:00
|
|
|
public function finishActivation(\Szurubooru\Entities\Token $token)
|
2014-09-08 13:06:32 +02:00
|
|
|
{
|
2014-09-09 12:34:57 +02:00
|
|
|
if ($token->purpose !== \Szurubooru\Entities\Token::PURPOSE_ACTIVATE)
|
2014-09-08 13:06:32 +02:00
|
|
|
throw new \Exception('This token is not an activation token.');
|
|
|
|
|
|
|
|
$user = $this->getByName($token->additionalData);
|
2014-09-09 19:38:16 +02:00
|
|
|
$user = $this->confirmUserEmail($user);
|
2014-09-09 12:34:57 +02:00
|
|
|
$this->userDao->save($user);
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->tokenService->invalidateByName($token->name);
|
|
|
|
}
|
|
|
|
|
2014-09-09 19:38:16 +02:00
|
|
|
public function getCustomAvatarSourcePath(\Szurubooru\Entities\User $user)
|
|
|
|
{
|
|
|
|
return 'avatars' . DIRECTORY_SEPARATOR . $user->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getBlankAvatarSourcePath()
|
|
|
|
{
|
|
|
|
return 'avatars' . DIRECTORY_SEPARATOR . 'blank.png';
|
|
|
|
}
|
|
|
|
|
2014-09-09 12:34:57 +02:00
|
|
|
private function sendActivationEmailIfNeeded(\Szurubooru\Entities\User $user)
|
2014-09-07 00:33:46 +02:00
|
|
|
{
|
2014-09-09 12:34:57 +02:00
|
|
|
if ($user->accessRank === \Szurubooru\Entities\User::ACCESS_RANK_ADMINISTRATOR or !$this->config->security->needEmailActivationToRegister)
|
2014-09-08 13:06:32 +02:00
|
|
|
{
|
2014-09-09 19:38:16 +02:00
|
|
|
$user = $this->confirmUserEmail($user);
|
2014-09-08 13:06:32 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
$this->sendActivationEmail($user);
|
|
|
|
}
|
2014-09-09 12:34:57 +02:00
|
|
|
return $user;
|
2014-09-08 13:06:32 +02:00
|
|
|
}
|
|
|
|
|
2014-09-09 19:38:16 +02:00
|
|
|
private function confirmUserEmail(\Szurubooru\Entities\User $user)
|
2014-09-08 13:06:32 +02:00
|
|
|
{
|
|
|
|
//security issue:
|
|
|
|
//1. two users set their unconfirmed mail to godzilla@empire.gov
|
|
|
|
//2. activation mail is sent to both of them
|
|
|
|
//3. first user confirms, ok
|
|
|
|
//4. second user confirms, ok
|
|
|
|
//5. two users share the same mail --> problem.
|
|
|
|
//by checking here again for users with such mail, this problem is solved with first-come first-serve approach:
|
|
|
|
//whoever confirms e-mail first, wins.
|
|
|
|
if ($this->userDao->getByEmail($user->emailUnconfirmed))
|
|
|
|
throw new \DomainException('This e-mail was already confirmed by someone else in the meantime.');
|
|
|
|
|
|
|
|
$user->email = $user->emailUnconfirmed;
|
|
|
|
$user->emailUnconfirmed = null;
|
2014-09-09 12:34:57 +02:00
|
|
|
return $user;
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
2014-08-31 09:03:11 +02:00
|
|
|
}
|