]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ApiBundle/Controller/UserRestController.php
Merge pull request #2708 from jcharaoui/import-disablecontentupdate
[github/wallabag/wallabag.git] / src / Wallabag / ApiBundle / Controller / UserRestController.php
CommitLineData
22510459
TC
1<?php
2
3namespace Wallabag\ApiBundle\Controller;
4
5use FOS\UserBundle\Event\UserEvent;
6use FOS\UserBundle\FOSUserEvents;
7use JMS\Serializer\SerializationContext;
8use Nelmio\ApiDocBundle\Annotation\ApiDoc;
5709ecb3 9use Symfony\Component\HttpFoundation\Request;
22510459 10use Symfony\Component\HttpFoundation\JsonResponse;
5709ecb3 11use Wallabag\UserBundle\Entity\User;
22510459
TC
12
13class UserRestController extends WallabagRestController
14{
15 /**
5709ecb3 16 * Retrieve current logged in user informations.
22510459
TC
17 *
18 * @ApiDoc()
19 *
20 * @return JsonResponse
21 */
22 public function getUserAction()
23 {
24 $this->validateAuthentication();
25
5709ecb3 26 return $this->sendUser($this->getUser());
22510459
TC
27 }
28
29 /**
5709ecb3 30 * Register an user.
22510459
TC
31 *
32 * @ApiDoc(
33 * requirements={
34 * {"name"="username", "dataType"="string", "required"=true, "description"="The user's username"},
5709ecb3 35 * {"name"="password", "dataType"="string", "required"=true, "description"="The user's password"},
22510459
TC
36 * {"name"="email", "dataType"="string", "required"=true, "description"="The user's email"}
37 * }
38 * )
5709ecb3
JB
39 *
40 * @todo Make this method (or the whole API) accessible only through https
41 *
22510459
TC
42 * @return JsonResponse
43 */
5709ecb3 44 public function putUserAction(Request $request)
22510459
TC
45 {
46 if (!$this->container->getParameter('fosuser_registration')) {
47 $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json');
5709ecb3 48
22510459
TC
49 return (new JsonResponse())->setJson($json)->setStatusCode(403);
50 }
51
5709ecb3
JB
52 $userManager = $this->get('fos_user.user_manager');
53 $user = $userManager->createUser();
54 // enable created user by default
55 $user->setEnabled(true);
22510459 56
5709ecb3
JB
57 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [
58 'csrf_protection' => false,
59 ]);
22510459 60
5709ecb3
JB
61 // simulate form submission
62 $form->submit([
63 'username' => $request->request->get('username'),
64 'plainPassword' => [
65 'first' => $request->request->get('password'),
66 'second' => $request->request->get('password'),
67 ],
68 'email' => $request->request->get('email'),
69 ]);
22510459 70
5709ecb3
JB
71 if ($form->isSubmitted() && false === $form->isValid()) {
72 $view = $this->view($form, 400);
73 $view->setFormat('json');
22510459 74
5709ecb3
JB
75 // handle errors in a more beautiful way than the default view
76 $data = json_decode($this->handleView($view)->getContent(), true)['children'];
77 $errors = [];
22510459 78
5709ecb3
JB
79 if (isset($data['username']['errors'])) {
80 $errors['username'] = $this->translateErrors($data['username']['errors']);
81 }
22510459 82
5709ecb3
JB
83 if (isset($data['email']['errors'])) {
84 $errors['email'] = $this->translateErrors($data['email']['errors']);
85 }
22510459 86
5709ecb3
JB
87 if (isset($data['plainPassword']['children']['first']['errors'])) {
88 $errors['password'] = $this->translateErrors($data['plainPassword']['children']['first']['errors']);
89 }
22510459 90
5709ecb3 91 $json = $this->get('serializer')->serialize(['error' => $errors], 'json');
22510459 92
5709ecb3
JB
93 return (new JsonResponse())->setJson($json)->setStatusCode(400);
94 }
22510459 95
5709ecb3 96 $userManager->updateUser($user);
22510459
TC
97
98 // dispatch a created event so the associated config will be created
5709ecb3 99 $event = new UserEvent($user, $request);
22510459
TC
100 $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
101
5709ecb3
JB
102 return $this->sendUser($user);
103 }
22510459 104
5709ecb3
JB
105 /**
106 * Send user response.
107 *
108 * @param User $user
109 *
110 * @return JsonResponse
111 */
112 private function sendUser(User $user)
113 {
114 $json = $this->get('serializer')->serialize(
115 $user,
116 'json',
117 SerializationContext::create()->setGroups(['user_api'])
118 );
22510459 119
5709ecb3 120 return (new JsonResponse())->setJson($json);
22510459
TC
121 }
122
5709ecb3
JB
123 /**
124 * Translate errors message.
125 *
126 * @param array $errors
127 *
128 * @return array
129 */
130 private function translateErrors($errors)
131 {
132 $translatedErrors = [];
133 foreach ($errors as $error) {
134 $translatedErrors[] = $this->get('translator')->trans($error);
135 }
136
137 return $translatedErrors;
138 }
22510459 139}