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\Form\FormInterface
;
12 use Symfony\Component\HttpFoundation\Request
;
13 use Symfony\Component\Routing\Annotation\Route
;
14 use Wallabag\UserBundle\Entity\User
;
15 use Wallabag\UserBundle\Form\SearchUserType
;
20 class ManageController
extends Controller
23 * Creates a new User entity.
25 * @Route("/new", name="user_new", methods={"GET", "POST"})
27 public function newAction(Request
$request)
29 $userManager = $this->container
->get('fos_user.user_manager');
31 $user = $userManager->createUser();
32 // enable created user by default
33 $user->setEnabled(true);
35 $form = $this->createEditForm('NewUserType', $user, $request);
37 if ($form->isSubmitted() && $form->isValid()) {
38 $user = $this->handleOtp($form, $user);
39 $userManager->updateUser($user);
41 // dispatch a created event so the associated config will be created
42 $event = new UserEvent($user, $request);
43 $this->get('event_dispatcher')->dispatch(FOSUserEvents
::USER_CREATED
, $event);
45 $this->get('session')->getFlashBag()->add(
47 $this->get('translator')->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
50 return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
53 return $this->render('WallabagUserBundle:Manage:new.html.twig', [
55 'form' => $form->createView(),
60 * Displays a form to edit an existing User entity.
62 * @Route("/{id}/edit", name="user_edit", methods={"GET", "POST"})
64 public function editAction(Request
$request, User
$user)
66 $userManager = $this->container
->get('fos_user.user_manager');
68 $deleteForm = $this->createDeleteForm($user);
69 $form = $this->createEditForm('UserType', $user, $request);
71 if ($form->isSubmitted() && $form->isValid()) {
72 $user = $this->handleOtp($form, $user);
73 $userManager->updateUser($user);
75 $this->get('session')->getFlashBag()->add(
77 $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
80 return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
83 return $this->render('WallabagUserBundle:Manage:edit.html.twig', [
85 'edit_form' => $form->createView(),
86 'delete_form' => $deleteForm->createView(),
87 'twofactor_auth' => $this->getParameter('twofactor_auth'),
92 * Deletes a User entity.
94 * @Route("/{id}", name="user_delete", methods={"DELETE"})
96 public function deleteAction(Request
$request, User
$user)
98 $form = $this->createDeleteForm($user);
99 $form->handleRequest($request);
101 if ($form->isSubmitted() && $form->isValid()) {
102 $this->get('session')->getFlashBag()->add(
104 $this->get('translator')->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
107 $em = $this->getDoctrine()->getManager();
112 return $this->redirectToRoute('user_index');
116 * @param Request $request
119 * @Route("/list/{page}", name="user_index", defaults={"page" = 1})
121 * Default parameter for page is hardcoded (in duplication of the defaults from the Route)
122 * because this controller is also called inside the layout template without any page as argument
124 * @return \Symfony\Component\HttpFoundation\Response
126 public function searchFormAction(Request
$request, $page = 1)
128 $em = $this->getDoctrine()->getManager();
129 $qb = $em->getRepository('WallabagUserBundle:User')->createQueryBuilder('u');
131 $form = $this->createForm(SearchUserType
::class);
132 $form->handleRequest($request);
134 if ($form->isSubmitted() && $form->isValid()) {
135 $this->get('logger')->info('searching users');
137 $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : '');
139 $qb = $em->getRepository('WallabagUserBundle:User')->getQueryBuilderForSearch($searchTerm);
142 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
143 $pagerFanta = new Pagerfanta($pagerAdapter);
144 $pagerFanta->setMaxPerPage(50);
147 $pagerFanta->setCurrentPage($page);
148 } catch (OutOfRangeCurrentPageException
$e) {
150 return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302);
154 return $this->render('WallabagUserBundle:Manage:index.html.twig', [
155 'searchForm' => $form->createView(),
156 'users' => $pagerFanta,
161 * Create a form to delete a User entity.
163 * @param User $user The User entity
165 * @return \Symfony\Component\Form\Form The form
167 private function createDeleteForm(User
$user)
169 return $this->createFormBuilder()
170 ->setAction($this->generateUrl('user_delete', ['id' => $user->getId()]))
171 ->setMethod('DELETE')
177 * Create a form to create or edit a User entity.
179 * @param string $type Might be NewUserType or UserType
180 * @param User $user The new / edit user
181 * @param Request $request The request
183 * @return FormInterface
185 private function createEditForm($type, User
$user, Request
$request)
187 $form = $this->createForm('Wallabag\UserBundle\Form\\' . $type, $user);
188 $form->handleRequest($request);
190 // `googleTwoFactor` isn't a field within the User entity, we need to define it's value in a different way
191 if (true === $user->isGoogleAuthenticatorEnabled() && false === $form->isSubmitted()) {
192 $form->get('googleTwoFactor')->setData(true);
199 * Handle OTP update, taking care to only have one 2fa enable at a time.
201 * @see ConfigController
203 * @param FormInterface $form
208 private function handleOtp(FormInterface
$form, User
$user)
210 if (true === $form->get('googleTwoFactor')->getData() && false === $user->isGoogleAuthenticatorEnabled()) {
211 $user->setGoogleAuthenticatorSecret($this->get('scheb_two_factor.security.google_authenticator')->generateSecret());
212 $user->setEmailTwoFactor(false);
217 $user->setGoogleAuthenticatorSecret(null);