]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/GroupBundle/Controller/ManageController.php
WIP
[github/wallabag/wallabag.git] / src / Wallabag / GroupBundle / Controller / ManageController.php
1 <?php
2
3 namespace Wallabag\GroupBundle\Controller;
4
5 use Pagerfanta\Adapter\DoctrineORMAdapter;
6 use Pagerfanta\Exception\OutOfRangeCurrentPageException;
7 use Pagerfanta\Pagerfanta;
8 use Strut\StrutBundle\Service\Sha256Salted;
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;
13 use Symfony\Component\HttpFoundation\Response;
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;
19
20 /**
21 * Group controller.
22 */
23 class ManageController extends Controller
24 {
25 /**
26 * Lists all public Group entities.
27 *
28 * @Route("/{page}", name="group_index", defaults={"page" = "1"})
29 * @Method("GET")
30 */
31 public function indexAction($page = 1)
32 {
33 $em = $this->getDoctrine()->getManager();
34
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 }
48
49 return $this->render('WallabagGroupBundle:Manage:index.html.twig', array(
50 'groups' => $pagerFanta,
51 'currentPage' => $page,
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 {
63 $group = new Group();
64
65 $form = $this->createForm(NewGroupType::class, $group);
66 $form->handleRequest($request);
67
68 if ($form->isSubmitted() && $form->isValid()) {
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
78 $em->persist($group);
79
80 $groupUser = new UserGroup($this->getUser(), $group, Group::ROLE_ADMIN);
81 $groupUser->setAccepted(true);
82 $em->persist($groupUser);
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 {
107 if ($this->getUser()->getGroupRoleForUser($group) < Group::ROLE_ADMIN) {
108 $this->createAccessDeniedException();
109 }
110
111 $deleteForm = $this->createDeleteForm($group);
112 $editForm = $this->createForm(GroupType::class, $group);
113 $editForm->handleRequest($request);
114
115 if ($editForm->isSubmitted() && $editForm->isValid()) {
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
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 }
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 }
211 }