3 namespace Wallabag\GroupBundle\Controller
;
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
;
23 class ManageController
extends Controller
26 * Lists all public Group entities.
28 * @Route("/{page}", requirements={"page" = "\d+"}, name="group_index", defaults={"page" = "1"})
31 public function indexAction($page = 1)
33 $em = $this->getDoctrine()->getManager();
35 $groups = $em->getRepository('WallabagGroupBundle:Group')->findPublicGroups();
37 $pagerAdapter = new DoctrineORMAdapter($groups->getQuery(), true, false);
38 $pagerFanta = new Pagerfanta($pagerAdapter);
39 $pagerFanta->setMaxPerPage(1);
42 $pagerFanta->setCurrentPage($page);
43 } catch (OutOfRangeCurrentPageException
$e) {
45 return $this->redirect($this->generateUrl('group_index', ['page' => $pagerFanta->getNbPages()]), 302);
49 return $this->render('WallabagGroupBundle:Manage:index.html.twig', array(
50 'groups' => $pagerFanta,
51 'currentPage' => $page,
56 * Creates a new Group entity.
58 * @Route("/new", name="group_new")
59 * @Method({"GET", "POST"})
61 public function newAction(Request
$request)
65 $form = $this->createForm(NewGroupType
::class, $group);
66 $form->handleRequest($request);
68 if ($form->isSubmitted() && $form->isValid()) {
69 $em = $this->getDoctrine()->getManager();
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);
80 $groupUser = new UserGroup($this->getUser(), $group, Group
::ROLE_ADMIN
);
81 $groupUser->setAccepted(true);
82 $em->persist($groupUser);
85 $this->get('session')->getFlashBag()->add(
87 $this->get('translator')->trans('flashes.group.notice.added', ['%name%' => $group->getName()])
90 return $this->redirectToRoute('group_edit', array('id' => $group->getId()));
93 return $this->render('WallabagGroupBundle:Manage:new.html.twig', array(
95 'form' => $form->createView(),
100 * Displays a form to edit an existing Group entity.
102 * @Route("/{id}/edit", name="group_edit")
103 * @Method({"GET", "POST"})
105 public function editAction(Request
$request, Group
$group)
107 if ($this->getUser()->getGroupRoleForUser($group) < Group
::ROLE_ADMIN
) {
108 $this->createAccessDeniedException();
111 $deleteForm = $this->createDeleteForm($group);
112 $editForm = $this->createForm(GroupType
::class, $group);
113 $editForm->handleRequest($request);
115 if ($editForm->isSubmitted() && $editForm->isValid()) {
116 $em = $this->getDoctrine()->getManager();
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);
124 $em->persist($group);
127 $this->get('session')->getFlashBag()->add(
129 $this->get('translator')->trans('flashes.group.notice.updated', ['%name%' => $group->getName()])
132 return $this->redirectToRoute('group_edit', array('id' => $group->getId()));
135 return $this->render('WallabagGroupBundle:Manage:edit.html.twig', array(
137 'edit_form' => $editForm->createView(),
138 'delete_form' => $deleteForm->createView(),
143 * Deletes a Group entity.
145 * @Route("/{id}", name="group_delete")
148 public function deleteAction(Request
$request, Group
$group)
150 $form = $this->createDeleteForm($group);
151 $form->handleRequest($request);
153 if ($form->isSubmitted() && $form->isValid()) {
154 $this->get('session')->getFlashBag()->add(
156 $this->get('translator')->trans('flashes.group.notice.deleted', ['%name%' => $group->getName()])
159 $em = $this->getDoctrine()->getManager();
164 return $this->redirectToRoute('group_index');
168 * Creates a form to delete a Group entity.
170 * @param Group $group The Group entity
172 * @return \Symfony\Component\Form\Form The form
174 private function createDeleteForm(Group
$group)
176 return $this->createFormBuilder()
177 ->setAction($this->generateUrl('group_delete', array('id' => $group->getId())))
178 ->setMethod('DELETE')
184 * @Route("/group-user-exclude/{group}/{user}", name="group-user-exclude")
186 * @param Group $group
191 public function excludeMemberAction(Group
$group, User
$user)
193 $logger = $this->get('logger');
194 $logger->info('User '.$this->getUser()->getUsername().' wants to exclude user '.$user->getUsername().' from group '.$group->getName());
196 if (!$this->getUser()->inGroup($group) || $this->getUser()->getGroupRoleForUser($group) < Group
::ROLE_MANAGE_USERS
) {
197 $logger->info('User '.$this->getUser()->getUsername().' has not enough rights on group '.$group->getName().' to exclude user '.$user->getUsername());
198 throw $this->createAccessDeniedException();
201 if ($user->inGroup($group) && $user->getGroupRoleForUser($group) < Group
::ROLE_ADMIN
) {
202 $em = $this->getDoctrine()->getManager();
204 $logger->info('Removing user '.$this->getUser()->getUsername().' from group '.$group->getName());
205 $em->remove($this->getUser()->getUserGroupFromGroup($group));
209 return $this->redirectToRoute('group-manage', ['group' => $group->getId()]);
211 throw $this->createAccessDeniedException();