1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Api; |
4
|
|
|
|
5
|
|
|
use App\Document\Access; |
6
|
|
|
use App\Document\Application; |
7
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
8
|
|
|
use Nelmio\ApiDocBundle\Annotation\Model; |
9
|
|
|
use Nelmio\ApiDocBundle\Annotation\Security; |
10
|
|
|
use Swagger\Annotations as SWG; |
11
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
12
|
|
|
use Symfony\Component\HttpFoundation\Request; |
13
|
|
|
use Symfony\Component\HttpFoundation\Response; |
14
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
15
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
16
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Provide routes for access API. |
20
|
|
|
* |
21
|
|
|
* @Route("/api/applications/{id}/access", name="api_access_", defaults={"_format": "json"}) |
22
|
|
|
*/ |
23
|
|
|
class AccessController extends AbstractController |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* List access. |
27
|
|
|
* |
28
|
|
|
* @Route("", name="index", methods={"GET"}) |
29
|
|
|
* @SWG\Response( |
30
|
|
|
* response=200, |
31
|
|
|
* description="Returns access", |
32
|
|
|
* @SWG\Schema( |
33
|
|
|
* type="array", |
34
|
|
|
* @SWG\Items(ref=@Model(type=Access::class, groups={"access_default", "user_default"})) |
35
|
|
|
* ) |
36
|
|
|
* ) |
37
|
|
|
* @SWG\Tag(name="applications") |
38
|
|
|
* @Security(name="Bearer") |
39
|
|
|
*/ |
40
|
|
|
public function getAll(Application $application): Response |
41
|
|
|
{ |
42
|
|
|
$this->denyAccessUnlessGranted(Access::ACCESS_OWNER, $application); |
43
|
|
|
|
44
|
|
|
return $this->json($application->getAccesses(), Response::HTTP_OK, [], ['groups' => ['access_default', 'user_default']]); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Create an access. |
49
|
|
|
* |
50
|
|
|
* @Route("/{userId}", name="create", methods={"POST"}) |
51
|
|
|
* @SWG\Response( |
52
|
|
|
* response=201, |
53
|
|
|
* description="Create an access", |
54
|
|
|
* @SWG\Schema( |
55
|
|
|
* type="object", |
56
|
|
|
* ref=@Model(type=Access::class, groups={"access_full", "user_default"}) |
57
|
|
|
* ) |
58
|
|
|
* ) |
59
|
|
|
* @SWG\Response( |
60
|
|
|
* response=400, |
61
|
|
|
* description="Invalid access" |
62
|
|
|
* ) |
63
|
|
|
* @SWG\Parameter( |
64
|
|
|
* name="body", |
65
|
|
|
* in="body", |
66
|
|
|
* description="JSON Payload", |
67
|
|
|
* required=true, |
68
|
|
|
* format="application/json", |
69
|
|
|
* @SWG\Schema( |
70
|
|
|
* type="object", |
71
|
|
|
* ref=@Model(type=Access::class, groups={"access_write"}) |
72
|
|
|
* ) |
73
|
|
|
* ) |
74
|
|
|
* @SWG\Tag(name="applications") |
75
|
|
|
* @Security(name="Bearer") |
76
|
|
|
*/ |
77
|
|
|
public function create( |
78
|
|
|
Application $application, |
79
|
|
|
DocumentManager $dm, |
80
|
|
|
SerializerInterface $serializer, |
81
|
|
|
ValidatorInterface $validator, |
82
|
|
|
Request $request, |
83
|
|
|
string $userId): Response |
84
|
|
|
{ |
85
|
|
|
$this->denyAccessUnlessGranted(Access::ACCESS_OWNER, $application); |
86
|
|
|
|
87
|
|
|
$data = $request->getContent(); |
88
|
|
|
|
89
|
|
|
/** @var Access $access */ |
90
|
|
|
$access = $serializer->deserialize($data, Access::class, 'json', ['groups' => ['access_write']]); |
91
|
|
|
|
92
|
|
|
$user = $dm->getRepository('App:User')->find($userId); |
93
|
|
|
|
94
|
|
|
if (!$user) { |
95
|
|
|
throw $this->createNotFoundException('invalid User'); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$access->setUser($user); |
99
|
|
|
|
100
|
|
|
$violations = $validator->validate($access); |
101
|
|
|
|
102
|
|
|
if ($violations->count() > 0) { |
103
|
|
|
return $this->json($violations, Response::HTTP_BAD_REQUEST); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$application->addAccess($access); |
107
|
|
|
$dm->flush(); |
108
|
|
|
|
109
|
|
|
return $this->json($access, Response::HTTP_CREATED, [], ['groups' => ['access_full', 'user_default']]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Delete an access. |
114
|
|
|
* |
115
|
|
|
* @Route("/{userId}", name="delete", methods={"DELETE"}) |
116
|
|
|
* @SWG\Response( |
117
|
|
|
* response=204, |
118
|
|
|
* description="Delete an access" |
119
|
|
|
* ) |
120
|
|
|
* @SWG\Tag(name="applications") |
121
|
|
|
* @Security(name="Bearer") |
122
|
|
|
*/ |
123
|
|
|
public function delete(DocumentManager $dm, Application $application, string $userId): Response |
124
|
|
|
{ |
125
|
|
|
$this->denyAccessUnlessGranted(Access::ACCESS_OWNER, $application); |
126
|
|
|
|
127
|
|
|
$application->removeAccess($userId); |
128
|
|
|
$dm->flush(); |
129
|
|
|
|
130
|
|
|
return new Response('', Response::HTTP_NO_CONTENT); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|