]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Controller/ManageController.php
Enable OTP 2FA
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / Controller / ManageController.php
1 <?php
2
3 namespace Wallabag\UserBundle\Controller;
4
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;
16
17 /**
18 * User controller.
19 */
20 class ManageController extends Controller
21 {
22 /**
23 * Creates a new User entity.
24 *
25 * @Route("/new", name="user_new", methods={"GET", "POST"})
26 */
27 public function newAction(Request $request)
28 {
29 $userManager = $this->container->get('fos_user.user_manager');
30
31 $user = $userManager->createUser();
32 // enable created user by default
33 $user->setEnabled(true);
34
35 $form = $this->createEditForm('NewUserType', $user, $request);
36
37 if ($form->isSubmitted() && $form->isValid()) {
38 $user = $this->handleOtp($form, $user);
39 $userManager->updateUser($user);
40
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);
44
45 $this->get('session')->getFlashBag()->add(
46 'notice',
47 $this->get('translator')->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
48 );
49
50 return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
51 }
52
53 return $this->render('WallabagUserBundle:Manage:new.html.twig', [
54 'user' => $user,
55 'form' => $form->createView(),
56 ]);
57 }
58
59 /**
60 * Displays a form to edit an existing User entity.
61 *
62 * @Route("/{id}/edit", name="user_edit", methods={"GET", "POST"})
63 */
64 public function editAction(Request $request, User $user)
65 {
66 $userManager = $this->container->get('fos_user.user_manager');
67
68 $deleteForm = $this->createDeleteForm($user);
69 $form = $this->createEditForm('UserType', $user, $request);
70
71 if ($form->isSubmitted() && $form->isValid()) {
72 $user = $this->handleOtp($form, $user);
73 $userManager->updateUser($user);
74
75 $this->get('session')->getFlashBag()->add(
76 'notice',
77 $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
78 );
79
80 return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
81 }
82
83 return $this->render('WallabagUserBundle:Manage:edit.html.twig', [
84 'user' => $user,
85 'edit_form' => $form->createView(),
86 'delete_form' => $deleteForm->createView(),
87 'twofactor_auth' => $this->getParameter('twofactor_auth'),
88 ]);
89 }
90
91 /**
92 * Deletes a User entity.
93 *
94 * @Route("/{id}", name="user_delete", methods={"DELETE"})
95 */
96 public function deleteAction(Request $request, User $user)
97 {
98 $form = $this->createDeleteForm($user);
99 $form->handleRequest($request);
100
101 if ($form->isSubmitted() && $form->isValid()) {
102 $this->get('session')->getFlashBag()->add(
103 'notice',
104 $this->get('translator')->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
105 );
106
107 $em = $this->getDoctrine()->getManager();
108 $em->remove($user);
109 $em->flush();
110 }
111
112 return $this->redirectToRoute('user_index');
113 }
114
115 /**
116 * @param Request $request
117 * @param int $page
118 *
119 * @Route("/list/{page}", name="user_index", defaults={"page" = 1})
120 *
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
123 *
124 * @return \Symfony\Component\HttpFoundation\Response
125 */
126 public function searchFormAction(Request $request, $page = 1)
127 {
128 $em = $this->getDoctrine()->getManager();
129 $qb = $em->getRepository('WallabagUserBundle:User')->createQueryBuilder('u');
130
131 $form = $this->createForm(SearchUserType::class);
132 $form->handleRequest($request);
133
134 if ($form->isSubmitted() && $form->isValid()) {
135 $this->get('logger')->info('searching users');
136
137 $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : '');
138
139 $qb = $em->getRepository('WallabagUserBundle:User')->getQueryBuilderForSearch($searchTerm);
140 }
141
142 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
143 $pagerFanta = new Pagerfanta($pagerAdapter);
144 $pagerFanta->setMaxPerPage(50);
145
146 try {
147 $pagerFanta->setCurrentPage($page);
148 } catch (OutOfRangeCurrentPageException $e) {
149 if ($page > 1) {
150 return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302);
151 }
152 }
153
154 return $this->render('WallabagUserBundle:Manage:index.html.twig', [
155 'searchForm' => $form->createView(),
156 'users' => $pagerFanta,
157 ]);
158 }
159
160 /**
161 * Create a form to delete a User entity.
162 *
163 * @param User $user The User entity
164 *
165 * @return \Symfony\Component\Form\Form The form
166 */
167 private function createDeleteForm(User $user)
168 {
169 return $this->createFormBuilder()
170 ->setAction($this->generateUrl('user_delete', ['id' => $user->getId()]))
171 ->setMethod('DELETE')
172 ->getForm()
173 ;
174 }
175
176 /**
177 * Create a form to create or edit a User entity.
178 *
179 * @param string $type Might be NewUserType or UserType
180 * @param User $user The new / edit user
181 * @param Request $request The request
182 *
183 * @return FormInterface
184 */
185 private function createEditForm($type, User $user, Request $request)
186 {
187 $form = $this->createForm('Wallabag\UserBundle\Form\\' . $type, $user);
188 $form->handleRequest($request);
189
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);
193 }
194
195 return $form;
196 }
197
198 /**
199 * Handle OTP update, taking care to only have one 2fa enable at a time.
200 *
201 * @see ConfigController
202 *
203 * @param FormInterface $form
204 * @param User $user
205 *
206 * @return User
207 */
208 private function handleOtp(FormInterface $form, User $user)
209 {
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);
213
214 return $user;
215 }
216
217 $user->setGoogleAuthenticatorSecret(null);
218
219 return $user;
220 }
221 }