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;
|
|
|
|
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,
|
|
|
|
\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;
|
|
|
|
$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-08-31 17:42:48 +02:00
|
|
|
public function register(\Szurubooru\FormData\RegistrationFormData $formData)
|
2014-08-31 09:03:11 +02:00
|
|
|
{
|
2014-09-02 09:09:07 +02:00
|
|
|
$this->validator->validateUserName($formData->name);
|
|
|
|
$this->validator->validatePassword($formData->password);
|
|
|
|
$this->validator->validateEmail($formData->email);
|
2014-08-31 09:03:11 +02:00
|
|
|
|
2014-08-31 17:42:48 +02:00
|
|
|
if ($this->userDao->getByName($formData->name))
|
|
|
|
throw new \DomainException('User with this name already exists.');
|
|
|
|
|
|
|
|
$user = new \Szurubooru\Entities\User();
|
|
|
|
$user->name = $formData->name;
|
|
|
|
$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-08-31 17:42:48 +02:00
|
|
|
|
|
|
|
//todo: send activation mail if necessary
|
2014-08-31 09:03:11 +02:00
|
|
|
|
|
|
|
return $this->userDao->save($user);
|
|
|
|
}
|
2014-09-05 13:50:51 +02:00
|
|
|
|
|
|
|
public function deleteByName($name)
|
|
|
|
{
|
|
|
|
$this->userDao->deleteByName($name);
|
|
|
|
return true;
|
|
|
|
}
|
2014-08-31 09:03:11 +02:00
|
|
|
}
|