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

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.             $user->setCreated(new \DateTime());
  49.             $managerRegistry->getManager()->persist($user);
  50.            // $em->persist($user);
  51.            // $em->flush();
  52.            $managerRegistry->getManager()->flush(); 
  53.             $this->addFlash('notice''Benutzer angelegt');
  54.             return $this->redirectToRoute('user_index');
  55.         }
  56.         return $this->render('@MenkeUser/user/new.html.twig', [
  57.             'user' => $user,
  58.             'form' => $form->createView(),
  59.         ]);
  60.     }
  61.     /**
  62.      * @Route("/user/{id}", name="user_show", methods="GET")
  63.      */
  64.     public function show(User $user): Response
  65.     {
  66.         $this->denyAccessUnlessGranted('ROLE_ADMIN'$user);
  67.         return $this->render('@MenkeUser/user/show.html.twig', ['user' => $user]);
  68.     }
  69.     /**
  70.      * @Route("/user/{id}/edit", name="user_edit", methods="GET|POST")
  71.      */
  72.     public function edit(Request $requestUser $userUserPasswordHasherInterface $passwordHasherManagerRegistry $managerRegistry): Response
  73.     {
  74.         $this->denyAccessUnlessGranted('ROLE_ADMIN'$user);
  75.         $password $user->getPassword();
  76.         $form $this->createForm(UserType::class, $user, [
  77.             'required_pw' => false,
  78.             'pw_editable' => true,
  79.             'be_role_selectable' => true
  80.         ]);
  81.         $form->handleRequest($request);
  82.         if ($form->isSubmitted() && $form->isValid()) {
  83.             $user $form->getData();
  84.             if (!empty($user->getPassword())) {
  85.                 $hash $passwordHasher->hashPassword($user$user->getPassword());
  86.                 $user->setPassword($hash);
  87.             } else {
  88.                 $user->setPassword($password);
  89.             }
  90.             $managerRegistry->getManager()->persist($user);
  91.             $user->setModified(new \datetime());
  92.             $managerRegistry->getManager()->flush();
  93.             $this->addFlash('notice''Benutzer gespeichert');
  94.             return $this->redirectToRoute('user_edit', ['id' => $user->getId()]);
  95.         }
  96.         $managerRegistry->getManager()->persist($user);
  97.         $user->setModified(new \datetime());
  98.         $managerRegistry->getManager()->flush();
  99.         return $this->render('@MenkeUser/user/edit.html.twig', [
  100.             'user' => $user,
  101.             'form' => $form->createView(),
  102.         ]);
  103.     }
  104.     /**
  105.      * @Route("/user/{id}", name="user_delete", methods="DELETE", requirements={"id"="\d+"})
  106.      */
  107.     public function delete(Request $requestUser $userManagerRegistry $managerRegistry): Response
  108.     {
  109.         $this->denyAccessUnlessGranted('ROLE_ADMIN'$user);
  110.         if ($this->isCsrfTokenValid('delete'.$user->getId(), $request->request->get('_token'))) {
  111.             $em $managerRegistry->getManager();
  112.             $em->remove($user);
  113.             $em->flush();
  114.         }
  115.         return $this->redirectToRoute('user_index');
  116.     }
  117.     /**
  118.      * @Route("/user/multiple", name="user_delete-multiple", methods="DELETE")
  119.      */
  120.     public function deleteMultiple(Request $requestUserRepository $userRepoManagerRegistry $managerRegistry): Response
  121.     {
  122.         if ($this->isCsrfTokenValid('delete_users'$request->request->get('_token'))) {
  123.             $em $managerRegistry->getManager();
  124.             /** @var array $deleteIds */
  125.             $deleteIds $request->request->get('delete');
  126.             foreach ($deleteIds as $id => $value) {
  127.                 if ($value) {
  128.                     $user $userRepo->find($id);
  129.                     $this->denyAccessUnlessGranted('ROLE_ADMIN'$user);
  130.                     $em->remove($user);
  131.                 }
  132.             }
  133.             $em->flush();
  134.         }
  135.         return $this->redirectToRoute('user_index');
  136.     }
  137.     /**
  138.      * @Route("/forgot-password", name="user_forgot", methods="GET|POST")
  139.      */
  140.     public function forgotPassword(
  141.         Request $request,
  142.         TokenGeneratorInterface $generator,
  143.         UserRepository $repo,
  144.         MailerService $mailer
  145.         ManagerRegistry $managerRegistry
  146.     ) {
  147.         $form $this->createForm(PasswordForgotType::class);
  148.         $form->handleRequest($request);
  149.         $userNotFound false;
  150.         $emailSent false;
  151.         if ($form->isSubmitted() && $form->isValid()) {
  152.             $em $managerRegistry->getManager();
  153.             $data $form->getData();
  154.             $user $repo->findOneBy(array('username' => $data['email']));
  155.             if ($user) {
  156.                 $user->setPasswordResetToken($generator->generateToken());
  157.                 $user->setPasswordRequestedAt(new \DateTime());
  158.                 $em->flush();
  159.                 $mailer->sendPasswordForgotMessage($user$user->getPasswordResetToken());
  160.                 $emailSent true;
  161.             } else {
  162.                 $userNotFound true;
  163.             }
  164.         }
  165.         return $this->render('@MenkeUser/user/forgot.html.twig', array(
  166.             'form' => $form->createView(),
  167.             'userNotFound' => $userNotFound,
  168.             'emailSent' => $emailSent,
  169.         ));
  170.     }
  171.     /**
  172.      * @Route("/reset-password/{token}", name="user_reset", methods="GET|POST")
  173.      */
  174.     public function resetPasswordAction(
  175.         Request $request,
  176.         UserRepository $repo,
  177.         MailerService $mailer,
  178.         UserPasswordHasherInterface $passwordHasher,
  179.         $token,
  180.         ManagerRegistry $managerRegistry
  181.     ) {
  182.         $user $repo->findOneBy(array('passwordResetToken' => $token));
  183.         $passwordReset false;
  184.         if (!$user) {
  185.             $response = new Response();
  186.             $response->setStatusCode(Response::HTTP_NOT_FOUND);
  187.             return $response;
  188.         }
  189.         $form $this->createForm(PasswordResetType::class);
  190.         $form->handleRequest($request);
  191.         if ($form->isSubmitted() && $form->isValid()) {
  192.             $data $form->getData();
  193.             $em $managerRegistry->getManager();
  194.             $hash $passwordHasher->hashPassword($user$data['password']);
  195.             $user->setPassword($hash);
  196.             $user->setPasswordResetToken(null);
  197.             $user->setPasswordResetAt(new \DateTime());
  198.             $em->flush();
  199.             $mailer->sendPasswordResetMessage($user);
  200.             $passwordReset true;
  201.         }
  202.         return $this->render('@MenkeUser/user/reset.html.twig', array(
  203.             'form' => $form->createView(),
  204.             'passwordReset' => $passwordReset
  205.         ));
  206.     }
  207.     /**
  208.      * @Route("/activate/{token}", name="user_activate", methods="GET")
  209.      */
  210.     public function activateAccountAction($tokenUserRepository $repo,  EventDispatcherInterface $dispatcherManagerRegistry $managerRegistry)
  211.     {
  212.         $user $repo->findOneBy(array('activateToken' => $token));
  213.         if (!$user) {
  214.             $response = new Response();
  215.             $response->setStatusCode(Response::HTTP_NOT_FOUND);
  216.             return $response;
  217.         }
  218.         $user->setActivateToken(null);
  219.         $user->setActivatedAt(new \DateTime());
  220.         $event = new UserActivatedEvent($user);
  221.         $dispatcher->dispatch($eventUserActivatedEvent::NAME);
  222.         $em $managerRegistry->getManager();
  223.         $em->flush();
  224.         return $this->render('@MenkeUser/user/activated.html.twig');
  225.     }
  226. }