2014-08-31 13:34:31 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Controllers;
|
|
|
|
|
|
|
|
final class UserController extends AbstractController
|
|
|
|
{
|
2014-08-31 14:07:46 +02:00
|
|
|
private $inputReader;
|
2014-08-31 17:42:48 +02:00
|
|
|
private $userService;
|
2014-08-31 13:34:31 +02:00
|
|
|
|
2014-08-31 14:07:46 +02:00
|
|
|
public function __construct(
|
|
|
|
\Szurubooru\Services\UserService $userService,
|
|
|
|
\Szurubooru\Helpers\InputReader $inputReader)
|
2014-08-31 13:34:31 +02:00
|
|
|
{
|
2014-08-31 14:07:46 +02:00
|
|
|
$this->inputReader = $inputReader;
|
2014-08-31 13:34:31 +02:00
|
|
|
$this->userService = $userService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function registerRoutes(\Szurubooru\Router $router)
|
|
|
|
{
|
2014-08-31 17:42:48 +02:00
|
|
|
$router->post('/api/users', [$this, 'register']);
|
2014-09-03 19:07:53 +02:00
|
|
|
$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-03 19:07:53 +02:00
|
|
|
public function getFiltered()
|
|
|
|
{
|
2014-09-04 19:07:57 +02:00
|
|
|
//todo: privilege checking
|
2014-09-03 19:07:53 +02:00
|
|
|
//todo: move this to form data constructor
|
|
|
|
$searchFormData = new \Szurubooru\FormData\SearchFormData;
|
|
|
|
$searchFormData->query = $this->inputReader->query;
|
|
|
|
$searchFormData->order = $this->inputReader->order;
|
|
|
|
$searchFormData->pageNumber = $this->inputReader->page;
|
|
|
|
$searchResult = $this->userService->getFiltered($searchFormData);
|
|
|
|
$entities = array_map(function($user) { return new \Szurubooru\ViewProxies\User($user); }, $searchResult->entities);
|
|
|
|
return [
|
|
|
|
'data' => $entities,
|
|
|
|
'pageSize' => $searchResult->filter->pageSize,
|
|
|
|
'totalRecords' => $searchResult->totalRecords];
|
|
|
|
}
|
|
|
|
|
2014-09-04 19:07:57 +02:00
|
|
|
public function getByName($name)
|
2014-09-03 19:07:53 +02:00
|
|
|
{
|
2014-09-04 19:07:57 +02:00
|
|
|
//todo: privilege checking
|
|
|
|
$user = $this->userService->getByName($name);
|
|
|
|
if (!$user)
|
|
|
|
throw new \DomainException('User with name "' . $name . '" was not found.');
|
|
|
|
return new \Szurubooru\ViewProxies\User($user);
|
2014-09-03 19:07:53 +02:00
|
|
|
}
|
|
|
|
|
2014-08-31 17:42:48 +02:00
|
|
|
public function register()
|
2014-08-31 13:34:31 +02:00
|
|
|
{
|
2014-09-04 19:07:57 +02:00
|
|
|
//todo: privilege checking
|
2014-08-31 17:42:48 +02:00
|
|
|
$input = new \Szurubooru\FormData\RegistrationFormData;
|
2014-09-03 19:07:53 +02:00
|
|
|
//todo: move this to form data constructor
|
2014-08-31 17:42:48 +02:00
|
|
|
$input->name = $this->inputReader->userName;
|
|
|
|
$input->password = $this->inputReader->password;
|
|
|
|
$input->email = $this->inputReader->email;
|
|
|
|
$user = $this->userService->register($input);
|
|
|
|
return new \Szurubooru\ViewProxies\User($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
|
|
|
{
|
|
|
|
throw new \BadMethodCallException('Not implemented');
|
|
|
|
}
|
|
|
|
}
|