1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Overwatch\TestBundle\Controller; |
4
|
|
|
|
5
|
|
|
use Nelmio\ApiDocBundle\Annotation\ApiDoc; |
6
|
|
|
use Overwatch\TestBundle\Entity\TestGroup; |
7
|
|
|
use Overwatch\UserBundle\Entity\User; |
8
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
9
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; |
10
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
11
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; |
12
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
13
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
14
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
15
|
|
|
use Symfony\Component\HttpFoundation\Request; |
16
|
|
|
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* TestGroupApiController |
20
|
|
|
* Handles API requests made for TestGroups |
21
|
|
|
* |
22
|
|
|
* @Route("/api/groups") |
23
|
|
|
*/ |
24
|
|
|
class TestGroupApiController extends Controller |
25
|
|
|
{ |
26
|
|
|
private $_em; |
27
|
|
|
|
28
|
23 |
|
public function setContainer(ContainerInterface $container = null) |
29
|
|
|
{ |
30
|
23 |
|
parent::setContainer($container); |
31
|
23 |
|
$this->_em = $this->getDoctrine()->getManager(); |
32
|
23 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Creates a new group |
36
|
|
|
* |
37
|
|
|
* @Route("") |
38
|
|
|
* @Method({"POST"}) |
39
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
40
|
|
|
* @ApiDoc( |
41
|
|
|
* resource=true, |
42
|
|
|
* parameters={ |
43
|
|
|
* {"name"="name", "description"="A user-friendly name for the group", "required"=true, "format"="Group 1", "dataType"="string"}, |
44
|
|
|
* }, |
45
|
|
|
* tags={ |
46
|
|
|
* "Super Admin" = "#ff1919" |
47
|
|
|
* } |
48
|
|
|
* ) |
49
|
|
|
*/ |
50
|
2 |
|
public function createGroupAction(Request $request) |
51
|
|
|
{ |
52
|
2 |
|
if ($request->request->get('name') === null) { |
53
|
1 |
|
return new JsonResponse('You must provide a name for the new group', JsonResponse::HTTP_UNPROCESSABLE_ENTITY); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$group = new TestGroup; |
57
|
|
|
$group |
58
|
1 |
|
->setName($request->request->get('name')) |
59
|
|
|
; |
60
|
|
|
|
61
|
1 |
|
$this->_em->persist($group); |
62
|
1 |
|
$this->_em->flush(); |
63
|
|
|
|
64
|
1 |
|
return new JsonResponse($group, JsonResponse::HTTP_CREATED); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Returns a list of all groups the current user has access to |
69
|
|
|
* |
70
|
|
|
* @Route("") |
71
|
|
|
* @Method({"GET"}) |
72
|
|
|
* @ApiDoc( |
73
|
|
|
* tags={ |
74
|
|
|
* "Super Admin" = "#ff1919", |
75
|
|
|
* "Admin" = "#ffff33", |
76
|
|
|
* "User" = "#75ff47" |
77
|
|
|
* } |
78
|
|
|
* ) |
79
|
|
|
*/ |
80
|
2 |
|
public function getAllGroupsAction() |
81
|
|
|
{ |
82
|
2 |
|
if ($this->isGranted('ROLE_SUPER_ADMIN')) { |
83
|
1 |
|
$groups = $this->_em->getRepository('OverwatchTestBundle:TestGroup')->findAll(); |
84
|
2 |
|
} elseif ($this->getUser() !== null) { |
85
|
1 |
|
$groups = $this->getUser()->getGroups()->toArray(); |
86
|
1 |
|
} else { |
87
|
|
|
throw new AccessDeniedHttpException('Please login'); |
88
|
|
|
} |
89
|
|
|
|
90
|
2 |
|
return new JsonResponse($groups); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns the details of the specified group |
95
|
|
|
* |
96
|
|
|
* @Route("/{id}") |
97
|
|
|
* @Method({"GET"}) |
98
|
|
|
* @Security("is_granted('view', group)") |
99
|
|
|
* @ApiDoc( |
100
|
|
|
* requirements={ |
101
|
|
|
* {"name"="id", "description"="The ID of the group to return", "dataType"="integer", "requirement"="\d+"} |
102
|
|
|
* }, |
103
|
|
|
* tags={ |
104
|
|
|
* "Super Admin" = "#ff1919", |
105
|
|
|
* "Admin" = "#ffff33", |
106
|
|
|
* "User" = "#75ff47" |
107
|
|
|
* } |
108
|
|
|
* ) |
109
|
|
|
*/ |
110
|
1 |
|
public function getGroupAction(TestGroup $group) |
111
|
|
|
{ |
112
|
1 |
|
return new JsonResponse($group); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Updates the given group |
117
|
|
|
* |
118
|
|
|
* @Route("/{id}") |
119
|
|
|
* @Method({"PUT"}) |
120
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
121
|
|
|
* @ApiDoc( |
122
|
|
|
* parameters={ |
123
|
|
|
* {"name"="name", "description"="A user-friendly name for the group", "required"=false, "format"="Group 1", "dataType"="string"} |
124
|
|
|
* }, |
125
|
|
|
* requirements={ |
126
|
|
|
* {"name"="id", "description"="The ID of the group to edit", "dataType"="integer", "requirement"="\d+"} |
127
|
|
|
* }, |
128
|
|
|
* tags={ |
129
|
|
|
* "Super Admin" = "#ff1919" |
130
|
|
|
* } |
131
|
|
|
* ) |
132
|
|
|
*/ |
133
|
1 |
|
public function updateGroupAction(Request $request, TestGroup $group) |
134
|
|
|
{ |
135
|
1 |
|
if ($request->request->has('name')) { |
136
|
1 |
|
$group->setName($request->request->get('name')); |
137
|
|
|
|
138
|
1 |
|
$this->_em->flush(); |
139
|
1 |
|
} |
140
|
|
|
|
141
|
1 |
|
return new JsonResponse($group); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Deletes the given group |
146
|
|
|
* |
147
|
|
|
* @Route("/{id}") |
148
|
|
|
* @Method({"DELETE"}) |
149
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
150
|
|
|
* @ApiDoc( |
151
|
|
|
* requirements={ |
152
|
|
|
* {"name"="id", "description"="The ID of the group to delete", "dataType"="integer", "requirement"="\d+"} |
153
|
|
|
* }, |
154
|
|
|
* tags={ |
155
|
|
|
* "Super Admin" = "#ff1919" |
156
|
|
|
* } |
157
|
|
|
* ) |
158
|
|
|
*/ |
159
|
2 |
|
public function deleteGroupAction(TestGroup $group) |
160
|
|
|
{ |
161
|
2 |
|
if ($group->getUsers()->count() + $group->getTests()->count() !== 0) { |
162
|
1 |
|
return new JsonResponse('This group still has users and/or tests in it. You must remove them before continuing.', JsonResponse::HTTP_UNPROCESSABLE_ENTITY); |
163
|
|
|
} |
164
|
|
|
|
165
|
1 |
|
$this->_em->remove($group); |
166
|
1 |
|
$this->_em->flush(); |
167
|
|
|
|
168
|
1 |
|
return new JsonResponse(null, JsonResponse::HTTP_NO_CONTENT); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Adds the given user to the given group |
173
|
|
|
* |
174
|
|
|
* @Route("/{groupId}/user/{userId}") |
175
|
|
|
* @Method({"POST"}) |
176
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
177
|
|
|
* @ParamConverter("group", class="OverwatchTestBundle:TestGroup", options={"id" = "groupId"}) |
178
|
|
|
* @ParamConverter("user", class="OverwatchUserBundle:User", options={"id" = "userId"}) |
179
|
|
|
* @ApiDoc( |
180
|
|
|
* resource=true, |
181
|
|
|
* requirements={ |
182
|
|
|
* {"name"="userId", "description"="The ID of the user", "dataType"="integer", "requirement"="\d+"}, |
183
|
|
|
* {"name"="groupId", "description"="The ID of the group", "dataType"="integer", "requirement"="\d+"} |
184
|
|
|
* }, |
185
|
|
|
* tags={ |
186
|
|
|
* "Super Admin" = "#ff1919" |
187
|
|
|
* } |
188
|
|
|
* ) |
189
|
|
|
*/ |
190
|
1 |
|
public function addUserToGroupAction(TestGroup $group, User $user) |
191
|
|
|
{ |
192
|
1 |
|
$group->addUser($user); |
193
|
1 |
|
$this->_em->flush(); |
194
|
|
|
|
195
|
1 |
|
return new JsonResponse($user, JsonResponse::HTTP_CREATED); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Removes the given user from the given group |
200
|
|
|
* |
201
|
|
|
* @Route("/{groupId}/user/{userId}") |
202
|
|
|
* @Method({"DELETE"}) |
203
|
|
|
* @Security("has_role('ROLE_SUPER_ADMIN')") |
204
|
|
|
* @ParamConverter("group", class="OverwatchTestBundle:TestGroup", options={"id" = "groupId"}) |
205
|
|
|
* @ParamConverter("user", class="OverwatchUserBundle:User", options={"id" = "userId"}) |
206
|
|
|
* @ApiDoc( |
207
|
|
|
* requirements={ |
208
|
|
|
* {"name"="userId", "description"="The ID of the user", "dataType"="integer", "requirement"="\d+"}, |
209
|
|
|
* {"name"="groupId", "description"="The ID of the group", "dataType"="integer", "requirement"="\d+"} |
210
|
|
|
* }, |
211
|
|
|
* tags={ |
212
|
|
|
* "Super Admin" = "#ff1919" |
213
|
|
|
* } |
214
|
|
|
* ) |
215
|
|
|
*/ |
216
|
1 |
|
public function removeUserFromGroupAction(TestGroup $group, User $user) |
217
|
|
|
{ |
218
|
1 |
|
$group->removeUser($user); |
219
|
1 |
|
$this->_em->flush(); |
220
|
|
|
|
221
|
1 |
|
return new JsonResponse($user); |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|