1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Api; |
4
|
|
|
|
5
|
|
|
use App\Document\Access; |
6
|
|
|
use App\Document\Application; |
7
|
|
|
use App\Document\User; |
8
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
9
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
10
|
|
|
use Nelmio\ApiDocBundle\Annotation\Security; |
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted; |
12
|
|
|
use Swagger\Annotations as SWG; |
13
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\HttpFoundation\Response; |
16
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
17
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
18
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Provide routes for application API. |
22
|
|
|
* |
23
|
|
|
* @Route("/api/applications", name="api_application_", defaults={"_format": "json"}) |
24
|
|
|
*/ |
25
|
|
|
class ApplicationController extends AbstractController |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* List applications. |
29
|
|
|
* |
30
|
|
|
* @Route("", name="index", methods={"GET"}) |
31
|
|
|
* @SWG\Response( |
32
|
|
|
* response=200, |
33
|
|
|
* description="Returns the applications", |
34
|
|
|
* @SWG\Schema( |
35
|
|
|
* type="array", |
36
|
|
|
* @SWG\Items(ref=@Model(type=Application::class, groups={"application_default"})) |
37
|
|
|
* ) |
38
|
|
|
* ) |
39
|
|
|
* @SWG\Parameter( |
40
|
|
|
* name="page", |
41
|
|
|
* in="query", |
42
|
|
|
* type="integer", |
43
|
|
|
* description="Page number", |
44
|
|
|
* default="1" |
45
|
|
|
* ) |
46
|
|
|
* @SWG\Parameter( |
47
|
|
|
* name="query", |
48
|
|
|
* in="query", |
49
|
|
|
* type="string", |
50
|
|
|
* description="Search query", |
51
|
|
|
* ) |
52
|
|
|
* @SWG\Tag(name="applications") |
53
|
|
|
* @Security(name="Bearer") |
54
|
|
|
*/ |
55
|
|
|
public function list(DocumentManager $dm, Request $request): Response |
56
|
|
|
{ |
57
|
|
|
$page = $request->query->getInt('page', 1); |
58
|
|
|
$query = $request->query->get('query'); |
59
|
|
|
|
60
|
|
|
$rs = [ |
61
|
|
|
'total' => $dm->getRepository('App:Application')->count($this->getUser(), $query), |
62
|
|
|
'items' => $dm->getRepository('App:Application')->search($this->getUser(), $page, $query, 20), |
63
|
|
|
]; |
64
|
|
|
|
65
|
|
|
return $this->json($rs, Response::HTTP_OK, [], ['groups' => ['application_default']]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get an application. |
70
|
|
|
* |
71
|
|
|
* @Route("/{id}", name="get", methods={"GET"}) |
72
|
|
|
* @SWG\Response( |
73
|
|
|
* response=200, |
74
|
|
|
* description="Returns the application", |
75
|
|
|
* @SWG\Schema(ref=@Model(type=Application::class, groups={"application_full"}) |
76
|
|
|
* ) |
77
|
|
|
* ) |
78
|
|
|
* @SWG\Tag(name="applications") |
79
|
|
|
* @Security(name="Bearer") |
80
|
|
|
*/ |
81
|
|
|
public function find(Application $application): Response |
82
|
|
|
{ |
83
|
|
|
$this->denyAccessUnlessGranted(Access::ACCESS_USER, $application); |
84
|
|
|
|
85
|
|
|
if ($this->isGranted([User::ROLE_MANAGER])) { |
86
|
|
|
$access = Access::ACCESS_OWNER; |
87
|
|
|
} else { |
88
|
|
|
$access = $application->getAccess($this->getUser()->getId())->getAccess(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$data = [ |
92
|
|
|
'item' => $application, |
93
|
|
|
'access' => $access, |
94
|
|
|
]; |
95
|
|
|
|
96
|
|
|
return $this->json($data, Response::HTTP_OK, [], ['groups' => ['application_full']]); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create an application. |
101
|
|
|
* |
102
|
|
|
* @Route("", name="create", methods={"POST"}) |
103
|
|
|
* @SWG\Response( |
104
|
|
|
* response=201, |
105
|
|
|
* description="Create an application", |
106
|
|
|
* @SWG\Schema( |
107
|
|
|
* type="object", |
108
|
|
|
* ref=@Model(type=Application::class, groups={"application_full"}) |
109
|
|
|
* ) |
110
|
|
|
* ) |
111
|
|
|
* @SWG\Response( |
112
|
|
|
* response=400, |
113
|
|
|
* description="Invalid application" |
114
|
|
|
* ) |
115
|
|
|
* @SWG\Parameter( |
116
|
|
|
* name="body", |
117
|
|
|
* in="body", |
118
|
|
|
* description="JSON Payload", |
119
|
|
|
* required=true, |
120
|
|
|
* format="application/json", |
121
|
|
|
* @SWG\Schema( |
122
|
|
|
* type="object", |
123
|
|
|
* ref=@Model(type=Application::class, groups={"application_write"}) |
124
|
|
|
* ) |
125
|
|
|
* ) |
126
|
|
|
* @SWG\Tag(name="applications") |
127
|
|
|
* @Security(name="Bearer") |
128
|
|
|
* |
129
|
|
|
* @IsGranted("ROLE_MANAGER") |
130
|
|
|
*/ |
131
|
|
|
public function create( |
132
|
|
|
DocumentManager $dm, |
133
|
|
|
SerializerInterface $serializer, |
134
|
|
|
ValidatorInterface $validator, |
135
|
|
|
Request $request): Response |
136
|
|
|
{ |
137
|
|
|
$data = $request->getContent(); |
138
|
|
|
|
139
|
|
|
/** @var Application $application */ |
140
|
|
|
$application = $serializer->deserialize($data, Application::class, 'json', ['groups' => ['application_write']]); |
141
|
|
|
|
142
|
|
|
$violations = $validator->validate($application); |
143
|
|
|
|
144
|
|
|
if ($violations->count() > 0) { |
145
|
|
|
return $this->json($violations, Response::HTTP_BAD_REQUEST); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
$dm->persist($application); |
149
|
|
|
$dm->flush(); |
150
|
|
|
|
151
|
|
|
return $this->json($application, Response::HTTP_CREATED, [], ['groups' => ['application_full']]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* update an application. |
156
|
|
|
* |
157
|
|
|
* @Route("/{id}", name="update", methods={"PUT"}) |
158
|
|
|
* @SWG\Response( |
159
|
|
|
* response=204, |
160
|
|
|
* description="Update an application" |
161
|
|
|
* ) |
162
|
|
|
* @SWG\Response( |
163
|
|
|
* response=400, |
164
|
|
|
* description="Invalid application" |
165
|
|
|
* ) |
166
|
|
|
* @SWG\Parameter( |
167
|
|
|
* name="body", |
168
|
|
|
* in="body", |
169
|
|
|
* description="JSON Payload", |
170
|
|
|
* required=true, |
171
|
|
|
* format="application/json", |
172
|
|
|
* @SWG\Schema( |
173
|
|
|
* type="object", |
174
|
|
|
* ref=@Model(type=Application::class, groups={"application_write"}) |
175
|
|
|
* ) |
176
|
|
|
* ) |
177
|
|
|
* @SWG\Tag(name="applications") |
178
|
|
|
* @Security(name="Bearer") |
179
|
|
|
*/ |
180
|
|
|
public function update( |
181
|
|
|
Application $application, |
182
|
|
|
DocumentManager $dm, |
183
|
|
|
SerializerInterface $serializer, |
184
|
|
|
ValidatorInterface $validator, |
185
|
|
|
Request $request): Response |
186
|
|
|
{ |
187
|
|
|
$this->denyAccessUnlessGranted(Access::ACCESS_OWNER, $application); |
188
|
|
|
|
189
|
|
|
$data = $request->getContent(); |
190
|
|
|
|
191
|
|
|
/* @var Application $application */ |
192
|
|
|
$serializer->deserialize($data, Application::class, 'json', [ |
193
|
|
|
'groups' => ['application_write'], |
194
|
|
|
'object_to_populate' => $application, |
195
|
|
|
]); |
196
|
|
|
|
197
|
|
|
$violations = $validator->validate($application); |
198
|
|
|
|
199
|
|
|
if ($violations->count() > 0) { |
200
|
|
|
return $this->json($violations, Response::HTTP_BAD_REQUEST); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
$dm->flush(); |
204
|
|
|
|
205
|
|
|
return new Response('', Response::HTTP_NO_CONTENT); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Delete an application. |
210
|
|
|
* |
211
|
|
|
* @Route("/{id}", name="delete", methods={"DELETE"}) |
212
|
|
|
* @SWG\Response( |
213
|
|
|
* response=204, |
214
|
|
|
* description="Delete an application" |
215
|
|
|
* ) |
216
|
|
|
* @SWG\Tag(name="applications") |
217
|
|
|
* @Security(name="Bearer") |
218
|
|
|
*/ |
219
|
|
|
public function delete(DocumentManager $dm, Application $application): Response |
220
|
|
|
{ |
221
|
|
|
$this->denyAccessUnlessGranted(Access::ACCESS_OWNER, $application); |
222
|
|
|
|
223
|
|
|
$dm->remove($application); |
224
|
|
|
$dm->flush(); |
225
|
|
|
|
226
|
|
|
return new Response('', Response::HTTP_NO_CONTENT); |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
|