3 namespace Wallabag\ApiBundle\Controller
;
5 use FOS\UserBundle\Event\UserEvent
;
6 use FOS\UserBundle\FOSUserEvents
;
7 use JMS\Serializer\SerializationContext
;
8 use Nelmio\ApiDocBundle\Annotation\ApiDoc
;
9 use Symfony\Component\HttpFoundation\Request
;
10 use Symfony\Component\HttpFoundation\JsonResponse
;
11 use Wallabag\UserBundle\Entity\User
;
13 class UserRestController
extends WallabagRestController
16 * Retrieve current logged in user informations.
20 * @return JsonResponse
22 public function getUserAction()
24 $this->validateAuthentication();
26 return $this->sendUser($this->getUser());
34 * {"name"="username", "dataType"="string", "required"=true, "description"="The user's username"},
35 * {"name"="password", "dataType"="string", "required"=true, "description"="The user's password"},
36 * {"name"="email", "dataType"="string", "required"=true, "description"="The user's email"}
40 * @todo Make this method (or the whole API) accessible only through https
42 * @return JsonResponse
44 public function putUserAction(Request
$request)
46 if (!$this->container
->getParameter('fosuser_registration')) {
47 $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json');
49 return (new JsonResponse())->setJson($json)->setStatusCode(403);
52 $userManager = $this->get('fos_user.user_manager');
53 $user = $userManager->createUser();
54 // enable created user by default
55 $user->setEnabled(true);
57 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [
58 'csrf_protection' => false,
61 // simulate form submission
63 'username' => $request->request
->get('username'),
65 'first' => $request->request
->get('password'),
66 'second' => $request->request
->get('password'),
68 'email' => $request->request
->get('email'),
71 if ($form->isSubmitted() && false === $form->isValid()) {
72 $view = $this->view($form, 400);
73 $view->setFormat('json');
75 // handle errors in a more beautiful way than the default view
76 $data = json_decode($this->handleView($view)->getContent(), true)['children'];
79 if (isset($data['username']['errors'])) {
80 $errors['username'] = $this->translateErrors($data['username']['errors']);
83 if (isset($data['email']['errors'])) {
84 $errors['email'] = $this->translateErrors($data['email']['errors']);
87 if (isset($data['plainPassword']['children']['first']['errors'])) {
88 $errors['password'] = $this->translateErrors($data['plainPassword']['children']['first']['errors']);
91 $json = $this->get('serializer')->serialize(['error' => $errors], 'json');
93 return (new JsonResponse())->setJson($json)->setStatusCode(400);
96 $userManager->updateUser($user);
98 // dispatch a created event so the associated config will be created
99 $event = new UserEvent($user, $request);
100 $this->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
102 return $this->sendUser($user);
106 * Send user response.
110 * @return JsonResponse
112 private function sendUser(User
$user)
114 $json = $this->get('serializer')->serialize(
117 SerializationContext
::create()->setGroups(['user_api'])
120 return (new JsonResponse())->setJson($json);
124 * Translate errors message.
126 * @param array $errors
130 private function translateErrors($errors)
132 $translatedErrors = [];
133 foreach ($errors as $error) {
134 $translatedErrors[] = $this->get('translator')->trans($error);
137 return $translatedErrors;