/
var
/
www
/
html
/
restaurants
/
src
/
Controller
/
Upload File
HOME
<?php namespace App\Controller; use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\Routing\Annotation\Route; use Symfony\Component\Security\Http\Authentication\AuthenticationUtils; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoder; use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; use Symfony\Component\Security\Guard\GuardAuthenticatorHandler; use App\Security\LoginFormAuthenticator; use App\Service\MailgunTransport; use App\Entity\Users; use App\Document\Item as ItemDocument; use App\Document\Place as PlaceDocument; use App\Controller\Controller; use App\Document\User as UserDocument; use App\Service\EmailSender; use App\Service\FileUploader; use Doctrine\ODM\MongoDB\DocumentManager; use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; use Symfony\Component\Routing\Generator\UrlGenerator; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; class UserController extends Controller { /** * * @var MailgunTransport */ private $mailgun; /** * * @var UrlGeneratorInterface */ private $urlGenerator; private $dm; /** * * @param MailgunTransport $mailgun * @param UrlGeneratorInterface $urlGenerator */ function __construct(MailgunTransport $mailgun, DocumentManager $dm, UrlGeneratorInterface $urlGenerator) { $this->mailgun = $mailgun; $this->urlGenerator = $urlGenerator; $this->dm = $dm; } /** * @Route("/business/start/{slug}/{token}", name="start_business") */ public function start(string $slug, string $token) { $user = $this->dm->getRepository(UserDocument::class)->findOneBy(['verification' => $token]); if(!$user){ throw new BadRequestHttpException('Link invalid'); } $place = $this->dm->getRepository(PlaceDocument::class)->findOneBy(['slug' => $slug]); return $this->render('Users/start.html.twig', ['email' => $user->getEmail(), 'token' => $token, 'place' => $place]); } /** * @Route("/business/start/{slug}", name="start", methods={"POST"}) * * @param UserPasswordEncoder $passwordEncoder * @param GuardAuthenticatorHandler $guardHandler * @param LoginFormAuthenticator $formAuthenticator * @param Request $request **/ public function startBusiness( string $slug, Request $request, GuardAuthenticatorHandler $guardHandler, LoginFormAuthenticator $formAuthenticator, UserPasswordEncoderInterface $passwordEncoder, EmailSender $sender) { $user = $this->dm->getRepository(UserDocument::class)->findOneBy(['email' => $_POST['email'], 'verification' => $_POST['token']]); $error = ''; $place = $this->dm->getRepository(PlaceDocument::class)->findOneBy(['slug' => $slug]); if ($user && ($_POST['password'] === $_POST['confirmpassword'])) { $user->setVerification(NULL); $user->setName($_POST['name']); $user->setPassword($passwordEncoder->encodePassword( $user, $_POST['password'] )); $place->setEnabled(true); $this->dm->flush(); $this->addFlash( 'success', 'Welcome to Cravvings' ); // $admin = $this->dm->getRepository(UserDocument::class)->findOneBy(['email' => 'obinna.okafor01@gmail.com']); $sender->sendPlainEmail('Users/notification.html.twig', 'obinna.okafor01@gmail.com', 'Registration complete', sprintf("Registrayion complete for %s", $user->getName())); return $guardHandler->authenticateUserAndHandleSuccess( $user, $request, $formAuthenticator, 'main' ); }else { $this->addFlash( 'error', 'Error Occured. Try again later' ); } return $this->render('Users/start.html.twig', ['email' => $user->getEmail(), 'token' => $_POST['token']]); } /** * [sendEmail description] * @param string $view The view to render * @param Users $user The user to send the email to * @param string $rand Random string * @param string $subject Email subject */ private function sendPlainEmail($view, $user, $subject, $text) { $message = $this->renderView( $view, [ 'name' => $user->getName(), 'text' => $text ] ); $response = $this->mailgun->send($user->getEmail(), $subject, $message); return $response; } /** * [sendEmail description] * @param string $view The view to render * @param Users $user The user to send the email to * @param string $rand Random string * @param string $subject Email subject */ private function sendEmail($view, $user, $subject, $link, $extra = null) { $message = $this->renderView( "Users/$view", [ 'name' => $user->getName(), 'extra' => $extra, 'link' => $link ] ); $response = $this->mailgun->send($user->getEmail(), $subject, $message); return $response; } /** * Generate a random string for email verification * @param int $length * @return string **/ private function randomString($length = 32) { $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $charactersLength = strlen($characters); $randomString = ''; for ($i = 0; $i < $length; $i++) { $randomString .= $characters[rand(0, $charactersLength - 1)]; } return $randomString; } public function sendPasswordResetEmail(UserDocument $user) { $rand = $this->randomString(64); $user->setReset($rand); $this->dm->flush(); $url = $this->urlGenerator->generate('password_reset', ['token' => $rand], UrlGenerator::ABSOLUTE_URL); $this->sendEmail('passwordemail.html.twig', $user, 'Password Reset', $url); } /** * @Route("/password/reset", name="password_email")] * @return Response **/ public function resetPassword() { $email = ''; $error = NULL; if (isset($_POST['email'])) { $email = $_POST['email']; $user = $this->dm->getRepository(UserDocument::class)->findOneBy(['email' => $email]); if ($user) { $this->sendPasswordResetEmail($user); return $this->render('Users/emailsent.html.twig', ['email' => $email]); } $error = 'Email not registered to any account'; } return $this->render('Users/passwordreset.html.twig', ['email' => $email, 'error' => $error]); } /** * @Route("/password/reset/{token}", name="password_reset") * * @param string $token **/ public function reset($token) { $user = $this->dm->getRepository(UserDocument::class)->findOneBy(['reset' => $token]); if ($user) { return $this->render('Users/passwordchange.html.twig', [ 'email' => $user->getEmail(), 'token' => $token ]); } return $this->redirectToRoute('app_login'); } /** * @Route("/password/change", name="password_change") * * @param UserPasswordEncoder $passwordEncoder **/ public function passwordChange(UserPasswordEncoderInterface $passwordEncoder) { $user = $this->dm->getRepository(UserDocument::class)->findOneBy(['email' => $_POST['email']]); $error = ''; if ($user && ($user->getReset() === $_POST['token']) && $_POST['password'] === $_POST['confirmpassword']) { $user->setReset(''); $user->setPassword($passwordEncoder->encodePassword( $user, $_POST['password'] )); $this->dm->flush(); $this->addFlash( 'success', 'Password changed successfully' ); }else { $this->addFlash( 'error', 'Password could not be changed at this time. Try again later' ); } return $this->redirectToRoute('app_login'); } /** * @Route("/login", name="app_login") * @param AuthenticationUtils $authenticationUtils * @return Response */ public function login(AuthenticationUtils $authenticationUtils): Response { if ($this->get('security.authorization_checker')->isGranted('ROLE_USER')) { $this->redirectToRoute('owned'); } // get the login error if there is one $error = $authenticationUtils->getLastAuthenticationError(); // last username entered by the user $lastUsername = $authenticationUtils->getLastUsername(); return $this->render('user/login.html.twig', ['last_username' => $lastUsername, 'error' => $error]); } /** * @Route("/ajax/login", name="ajax_login") * @param Request $request **/ public function rlogin(Request $request) { return $this->render('user/rlogin.html.twig'); } /** * @Route("/logout", name="app_logout") **/ public function logout() { } }