This repository has been archived on 2025-02-26. You can view files and clone it, but cannot push or open issues or pull requests.
szurubooru/src/Controllers/UserController.php

78 lines
2.4 KiB
PHP
Raw Normal View History

2014-08-31 13:34:31 +02:00
<?php
namespace Szurubooru\Controllers;
final class UserController extends AbstractController
{
2014-09-04 19:57:06 +02:00
private $authService;
2014-08-31 17:42:48 +02:00
private $userService;
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-09-04 19:57:06 +02:00
\Szurubooru\Services\AuthService $authService,
2014-08-31 14:07:46 +02:00
\Szurubooru\Services\UserService $userService,
2014-09-05 19:18:49 +02:00
\Szurubooru\Helpers\InputReader $inputReader,
\Szurubooru\Controllers\ViewProxies\UserViewProxy $userViewProxy)
2014-08-31 13:34:31 +02:00
{
2014-09-04 19:57:06 +02:00
$this->authService = $authService;
2014-08-31 13:34:31 +02:00
$this->userService = $userService;
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
}
public function registerRoutes(\Szurubooru\Router $router)
{
2014-08-31 17:42:48 +02:00
$router->post('/api/users', [$this, 'register']);
$router->get('/api/users', [$this, 'getFiltered']);
2014-09-04 19:07:57 +02:00
$router->get('/api/users/:name', [$this, 'getByName']);
$router->put('/api/users/:name', [$this, 'update']);
$router->delete('/api/users/:name', [$this, 'delete']);
2014-08-31 13:34:31 +02:00
}
2014-09-05 13:50:51 +02:00
public function getByName($name)
{
$this->authService->assertPrivilege(\Szurubooru\Privilege::PRIVILEGE_VIEW_USER);
$user = $this->userService->getByName($name);
if (!$user)
throw new \DomainException('User with name "' . $name . '" was not found.');
2014-09-05 19:18:49 +02:00
return $this->userViewProxy->fromEntity($user);
2014-09-05 13:50:51 +02:00
}
public function getFiltered()
{
2014-09-04 19:57:06 +02:00
$this->authService->assertPrivilege(\Szurubooru\Privilege::PRIVILEGE_LIST_USERS);
2014-09-05 19:18:49 +02:00
$searchFormData = new \Szurubooru\FormData\SearchFormData($this->inputReader);
$searchResult = $this->userService->getFiltered($searchFormData);
2014-09-05 19:18:49 +02:00
$entities = $this->userViewProxy->fromArray($searchResult->entities);
return [
'data' => $entities,
'pageSize' => $searchResult->filter->pageSize,
'totalRecords' => $searchResult->totalRecords];
}
2014-08-31 17:42:48 +02:00
public function register()
2014-08-31 13:34:31 +02:00
{
2014-09-04 19:57:06 +02:00
$this->authService->assertPrivilege(\Szurubooru\Privilege::PRIVILEGE_REGISTER);
2014-09-05 19:18:49 +02:00
$input = new \Szurubooru\FormData\RegistrationFormData($this->inputReader);
2014-08-31 17:42:48 +02:00
$user = $this->userService->register($input);
2014-09-05 19:18:49 +02:00
return $this->userViewProxy->fromEntity($user);
2014-08-31 13:34:31 +02:00
}
2014-09-04 19:07:57 +02:00
public function update($name)
2014-08-31 13:34:31 +02:00
{
throw new \BadMethodCallException('Not implemented');
}
2014-09-04 19:07:57 +02:00
public function delete($name)
2014-08-31 13:34:31 +02:00
{
2014-09-05 13:50:51 +02:00
if ($name == $this->authService->getLoggedInUser()->name)
$this->authService->assertPrivilege(\Szurubooru\Privilege::PRIVILEGE_DELETE_OWN_ACCOUNT);
else
$this->authService->assertPrivilege(\Szurubooru\Privilege::PRIVILEGE_DELETE_ACCOUNTS);
return $this->userService->deleteByName($name);
2014-08-31 13:34:31 +02:00
}
}