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
;
11 class UserRestController
extends WallabagRestController
14 * Retrieve user informations
18 * @return JsonResponse
20 public function getUserAction()
22 $this->validateAuthentication();
24 $serializationContext = SerializationContext
::create()->setGroups(['user_api']);
25 $json = $this->get('serializer')->serialize($this->getUser(), 'json', $serializationContext);
27 return (new JsonResponse())->setJson($json);
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"}
40 * @return JsonResponse
42 // TODO : Make this method (or the whole API) accessible only through https
43 public function putUserAction($username, $password, $email)
45 if (!$this->container
->getParameter('fosuser_registration')) {
46 $json = $this->get('serializer')->serialize(['error' => "Server doesn't allow registrations"], 'json');
47 return (new JsonResponse())->setJson($json)->setStatusCode(403);
50 if ($password === '') { // TODO : might be a good idea to enforce restrictions here
51 $json = $this->get('serializer')->serialize(['error' => 'Password is blank'], 'json');
52 return (new JsonResponse())->setJson($json)->setStatusCode(400);
56 // TODO : Make only one call to database by using a custom repository method
57 if ($this->getDoctrine()
58 ->getRepository('WallabagUserBundle:User')
59 ->findOneByUserName($username)) {
60 $json = $this->get('serializer')->serialize(['error' => 'Username is already taken'], 'json');
61 return (new JsonResponse())->setJson($json)->setStatusCode(409);
64 if ($this->getDoctrine()
65 ->getRepository('WallabagUserBundle:User')
66 ->findOneByEmail($email)) {
67 $json = $this->get('serializer')->serialize(['error' => 'An account with this email already exists'], 'json');
68 return (new JsonResponse())->setJson($json)->setStatusCode(409);
71 $em = $this->get('doctrine.orm.entity_manager');
73 $userManager = $this->get('fos_user.user_manager');
74 $user = $userManager->createUser();
76 $user->setUsername($username);
78 $user->setPlainPassword($password);
80 $user->setEmail($email);
82 $user->setEnabled(true);
83 $user->addRole('ROLE_USER');
87 // dispatch a created event so the associated config will be created
88 $event = new UserEvent($user);
89 $this->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
91 $serializationContext = SerializationContext
::create()->setGroups(['user_api']);
92 $json = $this->get('serializer')->serialize($user, 'json', $serializationContext);
94 return (new JsonResponse())->setJson($json);