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-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(
|
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-08-31 17:42:48 +02:00
|
|
|
\Szurubooru\Services\TimeService $timeService)
|
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-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);
|
|
|
|
}
|
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
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-07 00:33:46 +02:00
|
|
|
$this->validator->validateUserName($formData->userName);
|
2014-09-02 09:09:07 +02:00
|
|
|
$this->validator->validatePassword($formData->password);
|
|
|
|
$this->validator->validateEmail($formData->email);
|
2014-08-31 09:03:11 +02:00
|
|
|
|
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-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;
|
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-07 00:33:46 +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
|
|
|
|
2014-09-07 00:33:46 +02:00
|
|
|
public function updateUser($userName, \Szurubooru\FormData\UserEditFormData $formData)
|
2014-09-05 13:50:51 +02:00
|
|
|
{
|
2014-09-07 00:33:46 +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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2014-09-07 00:33:46 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|