get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_REMEMBERED')) { return $this->redirectToRoute('videotheque_liste'); } $authenticationUtils = $this->get('security.authentication_utils'); return $this->render('@App/security/login.html.twig', array ( 'last_username' => $authenticationUtils->getLastUsername(), 'error' => $authenticationUtils->getLastAuthenticationError() )); } /** * @Route("/admin/createuser", name="admin_createuser") */ public function createUserAction(Request $request, UserPasswordEncoderInterface $encoder) { $user = new User; $form = $this->createForm(UserType::class, $user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $encoded = $encoder->encodePassword($user, $user->getPassword()); $user->setPassword($encoded); $em = $this->getDoctrine()->getManager(); $em->persist($user); $em->flush(); $this->addFlash('success', 'L\'utilisateur a été ajouté'); return $this->redirectToRoute('admin_index'); } return $this->render('@App/security/createuser.html.twig', array ( 'form' => $form->createView() )); } /** * @Route("/admin/edituser/{id}", name="admin_edituser") */ public function editUserAction(Request $request, UserPasswordEncoderInterface $encoder, User $user) { $form = $this->createForm(UserEditType::class, $user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->flush(); $this->addFlash('success', 'L\'utilisateur a été modifié'); return $this->redirectToRoute('admin_index'); } return $this->render('@App/security/createuser.html.twig', array ( 'form' => $form->createView() )); } /** * @Route("/admin", name="admin_index") */ public function indexAction(Request $request) { $em = $this->getDoctrine()->getManager(); $repo = $em->getRepository('AppBundle:User'); $users = $repo->findAll(); return $this->render('@App/security/liste.html.twig', array( 'users' => $users )); } /** * @Route("/admin/deluser/{id}", name="admin_deluser") */ public function delUserAction (Request $request, User $user) { $form = $this->get('form.factory')->create(); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $em->remove($user); $em->flush(); $this->addFlash("success", "L'utilisateur a bien été supprimé"); return $this->redirectToRoute('admin_index'); } return $this->render('@App/security/supprimeruser.html.twig', array ( 'user' => $user, 'form' => $form->createView() )); } /** * @Route("/register", name="security_register") */ public function registerAction(Request $request, UserPasswordEncoderInterface $encoder) { $user = new User(); $user->setActivated(false); $form = $this->createForm(UserRegisterType::class, $user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $user->setRoles(array('ROLE_USER')); $encoded = $encoder->encodePassword($user, $user->getPassword()); $user->setPassword($encoded); $em = $this->getDoctrine()->getManager(); $em->persist($user); $em->flush(); $adresse = $this->generateUrl('security_activate', array('token'=>$user->getToken()), UrlGeneratorInterface::ABSOLUTE_URL); $mail = $this->get('film.mail'); $mail->sendMailActivation($user, $adresse); $this->addFlash('success', 'Votre compte a été créé. Un mail vient d\'être envoyé pour l\'activation du compte'); return $this->redirectToRoute('login'); } return $this->render('@App/security/register.html.twig', array( 'form' => $form->createView() )); } /** * @Route("/motdepasseoublie", name="security_envoyertoken") */ public function recupMdpAction(Request $request) { $form = $this->createFormBuilder() ->add('email', SearchType::class) ->add('Envoyer mail', SubmitType::class) ->getForm(); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $em = $this->getDoctrine()->getManager(); $data = $form->getData(); $user = $em->getRepository('AppBundle:User')->findOneBy(array('mail'=>$data['email'])); if ($user != null) { $user->setToken(); $em->flush(); $adresse = $this->generateUrl('security_resetpassword', array('token'=>$user->getToken()), UrlGeneratorInterface::ABSOLUTE_URL); $mail = $this->get('film.mail'); $mail->sendMailTokenMp($user, $adresse); $this->addFlash('success', "Un mail vous a été envoyé à ".$user->getUsername()." pour récupérer le mot de passe"); return $this->redirectToRoute('login'); } else { $this->addFlash('warning', "Cet email n'existe pas"); } } return $this->render('@App/security/chercheruserpourpassword.html.twig', array ( 'form' => $form->createView() )); } /** * @Route("/resetpassword/token={token}", name="security_resetpassword") */ public function resetPasswordAction(Request $request, UserPasswordEncoderInterface $encoder, User $user = null) { if ($user != null) { if ($user->isValidToken()) { $form = $this->createForm(UserEditPasswordType::class, $user); $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { $encoded = $encoder->encodePassword($user, $user->getPassword()); $user->setPassword($encoded); $user->setToken(); $em = $this->getDoctrine()->getManager(); $em->flush(); $this->addFlash('success', 'Mot de passe changé, veuillez vous connecter'); return $this->redirectToRoute('videotheque_liste'); } return $this->render('@App/security/password.html.twig', array( 'form' => $form->createView() )); } } $this->addFlash('warning', "Ce token n'est plus valide"); return $this->redirectToRoute('videotheque_liste'); } /** * Ne pas utiliser * @Route("/admin/resettokens", name="admin_resettokens") */ public function resetTokensAction() { $em = $this->getDoctrine()->getManager(); $users = $em->getRepository('AppBundle:User')->findAll(); foreach ($users as $user) { $user->setToken(); } $em->flush(); return $this->redirectToRoute('admin_index'); } /** * @Route("/activate/token={token}", name="security_activate") */ public function activateAction(Request $request, User $user = null) { if ($user != null) { if ($user->isValidToken()) { $user->setActivated(true); $em = $this->getDoctrine()->getManager(); $em->flush(); $this->addFlash('success', "Votre compte est activé"); return $this->redirectToRoute('login'); } } $this->addFlash('warning', "Ce token n'est plus valide"); return $this->redirectToRoute('videotheque_liste'); } }