packages/Menke/UserBundle/Controller/UserController.php line 152

Open in your IDE?
  1. <?php
  2. namespace Menke\UserBundle\Controller;
  3. use Menke\UserBundle\Entity\User;
  4. use Menke\UserBundle\Form\UserType;
  5. use Doctrine\Persistence\ManagerRegistry;
  6. use Menke\UserBundle\Service\MailerService;
  7. use Menke\UserBundle\Form\PasswordResetType;
  8. use Menke\UserBundle\Form\PasswordForgotType;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Menke\UserBundle\Event\UserActivatedEvent;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Menke\UserBundle\Repository\UserRepository;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. use Menke\UserBundle\Security\TokenGeneratorInterface;
  15. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  16. use Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface;
  17. class UserController extends AbstractClientableController
  18. {
  19.     /**
  20.      * @Route("/user/", name="user_index", methods="GET")
  21.      */
  22.     public function index(UserRepository $userRepository): Response
  23.     {
  24.         return $this->render('@MenkeUser/user/index.html.twig', [
  25.             'users' => $userRepository->findBy(['client' => $this->getCurrentClient(), 'role' => [User::ROLE_ADMINUser::ROLE_MANAGER]])
  26.         ]);
  27.     }
  28.     /**
  29.      * @Route("/user/new", name="user_new", methods="GET|POST")
  30.      */
  31.     public function new(Request $requestUserPasswordHasherInterface $passwordHasherManagerRegistry $managerRegistry): Response
  32.     {
  33.         $user = new User();
  34.         $form $this->createForm(UserType::class, $user, [
  35.             'be_role_selectable' => true,
  36.             'pw_editable' => true,
  37.         ]);
  38.         $form->handleRequest($request);
  39.         if ($form->isSubmitted() && $form->isValid()) {
  40.             $user $form->getData();
  41.             $hash $passwordHasher->hashPassword($user$user->getPassword());
  42.             $user->setPassword($hash);
  43.             $em $managerRegistry->getManager();
  44.             // ToDo: remove this assignment when client system is expanded
  45.             $client $this->getCurrentClient();
  46.             $user->setClient($client);
  47.             $user->setActivatedAt(new \DateTime());
  48.             $em->persist($user);
  49.             $em->flush();
  50.             $this->addFlash('notice''Benutzer angelegt');
  51.             return $this->redirectToRoute('user_index');
  52.         }
  53.         return $this->render('@MenkeUser/user/new.html.twig', [
  54.             'user' => $user,
  55.             'form' => $form->createView(),
  56.         ]);
  57.     }
  58.     /**
  59.      * @Route("/user/{id}", name="user_show", methods="GET")
  60.      */
  61.     public function show(User $user): Response
  62.     {
  63.         $this->denyAccessUnlessGranted('ROLE_ADMIN'$user);
  64.         return $this->render('@MenkeUser/user/show.html.twig', ['user' => $user]);
  65.     }
  66.     /**
  67.      * @Route("/user/{id}/edit", name="user_edit", methods="GET|POST")
  68.      */
  69.     public function edit(Request $requestUser $userUserPasswordHasherInterface $passwordHasherManagerRegistry $managerRegistry): Response
  70.     {
  71.         $this->denyAccessUnlessGranted('ROLE_ADMIN'$user);
  72.         $password $user->getPassword();
  73.         $form $this->createForm(UserType::class, $user, [
  74.             'required_pw' => false,
  75.             'pw_editable' => true,
  76.             'be_role_selectable' => true
  77.         ]);
  78.         $form->handleRequest($request);
  79.         if ($form->isSubmitted() && $form->isValid()) {
  80.             $user $form->getData();
  81.             if (!empty($user->getPassword())) {
  82.                 $hash $passwordHasher->hashPassword($user$user->getPassword());
  83.                 $user->setPassword($hash);
  84.             } else {
  85.                 $user->setPassword($password);
  86.             }
  87.             $managerRegistry->getManager()->flush();
  88.             $this->addFlash('notice''Benutzer gespeichert');
  89.             return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
  90.         }
  91.         return $this->render('@MenkeUser/user/edit.html.twig', [
  92.             'user' => $user,
  93.             'form' => $form->createView(),
  94.         ]);
  95.     }
  96.     /**
  97.      * @Route("/user/{id}", name="user_delete", methods="DELETE", requirements={"id"="\d+"})
  98.      */
  99.     public function delete(Request $requestUser $userManagerRegistry $managerRegistry): Response
  100.     {
  101.         $this->denyAccessUnlessGranted('ROLE_ADMIN'$user);
  102.         if ($this->isCsrfTokenValid('delete'.$user->getId(), $request->request->get('_token'))) {
  103.             $em $managerRegistry->getManager();
  104.             $em->remove($user);
  105.             $em->flush();
  106.         }
  107.         return $this->redirectToRoute('user_index');
  108.     }
  109.     /**
  110.      * @Route("/user/multiple", name="user_delete-multiple", methods="DELETE")
  111.      */
  112.     public function deleteMultiple(Request $requestUserRepository $userRepoManagerRegistry $managerRegistry): Response
  113.     {
  114.         if ($this->isCsrfTokenValid('delete_users'$request->request->get('_token'))) {
  115.             $em $managerRegistry->getManager();
  116.             /** @var array $deleteIds */
  117.             $deleteIds $request->request->get('delete');
  118.             foreach ($deleteIds as $id => $value) {
  119.                 if ($value) {
  120.                     $user $userRepo->find($id);
  121.                     $this->denyAccessUnlessGranted('ROLE_ADMIN'$user);
  122.                     $em->remove($user);
  123.                 }
  124.             }
  125.             $em->flush();
  126.         }
  127.         return $this->redirectToRoute('user_index');
  128.     }
  129.     /**
  130.      * @Route("/forgot-password", name="user_forgot", methods="GET|POST")
  131.      */
  132.     public function forgotPassword(
  133.         Request $request,
  134.         TokenGeneratorInterface $generator,
  135.         UserRepository $repo,
  136.         MailerService $mailer
  137.         ManagerRegistry $managerRegistry
  138.     ) {
  139.         $form $this->createForm(PasswordForgotType::class);
  140.         $form->handleRequest($request);
  141.         $userNotFound false;
  142.         $emailSent false;
  143.         if ($form->isSubmitted() && $form->isValid()) {
  144.             $em $managerRegistry->getManager();
  145.             $data $form->getData();
  146.             $user $repo->findOneBy(array('username' => $data['email']));
  147.             if ($user) {
  148.                 $user->setPasswordResetToken($generator->generateToken());
  149.                 $user->setPasswordRequestedAt(new \DateTime());
  150.                 $em->flush();
  151.                 $mailer->sendPasswordForgotMessage($user$user->getPasswordResetToken());
  152.                 $emailSent true;
  153.             } else {
  154.                 $userNotFound true;
  155.             }
  156.         }
  157.         return $this->render('@MenkeUser/user/forgot.html.twig', array(
  158.             'form' => $form->createView(),
  159.             'userNotFound' => $userNotFound,
  160.             'emailSent' => $emailSent,
  161.         ));
  162.     }
  163.     /**
  164.      * @Route("/reset-password/{token}", name="user_reset", methods="GET|POST")
  165.      */
  166.     public function resetPasswordAction(
  167.         Request $request,
  168.         UserRepository $repo,
  169.         MailerService $mailer,
  170.         UserPasswordHasherInterface $passwordHasher,
  171.         $token,
  172.         ManagerRegistry $managerRegistry
  173.     ) {
  174.         $user $repo->findOneBy(array('passwordResetToken' => $token));
  175.         $passwordReset false;
  176.         if (!$user) {
  177.             $response = new Response();
  178.             $response->setStatusCode(Response::HTTP_NOT_FOUND);
  179.             return $response;
  180.         }
  181.         $form $this->createForm(PasswordResetType::class);
  182.         $form->handleRequest($request);
  183.         if ($form->isSubmitted() && $form->isValid()) {
  184.             $data $form->getData();
  185.             $em $managerRegistry->getManager();
  186.             $hash $passwordHasher->hashPassword($user$data['password']);
  187.             $user->setPassword($hash);
  188.             $user->setPasswordResetToken(null);
  189.             $user->setPasswordResetAt(new \DateTime());
  190.             $em->flush();
  191.             $mailer->sendPasswordResetMessage($user);
  192.             $passwordReset true;
  193.         }
  194.         return $this->render('@MenkeUser/user/reset.html.twig', array(
  195.             'form' => $form->createView(),
  196.             'passwordReset' => $passwordReset
  197.         ));
  198.     }
  199.     /**
  200.      * @Route("/activate/{token}", name="user_activate", methods="GET")
  201.      */
  202.     public function activateAccountAction($tokenUserRepository $repo,  EventDispatcherInterface $dispatcherManagerRegistry $managerRegistry)
  203.     {
  204.         $user $repo->findOneBy(array('activateToken' => $token));
  205.         if (!$user) {
  206.             $response = new Response();
  207.             $response->setStatusCode(Response::HTTP_NOT_FOUND);
  208.             return $response;
  209.         }
  210.         $user->setActivateToken(null);
  211.         $user->setActivatedAt(new \DateTime());
  212.         $event = new UserActivatedEvent($user);
  213.         $dispatcher->dispatch($eventUserActivatedEvent::NAME);
  214.         $em $managerRegistry->getManager();
  215.         $em->flush();
  216.         return $this->render('@MenkeUser/user/activated.html.twig');
  217.     }
  218. }