]>
Commit | Line | Data |
---|---|---|
152fcccd JB |
1 | <?php |
2 | ||
3 | namespace Wallabag\UserBundle\Controller; | |
4 | ||
5 | use FOS\UserBundle\Event\UserEvent; | |
6 | use FOS\UserBundle\FOSUserEvents; | |
50cfd810 TC |
7 | use Pagerfanta\Adapter\DoctrineORMAdapter; |
8 | use Pagerfanta\Exception\OutOfRangeCurrentPageException; | |
9 | use Pagerfanta\Pagerfanta; | |
f808b016 | 10 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
a6b242a1 | 11 | use Symfony\Component\Form\FormInterface; |
f808b016 | 12 | use Symfony\Component\HttpFoundation\Request; |
115de64e | 13 | use Symfony\Component\Routing\Annotation\Route; |
152fcccd | 14 | use Wallabag\UserBundle\Entity\User; |
c37515f8 | 15 | use Wallabag\UserBundle\Form\SearchUserType; |
152fcccd JB |
16 | |
17 | /** | |
18 | * User controller. | |
19 | */ | |
20 | class ManageController extends Controller | |
21 | { | |
152fcccd JB |
22 | /** |
23 | * Creates a new User entity. | |
24 | * | |
115de64e | 25 | * @Route("/new", name="user_new", methods={"GET", "POST"}) |
152fcccd JB |
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 | ||
a6b242a1 | 35 | $form = $this->createEditForm('NewUserType', $user, $request); |
152fcccd JB |
36 | |
37 | if ($form->isSubmitted() && $form->isValid()) { | |
a6b242a1 | 38 | $user = $this->handleOtp($form, $user); |
152fcccd JB |
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 | ||
f808b016 | 50 | return $this->redirectToRoute('user_edit', ['id' => $user->getId()]); |
152fcccd JB |
51 | } |
52 | ||
f808b016 | 53 | return $this->render('WallabagUserBundle:Manage:new.html.twig', [ |
152fcccd JB |
54 | 'user' => $user, |
55 | 'form' => $form->createView(), | |
f808b016 | 56 | ]); |
152fcccd JB |
57 | } |
58 | ||
59 | /** | |
60 | * Displays a form to edit an existing User entity. | |
61 | * | |
115de64e | 62 | * @Route("/{id}/edit", name="user_edit", methods={"GET", "POST"}) |
152fcccd JB |
63 | */ |
64 | public function editAction(Request $request, User $user) | |
65 | { | |
a6b242a1 JB |
66 | $userManager = $this->container->get('fos_user.user_manager'); |
67 | ||
152fcccd | 68 | $deleteForm = $this->createDeleteForm($user); |
a6b242a1 | 69 | $form = $this->createEditForm('UserType', $user, $request); |
152fcccd | 70 | |
a6b242a1 JB |
71 | if ($form->isSubmitted() && $form->isValid()) { |
72 | $user = $this->handleOtp($form, $user); | |
73 | $userManager->updateUser($user); | |
152fcccd JB |
74 | |
75 | $this->get('session')->getFlashBag()->add( | |
76 | 'notice', | |
77 | $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()]) | |
78 | ); | |
79 | ||
f808b016 | 80 | return $this->redirectToRoute('user_edit', ['id' => $user->getId()]); |
152fcccd JB |
81 | } |
82 | ||
f808b016 | 83 | return $this->render('WallabagUserBundle:Manage:edit.html.twig', [ |
152fcccd | 84 | 'user' => $user, |
a6b242a1 | 85 | 'edit_form' => $form->createView(), |
152fcccd JB |
86 | 'delete_form' => $deleteForm->createView(), |
87 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), | |
f808b016 | 88 | ]); |
152fcccd JB |
89 | } |
90 | ||
91 | /** | |
92 | * Deletes a User entity. | |
93 | * | |
115de64e | 94 | * @Route("/{id}", name="user_delete", methods={"DELETE"}) |
152fcccd JB |
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 | ||
c37515f8 TC |
115 | /** |
116 | * @param Request $request | |
117 | * @param int $page | |
118 | * | |
b5b68779 | 119 | * @Route("/list/{page}", name="user_index", defaults={"page" = 1}) |
c37515f8 TC |
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 | */ | |
b5b68779 | 126 | public function searchFormAction(Request $request, $page = 1) |
c37515f8 | 127 | { |
b5b68779 NL |
128 | $em = $this->getDoctrine()->getManager(); |
129 | $qb = $em->getRepository('WallabagUserBundle:User')->createQueryBuilder('u'); | |
c37515f8 TC |
130 | |
131 | $form = $this->createForm(SearchUserType::class); | |
c37515f8 TC |
132 | $form->handleRequest($request); |
133 | ||
134 | if ($form->isSubmitted() && $form->isValid()) { | |
135 | $this->get('logger')->info('searching users'); | |
c37515f8 TC |
136 | |
137 | $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : ''); | |
138 | ||
50cfd810 | 139 | $qb = $em->getRepository('WallabagUserBundle:User')->getQueryBuilderForSearch($searchTerm); |
b5b68779 | 140 | } |
50cfd810 | 141 | |
b5b68779 NL |
142 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); |
143 | $pagerFanta = new Pagerfanta($pagerAdapter); | |
144 | $pagerFanta->setMaxPerPage(50); | |
50cfd810 | 145 | |
b5b68779 NL |
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); | |
50cfd810 | 151 | } |
c37515f8 TC |
152 | } |
153 | ||
b5b68779 NL |
154 | return $this->render('WallabagUserBundle:Manage:index.html.twig', [ |
155 | 'searchForm' => $form->createView(), | |
156 | 'users' => $pagerFanta, | |
c37515f8 TC |
157 | ]); |
158 | } | |
f808b016 JB |
159 | |
160 | /** | |
a6b242a1 | 161 | * Create a form to delete a User entity. |
f808b016 JB |
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 | } | |
a6b242a1 JB |
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 | } | |
152fcccd | 221 | } |