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