]>
Commit | Line | Data |
---|---|---|
36f30fa3 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\GroupBundle\Controller; | |
4 | ||
5847dd35 | 5 | use Pagerfanta\Adapter\ArrayAdapter; |
2041810a TC |
6 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
7 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | |
8 | use Pagerfanta\Pagerfanta; | |
9 | use Strut\StrutBundle\Service\Sha256Salted; | |
36f30fa3 NL |
10 | use Symfony\Component\HttpFoundation\Request; |
11 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
12 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
13 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
2041810a | 14 | use Symfony\Component\HttpFoundation\Response; |
36f30fa3 | 15 | use Wallabag\GroupBundle\Entity\Group; |
2041810a TC |
16 | use Wallabag\GroupBundle\Entity\UserGroup; |
17 | use Wallabag\GroupBundle\Form\GroupType; | |
18 | use Wallabag\GroupBundle\Form\NewGroupType; | |
5847dd35 | 19 | use Wallabag\GroupBundle\Form\UserGroupType; |
2041810a | 20 | use Wallabag\UserBundle\Entity\User; |
36f30fa3 NL |
21 | |
22 | /** | |
23 | * Group controller. | |
24 | */ | |
25 | class ManageController extends Controller | |
26 | { | |
27 | /** | |
2041810a | 28 | * Lists all public Group entities. |
36f30fa3 | 29 | * |
2585953e | 30 | * @Route("/{page}", requirements={"page" = "\d+"}, name="group_index", defaults={"page" = "1"}) |
36f30fa3 NL |
31 | * @Method("GET") |
32 | */ | |
2041810a | 33 | public function indexAction($page = 1) |
36f30fa3 NL |
34 | { |
35 | $em = $this->getDoctrine()->getManager(); | |
36 | ||
2041810a TC |
37 | $groups = $em->getRepository('WallabagGroupBundle:Group')->findPublicGroups(); |
38 | ||
39 | $pagerAdapter = new DoctrineORMAdapter($groups->getQuery(), true, false); | |
40 | $pagerFanta = new Pagerfanta($pagerAdapter); | |
41 | $pagerFanta->setMaxPerPage(1); | |
42 | ||
43 | try { | |
44 | $pagerFanta->setCurrentPage($page); | |
45 | } catch (OutOfRangeCurrentPageException $e) { | |
46 | if ($page > 1) { | |
47 | return $this->redirect($this->generateUrl('group_index', ['page' => $pagerFanta->getNbPages()]), 302); | |
48 | } | |
49 | } | |
36f30fa3 NL |
50 | |
51 | return $this->render('WallabagGroupBundle:Manage:index.html.twig', array( | |
2041810a TC |
52 | 'groups' => $pagerFanta, |
53 | 'currentPage' => $page, | |
36f30fa3 NL |
54 | )); |
55 | } | |
56 | ||
57 | /** | |
58 | * Creates a new Group entity. | |
59 | * | |
60 | * @Route("/new", name="group_new") | |
61 | * @Method({"GET", "POST"}) | |
62 | */ | |
63 | public function newAction(Request $request) | |
64 | { | |
2041810a | 65 | $group = new Group(); |
36f30fa3 | 66 | |
2041810a | 67 | $form = $this->createForm(NewGroupType::class, $group); |
36f30fa3 NL |
68 | $form->handleRequest($request); |
69 | ||
70 | if ($form->isSubmitted() && $form->isValid()) { | |
71 | $em = $this->getDoctrine()->getManager(); | |
2041810a TC |
72 | |
73 | if ($group->getAcceptSystem() == Group::ACCESS_PASSWORD) { | |
74 | /** @var Sha256Salted $encoder */ | |
75 | $encoder = $this->get('sha256salted_encoder'); | |
76 | $password = $encoder->encodePassword($group->getPassword(), $this->getParameter('secret')); | |
77 | $group->setPassword($password); | |
78 | } | |
79 | ||
36f30fa3 | 80 | $em->persist($group); |
2041810a TC |
81 | |
82 | $groupUser = new UserGroup($this->getUser(), $group, Group::ROLE_ADMIN); | |
83 | $groupUser->setAccepted(true); | |
84 | $em->persist($groupUser); | |
36f30fa3 NL |
85 | $em->flush(); |
86 | ||
87 | $this->get('session')->getFlashBag()->add( | |
88 | 'notice', | |
89 | $this->get('translator')->trans('flashes.group.notice.added', ['%name%' => $group->getName()]) | |
90 | ); | |
91 | ||
92 | return $this->redirectToRoute('group_edit', array('id' => $group->getId())); | |
93 | } | |
94 | ||
95 | return $this->render('WallabagGroupBundle:Manage:new.html.twig', array( | |
96 | 'group' => $group, | |
97 | 'form' => $form->createView(), | |
98 | )); | |
99 | } | |
100 | ||
101 | /** | |
102 | * Displays a form to edit an existing Group entity. | |
103 | * | |
104 | * @Route("/{id}/edit", name="group_edit") | |
105 | * @Method({"GET", "POST"}) | |
106 | */ | |
107 | public function editAction(Request $request, Group $group) | |
108 | { | |
2041810a TC |
109 | if ($this->getUser()->getGroupRoleForUser($group) < Group::ROLE_ADMIN) { |
110 | $this->createAccessDeniedException(); | |
111 | } | |
112 | ||
36f30fa3 | 113 | $deleteForm = $this->createDeleteForm($group); |
2041810a | 114 | $editForm = $this->createForm(GroupType::class, $group); |
36f30fa3 NL |
115 | $editForm->handleRequest($request); |
116 | ||
117 | if ($editForm->isSubmitted() && $editForm->isValid()) { | |
118 | $em = $this->getDoctrine()->getManager(); | |
2041810a TC |
119 | |
120 | if ($group->getAcceptSystem() === Group::ACCESS_PASSWORD) { | |
121 | $encoder = $this->get('sha256salted_encoder'); | |
122 | $password = $encoder->encodePassword($group->getPlainPassword(), $this->getParameter('secret')); | |
123 | $group->setPassword($password); | |
124 | } | |
125 | ||
36f30fa3 NL |
126 | $em->persist($group); |
127 | $em->flush(); | |
128 | ||
129 | $this->get('session')->getFlashBag()->add( | |
130 | 'notice', | |
131 | $this->get('translator')->trans('flashes.group.notice.updated', ['%name%' => $group->getName()]) | |
132 | ); | |
133 | ||
134 | return $this->redirectToRoute('group_edit', array('id' => $group->getId())); | |
135 | } | |
136 | ||
137 | return $this->render('WallabagGroupBundle:Manage:edit.html.twig', array( | |
138 | 'group' => $group, | |
139 | 'edit_form' => $editForm->createView(), | |
140 | 'delete_form' => $deleteForm->createView(), | |
141 | )); | |
142 | } | |
143 | ||
144 | /** | |
145 | * Deletes a Group entity. | |
146 | * | |
147 | * @Route("/{id}", name="group_delete") | |
148 | * @Method("DELETE") | |
149 | */ | |
150 | public function deleteAction(Request $request, Group $group) | |
151 | { | |
152 | $form = $this->createDeleteForm($group); | |
153 | $form->handleRequest($request); | |
154 | ||
155 | if ($form->isSubmitted() && $form->isValid()) { | |
156 | $this->get('session')->getFlashBag()->add( | |
157 | 'notice', | |
158 | $this->get('translator')->trans('flashes.group.notice.deleted', ['%name%' => $group->getName()]) | |
159 | ); | |
160 | ||
161 | $em = $this->getDoctrine()->getManager(); | |
162 | $em->remove($group); | |
163 | $em->flush(); | |
164 | } | |
165 | ||
166 | return $this->redirectToRoute('group_index'); | |
167 | } | |
168 | ||
169 | /** | |
170 | * Creates a form to delete a Group entity. | |
171 | * | |
172 | * @param Group $group The Group entity | |
173 | * | |
174 | * @return \Symfony\Component\Form\Form The form | |
175 | */ | |
176 | private function createDeleteForm(Group $group) | |
177 | { | |
178 | return $this->createFormBuilder() | |
179 | ->setAction($this->generateUrl('group_delete', array('id' => $group->getId()))) | |
180 | ->setMethod('DELETE') | |
181 | ->getForm() | |
182 | ; | |
183 | } | |
2041810a TC |
184 | |
185 | /** | |
186 | * @Route("/group-user-exclude/{group}/{user}", name="group-user-exclude") | |
2585953e | 187 | * |
2041810a | 188 | * @param Group $group |
2585953e NL |
189 | * @param User $user |
190 | * | |
2041810a TC |
191 | * @return Response |
192 | */ | |
193 | public function excludeMemberAction(Group $group, User $user) | |
194 | { | |
195 | $logger = $this->get('logger'); | |
2585953e | 196 | $logger->info('User '.$this->getUser()->getUsername().' wants to exclude user '.$user->getUsername().' from group '.$group->getName()); |
2041810a TC |
197 | |
198 | if (!$this->getUser()->inGroup($group) || $this->getUser()->getGroupRoleForUser($group) < Group::ROLE_MANAGE_USERS) { | |
2585953e | 199 | $logger->info('User '.$this->getUser()->getUsername().' has not enough rights on group '.$group->getName().' to exclude user '.$user->getUsername()); |
2041810a TC |
200 | throw $this->createAccessDeniedException(); |
201 | } | |
202 | ||
203 | if ($user->inGroup($group) && $user->getGroupRoleForUser($group) < Group::ROLE_ADMIN) { | |
204 | $em = $this->getDoctrine()->getManager(); | |
205 | ||
2585953e | 206 | $logger->info('Removing user '.$this->getUser()->getUsername().' from group '.$group->getName()); |
2041810a TC |
207 | $em->remove($this->getUser()->getUserGroupFromGroup($group)); |
208 | ||
209 | $em->flush(); | |
210 | ||
211 | return $this->redirectToRoute('group-manage', ['group' => $group->getId()]); | |
212 | } | |
213 | throw $this->createAccessDeniedException(); | |
214 | } | |
5847dd35 TC |
215 | |
216 | /** | |
217 | * @Route("/join/{group}", name="group_join") | |
218 | * @param Group $group | |
219 | * @return Response | |
220 | */ | |
221 | public function joinGroupAction(Group $group): Response | |
222 | { | |
223 | $em = $this->getDoctrine()->getManager(); | |
224 | ||
225 | if ($group->getAcceptSystem() === Group::ACCESS_PASSWORD) { | |
226 | return $this->redirectToRoute('group_password', ['group' => $group->getId()]); | |
227 | } | |
228 | $this->getUser()->addAGroup($group, $group->getDefaultRole()); | |
229 | ||
230 | $em->flush(); | |
231 | ||
232 | return $this->redirect($this->generateUrl('group_index'), 302); | |
233 | } | |
234 | ||
235 | /** | |
236 | * @Route("/manage/{group}/{page}", name="group-manage", defaults={"page" = "1"}) | |
237 | * @param Group $group | |
238 | * @return Response | |
239 | */ | |
240 | public function manageGroupUsersAction(Group $group, int $page): Response | |
241 | { | |
242 | if ($this->getUser()->getGroupRoleForUser($group) < Group::ROLE_MANAGE_USERS) { | |
243 | $this->createAccessDeniedException(); | |
244 | } | |
245 | ||
246 | $members = $this->get('wallabag_user.user_repository')->findGroupMembers($group->getId()); | |
247 | ||
248 | $pagerAdapter = new DoctrineORMAdapter($members->getQuery(), true, false); | |
249 | $pagerFanta = new Pagerfanta($pagerAdapter); | |
250 | $pagerFanta->setMaxPerPage(9); | |
251 | ||
252 | try { | |
253 | $pagerFanta->setCurrentPage($page); | |
254 | } catch (OutOfRangeCurrentPageException $e) { | |
255 | if ($page > 1) { | |
256 | return $this->redirect($this->generateUrl('groups', ['page' => $pagerFanta->getNbPages()]), 302); | |
257 | } | |
258 | } | |
259 | ||
260 | return $this->render('WallabagGroupBundle:Manage:manage.html.twig', [ | |
261 | 'members' => $pagerFanta, | |
262 | 'group' => $group, | |
263 | 'currentPage' => $page, | |
264 | ]); | |
265 | } | |
266 | ||
267 | /** | |
268 | * @Route("/leave/{group}", name="group_leave") | |
269 | * @param Group $group | |
270 | * @return Response | |
271 | */ | |
272 | public function leaveGroupAction(Group $group): Response | |
273 | { | |
274 | $logger = $this->get('logger'); | |
275 | $em = $this->getDoctrine()->getManager(); | |
276 | $removeGroup = false; | |
277 | ||
278 | if ($this->getUser()->getGroupRoleForUser($group) == Group::ROLE_ADMIN) { | |
279 | $logger->info('User ' . $this->getUser()->getUsername() . ' is the admin for group ' . $group->getName()); | |
280 | $newUser = $group->getUsers()->first(); | |
281 | $newUser->setGroupRole($group, Group::ROLE_ADMIN); | |
282 | $logger->info('The new admin for group ' . $group->getName() . ' is user ' . $newUser->getUsername()); | |
283 | } | |
284 | ||
285 | if ($group->getUsers()->count() <= 1) { | |
286 | $logger->info('User ' . $this->getUser()->getUsername() . ' was the last one on the group ' . $group->getName() . ' so it will be deleted'); | |
287 | $removeGroup = true; | |
288 | } | |
289 | ||
290 | $logger->info('Removing user ' . $this->getUser()->getUsername() . ' from group ' . $group->getName()); | |
291 | $em->remove($this->getUser()->getUserGroupFromGroup($group)); | |
292 | ||
293 | if ($removeGroup) { | |
294 | $logger->info("Removing group " . $group->getName() . " as it doesn't contains users anymore"); | |
295 | $em->remove($group); | |
296 | } | |
297 | ||
298 | $em->flush(); | |
299 | return $this->redirect($this->generateUrl('groups'), 302); | |
300 | } | |
301 | ||
302 | /** | |
303 | * @Route("/requests/{group}/{page}", name="group-requests", defaults={"page" = "1"}) | |
304 | * @param Request $request | |
305 | * @param int $page | |
306 | * @return Response | |
307 | */ | |
308 | public function showRequestsAction(Group $group, int $page): Response | |
309 | { | |
310 | if ($this->getUser()->getGroupRoleForUser($group) < Group::ROLE_MANAGE_USERS) { | |
311 | $this->createAccessDeniedException(); | |
312 | } | |
313 | ||
314 | $requests = $group->getRequests(); | |
315 | $pagerAdapter = new ArrayAdapter($requests->toArray()); | |
316 | ||
317 | $pagerFanta = new Pagerfanta($pagerAdapter); | |
318 | $pagerFanta->setMaxPerPage(9); | |
319 | ||
320 | try { | |
321 | $pagerFanta->setCurrentPage($page); | |
322 | } catch (OutOfRangeCurrentPageException $e) { | |
323 | if ($page > 1) { | |
324 | return $this->redirect($this->generateUrl('groups', ['page' => $pagerFanta->getNbPages()]), 302); | |
325 | } | |
326 | } | |
327 | ||
328 | return $this->render('WallabagGroupBundle:Manage:requests.html.twig', [ | |
329 | 'requests' => $pagerFanta, | |
330 | 'group' => $group, | |
331 | 'currentPage' => $page, | |
332 | ]); | |
333 | } | |
334 | ||
335 | /** | |
336 | * @Route("/activate/{group}/{user}/{accept}", name="group-activate", requirements={"accept" = "\d+"}) | |
337 | * @param Group $group | |
338 | * @param User $user | |
339 | * @param $accept | |
340 | * @return Response | |
341 | */ | |
342 | public function postRequestAction(Group $group, User $user, $accept): Response | |
343 | { | |
344 | if (!$this->getUser() < Group::ROLE_MANAGE_USERS) { | |
345 | $this->createAccessDeniedException("You don't have the rights to do this"); | |
346 | } | |
347 | ||
348 | $em = $this->getDoctrine()->getManager(); | |
349 | ||
350 | $accept = $accept == 1; | |
351 | $user->getUserGroupFromGroup($group)->setAccepted($accept); | |
352 | if (!$accept) { | |
353 | $em->remove($user->getUserGroupFromGroup($group)); | |
354 | } | |
355 | ||
356 | $em->flush(); | |
357 | ||
358 | return $this->redirectToRoute('group_index'); | |
359 | } | |
360 | ||
361 | /** | |
362 | * @Route("/user-edit/{group}/{user}", name="group-user-edit") | |
363 | * @param Request $request | |
364 | * @param Group $group | |
365 | * @param User $user | |
366 | * @return Response | |
367 | */ | |
368 | public function editGroupUsersAction(Request $request, Group $group, User $user): Response | |
369 | { | |
370 | if ($this->getUser()->getGroupRoleForUser($group) < Group::ROLE_MANAGE_USERS) { | |
371 | $this->createAccessDeniedException(); | |
372 | } | |
373 | ||
374 | $groupUser = $user->getUserGroupFromGroup($group); | |
375 | $editForm = $this->createForm(UserGroupType::class, $groupUser); | |
376 | $editForm->handleRequest($request); | |
377 | ||
378 | if ($editForm->isSubmitted() && $editForm->isValid()) { | |
379 | $em = $this->getDoctrine()->getManager(); | |
380 | $em->persist($groupUser); | |
381 | $em->flush(); | |
382 | ||
383 | $this->get('session')->getFlashBag()->add( | |
384 | 'notice', | |
385 | $this->get('translator')->trans('flashes.group.notice.user.edited', ['%user%' => $user->getUsername(), '%group%' => $group->getName()]) | |
386 | ); | |
387 | ||
388 | return $this->redirectToRoute('group-manage', ['group' => $group->getId()]); | |
389 | } | |
390 | ||
391 | return $this->render('WallabagGroupBundle:Manage:edit_user.html.twig', array( | |
392 | 'user' => $user, | |
393 | 'group' => $group, | |
394 | 'edit_form' => $editForm->createView(), | |
395 | )); | |
396 | } | |
36f30fa3 | 397 | } |