2014-08-31 13:34:31 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Controllers;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Config;
|
|
|
|
use Szurubooru\Controllers\ViewProxies\UserViewProxy;
|
|
|
|
use Szurubooru\FormData\RegistrationFormData;
|
|
|
|
use Szurubooru\FormData\UserEditFormData;
|
|
|
|
use Szurubooru\Helpers\InputReader;
|
|
|
|
use Szurubooru\Privilege;
|
|
|
|
use Szurubooru\Router;
|
|
|
|
use Szurubooru\SearchServices\Parsers\UserSearchParser;
|
|
|
|
use Szurubooru\Services\PrivilegeService;
|
|
|
|
use Szurubooru\Services\TokenService;
|
|
|
|
use Szurubooru\Services\UserService;
|
2014-08-31 13:34:31 +02:00
|
|
|
|
|
|
|
final class UserController extends AbstractController
|
|
|
|
{
|
2014-09-26 19:14:34 +02:00
|
|
|
private $config;
|
2014-09-06 10:00:26 +02:00
|
|
|
private $privilegeService;
|
2014-08-31 17:42:48 +02:00
|
|
|
private $userService;
|
2014-09-09 19:38:16 +02:00
|
|
|
private $tokenService;
|
2014-09-26 19:14:34 +02:00
|
|
|
private $userSearchParser;
|
2014-09-04 19:57:06 +02:00
|
|
|
private $inputReader;
|
2014-09-05 19:18:49 +02:00
|
|
|
private $userViewProxy;
|
2014-08-31 13:34:31 +02:00
|
|
|
|
2014-08-31 14:07:46 +02:00
|
|
|
public function __construct(
|
2014-10-08 14:47:47 +02:00
|
|
|
Config $config,
|
|
|
|
PrivilegeService $privilegeService,
|
|
|
|
UserService $userService,
|
|
|
|
TokenService $tokenService,
|
|
|
|
UserSearchParser $userSearchParser,
|
|
|
|
InputReader $inputReader,
|
|
|
|
UserViewProxy $userViewProxy)
|
2014-08-31 13:34:31 +02:00
|
|
|
{
|
2014-09-26 19:14:34 +02:00
|
|
|
$this->config = $config;
|
2014-09-06 10:00:26 +02:00
|
|
|
$this->privilegeService = $privilegeService;
|
2014-08-31 13:34:31 +02:00
|
|
|
$this->userService = $userService;
|
2014-09-09 19:38:16 +02:00
|
|
|
$this->tokenService = $tokenService;
|
2014-09-26 19:14:34 +02:00
|
|
|
$this->userSearchParser = $userSearchParser;
|
2014-09-04 19:57:06 +02:00
|
|
|
$this->inputReader = $inputReader;
|
2014-09-05 19:18:49 +02:00
|
|
|
$this->userViewProxy = $userViewProxy;
|
2014-08-31 13:34:31 +02:00
|
|
|
}
|
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
public function registerRoutes(Router $router)
|
2014-08-31 13:34:31 +02:00
|
|
|
{
|
2014-09-07 00:33:46 +02:00
|
|
|
$router->post('/api/users', [$this, 'createUser']);
|
2014-09-03 19:07:53 +02:00
|
|
|
$router->get('/api/users', [$this, 'getFiltered']);
|
2014-09-08 13:06:32 +02:00
|
|
|
$router->get('/api/users/:userNameOrEmail', [$this, 'getByNameOrEmail']);
|
|
|
|
$router->put('/api/users/:userNameOrEmail', [$this, 'updateUser']);
|
|
|
|
$router->delete('/api/users/:userNameOrEmail', [$this, 'deleteUser']);
|
|
|
|
$router->post('/api/password-reset/:userNameOrEmail', [$this, 'passwordReset']);
|
|
|
|
$router->post('/api/finish-password-reset/:tokenName', [$this, 'finishPasswordReset']);
|
|
|
|
$router->post('/api/activation/:userNameOrEmail', [$this, 'activation']);
|
|
|
|
$router->post('/api/finish-activation/:tokenName', [$this, 'finishActivation']);
|
2014-08-31 13:34:31 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
public function getByNameOrEmail($userNameOrEmail)
|
2014-09-05 13:50:51 +02:00
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->privilegeService->assertPrivilege(Privilege::VIEW_USERS);
|
2014-09-08 13:06:32 +02:00
|
|
|
$user = $this->userService->getByNameOrEmail($userNameOrEmail);
|
2014-09-05 19:18:49 +02:00
|
|
|
return $this->userViewProxy->fromEntity($user);
|
2014-09-05 13:50:51 +02:00
|
|
|
}
|
|
|
|
|
2014-09-03 19:07:53 +02:00
|
|
|
public function getFiltered()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->privilegeService->assertPrivilege(Privilege::LIST_USERS);
|
2014-09-04 19:57:06 +02:00
|
|
|
|
2014-09-26 19:14:34 +02:00
|
|
|
$filter = $this->userSearchParser->createFilterFromInputReader($this->inputReader);
|
|
|
|
$filter->setPageSize($this->config->users->usersPerPage);
|
|
|
|
$result = $this->userService->getFiltered($filter);
|
|
|
|
$entities = $this->userViewProxy->fromArray($result->getEntities());
|
2014-09-03 19:07:53 +02:00
|
|
|
return [
|
|
|
|
'data' => $entities,
|
2014-09-26 19:14:34 +02:00
|
|
|
'pageSize' => $result->getPageSize(),
|
|
|
|
'totalRecords' => $result->getTotalRecords()];
|
2014-09-03 19:07:53 +02:00
|
|
|
}
|
|
|
|
|
2014-09-07 00:33:46 +02:00
|
|
|
public function createUser()
|
2014-08-31 13:34:31 +02:00
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->privilegeService->assertPrivilege(Privilege::REGISTER);
|
|
|
|
$formData = new RegistrationFormData($this->inputReader);
|
2014-09-07 00:33:46 +02:00
|
|
|
$user = $this->userService->createUser($formData);
|
2014-09-10 18:06:49 +02:00
|
|
|
return $this->userViewProxy->fromEntity($user);
|
2014-08-31 13:34:31 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
public function updateUser($userNameOrEmail)
|
2014-08-31 13:34:31 +02:00
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
$user = $this->userService->getByNameOrEmail($userNameOrEmail);
|
2014-10-08 14:47:47 +02:00
|
|
|
$formData = new UserEditFormData($this->inputReader);
|
2014-09-07 00:33:46 +02:00
|
|
|
|
2014-09-14 18:30:15 +02:00
|
|
|
if ($formData->avatarStyle !== null || $formData->avatarContent !== null)
|
2014-09-07 00:33:46 +02:00
|
|
|
{
|
|
|
|
$this->privilegeService->assertPrivilege(
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->privilegeService->isLoggedIn($userNameOrEmail)
|
2014-10-08 14:47:47 +02:00
|
|
|
? Privilege::CHANGE_OWN_AVATAR_STYLE
|
|
|
|
: Privilege::CHANGE_ALL_AVATAR_STYLES);
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($formData->userName !== null)
|
|
|
|
{
|
|
|
|
$this->privilegeService->assertPrivilege(
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->privilegeService->isLoggedIn($userNameOrEmail)
|
2014-10-08 14:47:47 +02:00
|
|
|
? Privilege::CHANGE_OWN_NAME
|
|
|
|
: Privilege::CHANGE_ALL_NAMES);
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($formData->password !== null)
|
|
|
|
{
|
|
|
|
$this->privilegeService->assertPrivilege(
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->privilegeService->isLoggedIn($userNameOrEmail)
|
2014-10-08 14:47:47 +02:00
|
|
|
? Privilege::CHANGE_OWN_PASSWORD
|
|
|
|
: Privilege::CHANGE_ALL_PASSWORDS);
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($formData->email !== null)
|
|
|
|
{
|
|
|
|
$this->privilegeService->assertPrivilege(
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->privilegeService->isLoggedIn($userNameOrEmail)
|
2014-10-08 14:47:47 +02:00
|
|
|
? Privilege::CHANGE_OWN_EMAIL_ADDRESS
|
|
|
|
: Privilege::CHANGE_ALL_EMAIL_ADDRESSES);
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($formData->accessRank)
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$this->privilegeService->assertPrivilege(Privilege::CHANGE_ACCESS_RANK);
|
2014-09-07 00:33:46 +02:00
|
|
|
}
|
|
|
|
|
2014-09-07 14:50:16 +02:00
|
|
|
if ($formData->browsingSettings)
|
|
|
|
{
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->privilegeService->assertLoggedIn($userNameOrEmail);
|
2014-09-07 14:50:16 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
$user = $this->userService->updateUser($user, $formData);
|
2014-09-07 00:33:46 +02:00
|
|
|
return $this->userViewProxy->fromEntity($user);
|
2014-08-31 13:34:31 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
public function deleteUser($userNameOrEmail)
|
2014-08-31 13:34:31 +02:00
|
|
|
{
|
2014-09-06 10:00:26 +02:00
|
|
|
$this->privilegeService->assertPrivilege(
|
2014-09-08 13:06:32 +02:00
|
|
|
$this->privilegeService->isLoggedIn($userNameOrEmail)
|
2014-10-08 14:47:47 +02:00
|
|
|
? Privilege::DELETE_OWN_ACCOUNT
|
|
|
|
: Privilege::DELETE_ACCOUNTS);
|
2014-09-06 10:00:26 +02:00
|
|
|
|
2014-09-08 13:06:32 +02:00
|
|
|
$user = $this->userService->getByNameOrEmail($userNameOrEmail);
|
|
|
|
return $this->userService->deleteUser($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function passwordReset($userNameOrEmail)
|
|
|
|
{
|
|
|
|
$user = $this->userService->getByNameOrEmail($userNameOrEmail);
|
|
|
|
return $this->userService->sendPasswordResetEmail($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function activation($userNameOrEmail)
|
|
|
|
{
|
|
|
|
$user = $this->userService->getByNameOrEmail($userNameOrEmail, true);
|
|
|
|
return $this->userService->sendActivationEmail($user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function finishPasswordReset($tokenName)
|
|
|
|
{
|
2014-09-09 19:38:16 +02:00
|
|
|
$token = $this->tokenService->getByName($tokenName);
|
|
|
|
return ['newPassword' => $this->userService->finishPasswordReset($token)];
|
2014-09-08 13:06:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function finishActivation($tokenName)
|
|
|
|
{
|
2014-09-09 19:38:16 +02:00
|
|
|
$token = $this->tokenService->getByName($tokenName);
|
|
|
|
$this->userService->finishActivation($token);
|
2014-08-31 13:34:31 +02:00
|
|
|
}
|
|
|
|
}
|