3 namespace Wallabag\UserBundle\Controller
;
5 use FOS\UserBundle\Event\UserEvent
;
6 use FOS\UserBundle\FOSUserEvents
;
7 use Pagerfanta\Adapter\DoctrineORMAdapter
;
8 use Pagerfanta\Exception\OutOfRangeCurrentPageException
;
9 use Pagerfanta\Pagerfanta
;
10 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
11 use Symfony\Component\HttpFoundation\Request
;
12 use Symfony\Component\Routing\Annotation\Route
;
13 use Wallabag\UserBundle\Entity\User
;
14 use Wallabag\UserBundle\Form\SearchUserType
;
19 class ManageController
extends Controller
22 * Creates a new User entity.
24 * @Route("/new", name="user_new", methods={"GET", "POST"})
26 public function newAction(Request
$request)
28 $userManager = $this->container
->get('fos_user.user_manager');
30 $user = $userManager->createUser();
31 // enable created user by default
32 $user->setEnabled(true);
34 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user);
35 $form->handleRequest($request);
37 if ($form->isSubmitted() && $form->isValid()) {
38 $userManager->updateUser($user);
40 // dispatch a created event so the associated config will be created
41 $event = new UserEvent($user, $request);
42 $this->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
44 $this->get('session')->getFlashBag()->add(
46 $this->get('translator')->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
49 return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
52 return $this->render('WallabagUserBundle:Manage:new.html.twig', [
54 'form' => $form->createView(),
59 * Displays a form to edit an existing User entity.
61 * @Route("/{id}/edit", name="user_edit", methods={"GET", "POST"})
63 public function editAction(Request
$request, User
$user)
65 $deleteForm = $this->createDeleteForm($user);
66 $editForm = $this->createForm('Wallabag\UserBundle\Form\UserType', $user);
67 $editForm->handleRequest($request);
69 if ($editForm->isSubmitted() && $editForm->isValid()) {
70 $em = $this->getDoctrine()->getManager();
74 $this->get('session')->getFlashBag()->add(
76 $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
79 return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
82 return $this->render('WallabagUserBundle:Manage:edit.html.twig', [
84 'edit_form' => $editForm->createView(),
85 'delete_form' => $deleteForm->createView(),
86 'twofactor_auth' => $this->getParameter('twofactor_auth'),
91 * Deletes a User entity.
93 * @Route("/{id}", name="user_delete", methods={"DELETE"})
95 public function deleteAction(Request
$request, User
$user)
97 $form = $this->createDeleteForm($user);
98 $form->handleRequest($request);
100 if ($form->isSubmitted() && $form->isValid()) {
101 $this->get('session')->getFlashBag()->add(
103 $this->get('translator')->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
106 $em = $this->getDoctrine()->getManager();
111 return $this->redirectToRoute('user_index');
115 * @param Request $request
118 * @Route("/list/{page}", name="user_index", defaults={"page" = 1})
120 * Default parameter for page is hardcoded (in duplication of the defaults from the Route)
121 * because this controller is also called inside the layout template without any page as argument
123 * @return \Symfony\Component\HttpFoundation\Response
125 public function searchFormAction(Request
$request, $page = 1)
127 $em = $this->getDoctrine()->getManager();
128 $qb = $em->getRepository('WallabagUserBundle:User')->createQueryBuilder('u');
130 $form = $this->createForm(SearchUserType
::class);
131 $form->handleRequest($request);
133 if ($form->isSubmitted() && $form->isValid()) {
134 $this->get('logger')->info('searching users');
136 $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : '');
138 $qb = $em->getRepository('WallabagUserBundle:User')->getQueryBuilderForSearch($searchTerm);
141 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
142 $pagerFanta = new Pagerfanta($pagerAdapter);
143 $pagerFanta->setMaxPerPage(50);
146 $pagerFanta->setCurrentPage($page);
147 } catch (OutOfRangeCurrentPageException
$e) {
149 return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302);
153 return $this->render('WallabagUserBundle:Manage:index.html.twig', [
154 'searchForm' => $form->createView(),
155 'users' => $pagerFanta,
160 * Creates a form to delete a User entity.
162 * @param User $user The User entity
164 * @return \Symfony\Component\Form\Form The form
166 private function createDeleteForm(User
$user)
168 return $this->createFormBuilder()
169 ->setAction($this->generateUrl('user_delete', ['id' => $user->getId()]))
170 ->setMethod('DELETE')