szurubooru/src/Services/UserService.php

68 lines
2.2 KiB
PHP
Raw Normal View History

2014-08-31 09:03:11 +02:00
<?php
namespace Szurubooru\Services;
class UserService
{
2014-08-31 14:07:46 +02:00
private $config;
2014-08-31 17:42:48 +02:00
private $userDao;
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-08-31 17:42:48 +02:00
\Szurubooru\Config $config,
2014-08-31 14:07:46 +02:00
\Szurubooru\Dao\UserDao $userDao,
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-08-31 14:07:46 +02:00
$this->config = $config;
2014-08-31 17:42:48 +02:00
$this->userDao = $userDao;
$this->passwordService = $passwordService;
$this->emailService = $emailService;
$this->timeService = $timeService;
2014-08-31 09:03:11 +02:00
}
2014-08-31 17:42:48 +02:00
public function register(\Szurubooru\FormData\RegistrationFormData $formData)
2014-08-31 09:03:11 +02:00
{
2014-08-31 17:42:48 +02:00
$this->validateUserName($formData->name);
$this->passwordService->validatePassword($formData->password);
$this->emailService->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.');
//todo: privilege checking
$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-01 19:43:49 +02:00
//todo: refactor this to generic validation
2014-08-31 17:42:48 +02:00
public function validateUserName(&$userName)
2014-08-31 14:07:46 +02:00
{
2014-08-31 17:42:48 +02:00
$userName = trim($userName);
2014-08-31 14:07:46 +02:00
if (!$userName)
throw new \DomainException('User name cannot be empty.');
$minUserNameLength = intval($this->config->users->minUserNameLength);
2014-09-01 19:43:49 +02:00
$maxUserNameLength = intval($this->config->users->maxUserNameLength);
2014-08-31 14:07:46 +02:00
if (strlen($userName) < $minUserNameLength)
throw new \DomainException('User name must have at least ' . $minUserNameLength . ' character(s).');
2014-09-01 19:43:49 +02:00
if (strlen($userName) > $maxUserNameLength)
throw new \DomainException('User name must have at most ' . $maxUserNameLength . ' character(s).');
2014-08-31 14:07:46 +02:00
}
2014-08-31 09:03:11 +02:00
}