szurubooru/src/Services/UserService.php

153 lines
4.7 KiB
PHP
Raw Normal View History

2014-08-31 09:03:11 +02:00
<?php
namespace Szurubooru\Services;
class UserService
{
private $config;
private $validator;
2014-08-31 17:42:48 +02:00
private $userDao;
private $userSearchService;
2014-08-31 17:42:48 +02:00
private $passwordService;
private $emailService;
private $fileService;
2014-08-31 17:42:48 +02:00
private $timeService;
2014-08-31 09:03:11 +02:00
2014-08-31 14:07:46 +02:00
public function __construct(
\Szurubooru\Config $config,
\Szurubooru\Validator $validator,
2014-08-31 14:07:46 +02:00
\Szurubooru\Dao\UserDao $userDao,
\Szurubooru\Dao\Services\UserSearchService $userSearchService,
2014-08-31 17:42:48 +02:00
\Szurubooru\Services\PasswordService $passwordService,
\Szurubooru\Services\EmailService $emailService,
\Szurubooru\Services\FileService $fileService,
2014-08-31 17:42:48 +02:00
\Szurubooru\Services\TimeService $timeService)
2014-08-31 09:03:11 +02:00
{
$this->config = $config;
$this->validator = $validator;
2014-08-31 17:42:48 +02:00
$this->userDao = $userDao;
$this->userSearchService = $userSearchService;
2014-08-31 17:42:48 +02:00
$this->passwordService = $passwordService;
$this->emailService = $emailService;
$this->fileService = $fileService;
2014-08-31 17:42:48 +02:00
$this->timeService = $timeService;
2014-08-31 09:03:11 +02:00
}
2014-09-04 19:07:57 +02:00
public function getByName($name)
{
return $this->userDao->getByName($name);
}
public function getFiltered(\Szurubooru\FormData\SearchFormData $formData)
{
$pageSize = intval($this->config->users->usersPerPage);
$this->validator->validateNumber($formData->page);
$searchFilter = new \Szurubooru\Dao\SearchFilter($pageSize, $formData);
return $this->userSearchService->getFiltered($searchFilter);
}
public function createUser(\Szurubooru\FormData\RegistrationFormData $formData)
2014-08-31 09:03:11 +02:00
{
$this->validator->validateUserName($formData->userName);
$this->validator->validatePassword($formData->password);
$this->validator->validateEmail($formData->email);
2014-08-31 09:03:11 +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();
$user->name = $formData->userName;
2014-08-31 17:42:48 +02:00
$user->email = $formData->email;
$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;
$user->avatarStyle = \Szurubooru\Entities\User::AVATAR_STYLE_GRAVATAR;
2014-08-31 17:42:48 +02:00
$this->sendActivationMailIfNeeded($user);
2014-08-31 09:03:11 +02:00
return $this->userDao->save($user);
}
2014-09-05 13:50:51 +02:00
public function updateUser($userName, \Szurubooru\FormData\UserEditFormData $formData)
2014-09-05 13:50:51 +02:00
{
$user = $this->getByName($userName);
if (!$user)
throw new \InvalidArgumentException('User with name "' . $userName . '" was not found.');
if ($formData->avatarStyle !== null)
{
$user->avatarStyle = \Szurubooru\Helpers\EnumHelper::avatarStyleFromString($formData->avatarStyle);
if ($formData->avatarContent)
$this->fileService->saveFromBase64($formData->avatarContent, $this->getCustomAvatarSourcePath($user));
}
if ($formData->userName !== null and $formData->userName != $user->name)
{
$this->validator->validateUserName($formData->userName);
if ($this->userDao->getByName($formData->userName))
throw new \DomainException('User with this name already exists.');
$user->name = $formData->userName;
}
if ($formData->password !== null)
{
$this->validator->validatePassword($formData->password);
$user->passwordHash = $this->passwordService->getHash($formData->password);
}
if ($formData->email !== null)
{
$this->validator->validateEmail($formData->email);
$user->email = $formData->email;
}
if ($formData->accessRank !== null)
{
$user->accessRank = \Szurubooru\Helpers\EnumHelper::accessRankFromString($formData->accessRank);
}
2014-09-07 14:50:16 +02:00
if ($formData->browsingSettings !== null)
{
if (!is_string($formData->browsingSettings))
throw new \InvalidArgumentException('Browsing settings must be stringified JSON.');
if (strlen($formData->browsingSettings) > 2000)
throw new \InvalidArgumentException('Stringified browsing settings can have at most 2000 characters.');
$user->browsingSettings = $formData->browsingSettings;
}
if ($formData->email !== null)
$this->sendActivationMailIfNeeded($user);
return $this->userDao->save($user);
}
public function deleteUserByName($userName)
{
$user = $this->getByName($userName);
if (!$user)
throw new \InvalidArgumentException('User with name "' . $userName . '" was not found.');
$this->userDao->deleteByName($userName);
$this->fileService->delete($this->getCustomAvatarSourcePath($user));
2014-09-05 13:50:51 +02:00
return true;
}
public function getCustomAvatarSourcePath($user)
{
return 'avatars' . DIRECTORY_SEPARATOR . $user->id;
}
public function getBlankAvatarSourcePath()
{
return 'avatars' . DIRECTORY_SEPARATOR . 'blank.png';
}
private function sendActivationMailIfNeeded(\Szurubooru\Entities\User &$user)
{
//todo
}
2014-08-31 09:03:11 +02:00
}