]>
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 JB |
10 | use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
11 | use Symfony\Component\HttpFoundation\Request; | |
115de64e | 12 | use Symfony\Component\Routing\Annotation\Route; |
152fcccd | 13 | use Wallabag\UserBundle\Entity\User; |
c37515f8 | 14 | use Wallabag\UserBundle\Form\SearchUserType; |
152fcccd JB |
15 | |
16 | /** | |
17 | * User controller. | |
18 | */ | |
19 | class ManageController extends Controller | |
20 | { | |
152fcccd JB |
21 | /** |
22 | * Creates a new User entity. | |
23 | * | |
115de64e | 24 | * @Route("/new", name="user_new", methods={"GET", "POST"}) |
152fcccd JB |
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 | ||
2dfbe9e5 JB |
34 | $form = $this->createForm('Wallabag\UserBundle\Form\NewUserType', $user); |
35 | $form->handleRequest($request); | |
152fcccd JB |
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 | ||
f808b016 | 49 | return $this->redirectToRoute('user_edit', ['id' => $user->getId()]); |
152fcccd JB |
50 | } |
51 | ||
f808b016 | 52 | return $this->render('WallabagUserBundle:Manage:new.html.twig', [ |
152fcccd JB |
53 | 'user' => $user, |
54 | 'form' => $form->createView(), | |
f808b016 | 55 | ]); |
152fcccd JB |
56 | } |
57 | ||
58 | /** | |
59 | * Displays a form to edit an existing User entity. | |
60 | * | |
115de64e | 61 | * @Route("/{id}/edit", name="user_edit", methods={"GET", "POST"}) |
152fcccd JB |
62 | */ |
63 | public function editAction(Request $request, User $user) | |
64 | { | |
a6b242a1 JB |
65 | $userManager = $this->container->get('fos_user.user_manager'); |
66 | ||
152fcccd | 67 | $deleteForm = $this->createDeleteForm($user); |
2dfbe9e5 JB |
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 | } | |
152fcccd | 75 | |
a6b242a1 | 76 | if ($form->isSubmitted() && $form->isValid()) { |
2dfbe9e5 JB |
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 | ||
a6b242a1 | 87 | $userManager->updateUser($user); |
152fcccd JB |
88 | |
89 | $this->get('session')->getFlashBag()->add( | |
90 | 'notice', | |
91 | $this->get('translator')->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()]) | |
92 | ); | |
93 | ||
f808b016 | 94 | return $this->redirectToRoute('user_edit', ['id' => $user->getId()]); |
152fcccd JB |
95 | } |
96 | ||
f808b016 | 97 | return $this->render('WallabagUserBundle:Manage:edit.html.twig', [ |
152fcccd | 98 | 'user' => $user, |
a6b242a1 | 99 | 'edit_form' => $form->createView(), |
152fcccd JB |
100 | 'delete_form' => $deleteForm->createView(), |
101 | 'twofactor_auth' => $this->getParameter('twofactor_auth'), | |
f808b016 | 102 | ]); |
152fcccd JB |
103 | } |
104 | ||
105 | /** | |
106 | * Deletes a User entity. | |
107 | * | |
115de64e | 108 | * @Route("/{id}", name="user_delete", methods={"DELETE"}) |
152fcccd JB |
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 | ||
c37515f8 | 129 | /** |
8d4ed0df | 130 | * @param int $page |
c37515f8 | 131 | * |
b5b68779 | 132 | * @Route("/list/{page}", name="user_index", defaults={"page" = 1}) |
c37515f8 TC |
133 | * |
134 | * Default parameter for page is hardcoded (in duplication of the defaults from the Route) | |
135 | * because this controller is also called inside the layout template without any page as argument | |
136 | * | |
137 | * @return \Symfony\Component\HttpFoundation\Response | |
138 | */ | |
b5b68779 | 139 | public function searchFormAction(Request $request, $page = 1) |
c37515f8 | 140 | { |
b5b68779 NL |
141 | $em = $this->getDoctrine()->getManager(); |
142 | $qb = $em->getRepository('WallabagUserBundle:User')->createQueryBuilder('u'); | |
c37515f8 TC |
143 | |
144 | $form = $this->createForm(SearchUserType::class); | |
c37515f8 TC |
145 | $form->handleRequest($request); |
146 | ||
147 | if ($form->isSubmitted() && $form->isValid()) { | |
c37515f8 TC |
148 | $searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : ''); |
149 | ||
50cfd810 | 150 | $qb = $em->getRepository('WallabagUserBundle:User')->getQueryBuilderForSearch($searchTerm); |
b5b68779 | 151 | } |
50cfd810 | 152 | |
b5b68779 NL |
153 | $pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false); |
154 | $pagerFanta = new Pagerfanta($pagerAdapter); | |
155 | $pagerFanta->setMaxPerPage(50); | |
50cfd810 | 156 | |
b5b68779 NL |
157 | try { |
158 | $pagerFanta->setCurrentPage($page); | |
159 | } catch (OutOfRangeCurrentPageException $e) { | |
160 | if ($page > 1) { | |
161 | return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302); | |
50cfd810 | 162 | } |
c37515f8 TC |
163 | } |
164 | ||
b5b68779 NL |
165 | return $this->render('WallabagUserBundle:Manage:index.html.twig', [ |
166 | 'searchForm' => $form->createView(), | |
167 | 'users' => $pagerFanta, | |
c37515f8 TC |
168 | ]); |
169 | } | |
f808b016 JB |
170 | |
171 | /** | |
a6b242a1 | 172 | * Create a form to delete a User entity. |
f808b016 JB |
173 | * |
174 | * @param User $user The User entity | |
175 | * | |
176 | * @return \Symfony\Component\Form\Form The form | |
177 | */ | |
178 | private function createDeleteForm(User $user) | |
179 | { | |
180 | return $this->createFormBuilder() | |
181 | ->setAction($this->generateUrl('user_delete', ['id' => $user->getId()])) | |
182 | ->setMethod('DELETE') | |
183 | ->getForm() | |
184 | ; | |
185 | } | |
152fcccd | 186 | } |