src/Controller/AccountController.php line 14

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Pimcore;
  4. use Pimcore\Model\DataObject\Customer;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. /**
  9.  * @Route("/account")
  10.  */
  11. class AccountController extends \Sintra\CMSCoreBundle\Controller\AccountController
  12. {
  13.     /** @var TranslatorInterface */
  14.     protected $translator;
  15.     public function __construct(TranslatorInterface $translator)
  16.     {
  17.         $this->translator $translator;
  18.     }
  19.     /**
  20.      * @Route("/password-change", methods={"GET"})
  21.      *
  22.      * @param Request $request
  23.      * @return void
  24.      */
  25.     public function passwordChangeAction(Request $request)
  26.     {
  27.         $user $this->getUser();
  28.         if ($user && ($user instanceof Customer)) {
  29.             $guid uuid_create();
  30.             $user->setPasswordRecoveryToken($guid);
  31.             $user->save();
  32.             $language $user->getCustomerLanguage();
  33.             if (empty($language) || !in_array($language, array("en""it"))) {
  34.                 $language "en";
  35.             }
  36.             //generare email
  37.             $emailUrl \Pimcore\Tool::getHostUrl("https") . '/password-restore' '/' $guid;
  38.             $mail = new \Pimcore\Mail();
  39.             $mail->to($user->getEmail());
  40.             $mail->subject("Kemon - ".$this->translator->trans("password_recovery", [], 'messages'$language));
  41.             $mail->html("<p>".$this->translator->trans("greetings", [], 'messages'$language)." " $user->getFirstname() . ",</p>
  42.                         <br>
  43.                         <p>".$this->translator->trans("password_recovery_request", [], 'messages'$language)."</p>
  44.                         <p>".$this->translator->trans("click_the_following_link", [], 'messages'$language)."</p>
  45.                         <br><a href='$emailUrl'>$emailUrl</a><br>
  46.                         <p>".$this->translator->trans("have_a_nice_day", [], 'messages'$language).",</p><br>
  47.                         <p>".$this->translator->trans("kemon_team", [], 'messages'$language)."</p>");
  48.             $mail->send();
  49.             return $this->redirectToRoute('home', array('success' => "password_recovery_success"));
  50.         } else {
  51.             return $this->redirectToRoute('home', array('error' => "password_recovery_error"));
  52.         }
  53.     }
  54.     /**
  55.      * @Route("/password-modify", methods={"GET"})
  56.      *
  57.      * @param Request $request
  58.      * @return void
  59.      */
  60.     public function passwordModifyAction(Request $request)
  61.     {
  62.         $user $this->getUser();
  63.         if ($user && ($user instanceof Customer)) {
  64.             $language "en";
  65.             return $this->render('account/passwordModify.html.twig', array(
  66.                 'locale' => $language,
  67.                 'languages' => $user->getLanguages(),
  68.                 'psw_criter_upper' => $this->translator->trans("error_password_uppercase", [], 'messages'$language),
  69.                 'psw_criter_lower' => $this->translator->trans("error_password_lowercase", [], 'messages'$language),
  70.                 'psw_criter_special' => $this->translator->trans("error_password_special", [], 'messages'$language),
  71.                 'psw_criter_twelve' => $this->translator->trans("error_password_number", [], 'messages'$language),
  72.                 'psw_criter_number' => $this->translator->trans("error_password_twelve", [], 'messages'$language)
  73.             ));
  74.         } else {
  75.             return $this->redirectToRoute('home');
  76.         }
  77.     }
  78.     /**
  79.      * @Route("/password-modify", methods={"POST"})
  80.      */
  81.     public function passwordAccountModifyAction(Request $request)
  82.     {
  83.         $old_password $request->get('old_password');
  84.         $new_password $request->get('new_password');
  85.         $repeat_new_password $request->get('repeat_new_password');
  86.         $user $this->getUser();
  87.         
  88.         if ($user && ($user instanceof Customer)) {
  89.             $errors = [];
  90.             $language "en";
  91.             $error_old_password "";
  92.             $errors_different_password "";
  93.             if(!password_verify($old_password,$user->getPassword())){
  94.                 $error_old_password $this->translator->trans("error_old_password", [], 'messages'$language); 
  95.             }
  96.             if ($new_password == $repeat_new_password) {
  97.                 if (preg_match('/[A-Z]/'$new_password) == false) {
  98.                     $errors[] = "upper_error";
  99.                 }
  100.                 if (preg_match('/[a-z]/'$new_password) == false) {
  101.                     $errors[] = "lower_error";
  102.                 }
  103.                 if (preg_match('/[\'^£$%&*()}{@#~?><>,!|=_+¬-]/'$new_password) == false) {
  104.                     $errors[] = "special_error";
  105.                 }
  106.                 if (preg_match('/[0-9]/'$new_password) == false) {
  107.                     $errors[] = "number_error";
  108.                 }
  109.                 if (strlen($new_password) < 6) {
  110.                     $errors[] = "six_error";
  111.                 }
  112.             } else {
  113.                 $errors_different_password $this->translator->trans("error_password_different", [], 'messages'$language);
  114.             }
  115.             if (count($errors) > || (empty($error_old_password) == false) || (empty($errors_different_password) == false)) {
  116.                 return $this->render('account/passwordModify.html.twig', array(
  117.                     'errors' => $errors,
  118.                     'error_old_password' => $error_old_password,
  119.                     'errors_different_password' => $errors_different_password,
  120.                     'psw_criter_upper' => $this->translator->trans("error_password_uppercase", [], 'messages'$language),
  121.                     'psw_criter_lower' => $this->translator->trans("error_password_lowercase", [], 'messages'$language),
  122.                     'psw_criter_special' => $this->translator->trans("error_password_special", [], 'messages'$language),
  123.                     'psw_criter_twelve' => $this->translator->trans("error_password_number", [], 'messages'$language),
  124.                     'psw_criter_number' => $this->translator->trans("error_password_twelve", [], 'messages'$language),
  125.                     'locale' => $language,
  126.                     'languages' => $user->getLanguages()
  127.                 ));
  128.             } else {
  129.                 $user->setPassword(strval($new_password));
  130.                 $user->setPasswordRecoveryToken(null);
  131.                 $user->save();
  132.                 return $this->redirectToRoute('home');
  133.                 
  134.             }
  135.         } else {
  136.             return $this->redirectToRoute('home');
  137.         }
  138.     }
  139.     /**
  140.      * @Route("/password-restore", methods={"POST"}, name="password-restore-process")
  141.      */
  142.     public function passwordRestoreAction(Request $request)
  143.     {
  144.         $guid $request->get('guid');
  145.         $title $request->get('title');
  146.         $new_password $request->get('new_password');
  147.         $repeat_new_password $request->get('repeat_new_password');
  148.         if (!(empty($guid))) {
  149.             $customer Customer::getByPasswordRecoveryToken($guid1);
  150.             $language "en";
  151.             $errors = [];
  152.             $errors_different_password "";
  153.             if ($new_password == $repeat_new_password) {
  154.                 if (preg_match('/[A-Z]/'$new_password) == false) {
  155.                     $errors[] = "upper_error";
  156.                 }
  157.                 if (preg_match('/[a-z]/'$new_password) == false) {
  158.                     $errors[] = "lower_error";
  159.                 }
  160.                 if (preg_match('/[\'^£$%&*()}{@#~?><>,!|=_+¬-]/'$new_password) == false) {
  161.                     $errors[] = "special_error";
  162.                 }
  163.                 if (preg_match('/[0-9]/'$new_password) == false) {
  164.                     $errors[] = "number_error";
  165.                 }
  166.                 if (strlen($new_password) < 6) {
  167.                     $errors[] = "six_error";
  168.                 }
  169.             } else {
  170.                 $errors_different_password $this->translator->trans("error_password_different", [], 'messages'$language);
  171.             }
  172.             if (count($errors) > || (empty($errors_different_password) == false)) {
  173.                 return $this->render('account/passwordRestore.html.twig', array(
  174.                     'errors' => $errors,
  175.                     'errors_different_password' => $errors_different_password,
  176.                     'psw_criter_upper' => $this->translator->trans("error_password_uppercase", [], 'messages'$language),
  177.                     'psw_criter_lower' => $this->translator->trans("error_password_lowercase", [], 'messages'$language),
  178.                     'psw_criter_special' => $this->translator->trans("error_password_special", [], 'messages'$language),
  179.                     'psw_criter_twelve' => $this->translator->trans("error_password_number", [], 'messages'$language),
  180.                     'psw_criter_number' => $this->translator->trans("error_password_twelve", [], 'messages'$language),
  181.                     'title' => $title,
  182.                     'guid' => $guid,
  183.                     'locale' => $language
  184.                 ));
  185.             } else {
  186.                 $customer->setPassword(strval($new_password));
  187.                 $customer->setPasswordRecoveryToken(null);
  188.                 $customer->save();
  189.                 return $this->render('account/login.html.twig', array(
  190.                     'success' => $this->translator->trans("password_restored_msg", [], 'messages'$language),
  191.                     'error' => "",
  192.                     'locale' => $language
  193.                 ));
  194.                 
  195.             }
  196.         }
  197.     }
  198.     /**
  199.      * @Route("/password-recovery", methods={"POST"})
  200.      *
  201.      * @param Request $request
  202.      * @return void
  203.      */
  204.     public function passwordAccountRecoveryAction(Request $request)
  205.     {
  206.         $email $request->get('email');
  207.         $customerByEmail Customer::getByEmail($email1);
  208.         if ($email && ($customerByEmail instanceof Customer)) {
  209.             $guid uuid_create();
  210.             $customerByEmail->setPasswordRecoveryToken($guid);
  211.             $customerByEmail->save();
  212.             $language $customerByEmail->getCustomerLanguage();
  213.             if (empty($language) || !in_array($language, array("en""it"))) {
  214.                 $language "en";
  215.             }
  216.             //generare email
  217.             $emailUrl \Pimcore\Tool::getHostUrl("https") . '/password-restore' '/' $guid;
  218.             $mail = new \Pimcore\Mail();
  219.             $mail->to($email);
  220.             $mail->subject("Kemon - ".$this->translator->trans("password_recovery", [], 'messages'$language));
  221.             $mail->html("<p>".$this->translator->trans("greetings", [], 'messages'$language)." " $customerByEmail->getFirstname() . ",</p>
  222.                         <br>
  223.                         <p>".$this->translator->trans("password_recovery_request", [], 'messages'$language)."</p>
  224.                         <p>".$this->translator->trans("click_the_following_link", [], 'messages'$language)."</p>
  225.                         <br><a href='$emailUrl'>$emailUrl</a><br>
  226.                         <p>".$this->translator->trans("have_a_nice_day", [], 'messages'$language).",</p><br>
  227.                         <p>".$this->translator->trans("kemon_team", [], 'messages'$language)."</p>");
  228.             $mail->send();
  229.             return $this->render('account/passwordRecovery.html.twig', array(
  230.                 'success' => $this->translator->trans("email_recovery_sent", [], 'messages'"en"),"locale"=>"en"));
  231.         } else {
  232.             return $this->render('account/passwordRecovery.html.twig', array(
  233.                 'error' => $this->translator->trans("email_recovery_error", [], 'messages'"en"),"locale"=>"en"));
  234.         }
  235.     }
  236. }