]>
Commit | Line | Data |
---|---|---|
22510459 TC |
1 | <?php |
2 | ||
3 | namespace Wallabag\ApiBundle\Controller; | |
4 | ||
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 | ||
11 | class UserRestController extends WallabagRestController | |
12 | { | |
13 | /** | |
14 | * Retrieve user informations | |
15 | * | |
16 | * @ApiDoc() | |
17 | * | |
18 | * @return JsonResponse | |
19 | */ | |
20 | public function getUserAction() | |
21 | { | |
22 | $this->validateAuthentication(); | |
23 | ||
24 | $serializationContext = SerializationContext::create()->setGroups(['user_api']); | |
25 | $json = $this->get('serializer')->serialize($this->getUser(), 'json', $serializationContext); | |
26 | ||
27 | return (new JsonResponse())->setJson($json); | |
28 | } | |
29 | ||
30 | /** | |
31 | * Register an user | |
32 | * | |
33 | * @ApiDoc( | |
34 | * requirements={ | |
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 | * } | |
39 | * ) | |
40 | * @return JsonResponse | |
41 | */ | |
42 | // TODO : Make this method (or the whole API) accessible only through https | |
43 | public function putUserAction($username, $password, $email) | |
44 | { | |
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); | |
48 | } | |
49 | ||
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); | |
53 | } | |
54 | ||
55 | ||
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); | |
62 | } | |
63 | ||
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); | |
69 | } | |
70 | ||
71 | $em = $this->get('doctrine.orm.entity_manager'); | |
72 | ||
73 | $userManager = $this->get('fos_user.user_manager'); | |
74 | $user = $userManager->createUser(); | |
75 | ||
76 | $user->setUsername($username); | |
77 | ||
78 | $user->setPlainPassword($password); | |
79 | ||
80 | $user->setEmail($email); | |
81 | ||
82 | $user->setEnabled(true); | |
83 | $user->addRole('ROLE_USER'); | |
84 | ||
85 | $em->persist($user); | |
86 | ||
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); | |
90 | ||
91 | $serializationContext = SerializationContext::create()->setGroups(['user_api']); | |
92 | $json = $this->get('serializer')->serialize($user, 'json', $serializationContext); | |
93 | ||
94 | return (new JsonResponse())->setJson($json); | |
95 | ||
96 | } | |
97 | ||
98 | } |