2016-09-30 18:09:06 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Wallabag\UserBundle\Controller;
|
|
|
|
|
|
|
|
use FOS\UserBundle\Event\UserEvent;
|
|
|
|
use FOS\UserBundle\FOSUserEvents;
|
2022-08-28 00:01:46 +00:00
|
|
|
use FOS\UserBundle\Model\UserManagerInterface;
|
2020-07-29 04:36:43 +00:00
|
|
|
use Pagerfanta\Doctrine\ORM\QueryAdapter as DoctrineORMAdapter;
|
2017-04-27 14:23:54 +00:00
|
|
|
use Pagerfanta\Exception\OutOfRangeCurrentPageException;
|
|
|
|
use Pagerfanta\Pagerfanta;
|
2022-08-28 00:01:46 +00:00
|
|
|
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Google\GoogleAuthenticatorInterface;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
|
2022-08-28 00:01:46 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2022-12-13 12:39:24 +00:00
|
|
|
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
|
2022-08-28 14:59:43 +00:00
|
|
|
use Symfony\Component\Form\Form;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2022-08-28 14:59:43 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Response;
|
2022-08-28 00:01:46 +00:00
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface;
|
2018-10-04 12:07:20 +00:00
|
|
|
use Symfony\Component\Routing\Annotation\Route;
|
2022-08-28 00:01:46 +00:00
|
|
|
use Symfony\Component\Translation\TranslatorInterface;
|
2016-09-30 18:09:06 +00:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2022-09-01 18:54:56 +00:00
|
|
|
use Wallabag\UserBundle\Form\NewUserType;
|
2017-04-27 13:58:32 +00:00
|
|
|
use Wallabag\UserBundle\Form\SearchUserType;
|
2022-09-01 18:54:56 +00:00
|
|
|
use Wallabag\UserBundle\Form\UserType;
|
2016-09-30 18:09:06 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* User controller.
|
|
|
|
*/
|
|
|
|
class ManageController extends Controller
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Creates a new User entity.
|
|
|
|
*
|
2018-10-04 12:07:20 +00:00
|
|
|
* @Route("/new", name="user_new", methods={"GET", "POST"})
|
2016-09-30 18:09:06 +00:00
|
|
|
*/
|
|
|
|
public function newAction(Request $request)
|
|
|
|
{
|
2022-08-28 00:01:46 +00:00
|
|
|
$userManager = $this->container->get(UserManagerInterface::class);
|
2016-09-30 18:09:06 +00:00
|
|
|
|
|
|
|
$user = $userManager->createUser();
|
2022-11-23 14:51:33 +00:00
|
|
|
\assert($user instanceof User);
|
2016-09-30 18:09:06 +00:00
|
|
|
// enable created user by default
|
|
|
|
$user->setEnabled(true);
|
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$form = $this->createForm(NewUserType::class, $user);
|
2018-12-02 17:39:02 +00:00
|
|
|
$form->handleRequest($request);
|
2016-09-30 18:09:06 +00:00
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
$userManager->updateUser($user);
|
|
|
|
|
|
|
|
// dispatch a created event so the associated config will be created
|
|
|
|
$event = new UserEvent($user, $request);
|
2022-12-13 12:39:24 +00:00
|
|
|
LegacyEventDispatcherProxy::decorate($this->get(EventDispatcherInterface::class))->dispatch($event, FOSUserEvents::USER_CREATED);
|
2016-09-30 18:09:06 +00:00
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
$this->get(SessionInterface::class)->getFlashBag()->add(
|
2016-09-30 18:09:06 +00:00
|
|
|
'notice',
|
2022-08-28 00:01:46 +00:00
|
|
|
$this->get(TranslatorInterface::class)->trans('flashes.user.notice.added', ['%username%' => $user->getUsername()])
|
2016-09-30 18:09:06 +00:00
|
|
|
);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
|
2016-09-30 18:09:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 22:47:22 +00:00
|
|
|
return $this->render('@WallabagUser/Manage/new.html.twig', [
|
2016-09-30 18:09:06 +00:00
|
|
|
'user' => $user,
|
|
|
|
'form' => $form->createView(),
|
2017-07-01 07:52:38 +00:00
|
|
|
]);
|
2016-09-30 18:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Displays a form to edit an existing User entity.
|
|
|
|
*
|
2018-10-04 12:07:20 +00:00
|
|
|
* @Route("/{id}/edit", name="user_edit", methods={"GET", "POST"})
|
2016-09-30 18:09:06 +00:00
|
|
|
*/
|
|
|
|
public function editAction(Request $request, User $user)
|
|
|
|
{
|
2022-08-28 00:01:46 +00:00
|
|
|
$userManager = $this->container->get(UserManagerInterface::class);
|
2018-12-02 11:43:05 +00:00
|
|
|
|
2016-09-30 18:09:06 +00:00
|
|
|
$deleteForm = $this->createDeleteForm($user);
|
2022-09-01 18:54:56 +00:00
|
|
|
$form = $this->createForm(UserType::class, $user);
|
2018-12-02 17:39:02 +00:00
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
// `googleTwoFactor` isn't a field within the User entity, we need to define it's value in a different way
|
|
|
|
if ($this->getParameter('twofactor_auth') && true === $user->isGoogleAuthenticatorEnabled() && false === $form->isSubmitted()) {
|
|
|
|
$form->get('googleTwoFactor')->setData(true);
|
|
|
|
}
|
2016-09-30 18:09:06 +00:00
|
|
|
|
2018-12-02 11:43:05 +00:00
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2018-12-02 17:39:02 +00:00
|
|
|
// handle creation / reset of the OTP secret if checkbox changed from the previous state
|
|
|
|
if ($this->getParameter('twofactor_auth')) {
|
|
|
|
if (true === $form->get('googleTwoFactor')->getData() && false === $user->isGoogleAuthenticatorEnabled()) {
|
2022-08-28 00:01:46 +00:00
|
|
|
$user->setGoogleAuthenticatorSecret($this->get(GoogleAuthenticatorInterface::class)->generateSecret());
|
2018-12-02 17:39:02 +00:00
|
|
|
$user->setEmailTwoFactor(false);
|
|
|
|
} elseif (false === $form->get('googleTwoFactor')->getData() && true === $user->isGoogleAuthenticatorEnabled()) {
|
|
|
|
$user->setGoogleAuthenticatorSecret(null);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-02 11:43:05 +00:00
|
|
|
$userManager->updateUser($user);
|
2016-09-30 18:09:06 +00:00
|
|
|
|
2022-08-28 00:01:46 +00:00
|
|
|
$this->get(SessionInterface::class)->getFlashBag()->add(
|
2016-09-30 18:09:06 +00:00
|
|
|
'notice',
|
2022-08-28 00:01:46 +00:00
|
|
|
$this->get(TranslatorInterface::class)->trans('flashes.user.notice.updated', ['%username%' => $user->getUsername()])
|
2016-09-30 18:09:06 +00:00
|
|
|
);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
|
2016-09-30 18:09:06 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 22:47:22 +00:00
|
|
|
return $this->render('@WallabagUser/Manage/edit.html.twig', [
|
2016-09-30 18:09:06 +00:00
|
|
|
'user' => $user,
|
2018-12-02 11:43:05 +00:00
|
|
|
'edit_form' => $form->createView(),
|
2016-09-30 18:09:06 +00:00
|
|
|
'delete_form' => $deleteForm->createView(),
|
|
|
|
'twofactor_auth' => $this->getParameter('twofactor_auth'),
|
2017-07-01 07:52:38 +00:00
|
|
|
]);
|
2016-09-30 18:09:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes a User entity.
|
|
|
|
*
|
2018-10-04 12:07:20 +00:00
|
|
|
* @Route("/{id}", name="user_delete", methods={"DELETE"})
|
2016-09-30 18:09:06 +00:00
|
|
|
*/
|
|
|
|
public function deleteAction(Request $request, User $user)
|
|
|
|
{
|
|
|
|
$form = $this->createDeleteForm($user);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
2022-08-28 00:01:46 +00:00
|
|
|
$this->get(SessionInterface::class)->getFlashBag()->add(
|
2016-09-30 18:09:06 +00:00
|
|
|
'notice',
|
2022-08-28 00:01:46 +00:00
|
|
|
$this->get(TranslatorInterface::class)->trans('flashes.user.notice.deleted', ['%username%' => $user->getUsername()])
|
2016-09-30 18:09:06 +00:00
|
|
|
);
|
|
|
|
|
2022-11-23 14:51:33 +00:00
|
|
|
$em = $this->get('doctrine')->getManager();
|
2016-09-30 18:09:06 +00:00
|
|
|
$em->remove($user);
|
|
|
|
$em->flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->redirectToRoute('user_index');
|
|
|
|
}
|
|
|
|
|
2017-04-27 13:58:32 +00:00
|
|
|
/**
|
2019-11-12 13:18:58 +00:00
|
|
|
* @param int $page
|
2017-04-27 13:58:32 +00:00
|
|
|
*
|
2017-05-02 13:27:58 +00:00
|
|
|
* @Route("/list/{page}", name="user_index", defaults={"page" = 1})
|
2017-04-27 13:58:32 +00:00
|
|
|
*
|
|
|
|
* Default parameter for page is hardcoded (in duplication of the defaults from the Route)
|
|
|
|
* because this controller is also called inside the layout template without any page as argument
|
|
|
|
*
|
2022-08-28 14:59:43 +00:00
|
|
|
* @return Response
|
2017-04-27 13:58:32 +00:00
|
|
|
*/
|
2017-05-02 13:27:58 +00:00
|
|
|
public function searchFormAction(Request $request, $page = 1)
|
2017-04-27 13:58:32 +00:00
|
|
|
{
|
2022-11-23 14:51:33 +00:00
|
|
|
$em = $this->get('doctrine')->getManager();
|
2022-08-25 19:37:10 +00:00
|
|
|
$qb = $em->getRepository(User::class)->createQueryBuilder('u');
|
2017-04-27 13:58:32 +00:00
|
|
|
|
|
|
|
$form = $this->createForm(SearchUserType::class);
|
|
|
|
$form->handleRequest($request);
|
|
|
|
|
|
|
|
if ($form->isSubmitted() && $form->isValid()) {
|
|
|
|
$searchTerm = (isset($request->get('search_user')['term']) ? $request->get('search_user')['term'] : '');
|
|
|
|
|
2022-08-25 19:37:10 +00:00
|
|
|
$qb = $em->getRepository(User::class)->getQueryBuilderForSearch($searchTerm);
|
2017-05-02 13:27:58 +00:00
|
|
|
}
|
2017-04-27 14:23:54 +00:00
|
|
|
|
2017-05-02 13:27:58 +00:00
|
|
|
$pagerAdapter = new DoctrineORMAdapter($qb->getQuery(), true, false);
|
|
|
|
$pagerFanta = new Pagerfanta($pagerAdapter);
|
|
|
|
$pagerFanta->setMaxPerPage(50);
|
2017-04-27 14:23:54 +00:00
|
|
|
|
2017-05-02 13:27:58 +00:00
|
|
|
try {
|
|
|
|
$pagerFanta->setCurrentPage($page);
|
|
|
|
} catch (OutOfRangeCurrentPageException $e) {
|
|
|
|
if ($page > 1) {
|
|
|
|
return $this->redirect($this->generateUrl('user_index', ['page' => $pagerFanta->getNbPages()]), 302);
|
2017-04-27 14:23:54 +00:00
|
|
|
}
|
2017-04-27 13:58:32 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 22:47:22 +00:00
|
|
|
return $this->render('@WallabagUser/Manage/index.html.twig', [
|
2017-05-02 13:27:58 +00:00
|
|
|
'searchForm' => $form->createView(),
|
|
|
|
'users' => $pagerFanta,
|
2017-04-27 13:58:32 +00:00
|
|
|
]);
|
|
|
|
}
|
2017-07-01 07:52:38 +00:00
|
|
|
|
|
|
|
/**
|
2018-12-02 11:43:05 +00:00
|
|
|
* Create a form to delete a User entity.
|
2017-07-01 07:52:38 +00:00
|
|
|
*
|
|
|
|
* @param User $user The User entity
|
|
|
|
*
|
2022-08-28 14:59:43 +00:00
|
|
|
* @return Form The form
|
2017-07-01 07:52:38 +00:00
|
|
|
*/
|
|
|
|
private function createDeleteForm(User $user)
|
|
|
|
{
|
|
|
|
return $this->createFormBuilder()
|
|
|
|
->setAction($this->generateUrl('user_delete', ['id' => $user->getId()]))
|
|
|
|
->setMethod('DELETE')
|
|
|
|
->getForm()
|
|
|
|
;
|
|
|
|
}
|
2016-09-30 18:09:06 +00:00
|
|
|
}
|