]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/UserBundle/Controller/ManageController.php
63a06206150d6cba59766a6adeaec345847e4f1a
[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\HttpFoundation\Request;
12 use Symfony\Component\Routing\Annotation\Route;
13 use Wallabag\UserBundle\Entity\User;
14 use Wallabag\UserBundle\Form\SearchUserType;
15
16 /**
17 * User controller.
18 */
19 class ManageController extends Controller
20 {
21 /**
22 * Creates a new User entity.
23 *
24 * @Route("/new", name="user_new", methods={"GET", "POST"})
25 */
26 public function newAction(Request $request)
27 {
28 $userManager = $this->container->get('fos_user.user_manager');
29
30 $user = $userManager->createUser();
31 // enable created user by default
32 $user->setEnabled(true);
33
34 $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user);
35 $form->handleRequest($request);
36
37 if ($form->isSubmitted() && $form->isValid()) {
38 $userManager->updateUser($user);
39
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);
43
44 $this->get('session')->getFlashBag()->add(
45 'notice',
46 $this->get('translator')->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
47 );
48
49 return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
50 }
51
52 return $this->render('WallabagUserBundle:Manage:new.html.twig', [
53 'user' => $user,
54 'form' => $form->createView(),
55 ]);
56 }
57
58 /**
59 * Displays a form to edit an existing User entity.
60 *
61 * @Route("/{id}/edit", name="user_edit", methods={"GET", "POST"})
62 */
63 public function editAction(Request $request, User $user)
64 {
65 $userManager = $this->container->get('fos_user.user_manager');
66
67 $deleteForm = $this->createDeleteForm($user);
68 $form = $this->createForm('Wallabag\UserBundle\Form\UserType', $user);
69 $form->handleRequest($request);
70
71 // `googleTwoFactor` isn't a field within the User entity, we need to define it's value in a different way
72 if ($this->getParameter('twofactor_auth') && true === $user->isGoogleAuthenticatorEnabled() && false === $form->isSubmitted()) {
73 $form->get('googleTwoFactor')->setData(true);
74 }
75
76 if ($form->isSubmitted() && $form->isValid()) {
77 // handle creation / reset of the OTP secret if checkbox changed from the previous state
78 if ($this->getParameter('twofactor_auth')) {
79 if (true === $form->get('googleTwoFactor')->getData() && false === $user->isGoogleAuthenticatorEnabled()) {
80 $user->setGoogleAuthenticatorSecret($this->get('scheb_two_factor.security.google_authenticator')->generateSecret());
81 $user->setEmailTwoFactor(false);
82 } elseif (false === $form->get('googleTwoFactor')->getData() && true === $user->isGoogleAuthenticatorEnabled()) {
83 $user->setGoogleAuthenticatorSecret(null);
84 }
85 }
86
87 $userManager->updateUser($user);
88
89 $this->get('session')->getFlashBag()->add(
90 'notice',
91 $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
92 );
93
94 return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
95 }
96
97 return $this->render('WallabagUserBundle:Manage:edit.html.twig', [
98 'user' => $user,
99 'edit_form' => $form->createView(),
100 'delete_form' => $deleteForm->createView(),
101 'twofactor_auth' => $this->getParameter('twofactor_auth'),
102 ]);
103 }
104
105 /**
106 * Deletes a User entity.
107 *
108 * @Route("/{id}", name="user_delete", methods={"DELETE"})
109 */
110 public function deleteAction(Request $request, User $user)
111 {
112 $form = $this->createDeleteForm($user);
113 $form->handleRequest($request);
114
115 if ($form->isSubmitted() && $form->isValid()) {
116 $this->get('session')->getFlashBag()->add(
117 'notice',
118 $this->get('translator')->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
119 );
120
121 $em = $this->getDoctrine()->getManager();
122 $em->remove($user);
123 $em->flush();
124 }
125
126 return $this->redirectToRoute('user_index');
127 }
128
129 /**
130 * @param Request $request
131 * @param int $page
132 *
133 * @Route("/list/{page}", name="user_index", defaults={"page" = 1})
134 *
135 * Default parameter for page is hardcoded (in duplication of the defaults from the Route)
136 * because this controller is also called inside the layout template without any page as argument
137 *
138 * @return \Symfony\Component\HttpFoundation\Response
139 */
140 public function searchFormAction(Request $request, $page = 1)
141 {
142 $em = $this->getDoctrine()->getManager();
143 $qb = $em->getRepository('WallabagUserBundle:User')->createQueryBuilder('u');
144
145 $form = $this->createForm(SearchUserType::class);
146 $form->handleRequest($request);
147
148 if ($form->isSubmitted() && $form->isValid()) {
149 $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : '');
150
151 $qb = $em->getRepository('WallabagUserBundle:User')->getQueryBuilderForSearch($searchTerm);
152 }
153
154 $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
155 $pagerFanta = new Pagerfanta($pagerAdapter);
156 $pagerFanta->setMaxPerPage(50);
157
158 try {
159 $pagerFanta->setCurrentPage($page);
160 } catch (OutOfRangeCurrentPageException $e) {
161 if ($page > 1) {
162 return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302);
163 }
164 }
165
166 return $this->render('WallabagUserBundle:Manage:index.html.twig', [
167 'searchForm' => $form->createView(),
168 'users' => $pagerFanta,
169 ]);
170 }
171
172 /**
173 * Create a form to delete a User entity.
174 *
175 * @param User $user The User entity
176 *
177 * @return \Symfony\Component\Form\Form The form
178 */
179 private function createDeleteForm(User $user)
180 {
181 return $this->createFormBuilder()
182 ->setAction($this->generateUrl('user_delete', ['id' => $user->getId()]))
183 ->setMethod('DELETE')
184 ->getForm()
185 ;
186 }
187 }