3 namespace Wallabag\UserBundle\Controller
;
5 use FOS\UserBundle\Event\UserEvent
;
6 use FOS\UserBundle\FOSUserEvents
;
7 use Symfony\Component\HttpFoundation\Request
;
8 use Symfony\Bundle\FrameworkBundle\Controller\Controller
;
9 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method
;
10 use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route
;
11 use Wallabag\UserBundle\Entity\User
;
12 use Wallabag\CoreBundle\Entity\Config
;
13 use Wallabag\UserBundle\Form\SearchUserType
;
18 class ManageController
extends Controller
21 * Lists all User entities.
23 * @Route("/", name="user_index")
26 public function indexAction()
28 $em = $this->getDoctrine()->getManager();
30 $users = $em->getRepository('WallabagUserBundle:User')->findAll();
32 return $this->render('WallabagUserBundle:Manage:index.html.twig', array(
38 * Creates a new User entity.
40 * @Route("/new", name="user_new")
41 * @Method({"GET", "POST"})
43 public function newAction(Request
$request)
45 $userManager = $this->container
->get('fos_user.user_manager');
47 $user = $userManager->createUser();
48 // enable created user by default
49 $user->setEnabled(true);
51 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user, [
52 'validation_groups' => ['Profile'],
54 $form->handleRequest($request);
56 if ($form->isSubmitted() && $form->isValid()) {
57 $userManager->updateUser($user);
59 // dispatch a created event so the associated config will be created
60 $event = new UserEvent($user, $request);
61 $this->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
63 $this->get('session')->getFlashBag()->add(
65 $this->get('translator')->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
68 return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
71 return $this->render('WallabagUserBundle:Manage:new.html.twig', array(
73 'form' => $form->createView(),
78 * Displays a form to edit an existing User entity.
80 * @Route("/{id}/edit", name="user_edit")
81 * @Method({"GET", "POST"})
83 public function editAction(Request
$request, User
$user)
85 $deleteForm = $this->createDeleteForm($user);
86 $editForm = $this->createForm('Wallabag\UserBundle\Form\UserType', $user);
87 $editForm->handleRequest($request);
89 if ($editForm->isSubmitted() && $editForm->isValid()) {
90 $em = $this->getDoctrine()->getManager();
94 $this->get('session')->getFlashBag()->add(
96 $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
99 return $this->redirectToRoute('user_edit', array('id' => $user->getId()));
102 return $this->render('WallabagUserBundle:Manage:edit.html.twig', array(
104 'edit_form' => $editForm->createView(),
105 'delete_form' => $deleteForm->createView(),
106 'twofactor_auth' => $this->getParameter('twofactor_auth'),
111 * Deletes a User entity.
113 * @Route("/{id}", name="user_delete")
116 public function deleteAction(Request
$request, User
$user)
118 $form = $this->createDeleteForm($user);
119 $form->handleRequest($request);
121 if ($form->isSubmitted() && $form->isValid()) {
122 $this->get('session')->getFlashBag()->add(
124 $this->get('translator')->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
127 $em = $this->getDoctrine()->getManager();
132 return $this->redirectToRoute('user_index');
136 * Creates a form to delete a User entity.
138 * @param User $user The User entity
140 * @return \Symfony\Component\Form\Form The form
142 private function createDeleteForm(User
$user)
144 return $this->createFormBuilder()
145 ->setAction($this->generateUrl('user_delete', array('id' => $user->getId())))
146 ->setMethod('DELETE')
152 * @param Request $request
155 * @Route("/search/{page}", name="user-search", defaults={"page" = 1})
157 * Default parameter for page is hardcoded (in duplication of the defaults from the Route)
158 * because this controller is also called inside the layout template without any page as argument
160 * @return \Symfony\Component\HttpFoundation\Response
162 public function searchFormAction(Request
$request, $page = 1, $currentRoute = null)
164 // fallback to retrieve currentRoute from query parameter instead of injected one (when using inside a template)
165 if (null === $currentRoute && $request->query
->has('currentRoute')) {
166 $currentRoute = $request->query
->get('currentRoute');
169 $form = $this->createForm(SearchUserType
::class);
171 $form->handleRequest($request);
173 if ($form->isSubmitted() && $form->isValid()) {
174 $this->get('logger')->info('searching users');
175 $em = $this->getDoctrine()->getManager();
177 $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : '');
179 $users = $em->getRepository('WallabagUserBundle:User')->getUsersForSearch($searchTerm);
181 return $this->render('WallabagUserBundle:Manage:index.html.twig', array(
186 return $this->render('WallabagUserBundle:Manage:search_form.html.twig', [
187 'form' => $form->createView(),
188 'currentRoute' => $currentRoute,