diff options
author | Thomas Citharel <tcit@tcit.fr> | 2017-01-31 21:13:33 +0100 |
---|---|---|
committer | Thomas Citharel <tcit@tcit.fr> | 2017-06-23 09:26:41 +0200 |
commit | 2041810adbc6e663e4520337805c0003c77762e2 (patch) | |
tree | 1715a1e57a0275efe0970807c65f424def897034 /src/Wallabag/GroupBundle/Controller/ManageController.php | |
parent | b115623e2c536bcfd59760347f22b4d70e1d0661 (diff) | |
download | wallabag-2041810adbc6e663e4520337805c0003c77762e2.tar.gz wallabag-2041810adbc6e663e4520337805c0003c77762e2.tar.zst wallabag-2041810adbc6e663e4520337805c0003c77762e2.zip |
WIP
Diffstat (limited to 'src/Wallabag/GroupBundle/Controller/ManageController.php')
-rw-r--r-- | src/Wallabag/GroupBundle/Controller/ManageController.php | 90 |
1 files changed, 82 insertions, 8 deletions
diff --git a/src/Wallabag/GroupBundle/Controller/ManageController.php b/src/Wallabag/GroupBundle/Controller/ManageController.php index 7015a465..94196040 100644 --- a/src/Wallabag/GroupBundle/Controller/ManageController.php +++ b/src/Wallabag/GroupBundle/Controller/ManageController.php | |||
@@ -2,11 +2,20 @@ | |||
2 | 2 | ||
3 | namespace Wallabag\GroupBundle\Controller; | 3 | namespace Wallabag\GroupBundle\Controller; |
4 | 4 | ||
5 | use Pagerfanta\Adapter\DoctrineORMAdapter; | ||
6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | ||
7 | use Pagerfanta\Pagerfanta; | ||
8 | use Strut\StrutBundle\Service\Sha256Salted; | ||
5 | use Symfony\Component\HttpFoundation\Request; | 9 | use Symfony\Component\HttpFoundation\Request; |
6 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | 10 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | 11 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; |
8 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | 12 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; |
13 | use Symfony\Component\HttpFoundation\Response; | ||
9 | use Wallabag\GroupBundle\Entity\Group; | 14 | use Wallabag\GroupBundle\Entity\Group; |
15 | use Wallabag\GroupBundle\Entity\UserGroup; | ||
16 | use Wallabag\GroupBundle\Form\GroupType; | ||
17 | use Wallabag\GroupBundle\Form\NewGroupType; | ||
18 | use Wallabag\UserBundle\Entity\User; | ||
10 | 19 | ||
11 | /** | 20 | /** |
12 | * Group controller. | 21 | * Group controller. |
@@ -14,19 +23,32 @@ use Wallabag\GroupBundle\Entity\Group; | |||
14 | class ManageController extends Controller | 23 | class ManageController extends Controller |
15 | { | 24 | { |
16 | /** | 25 | /** |
17 | * Lists all Group entities. | 26 | * Lists all public Group entities. |
18 | * | 27 | * |
19 | * @Route("/", name="group_index") | 28 | * @Route("/{page}", name="group_index", defaults={"page" = "1"}) |
20 | * @Method("GET") | 29 | * @Method("GET") |
21 | */ | 30 | */ |
22 | public function indexAction() | 31 | public function indexAction($page = 1) |
23 | { | 32 | { |
24 | $em = $this->getDoctrine()->getManager(); | 33 | $em = $this->getDoctrine()->getManager(); |
25 | 34 | ||
26 | $groups = $em->getRepository('WallabagGroupBundle:Group')->findAll(); | 35 | $groups = $em->getRepository('WallabagGroupBundle:Group')->findPublicGroups(); |
36 | |||
37 | $pagerAdapter = new DoctrineORMAdapter($groups->getQuery(), true, false); | ||
38 | $pagerFanta = new Pagerfanta($pagerAdapter); | ||
39 | $pagerFanta->setMaxPerPage(1); | ||
40 | |||
41 | try { | ||
42 | $pagerFanta->setCurrentPage($page); | ||
43 | } catch (OutOfRangeCurrentPageException $e) { | ||
44 | if ($page > 1) { | ||
45 | return $this->redirect($this->generateUrl('group_index', ['page' => $pagerFanta->getNbPages()]), 302); | ||
46 | } | ||
47 | } | ||
27 | 48 | ||
28 | return $this->render('WallabagGroupBundle:Manage:index.html.twig', array( | 49 | return $this->render('WallabagGroupBundle:Manage:index.html.twig', array( |
29 | 'groups' => $groups, | 50 | 'groups' => $pagerFanta, |
51 | 'currentPage' => $page, | ||
30 | )); | 52 | )); |
31 | } | 53 | } |
32 | 54 | ||
@@ -38,14 +60,26 @@ class ManageController extends Controller | |||
38 | */ | 60 | */ |
39 | public function newAction(Request $request) | 61 | public function newAction(Request $request) |
40 | { | 62 | { |
41 | $group = new Group(''); | 63 | $group = new Group(); |
42 | 64 | ||
43 | $form = $this->createForm('Wallabag\GroupBundle\Form\NewGroupType', $group); | 65 | $form = $this->createForm(NewGroupType::class, $group); |
44 | $form->handleRequest($request); | 66 | $form->handleRequest($request); |
45 | 67 | ||
46 | if ($form->isSubmitted() && $form->isValid()) { | 68 | if ($form->isSubmitted() && $form->isValid()) { |
47 | $em = $this->getDoctrine()->getManager(); | 69 | $em = $this->getDoctrine()->getManager(); |
70 | |||
71 | if ($group->getAcceptSystem() == Group::ACCESS_PASSWORD) { | ||
72 | /** @var Sha256Salted $encoder */ | ||
73 | $encoder = $this->get('sha256salted_encoder'); | ||
74 | $password = $encoder->encodePassword($group->getPassword(), $this->getParameter('secret')); | ||
75 | $group->setPassword($password); | ||
76 | } | ||
77 | |||
48 | $em->persist($group); | 78 | $em->persist($group); |
79 | |||
80 | $groupUser = new UserGroup($this->getUser(), $group, Group::ROLE_ADMIN); | ||
81 | $groupUser->setAccepted(true); | ||
82 | $em->persist($groupUser); | ||
49 | $em->flush(); | 83 | $em->flush(); |
50 | 84 | ||
51 | $this->get('session')->getFlashBag()->add( | 85 | $this->get('session')->getFlashBag()->add( |
@@ -70,12 +104,23 @@ class ManageController extends Controller | |||
70 | */ | 104 | */ |
71 | public function editAction(Request $request, Group $group) | 105 | public function editAction(Request $request, Group $group) |
72 | { | 106 | { |
107 | if ($this->getUser()->getGroupRoleForUser($group) < Group::ROLE_ADMIN) { | ||
108 | $this->createAccessDeniedException(); | ||
109 | } | ||
110 | |||
73 | $deleteForm = $this->createDeleteForm($group); | 111 | $deleteForm = $this->createDeleteForm($group); |
74 | $editForm = $this->createForm('Wallabag\GroupBundle\Form\GroupType', $group); | 112 | $editForm = $this->createForm(GroupType::class, $group); |
75 | $editForm->handleRequest($request); | 113 | $editForm->handleRequest($request); |
76 | 114 | ||
77 | if ($editForm->isSubmitted() && $editForm->isValid()) { | 115 | if ($editForm->isSubmitted() && $editForm->isValid()) { |
78 | $em = $this->getDoctrine()->getManager(); | 116 | $em = $this->getDoctrine()->getManager(); |
117 | |||
118 | if ($group->getAcceptSystem() === Group::ACCESS_PASSWORD) { | ||
119 | $encoder = $this->get('sha256salted_encoder'); | ||
120 | $password = $encoder->encodePassword($group->getPlainPassword(), $this->getParameter('secret')); | ||
121 | $group->setPassword($password); | ||
122 | } | ||
123 | |||
79 | $em->persist($group); | 124 | $em->persist($group); |
80 | $em->flush(); | 125 | $em->flush(); |
81 | 126 | ||
@@ -134,4 +179,33 @@ class ManageController extends Controller | |||
134 | ->getForm() | 179 | ->getForm() |
135 | ; | 180 | ; |
136 | } | 181 | } |
182 | |||
183 | /** | ||
184 | * @Route("/group-user-exclude/{group}/{user}", name="group-user-exclude") | ||
185 | * @param Group $group | ||
186 | * @param User $user | ||
187 | * @return Response | ||
188 | */ | ||
189 | public function excludeMemberAction(Group $group, User $user) | ||
190 | { | ||
191 | $logger = $this->get('logger'); | ||
192 | $logger->info('User ' . $this->getUser()->getUsername() . ' wants to exclude user ' . $user->getUsername() . ' from group ' . $group->getName()); | ||
193 | |||
194 | if (!$this->getUser()->inGroup($group) || $this->getUser()->getGroupRoleForUser($group) < Group::ROLE_MANAGE_USERS) { | ||
195 | $logger->info('User ' . $this->getUser()->getUsername() . ' has not enough rights on group ' . $group->getName() . ' to exclude user ' . $user->getUsername()); | ||
196 | throw $this->createAccessDeniedException(); | ||
197 | } | ||
198 | |||
199 | if ($user->inGroup($group) && $user->getGroupRoleForUser($group) < Group::ROLE_ADMIN) { | ||
200 | $em = $this->getDoctrine()->getManager(); | ||
201 | |||
202 | $logger->info('Removing user ' . $this->getUser()->getUsername() . ' from group ' . $group->getName()); | ||
203 | $em->remove($this->getUser()->getUserGroupFromGroup($group)); | ||
204 | |||
205 | $em->flush(); | ||
206 | |||
207 | return $this->redirectToRoute('group-manage', ['group' => $group->getId()]); | ||
208 | } | ||
209 | throw $this->createAccessDeniedException(); | ||
210 | } | ||
137 | } | 211 | } |