bundles/AccountBundle/Controller/AccountController.php line 131

Open in your IDE?
  1. <?php
  2. namespace AccountBundle\Controller;
  3. use AccountBundle\Form\ActivateFormType;
  4. use AccountBundle\Form\EditAccountFormType;
  5. use AccountBundle\Form\LoginFormType;
  6. use AccountBundle\Form\LostPasswordFormType;
  7. use AccountBundle\Form\SignupFormType;
  8. use App\Services\CheckwebsitesettingService;
  9. use AccountBundle\Services\AccountSendMailService;
  10. use App\Services\ReCaptchaService;
  11. use MultilingualBundle\Service\DocumentLookupService;
  12. use Pimcore\Model\DataObject;
  13. use Pimcore\Model\User;
  14. use Pimcore\Translation\Translator;
  15. use Pimcore\Twig\Extension\Templating\PimcoreUrl;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  17. use Symfony\Component\HttpFoundation\Request;
  18. use Symfony\Component\HttpFoundation\Response;
  19. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  20. use Symfony\Component\Routing\Annotation\Route;
  21. class AccountController extends \App\Controller\BaseController
  22. {
  23.     protected $recaptcha;
  24.     protected $recaptchaVersion;
  25.     protected $recaptchaPublicKey;
  26.     protected $checkwebsitesettingService;
  27.     protected $inotherlang;
  28.     protected $translator;
  29.     protected $session;
  30.     protected $sendmailAccount;
  31.     protected $secretKey;
  32.     protected $currentLanguage;
  33.     /**
  34.      * @var PimcoreUrl
  35.      */
  36.     protected $pimcoreUrl;
  37.     /**
  38.      *
  39.      * @param PimcoreUrl $pimcoreUrl
  40.      */
  41.     public function __construct(CheckwebsitesettingService $checkwebsitesettingServiceDocumentLookupService $inotherlangTranslator $translatorReCaptchaService $recaptchaAccountSendMailService $sendmailAccountParameterBagInterface $paramsPimcoreUrl $pimcoreUrlSessionInterface $session){
  42.         $this->checkwebsitesettingService $checkwebsitesettingService;
  43.         $this->inotherlang $inotherlang;
  44.         $this->translator $translator;
  45.         $this->pimcoreUrl $pimcoreUrl;
  46.         $this->session $session;
  47.         $this->recaptcha $recaptcha;
  48.         $this->recaptchaVersion $recaptcha->getVersion();
  49.         $this->recaptchaPublicKey $recaptcha->getPublicKey();
  50.         $this->sendmailAccount $sendmailAccount;
  51.         $this->secretKey $params->get('account.secretkey');
  52.     }
  53.     /**
  54.      *    Parent Account page
  55.      *
  56.      * @param Request $request
  57.      * @return Response
  58.      */
  59.     public function parentDocAction(Request $request\Pimcore\Config\Config $websiteConfig){
  60.         $session $request->getSession();
  61.         if($session->get('userLogged')) {
  62.             $userObj $session->get('userLogged');
  63.             return $this->redirect($this->pimcoreUrl->__invoke(
  64.                 [],
  65.                 'account-page',
  66.                 true
  67.             ));
  68.         }else{
  69.             return $this->redirect($this->pimcoreUrl->__invoke(
  70.                 [],
  71.                 'login-page',
  72.                 true
  73.             ));
  74.         }
  75.     }
  76.     /**
  77.      *    Login / Logout view page
  78.      *
  79.      * @param Request $request
  80.      * @return Response
  81.      */
  82.     public function loginLogoutAction(Request $request)
  83.     {
  84.         $userLoggedIn false;
  85.         $session $request->getSession();
  86.         if($session->get('userLogged')) {
  87.             $userLoggedIn true;
  88.         }
  89.         return $this->render('@Account/account/login-logout.html.twig', [
  90.             'userLoggedIn' => $userLoggedIn,
  91.         ]);
  92.     }
  93.     /**
  94.      *    Login page
  95.      *
  96.      * @Route("{_locale}/account/login", name="login-page")
  97.      *
  98.      * @param Request $request
  99.      * @return Response
  100.      */
  101.     public function loginAction(Request $request\Pimcore\Config\Config $websiteConfig)
  102.     {
  103.         $signupsuccess false;
  104.         $formName LoginFormType::class;
  105.         $session $request->getSession();
  106.         // Check if user is already logged in
  107.         if($session->get('userLogged')) {
  108.             return $this->redirect($this->pimcoreUrl->__invoke(
  109.                 [],
  110.                 'account-page',
  111.                 true
  112.             ));
  113.         }
  114.         if($formName){
  115.             $form $this->createForm($formName);
  116.             $form->handleRequest($request);
  117.         }
  118.         // Check if account exist
  119.         if ($form->isSubmitted()) {
  120.             if ($form->isValid()) {
  121.                 $params $request->request->all();
  122.                 $data $form->getData();
  123.                 if($this->recaptchaVersion){
  124.                     if($this->recaptcha->captchaverify($params)){
  125.                         $redirect $this->loginToAccount($data$request$session);
  126.                     }else{
  127.                         $message $this->translator->trans("Captcha code is niet correct!");
  128.                         $this->addFlash("warning"$message);
  129.                     }
  130.                 }else{
  131.                     $redirect $this->loginToAccount($data$request$session);
  132.                 }
  133.                 return $this->redirect($redirect);
  134.             }
  135.         }
  136.         return $this->render('@Account/account/login.html.twig', [
  137.             'form' => $form->createView(),
  138.             'signupsuccess' => $signupsuccess,
  139.             'recaptcha' => $this->recaptchaVersion,
  140.             'recaptchaPublic' => $this->recaptchaPublicKey
  141.         ]);
  142.     }
  143.     protected function loginToAccount($data$request$session)
  144.     {
  145.         $email $data['email'];
  146.         $password $data['password'];
  147.         if (!empty($password) && !empty($email)) {
  148.             $userObject DataObject\UserPortal::getByEmail($email, array('limit' => 1));
  149.             /** @var \Pimcore\Model\DataObject\UserPortal $userObject */
  150.             if ($userObject) {
  151.                 if (password_verify("$password"$userObject->getPassword())) {
  152.                     $session->set('userLogged'$userObject);
  153.                     $redirectUri $request->get("r"); // If return uri is given
  154.                     if ($redirectUri) {
  155.                         $redirectUri base64_decode($redirectUri);
  156.                         return $redirectUri;
  157.                     } else {
  158.                         $message $this->translator->trans("Successfully logged in");
  159.                         $this->addFlash("info"$message);
  160.                         return $this->pimcoreUrl->__invoke(
  161.                             [],
  162.                             'cart-page',
  163.                             true
  164.                         );
  165.                     }
  166.                 } else {
  167.                     $message $this->translator->trans("Password incorrect!");
  168.                     $this->addFlash("warning"$message);
  169.                     return $this->pimcoreUrl->__invoke(
  170.                         [],
  171.                         'login-page',
  172.                         true
  173.                     );
  174.                 }
  175.             } else {
  176.                 $message $this->translator->trans("User doesn't exist!");
  177.                 $this->addFlash("warning"$message);
  178.                 return $this->pimcoreUrl->__invoke(
  179.                     [],
  180.                     'login-page',
  181.                     true
  182.                 );
  183.             }
  184.         } else {
  185.             $message $this->translator->trans("User or password incorrect!");
  186.             $this->addFlash("warning"$message);
  187.             return $this->pimcoreUrl->__invoke(
  188.                 [],
  189.                 'login-page',
  190.                 true
  191.             );
  192.         }
  193.     }
  194.     /**
  195.      *    Logout page
  196.      *
  197.      * @Route("{_locale}/account/logout", name="logout-page")
  198.      *
  199.      * @param Request $request
  200.      * @return Response
  201.      */
  202.     public function logoutAction(Request $request\Pimcore\Config\Config $websiteConfig)
  203.     {
  204.         $session $request->getSession();
  205.         $session->set('userLogged'false);
  206.         return $this->redirect($this->pimcoreUrl->__invoke(
  207.             [],
  208.             'login-page',
  209.             true
  210.         ));
  211.     }
  212.     /**
  213.      *    Signup page
  214.      *
  215.      * @Route("{_locale}/account/signup", name="signup-page")
  216.      *
  217.      * @param Request $request
  218.      * @return Response
  219.      */
  220.     public function signupAction(Request $request\Pimcore\Config\Config $websiteConfig)
  221.     {
  222.         $signupsuccess false;
  223.         $formName SignupFormType::class;
  224.         $emailTemplate 'email_account_signup';           // Email template contact
  225.         $emailTemplateConfirm 'email_account_signup_customer';   // Email template contact confirm
  226.         $confirmAccountOnRegister $websiteConfig->get('confirmAccountOnRegister');
  227.         if ($formName) {
  228.             $form $this->createForm($formName);
  229.             $form->handleRequest($request);
  230.         }
  231.         if ($this->checkwebsitesettingService->check("loginportal_userPath""object")) {
  232.             $folderUsersPath $websiteConfig->get('loginportal_userPath');
  233.             if($folderUsersPath){
  234.                 $folderUsersFullPath $folderUsersPath->getFullpath();
  235.                 $folderUsersObject DataObject\Folder::getByPath($folderUsersFullPath);
  236.             }
  237.             if(!empty($folderUsersObject)) {
  238.                 $folderUsersId $folderUsersObject->getId();
  239.                 if ($form->isSubmitted()) {
  240.                     if ($form->isValid()) {
  241.                         $params $request->request->all();
  242.                         if($confirmAccountOnRegister){
  243.                             $message $this->translator->trans("Er werd een activatie mail verstuurd!");
  244.                         }else{
  245.                             $message $this->translator->trans("Uw account wordt gereviewed voor activatie.");
  246.                         }
  247.                         if($this->recaptchaVersion){
  248.                             if($this->recaptcha->captchaverify($params)){
  249.                                 $data $form->getData();
  250.                                 $this->sendmailAccount->_sendMailDefaultForm($websiteConfig$data"$emailTemplate""$emailTemplateConfirm""$message""");
  251.                                 $signupsuccess true;
  252.                             }else{
  253.                                 $message $this->translator->trans("Captcha code is niet correct!");
  254.                                 $this->addFlash("warning"$message);
  255.                             }
  256.                         }else{
  257.                             $data $form->getData();
  258.                             $this->sendmailAccount->_sendMailDefaultForm($websiteConfig$data"$emailTemplate""$emailTemplateConfirm""$message""");
  259.                             $signupsuccess true;
  260.                         }
  261.                     }
  262.                 }
  263.             }
  264.         }
  265.         return $this->render('@Account/account/signup.html.twig', [
  266.             'form' => $form->createView(),
  267.             'signupsuccess' => $signupsuccess,
  268.             'recaptcha' => $this->recaptchaVersion,
  269.             'recaptchaPublic' => $this->recaptchaPublicKey
  270.         ]);
  271.     }
  272.     /**
  273.      *    Activate account page
  274.      *
  275.      * @Route("{_locale}/account/activate", name="activate-page")
  276.      *
  277.      * @param Request $request
  278.      * @return Response
  279.      */
  280.     public function activateAction(Request $request)
  281.     {
  282.         $currentLanguage $request->attributes->get('_locale');
  283.         $hash str_replace($this->secretKey,"",base64_decode($request->get("c")));
  284.         $id $request->get("id");
  285.         $email $request->get("e");
  286.         $sendsuccessfull false;
  287.         if(!empty($hash) && (!empty($id) || !empty($email))) {
  288.             if($email){
  289.                 $findKlant = new DataObject\UserPortal\Listing();
  290.                 $findKlant->setCondition("email = :email", ["email" => $email]);
  291.                 $findKlant->setLimit(1);
  292.                 $findKlant->setUnpublished(true);
  293.                 foreach ($findKlant as $klant){
  294.                     $id $klant->getId();
  295.                 }
  296.             }
  297.             $lidObject DataObject\UserPortal::getById($id);
  298.             if(!empty($lidObject)) {
  299.                 if($hash === $lidObject->getPassword()){
  300.                     $formName ActivateFormType::class;
  301.                     if ($formName) {
  302.                         $form $this->createForm($formName);
  303.                         $form->handleRequest($request);
  304.                     }
  305.                     if ($form->isSubmitted()) {
  306.                         if ($form->isValid()) {
  307.                             $data $form->getData();
  308.                             if ($data['password'] != $data['confirmpassword']) {
  309.                                 $message $this->translator->trans("The passwords specified must be identical!");
  310.                                 $this->addFlash("success"$message);
  311.                                 return $this->redirect($this->pimcoreUrl->__invoke(
  312.                                         [],
  313.                                         'activate-page',
  314.                                         true
  315.                                     ) . '?id=' $id '&code=' $request->get("c"));
  316.                             }
  317.                             $lidObject->setPassword($data["password"]);
  318.                             try {
  319.                                 $lidObject->save();
  320.                                 // message to confirm that new password was save
  321.                                 $message $this->translator->trans("Your new password is set! You can now login.");
  322.                                 $this->addFlash("success"$message);
  323.                                 return $this->redirect($this->pimcoreUrl->__invoke(
  324.                                     [],
  325.                                     'login-page',
  326.                                     true
  327.                                 ));
  328.                             } catch (\Exception $e) {
  329.                                 $message date("d-m-Y H:i:s") . ' ' $e->getMessage() . "\n";
  330.                                 error_log($message3PIMCORE_LOG_DIRECTORY "/system.log");
  331.                             }
  332.                         }
  333.                     }
  334.                 }elseif(base64_decode($hash) === $lidObject->getEmail()){
  335.                     $lidObject->setPublished(true);
  336.                     try {
  337.                         $lidObject->save();
  338.                         // message to confirm that new password was save
  339.                         $message $this->translator->trans("Your account has been activated. You can now login.");
  340.                         $this->addFlash("success"$message);
  341.                         return $this->redirect($this->pimcoreUrl->__invoke(
  342.                             [],
  343.                             'login-page',
  344.                             true
  345.                         ));
  346.                     } catch (\Exception $e) {
  347.                         $message date("d-m-Y H:i:s") . ' ' $e->getMessage() . "\n";
  348.                         error_log($message3PIMCORE_LOG_DIRECTORY "/system.log");
  349.                     }
  350.                 }
  351.             }
  352.         }
  353.         return $this->render('@Account/account/activate.html.twig', [
  354.             'form' => $form->createView(),
  355.             'sendsuccessfull' => $sendsuccessfull,
  356.             'recaptcha' => $this->recaptchaVersion,
  357.             'recaptchaPublic' => $this->recaptchaPublicKey
  358.         ]);
  359.     }
  360.     /**
  361.      *    Lost password page
  362.      *
  363.      * @Route("{_locale}/account/lostpassword", name="lostpassword-page")
  364.      *
  365.      * @param Request $request
  366.      * @return Response
  367.      */
  368.     public function lostpasswordAction(Request $request\Pimcore\Config\Config $websiteConfig)
  369.     {
  370.         $sendsuccessfull false;
  371.         $formName LostPasswordFormType::class;
  372.         $emailTemplate 'login_portal_email_lostpassword';
  373.         if ($formName) {
  374.             $form $this->createForm($formName);
  375.             $form->handleRequest($request);
  376.         }
  377.         if ($form->isSubmitted()) {
  378.             if ($form->isValid()) {
  379.                 $params $request->request->all();
  380.                 $activatePath $this->pimcoreUrl->__invoke(
  381.                     [],
  382.                     'activate-page',
  383.                     true
  384.                 );
  385.                 if($this->recaptchaVersion){
  386.                     if($this->recaptcha->captchaverify($params)){
  387.                         $data $form->getData();
  388.                         $this->sendmailAccount->_sendMailForgotPasswordForm($websiteConfig$data$emailTemplate"""""$activatePath");
  389.                         $sendsuccessfull true;
  390.                     }else{
  391.                         $message $this->translator->trans("Captcha code is niet correct!");
  392.                         $this->addFlash("warning"$message);
  393.                     }
  394.                 }else{
  395.                     $data $form->getData();
  396.                     $this->sendmailAccount->_sendMailForgotPasswordForm($websiteConfig$data$emailTemplate"""""$activatePath");
  397.                     $sendsuccessfull true;
  398.                 }
  399.                 return $this->redirectToRoute('account-page');
  400.             }
  401.         }
  402.         return $this->render('@Account/account/lostpassword.html.twig', [
  403.             'form' => $form->createView(),
  404.             'sendsuccessfull' => $sendsuccessfull,
  405.             'recaptcha' => $this->recaptchaVersion,
  406.             'recaptchaPublic' => $this->recaptchaPublicKey
  407.         ]);
  408.     }
  409.     /**
  410.      *    View account page
  411.      *
  412.      * @Route("{_locale}/account/view", name="account-page")
  413.      *
  414.      * @param Request $request
  415.      * @return Response
  416.      */
  417.     public function accountAction(Request $request)
  418.     {
  419.         $session $request->getSession();
  420.         // Check if user is already logged in
  421.         if($session->get('userLogged')) {
  422.             $userObj $session->get('userLogged');
  423.             if ($userObj->getGeboorte_datum()){
  424.                 $date $userObj->getGeboorte_datum()->format("d/m/Y");
  425.             }else{
  426.                 $date null;
  427.             }
  428.             return $this->render('@Account/account/account.html.twig',[
  429.                 'user' => $userObj,
  430.                 'date' => $date
  431.             ]);
  432.         }else{
  433.             return $this->redirect($this->pimcoreUrl->__invoke(
  434.                 [],
  435.                 'login-page',
  436.                 true
  437.             ));
  438.         }
  439.     }
  440.     /**
  441.      *    Edit account page
  442.      *
  443.      * @Route("{_locale}/account/edit", name="editaccount-page")
  444.      *
  445.      * @param Request $request
  446.      * @return Response
  447.      */
  448.     public function editaccountAction(Request $request)
  449.     {
  450.         $session $request->getSession();
  451.         // Check if user is already logged in
  452.         if($session->get('userLogged')) {
  453.             $sendsuccessfull false;
  454.             $formName EditAccountFormType::class;
  455.             if ($formName) {
  456.                 $form $this->createForm($formName);
  457.                 $form->handleRequest($request);
  458.             }
  459.             $userObj $session->get('userLogged');
  460.             if (!empty($userObj)) {
  461.                 $userId $userObj->getId();
  462.                 $userObject DataObject\UserPortal::getById($userId);
  463.             }
  464.             if ($form->isSubmitted()) {
  465.                 if ($form->isValid()) {
  466.                     $params $request->request->all();
  467.                     if($this->recaptchaVersion){
  468.                         if($this->recaptcha->captchaverify($params)){
  469.                             $data $form->getData();
  470.                             $this->saveUserData($data$userObject);
  471.                             $session->set('userLogged'$userObject);
  472.                             $sendsuccessfull true;
  473.                         }else{
  474.                             $message $this->translator->trans("Captcha code is niet correct!");
  475.                             $this->addFlash("warning"$message);
  476.                         }
  477.                     }else{
  478.                         $data $form->getData();
  479.                         $this->saveUserData($data$userObject);
  480.                         $session->set('userLogged'$userObject);
  481.                         $sendsuccessfull true;
  482.                     }
  483.                     return $this->redirectToRoute('account-page');
  484.                 }
  485.             }
  486.             return $this->render('@Account/account/editaccount.html.twig', [
  487.                 'form' => $form->createView(),
  488.                 'userObject' => $userObject,
  489.                 'sendsuccessfull' => $sendsuccessfull,
  490.                 'recaptcha' => $this->recaptchaVersion,
  491.                 'recaptchaPublic' => $this->recaptchaPublicKey
  492.             ]);
  493.         }else{
  494.             return $this->redirect($this->pimcoreUrl->__invoke(
  495.                 [],
  496.                 'login-page',
  497.                 true
  498.             ));
  499.         }
  500.     }
  501.     protected function saveUserData($data$userObject){
  502.         /** @var \Pimcore\Model\DataObject\UserPortal $userObject */
  503.         $userObject->setNaam($data['lastname']);
  504.         $userObject->setVoornaam($data['firstname']);
  505.         $userObject->setTel($data['phone']);
  506.         $userObject->setMobile($data['mobile']);
  507.         $userObject->setAdres_straat($data['address']);
  508.         $userObject->setAdres_nr($data['number']);
  509.         $userObject->setAdres_postcode($data['postcode']);
  510.         $userObject->setAdres_stad($data['city']);
  511.         /*$userObject->setNewsletter($data['newsletter']);*/
  512.         $userObject->setGeboorte_datum($data['birthday']);
  513.         $userObject->setAdres_box($data['box']);
  514.         $userObject->setAdres_land($data['country']);
  515.         try {
  516.             if($userObject->save()) {
  517.                 $message $this->translator->trans("De gegevens werden bewaard!");
  518.                 $message $this->translator->trans($message);
  519.                 $this->session->getFlashBag()->add('success'$message);
  520.                 return $this->redirect($this->pimcoreUrl->__invoke(
  521.                     [],
  522.                     'editaccount-page',
  523.                     true
  524.                 ));
  525.             }
  526.         } catch (\Exception $e) {
  527.             $message date("d-m-Y H:i:s") . ' ' $e->getMessage() . "\n";
  528.             error_log($message3PIMCORE_LOG_DIRECTORY "/system.log");
  529.         }
  530.     }
  531.     /**
  532.      *    Default protected page
  533.      */
  534.     public function defaultprotectedAction(Request $request)
  535.     {
  536.         $session $request->getSession();
  537.         if(empty($session->get('userLogged')) || !$this->editmode) {
  538.             return $this->redirect($this->pimcoreUrl->__invoke(
  539.                     [],
  540.                     'login-page',
  541.                     true
  542.                 )."?r=".base64_encode($this->document->getFullPath()));
  543.         }
  544.     }
  545.     private function random_str($length$keyspace '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
  546.     {
  547.         $pieces = [];
  548.         $max mb_strlen($keyspace'8bit') - 1;
  549.         for ($i 0$i $length; ++$i) {
  550.             $pieces []= $keyspace[random_int(0$max)];
  551.         }
  552.         return implode(''$pieces);
  553.     }
  554. }