2017-04-29 17:22:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\ApiBundle\Controller;
|
|
|
|
|
|
|
|
use FOS\UserBundle\Event\UserEvent;
|
|
|
|
use FOS\UserBundle\FOSUserEvents;
|
|
|
|
use JMS\Serializer\SerializationContext;
|
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
|
2017-05-30 05:56:01 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2017-04-29 17:22:50 +00:00
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse;
|
2017-05-30 05:56:01 +00:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2017-04-29 17:22:50 +00:00
|
|
|
|
|
|
|
class UserRestController extends WallabagRestController
|
|
|
|
{
|
|
|
|
/**
|
2017-05-30 05:56:01 +00:00
|
|
|
* Retrieve current logged in user informations.
|
2017-04-29 17:22:50 +00:00
|
|
|
*
|
|
|
|
* @ApiDoc()
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
public function getUserAction()
|
|
|
|
{
|
|
|
|
$this->validateAuthentication();
|
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
return $this->sendUser($this->getUser());
|
2017-04-29 17:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-30 05:56:01 +00:00
|
|
|
* Register an user.
|
2017-04-29 17:22:50 +00:00
|
|
|
*
|
|
|
|
* @ApiDoc(
|
|
|
|
* requirements={
|
|
|
|
* {"name"="username", "dataType"="string", "required"=true, "description"="The user's username"},
|
2017-05-30 05:56:01 +00:00
|
|
|
* {"name"="password", "dataType"="string", "required"=true, "description"="The user's password"},
|
2017-04-29 17:22:50 +00:00
|
|
|
* {"name"="email", "dataType"="string", "required"=true, "description"="The user's email"}
|
|
|
|
* }
|
|
|
|
* )
|
2017-05-30 05:56:01 +00:00
|
|
|
*
|
|
|
|
* @todo Make this method (or the whole API) accessible only through https
|
|
|
|
*
|
2017-04-29 17:22:50 +00:00
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
2017-05-30 05:56:01 +00:00
|
|
|
public function putUserAction(Request $request)
|
2017-04-29 17:22:50 +00:00
|
|
|
{
|
2017-06-02 08:19:33 +00:00
|
|
|
if (!$this->getParameter('fosuser_registration') || !$this->get('craue_config')->get('api_user_registration')) {
|
2017-04-29 17:22:50 +00:00
|
|
|
$json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json');
|
2017-05-30 05:56:01 +00:00
|
|
|
|
2017-04-29 17:22:50 +00:00
|
|
|
return (new JsonResponse())->setJson($json)->setStatusCode(403);
|
|
|
|
}
|
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
$userManager = $this->get('fos_user.user_manager');
|
|
|
|
$user = $userManager->createUser();
|
2017-06-02 08:19:33 +00:00
|
|
|
// user will be disabled BY DEFAULT to avoid spamming account to be created
|
|
|
|
$user->setEnabled(false);
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
$form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [
|
|
|
|
'csrf_protection' => false,
|
|
|
|
]);
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
// simulate form submission
|
|
|
|
$form->submit([
|
|
|
|
'username' => $request->request->get('username'),
|
|
|
|
'plainPassword' => [
|
|
|
|
'first' => $request->request->get('password'),
|
|
|
|
'second' => $request->request->get('password'),
|
|
|
|
],
|
|
|
|
'email' => $request->request->get('email'),
|
|
|
|
]);
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
if ($form->isSubmitted() && false === $form->isValid()) {
|
|
|
|
$view = $this->view($form, 400);
|
|
|
|
$view->setFormat('json');
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
// handle errors in a more beautiful way than the default view
|
|
|
|
$data = json_decode($this->handleView($view)->getContent(), true)['children'];
|
|
|
|
$errors = [];
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
if (isset($data['username']['errors'])) {
|
|
|
|
$errors['username'] = $this->translateErrors($data['username']['errors']);
|
|
|
|
}
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
if (isset($data['email']['errors'])) {
|
|
|
|
$errors['email'] = $this->translateErrors($data['email']['errors']);
|
|
|
|
}
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
if (isset($data['plainPassword']['children']['first']['errors'])) {
|
|
|
|
$errors['password'] = $this->translateErrors($data['plainPassword']['children']['first']['errors']);
|
|
|
|
}
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
$json = $this->get('serializer')->serialize(['error' => $errors], 'json');
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
return (new JsonResponse())->setJson($json)->setStatusCode(400);
|
|
|
|
}
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
$userManager->updateUser($user);
|
2017-04-29 17:22:50 +00:00
|
|
|
|
|
|
|
// dispatch a created event so the associated config will be created
|
2017-05-30 05:56:01 +00:00
|
|
|
$event = new UserEvent($user, $request);
|
2017-04-29 17:22:50 +00:00
|
|
|
$this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
|
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
return $this->sendUser($user);
|
|
|
|
}
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
/**
|
|
|
|
* Send user response.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*
|
|
|
|
* @return JsonResponse
|
|
|
|
*/
|
|
|
|
private function sendUser(User $user)
|
|
|
|
{
|
|
|
|
$json = $this->get('serializer')->serialize(
|
|
|
|
$user,
|
|
|
|
'json',
|
|
|
|
SerializationContext::create()->setGroups(['user_api'])
|
|
|
|
);
|
2017-04-29 17:22:50 +00:00
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
return (new JsonResponse())->setJson($json);
|
2017-04-29 17:22:50 +00:00
|
|
|
}
|
|
|
|
|
2017-05-30 05:56:01 +00:00
|
|
|
/**
|
|
|
|
* Translate errors message.
|
|
|
|
*
|
|
|
|
* @param array $errors
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function translateErrors($errors)
|
|
|
|
{
|
|
|
|
$translatedErrors = [];
|
|
|
|
foreach ($errors as $error) {
|
|
|
|
$translatedErrors[] = $this->get('translator')->trans($error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $translatedErrors;
|
|
|
|
}
|
2017-04-29 17:22:50 +00:00
|
|
|
}
|