]>
Commit | Line | Data |
---|---|---|
36f30fa3 NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\GroupBundle\Controller; | |
4 | ||
2041810a TC |
5 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
6 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | |
7 | use Pagerfanta\Pagerfanta; | |
8 | use Strut\StrutBundle\Service\Sha256Salted; | |
36f30fa3 NL |
9 | use Symfony\Component\HttpFoundation\Request; |
10 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
11 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method; | |
12 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
2041810a | 13 | use Symfony\Component\HttpFoundation\Response; |
36f30fa3 | 14 | use Wallabag\GroupBundle\Entity\Group; |
2041810a TC |
15 | use Wallabag\GroupBundle\Entity\UserGroup; |
16 | use Wallabag\GroupBundle\Form\GroupType; | |
17 | use Wallabag\GroupBundle\Form\NewGroupType; | |
18 | use Wallabag\UserBundle\Entity\User; | |
36f30fa3 NL |
19 | |
20 | /** | |
21 | * Group controller. | |
22 | */ | |
23 | class ManageController extends Controller | |
24 | { | |
25 | /** | |
2041810a | 26 | * Lists all public Group entities. |
36f30fa3 | 27 | * |
2585953e | 28 | * @Route("/{page}", requirements={"page" = "\d+"}, name="group_index", defaults={"page" = "1"}) |
36f30fa3 NL |
29 | * @Method("GET") |
30 | */ | |
2041810a | 31 | public function indexAction($page = 1) |
36f30fa3 NL |
32 | { |
33 | $em = $this->getDoctrine()->getManager(); | |
34 | ||
2041810a TC |
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 | } | |
36f30fa3 NL |
48 | |
49 | return $this->render('WallabagGroupBundle:Manage:index.html.twig', array( | |
2041810a TC |
50 | 'groups' => $pagerFanta, |
51 | 'currentPage' => $page, | |
36f30fa3 NL |
52 | )); |
53 | } | |
54 | ||
55 | /** | |
56 | * Creates a new Group entity. | |
57 | * | |
58 | * @Route("/new", name="group_new") | |
59 | * @Method({"GET", "POST"}) | |
60 | */ | |
61 | public function newAction(Request $request) | |
62 | { | |
2041810a | 63 | $group = new Group(); |
36f30fa3 | 64 | |
2041810a | 65 | $form = $this->createForm(NewGroupType::class, $group); |
36f30fa3 NL |
66 | $form->handleRequest($request); |
67 | ||
68 | if ($form->isSubmitted() && $form->isValid()) { | |
69 | $em = $this->getDoctrine()->getManager(); | |
2041810a TC |
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 | ||
36f30fa3 | 78 | $em->persist($group); |
2041810a TC |
79 | |
80 | $groupUser = new UserGroup($this->getUser(), $group, Group::ROLE_ADMIN); | |
81 | $groupUser->setAccepted(true); | |
82 | $em->persist($groupUser); | |
36f30fa3 NL |
83 | $em->flush(); |
84 | ||
85 | $this->get('session')->getFlashBag()->add( | |
86 | 'notice', | |
87 | $this->get('translator')->trans('flashes.group.notice.added', ['%name%' => $group->getName()]) | |
88 | ); | |
89 | ||
90 | return $this->redirectToRoute('group_edit', array('id' => $group->getId())); | |
91 | } | |
92 | ||
93 | return $this->render('WallabagGroupBundle:Manage:new.html.twig', array( | |
94 | 'group' => $group, | |
95 | 'form' => $form->createView(), | |
96 | )); | |
97 | } | |
98 | ||
99 | /** | |
100 | * Displays a form to edit an existing Group entity. | |
101 | * | |
102 | * @Route("/{id}/edit", name="group_edit") | |
103 | * @Method({"GET", "POST"}) | |
104 | */ | |
105 | public function editAction(Request $request, Group $group) | |
106 | { | |
2041810a TC |
107 | if ($this->getUser()->getGroupRoleForUser($group) < Group::ROLE_ADMIN) { |
108 | $this->createAccessDeniedException(); | |
109 | } | |
110 | ||
36f30fa3 | 111 | $deleteForm = $this->createDeleteForm($group); |
2041810a | 112 | $editForm = $this->createForm(GroupType::class, $group); |
36f30fa3 NL |
113 | $editForm->handleRequest($request); |
114 | ||
115 | if ($editForm->isSubmitted() && $editForm->isValid()) { | |
116 | $em = $this->getDoctrine()->getManager(); | |
2041810a TC |
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 | ||
36f30fa3 NL |
124 | $em->persist($group); |
125 | $em->flush(); | |
126 | ||
127 | $this->get('session')->getFlashBag()->add( | |
128 | 'notice', | |
129 | $this->get('translator')->trans('flashes.group.notice.updated', ['%name%' => $group->getName()]) | |
130 | ); | |
131 | ||
132 | return $this->redirectToRoute('group_edit', array('id' => $group->getId())); | |
133 | } | |
134 | ||
135 | return $this->render('WallabagGroupBundle:Manage:edit.html.twig', array( | |
136 | 'group' => $group, | |
137 | 'edit_form' => $editForm->createView(), | |
138 | 'delete_form' => $deleteForm->createView(), | |
139 | )); | |
140 | } | |
141 | ||
142 | /** | |
143 | * Deletes a Group entity. | |
144 | * | |
145 | * @Route("/{id}", name="group_delete") | |
146 | * @Method("DELETE") | |
147 | */ | |
148 | public function deleteAction(Request $request, Group $group) | |
149 | { | |
150 | $form = $this->createDeleteForm($group); | |
151 | $form->handleRequest($request); | |
152 | ||
153 | if ($form->isSubmitted() && $form->isValid()) { | |
154 | $this->get('session')->getFlashBag()->add( | |
155 | 'notice', | |
156 | $this->get('translator')->trans('flashes.group.notice.deleted', ['%name%' => $group->getName()]) | |
157 | ); | |
158 | ||
159 | $em = $this->getDoctrine()->getManager(); | |
160 | $em->remove($group); | |
161 | $em->flush(); | |
162 | } | |
163 | ||
164 | return $this->redirectToRoute('group_index'); | |
165 | } | |
166 | ||
167 | /** | |
168 | * Creates a form to delete a Group entity. | |
169 | * | |
170 | * @param Group $group The Group entity | |
171 | * | |
172 | * @return \Symfony\Component\Form\Form The form | |
173 | */ | |
174 | private function createDeleteForm(Group $group) | |
175 | { | |
176 | return $this->createFormBuilder() | |
177 | ->setAction($this->generateUrl('group_delete', array('id' => $group->getId()))) | |
178 | ->setMethod('DELETE') | |
179 | ->getForm() | |
180 | ; | |
181 | } | |
2041810a TC |
182 | |
183 | /** | |
184 | * @Route("/group-user-exclude/{group}/{user}", name="group-user-exclude") | |
2585953e | 185 | * |
2041810a | 186 | * @param Group $group |
2585953e NL |
187 | * @param User $user |
188 | * | |
2041810a TC |
189 | * @return Response |
190 | */ | |
191 | public function excludeMemberAction(Group $group, User $user) | |
192 | { | |
193 | $logger = $this->get('logger'); | |
2585953e | 194 | $logger->info('User '.$this->getUser()->getUsername().' wants to exclude user '.$user->getUsername().' from group '.$group->getName()); |
2041810a TC |
195 | |
196 | if (!$this->getUser()->inGroup($group) || $this->getUser()->getGroupRoleForUser($group) < Group::ROLE_MANAGE_USERS) { | |
2585953e | 197 | $logger->info('User '.$this->getUser()->getUsername().' has not enough rights on group '.$group->getName().' to exclude user '.$user->getUsername()); |
2041810a TC |
198 | throw $this->createAccessDeniedException(); |
199 | } | |
200 | ||
201 | if ($user->inGroup($group) && $user->getGroupRoleForUser($group) < Group::ROLE_ADMIN) { | |
202 | $em = $this->getDoctrine()->getManager(); | |
203 | ||
2585953e | 204 | $logger->info('Removing user '.$this->getUser()->getUsername().' from group '.$group->getName()); |
2041810a TC |
205 | $em->remove($this->getUser()->getUserGroupFromGroup($group)); |
206 | ||
207 | $em->flush(); | |
208 | ||
209 | return $this->redirectToRoute('group-manage', ['group' => $group->getId()]); | |
210 | } | |
211 | throw $this->createAccessDeniedException(); | |
212 | } | |
36f30fa3 | 213 | } |