src/Controller/SecurityController.php line 27

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Service\SocketlabsService;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Routing\Annotation\Route;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10. use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
  11. class SecurityController extends AbstractController
  12. {
  13.     /**
  14.      * @Route("/login", name="app_login")
  15.      */
  16.     public function login(AuthenticationUtils $authenticationUtils): Response
  17.     {
  18.         // get the login error if there is one
  19.         $error $authenticationUtils->getLastAuthenticationError();
  20.         // last username entered by the user
  21.         $lastUsername $authenticationUtils->getLastUsername();
  22.         return $this->render('theme_' config_theme '/login.html.twig', ['last_username' => $lastUsername'error' => $error]);
  23.     }
  24.     /**
  25.      * @Route("/logout", name="app_logout")
  26.      */
  27.     public function logout()
  28.     {
  29.         throw new \LogicException('This method can be blank - it will be intercepted by the logout key on your firewall.');
  30.     }
  31.     /**
  32.      * @Route("/register", name="app_register")
  33.      */
  34.     public function register(Request $requestUserPasswordEncoderInterface $encoderSocketlabsService $socketlabs)
  35.     {
  36.         $message '';
  37.         $p = [];
  38.         if ($request->getMethod() == 'POST') {
  39.             $p $_POST;
  40.             #pre($p,1);
  41.             $username $p['email'];
  42.             if (filter_var($username,FILTER_VALIDATE_EMAIL)) {
  43.                 $em $this->getDoctrine()->getManager();
  44.                 $user_exists $em->getRepository(User::class)->findBy(['email' => $username]);
  45.                 if (!$user_exists) {
  46.                     $password $p['password'];
  47.                     $firstname $p['firstname'];
  48.                     $lastname $p['lastname'];
  49.                     $phone $p['phone'];
  50.                     $address $p['address'];
  51.                     $postalnumber $p['postalnumber'];
  52.                     $postalname $p['postalname'];
  53.                     $user = new User();
  54.                     $user->setEmail($username);
  55.                     $user->setPassword($encoder->encodePassword($user,$password));
  56.                     $user->setFirstname($firstname);
  57.                     $user->setLastname($lastname);
  58.                     $user->setPhone($phone);
  59.                     $user->setAddress($address);
  60.                     $user->setPostalnumber($postalnumber);
  61.                     $user->setPostalname($postalname);
  62.                     $em->persist($user);
  63.                     $em->flush();
  64.                     $html '<b>Hei,</b><br>';
  65.                     $html .= '<br>';
  66.                     $html .= 'Du er registrert som bruker på Etikettbutikken!<br><br>';
  67.                     $html .= 'Brukernavn : ' $username '<br>';
  68.                     $html .= 'Passord : ' $password '<br>';
  69.                     $html .= '<br>';
  70.                     $html .= '<a href="https://web2print.raaness.no/login">Klikk her for å logge inn!</a>';
  71.                     $socketlabs->sendEmail($username,$firstname ' ' $lastname,'Takk for registrering',$html);
  72.                     $message 'Bruker "' $username '" opprettet! Bekreftelse er sendt på mail.&nbsp;&nbsp;<a class="text-bold" href="/login">Logg inn!</a>';
  73.                 } else {
  74.                     // User exists...
  75.                     $message 'Bruker eksisterer allerede...';
  76.                 }
  77.             } else {
  78.                 // Invalid email address
  79.                 $message 'Ulovlig email-adresse...';
  80.             }
  81.         }
  82.         return $this->render('theme_' config_theme '/register.html.twig', [
  83.             'message' => $message,
  84.             'post' => $p,
  85.         ]);
  86.     }
  87.     /**
  88.      * @Route("/forgotpasswd", name="app_forgotpasswd")
  89.      */
  90.     public function forgotpasswd(Request $requestUserPasswordEncoderInterface $encoderSocketlabsService $socketlabs)
  91.     {
  92.         $new_password '';
  93.         if ($request->getMethod() == 'POST') {
  94.             $new_password generateRandomString(8);
  95.             $em $this->getDoctrine()->getManager();
  96.             $user $em->getRepository(User::class)->findOneBy(['email' => $_POST['email']]);
  97.             $user->setPassword($encoder->encodePassword($user,$new_password));
  98.             $em->persist($user);
  99.             $em->flush();
  100.             $html 'Ditt nye passord er : ' $new_password;
  101.             $mail_response $socketlabs->sendEmail($user->getEmail(),$user->getFirstname() . ' ' $user->getLastname(),'Nytt passord',$html);
  102.         }
  103.         return $this->render('theme_' config_theme '/forgotpasswd.html.twig', [
  104.             'new_password' => $new_password,
  105.         ]);
  106.     }
  107. }