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\JsonResponse
;
10 use Symfony\Component\HttpFoundation\Request
;
11 use Wallabag\ApiBundle\Entity\Client
;
12 use Wallabag\UserBundle\Entity\User
;
14 class UserRestController
extends WallabagRestController
17 * Retrieve current logged in user informations.
21 * @return JsonResponse
23 public function getUserAction()
25 $this->validateAuthentication();
27 return $this->sendUser($this->getUser());
31 * Register an user and create a client.
35 * {"name"="username", "dataType"="string", "required"=true, "description"="The user's username"},
36 * {"name"="password", "dataType"="string", "required"=true, "description"="The user's password"},
37 * {"name"="email", "dataType"="string", "required"=true, "description"="The user's email"},
38 * {"name"="client_name", "dataType"="string", "required"=true, "description"="The client name (to be used by your app)"}
42 * @todo Make this method (or the whole API) accessible only through https
44 * @return JsonResponse
46 public function putUserAction(Request
$request)
48 if (!$this->container
->getParameter('fosuser_registration') || !$this->get('craue_config')->get('api_user_registration')) {
49 $json = $this->get('jms_serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json');
51 return (new JsonResponse())
53 ->setStatusCode(JsonResponse
::HTTP_FORBIDDEN
);
56 $userManager = $this->get('fos_user.user_manager');
57 $user = $userManager->createUser();
58 // user will be disabled BY DEFAULT to avoid spamming account to be enabled
59 $user->setEnabled(false);
61 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [
62 'csrf_protection' => false,
65 // simulate form submission
67 'username' => $request->request
->get('username'),
69 'first' => $request->request
->get('password'),
70 'second' => $request->request
->get('password'),
72 'email' => $request->request
->get('email'),
75 if ($form->isSubmitted() && false === $form->isValid()) {
76 $view = $this->view($form, 400);
77 $view->setFormat('json');
79 // handle errors in a more beautiful way than the default view
80 $data = json_decode($this->handleView($view)->getContent(), true)['errors']['children'];
83 if (isset($data['username']['errors'])) {
84 $errors['username'] = $this->translateErrors($data['username']['errors']);
87 if (isset($data['email']['errors'])) {
88 $errors['email'] = $this->translateErrors($data['email']['errors']);
91 if (isset($data['plainPassword']['children']['first']['errors'])) {
92 $errors['password'] = $this->translateErrors($data['plainPassword']['children']['first']['errors']);
95 $json = $this->get('jms_serializer')->serialize(['error' => $errors], 'json');
97 return (new JsonResponse())
99 ->setStatusCode(JsonResponse
::HTTP_BAD_REQUEST
);
102 // create a default client
103 $client = new Client($user);
104 $client->setName($request->request
->get('client_name', 'Default client'));
106 $this->getDoctrine()->getManager()->persist($client);
108 $user->addClient($client);
110 $userManager->updateUser($user);
112 // dispatch a created event so the associated config will be created
113 $event = new UserEvent($user, $request);
114 $this->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
116 return $this->sendUser($user, 'user_api_with_client', JsonResponse
::HTTP_CREATED
);
120 * Send user response.
123 * @param string $group Used to define with serialized group might be used
124 * @param int $status HTTP Status code to send
126 * @return JsonResponse
128 private function sendUser(User
$user, $group = 'user_api', $status = JsonResponse
::HTTP_OK
)
130 $json = $this->get('jms_serializer')->serialize(
133 SerializationContext
::create()->setGroups([$group])
136 return (new JsonResponse())
138 ->setStatusCode($status);
142 * Translate errors message.
144 * @param array $errors
148 private function translateErrors($errors)
150 $translatedErrors = [];
151 foreach ($errors as $error) {
152 $translatedErrors[] = $this->get('translator')->trans($error);
155 return $translatedErrors;