diff options
Diffstat (limited to 'src/Wallabag/ApiBundle')
-rw-r--r-- | src/Wallabag/ApiBundle/Controller/UserRestController.php | 20 | ||||
-rw-r--r-- | src/Wallabag/ApiBundle/Entity/Client.php | 23 |
2 files changed, 38 insertions, 5 deletions
diff --git a/src/Wallabag/ApiBundle/Controller/UserRestController.php b/src/Wallabag/ApiBundle/Controller/UserRestController.php index 8f675b8d..becbbb9e 100644 --- a/src/Wallabag/ApiBundle/Controller/UserRestController.php +++ b/src/Wallabag/ApiBundle/Controller/UserRestController.php | |||
@@ -9,6 +9,7 @@ use Nelmio\ApiDocBundle\Annotation\ApiDoc; | |||
9 | use Symfony\Component\HttpFoundation\Request; | 9 | use Symfony\Component\HttpFoundation\Request; |
10 | use Symfony\Component\HttpFoundation\JsonResponse; | 10 | use Symfony\Component\HttpFoundation\JsonResponse; |
11 | use Wallabag\UserBundle\Entity\User; | 11 | use Wallabag\UserBundle\Entity\User; |
12 | use Wallabag\ApiBundle\Entity\Client; | ||
12 | 13 | ||
13 | class UserRestController extends WallabagRestController | 14 | class UserRestController extends WallabagRestController |
14 | { | 15 | { |
@@ -97,29 +98,38 @@ class UserRestController extends WallabagRestController | |||
97 | ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST); | 98 | ->setStatusCode(JsonResponse::HTTP_BAD_REQUEST); |
98 | } | 99 | } |
99 | 100 | ||
101 | // create a default client | ||
102 | $client = new Client($user); | ||
103 | $client->setName('Default client'); | ||
104 | |||
105 | $this->getDoctrine()->getManager()->persist($client); | ||
106 | |||
107 | $user->addClient($client); | ||
108 | |||
100 | $userManager->updateUser($user); | 109 | $userManager->updateUser($user); |
101 | 110 | ||
102 | // dispatch a created event so the associated config will be created | 111 | // dispatch a created event so the associated config will be created |
103 | $event = new UserEvent($user, $request); | 112 | $event = new UserEvent($user, $request); |
104 | $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); | 113 | $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event); |
105 | 114 | ||
106 | return $this->sendUser($user, JsonResponse::HTTP_CREATED); | 115 | return $this->sendUser($user, 'user_api_with_client', JsonResponse::HTTP_CREATED); |
107 | } | 116 | } |
108 | 117 | ||
109 | /** | 118 | /** |
110 | * Send user response. | 119 | * Send user response. |
111 | * | 120 | * |
112 | * @param User $user | 121 | * @param User $user |
113 | * @param int $status HTTP Status code to send | 122 | * @param string $group Used to define with serialized group might be used |
123 | * @param int $status HTTP Status code to send | ||
114 | * | 124 | * |
115 | * @return JsonResponse | 125 | * @return JsonResponse |
116 | */ | 126 | */ |
117 | private function sendUser(User $user, $status = JsonResponse::HTTP_OK) | 127 | private function sendUser(User $user, $group = 'user_api', $status = JsonResponse::HTTP_OK) |
118 | { | 128 | { |
119 | $json = $this->get('serializer')->serialize( | 129 | $json = $this->get('serializer')->serialize( |
120 | $user, | 130 | $user, |
121 | 'json', | 131 | 'json', |
122 | SerializationContext::create()->setGroups(['user_api']) | 132 | SerializationContext::create()->setGroups([$group]) |
123 | ); | 133 | ); |
124 | 134 | ||
125 | return (new JsonResponse()) | 135 | return (new JsonResponse()) |
diff --git a/src/Wallabag/ApiBundle/Entity/Client.php b/src/Wallabag/ApiBundle/Entity/Client.php index 9ed9f980..c15fd3fa 100644 --- a/src/Wallabag/ApiBundle/Entity/Client.php +++ b/src/Wallabag/ApiBundle/Entity/Client.php | |||
@@ -5,6 +5,9 @@ namespace Wallabag\ApiBundle\Entity; | |||
5 | use Doctrine\ORM\Mapping as ORM; | 5 | use Doctrine\ORM\Mapping as ORM; |
6 | use FOS\OAuthServerBundle\Entity\Client as BaseClient; | 6 | use FOS\OAuthServerBundle\Entity\Client as BaseClient; |
7 | use Wallabag\UserBundle\Entity\User; | 7 | use Wallabag\UserBundle\Entity\User; |
8 | use JMS\Serializer\Annotation\Groups; | ||
9 | use JMS\Serializer\Annotation\SerializedName; | ||
10 | use JMS\Serializer\Annotation\VirtualProperty; | ||
8 | 11 | ||
9 | /** | 12 | /** |
10 | * @ORM\Table("oauth2_clients") | 13 | * @ORM\Table("oauth2_clients") |
@@ -23,6 +26,8 @@ class Client extends BaseClient | |||
23 | * @var string | 26 | * @var string |
24 | * | 27 | * |
25 | * @ORM\Column(name="name", type="text", nullable=false) | 28 | * @ORM\Column(name="name", type="text", nullable=false) |
29 | * | ||
30 | * @Groups({"user_api_with_client"}) | ||
26 | */ | 31 | */ |
27 | protected $name; | 32 | protected $name; |
28 | 33 | ||
@@ -37,6 +42,14 @@ class Client extends BaseClient | |||
37 | protected $accessTokens; | 42 | protected $accessTokens; |
38 | 43 | ||
39 | /** | 44 | /** |
45 | * @var string | ||
46 | * | ||
47 | * @SerializedName("client_secret") | ||
48 | * @Groups({"user_api_with_client"}) | ||
49 | */ | ||
50 | protected $secret; | ||
51 | |||
52 | /** | ||
40 | * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="clients") | 53 | * @ORM\ManyToOne(targetEntity="Wallabag\UserBundle\Entity\User", inversedBy="clients") |
41 | */ | 54 | */ |
42 | private $user; | 55 | private $user; |
@@ -78,4 +91,14 @@ class Client extends BaseClient | |||
78 | { | 91 | { |
79 | return $this->user; | 92 | return $this->user; |
80 | } | 93 | } |
94 | |||
95 | /** | ||
96 | * @VirtualProperty | ||
97 | * @SerializedName("client_id") | ||
98 | * @Groups({"user_api_with_client"}) | ||
99 | */ | ||
100 | public function getClientId() | ||
101 | { | ||
102 | return $this->getId().'_'.$this->getRandomId(); | ||
103 | } | ||
81 | } | 104 | } |