diff options
author | Thomas Citharel <tcit@tcit.fr> | 2017-04-29 19:22:50 +0200 |
---|---|---|
committer | Jeremy Benoist <jeremy.benoist@gmail.com> | 2017-05-29 10:16:23 +0200 |
commit | 2251045901875aa815dee43ec467fb1af8d416d0 (patch) | |
tree | 45d49bfaee4afdc502231ce60f835846a22afeca | |
parent | b788add08787863ac2a0e68ddaf4620da4b2b33c (diff) | |
download | wallabag-2251045901875aa815dee43ec467fb1af8d416d0.tar.gz wallabag-2251045901875aa815dee43ec467fb1af8d416d0.tar.zst wallabag-2251045901875aa815dee43ec467fb1af8d416d0.zip |
WIP
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/UserRestController.php | 98 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Resources/config/routing_rest.yml | 5 | ||||
-rw-r--r-- | src/Wallabag/UserBundle/Entity/User.php | 5 |
3 files changed, 108 insertions, 0 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/UserRestController.php b/src/Wallabag/ApiBundle/Controller/UserRestController.php new file mode 100644 index 00000000..c5ffbdf1 --- /dev/null +++ b/src/Wallabag/ApiBundle/Controller/UserRestController.php | |||
@@ -0,0 +1,98 @@ | |||
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 | } | ||
diff --git a/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml b/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml index 57d37f4b..c0283e71 100644 --- a/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml +++ b/src/Wallabag/ApiBundle/Resources/config/routing_rest.yml | |||
@@ -17,3 +17,8 @@ misc: | |||
17 | type: rest | 17 | type: rest |
18 | resource: "WallabagApiBundle:WallabagRest" | 18 | resource: "WallabagApiBundle:WallabagRest" |
19 | name_prefix: api_ | 19 | name_prefix: api_ |
20 | |||
21 | user: | ||
22 | type: rest | ||
23 | resource: "WallabagApiBundle:UserRest" | ||
24 | name_prefix: api_ | ||
diff --git a/src/Wallabag/UserBundle/Entity/User.php b/src/Wallabag/UserBundle/Entity/User.php index 3a167de7..1863c966 100644 --- a/src/Wallabag/UserBundle/Entity/User.php +++ b/src/Wallabag/UserBundle/Entity/User.php | |||
@@ -4,6 +4,7 @@ namespace Wallabag\UserBundle\Entity; | |||
4 | 4 | ||
5 | use Doctrine\Common\Collections\ArrayCollection; | 5 | use Doctrine\Common\Collections\ArrayCollection; |
6 | use Doctrine\ORM\Mapping as ORM; | 6 | use Doctrine\ORM\Mapping as ORM; |
7 | use JMS\Serializer\Annotation\Groups; | ||
7 | use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; | 8 | use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface; |
8 | use Scheb\TwoFactorBundle\Model\TrustedComputerInterface; | 9 | use Scheb\TwoFactorBundle\Model\TrustedComputerInterface; |
9 | use FOS\UserBundle\Model\User as BaseUser; | 10 | use FOS\UserBundle\Model\User as BaseUser; |
@@ -35,6 +36,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf | |||
35 | * @ORM\Column(name="id", type="integer") | 36 | * @ORM\Column(name="id", type="integer") |
36 | * @ORM\Id | 37 | * @ORM\Id |
37 | * @ORM\GeneratedValue(strategy="AUTO") | 38 | * @ORM\GeneratedValue(strategy="AUTO") |
39 | * @Groups({"user_api"}) | ||
38 | */ | 40 | */ |
39 | protected $id; | 41 | protected $id; |
40 | 42 | ||
@@ -42,6 +44,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf | |||
42 | * @var string | 44 | * @var string |
43 | * | 45 | * |
44 | * @ORM\Column(name="name", type="text", nullable=true) | 46 | * @ORM\Column(name="name", type="text", nullable=true) |
47 | * @Groups({"user_api"}) | ||
45 | */ | 48 | */ |
46 | protected $name; | 49 | protected $name; |
47 | 50 | ||
@@ -49,6 +52,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf | |||
49 | * @var date | 52 | * @var date |
50 | * | 53 | * |
51 | * @ORM\Column(name="created_at", type="datetime") | 54 | * @ORM\Column(name="created_at", type="datetime") |
55 | * @Groups({"user_api"}) | ||
52 | */ | 56 | */ |
53 | protected $createdAt; | 57 | protected $createdAt; |
54 | 58 | ||
@@ -56,6 +60,7 @@ class User extends BaseUser implements TwoFactorInterface, TrustedComputerInterf | |||
56 | * @var date | 60 | * @var date |
57 | * | 61 | * |
58 | * @ORM\Column(name="updated_at", type="datetime") | 62 | * @ORM\Column(name="updated_at", type="datetime") |
63 | * @Groups({"user_api"}) | ||
59 | */ | 64 | */ |
60 | protected $updatedAt; | 65 | protected $updatedAt; |
61 | 66 | ||