]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/ApiBundle/Controller/UserRestController.php
Merge pull request #3093 from aaa2000/annotation-error-on-save
[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 45 {
426bb453 46 if (!$this->getParameter('fosuser_registration') || !$this->get('craue_config')->get('api_user_registration')) {
22510459 47 $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json');
5709ecb3 48
a1e61874
JB
49 return (new JsonResponse())
50 ->setJson($json)
51 ->setStatusCode(JsonResponse::HTTP_FORBIDDEN);
22510459
TC
52 }
53
5709ecb3
JB
54 $userManager = $this->get('fos_user.user_manager');
55 $user = $userManager->createUser();
1b9cd917 56 // user will be disabled BY DEFAULT to avoid spamming account to be enabled
426bb453 57 $user->setEnabled(false);
22510459 58
5709ecb3
JB
59 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [
60 'csrf_protection' => false,
61 ]);
22510459 62
5709ecb3
JB
63 // simulate form submission
64 $form->submit([
65 'username' => $request->request->get('username'),
66 'plainPassword' => [
67 'first' => $request->request->get('password'),
68 'second' => $request->request->get('password'),
69 ],
70 'email' => $request->request->get('email'),
71 ]);
22510459 72
5709ecb3
JB
73 if ($form->isSubmitted() && false === $form->isValid()) {
74 $view = $this->view($form, 400);
75 $view->setFormat('json');
22510459 76
5709ecb3
JB
77 // handle errors in a more beautiful way than the default view
78 $data = json_decode($this->handleView($view)->getContent(), true)['children'];
79 $errors = [];
22510459 80
5709ecb3
JB
81 if (isset($data['username']['errors'])) {
82 $errors['username'] = $this->translateErrors($data['username']['errors']);
83 }
22510459 84
5709ecb3
JB
85 if (isset($data['email']['errors'])) {
86 $errors['email'] = $this->translateErrors($data['email']['errors']);
87 }
22510459 88
5709ecb3
JB
89 if (isset($data['plainPassword']['children']['first']['errors'])) {
90 $errors['password'] = $this->translateErrors($data['plainPassword']['children']['first']['errors']);
91 }
22510459 92
5709ecb3 93 $json = $this->get('serializer')->serialize(['error' => $errors], 'json');
22510459 94
a1e61874
JB
95 return (new JsonResponse())
96 ->setJson($json)
97 ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST);
5709ecb3 98 }
22510459 99
5709ecb3 100 $userManager->updateUser($user);
22510459
TC
101
102 // dispatch a created event so the associated config will be created
5709ecb3 103 $event = new UserEvent($user, $request);
22510459
TC
104 $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
105
a1e61874 106 return $this->sendUser($user, JsonResponse::HTTP_CREATED);
5709ecb3 107 }
22510459 108
5709ecb3
JB
109 /**
110 * Send user response.
111 *
112 * @param User $user
a1e61874 113 * @param int $status HTTP Status code to send
5709ecb3
JB
114 *
115 * @return JsonResponse
116 */
a1e61874 117 private function sendUser(User $user, $status = JsonResponse::HTTP_OK)
5709ecb3
JB
118 {
119 $json = $this->get('serializer')->serialize(
120 $user,
121 'json',
122 SerializationContext::create()->setGroups(['user_api'])
123 );
22510459 124
a1e61874
JB
125 return (new JsonResponse())
126 ->setJson($json)
127 ->setStatusCode($status);
22510459
TC
128 }
129
5709ecb3
JB
130 /**
131 * Translate errors message.
132 *
133 * @param array $errors
134 *
135 * @return array
136 */
137 private function translateErrors($errors)
138 {
139 $translatedErrors = [];
140 foreach ($errors as $error) {
141 $translatedErrors[] = $this->get('translator')->trans($error);
142 }
143
144 return $translatedErrors;
145 }
22510459 146}