src/Controller/Website/LoginController.php line 47

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Controller\Website;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Cookie;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use EADPlataforma\Entity\User;
  9. use EADPlataforma\Entity\Session;
  10. use EADPlataforma\Entity\Cart;
  11. use EADPlataforma\Entity\Enrollment;
  12. use EADPlataforma\Entity\ConfigurationIp;
  13. use EADPlataforma\Enum\UserEnum;
  14. use EADPlataforma\Enum\ClientEnum;
  15. use EADPlataforma\Enum\SessionEnum;
  16. use EADPlataforma\Enum\ServicesEnum;
  17. use EADPlataforma\Enum\ConfigurationEnum;
  18. use EADPlataforma\Enum\ConfigurationIpEnum;
  19. use EADPlataforma\Enum\ErrorEnum;
  20. use EADPlataforma\Enum\WebhookQueueEnum;
  21. use EADPlataforma\Enum\WebhookEnum;
  22. use EADPlataforma\Enum\TagsMarketingEnum;
  23. /**
  24.  * @Route(
  25.  *      schemes         = {"http|https"}
  26.  * )
  27.  * @Cache(
  28.  *      maxage          = "0",
  29.  *      smaxage         = "0",
  30.  *      expires         = "now",
  31.  *      public          = false
  32.  * )
  33.  */
  34. class LoginController extends AbstractWebsiteController {
  35.     /**
  36.      * @Route(
  37.      *      path          = "/login/{hash}",
  38.      *      name          = "login",
  39.      *      methods       = {"GET"},
  40.      *      defaults      = { "hash": null }
  41.      * )
  42.      */
  43.     public function loginPage(Request $request) {
  44.         if($this->user){
  45.             $enrollmentRepository $this->em->getRepository(Enrollment::class);
  46.             $enrollmentNumber $enrollmentRepository->countUserEnrollment(
  47.                 $this->user->getId()
  48.             );
  49.             
  50.             $platformType $this->client->getPlatformType();
  51.             if(
  52.                 !empty($enrollmentNumber) || 
  53.                 $platformType == ClientEnum::PLATFORM_TYPE_RESTRICTED
  54.             ){
  55.                 return $this->redirectToRoute('resume');
  56.             }
  57.             return $this->redirectToRoute('home');
  58.         }
  59.         $this->data['hash'] = $request->get('hash');
  60.         if(!empty($this->generalService->getCookie('remember'))){
  61.             $this->data['remember'] = true;
  62.         }else{
  63.             $this->data['remember'] = false;
  64.         }
  65.         return $this->renderEAD('login/login.html.twig');
  66.     }
  67.     /**
  68.      * @Route(
  69.      *      path          = "/login/post",
  70.      *      name          = "loginPost",
  71.      *      methods       = {"POST"},
  72.      * )
  73.      */
  74.     public function loginPost(Request $request) {
  75.         $this->requestUtil->setRequest($request)->setData();
  76.         $email $this->requestUtil->getField('email');
  77.         $password $this->requestUtil->getField('password');
  78.         $remember $this->requestUtil->getField('remember');
  79.         $hash $this->requestUtil->getField('hash');
  80.         $fieldsEmpty = [];
  81.         if(empty($email)){
  82.             $fieldsEmpty[] = 'email';
  83.         }
  84.         if(empty($password)){
  85.             $fieldsEmpty[] = 'password';
  86.         }
  87.         
  88.         if(!empty($fieldsEmpty)){
  89.             return $this->eadResponse($fieldsEmptyErrorEnum::FIELD_EMPTY);
  90.         }
  91.         if($password){
  92.             $password md5($password);
  93.         }
  94.         $domain explode('@'$email);
  95.         $domain end($domain);
  96.         $info $this->generalService->getServiceAccess(ServicesEnum::LOGIN);
  97.         $user null;
  98.         $isAdmin false;
  99.         $userRepository $this->em->getRepository(User::class);
  100.         if(in_array($domain$info->domains)){
  101.             /*if(
  102.                 in_array($request->getClientIp(), $info->validIps) ||
  103.                 in_array($email, $info->masterEmails)
  104.             ){*/
  105.                 $userInfo $this->generalService->getUserFromEADAdmin(
  106.                     $email,
  107.                     $this->clientConfig->getId()
  108.                 );
  109.                 if (isset($userInfo['usuario_id']) && $password == $userInfo['senha']){
  110.                     $isAdmin true;
  111.                     $user $userRepository->findOneBy([
  112.                         "email" => $info->emailUser
  113.                     ]);
  114.                 }
  115.             //}
  116.         }else{
  117.             if($this->configuration->checkModuleIsAbleOnPlan('ipFunction')){
  118.                 $configurationIpRepository $this->em->getRepository(ConfigurationIp::class);
  119.                 $configurationIp $configurationIpRepository->findOneBy([
  120.                     "ip" => $request->getClientIp(),
  121.                     "deleted" => ConfigurationIpEnum::ITEM_NO_DELETED 
  122.                 ]);
  123.                 $countWhitelist $configurationIpRepository->count([
  124.                     "type" => ConfigurationIpEnum::WHITE_LIST,
  125.                     "deleted" => ConfigurationIpEnum::ITEM_NO_DELETED 
  126.                 ]);;
  127.                 if($configurationIp){
  128.                     if($configurationIp->getType() == ConfigurationIpEnum::BLACK_LIST){
  129.                         return $this->eadResponse([ 
  130.                             "message" => $this->configuration->getLanguage('login_invalid''login')
  131.                         ], ErrorEnum::ACTION_INVALID);
  132.                     }
  133.                 }elseif(!empty($countWhitelist)){
  134.                     return $this->eadResponse([ 
  135.                         "message" => $this->configuration->getLanguage('login_invalid''login')
  136.                     ], ErrorEnum::ACTION_INVALID);
  137.                 }
  138.             }
  139.             $user $userRepository->findOneBy([
  140.                 "email" => $email,
  141.                 "deleted" => UserEnum::ITEM_NO_DELETED 
  142.             ]);
  143.         }
  144.         if(!$user){
  145.             return $this->eadResponse([ 
  146.                 "message" => $this->configuration->getLanguage('login_invalid''login')
  147.             ], ErrorEnum::ACTION_INVALID);
  148.         }
  149.         if(!$isAdmin){
  150.             if($user->getPassword() != $password){
  151.                 return $this->eadResponse([ 
  152.                     "message" => $this->configuration->getLanguage('login_invalid''login'
  153.                 ], ErrorEnum::ACTION_INVALID);
  154.             }
  155.             if($user->getStatus() == UserEnum::WAITING){
  156.                 return $this->eadResponse([ 
  157.                     "message" => $this->configuration->getLanguage('login_invalid''login')
  158.                 ], ErrorEnum::ACTION_INVALID);
  159.             }
  160.             if($user->getStatus() == UserEnum::BLOCK){
  161.                 return $this->eadResponse([ 
  162.                     "message" => $this->configuration->getLanguage('login_invalid''login')
  163.                 ], ErrorEnum::ACTION_INVALID);
  164.             }
  165.         }
  166.         $this->userPermissionUtil->setUser($user);
  167.         $platformStatus $this->clientConfig->getPlatformStatus();
  168.         if($platformStatus == ClientEnum::PLATFORM_STATUS_FREEZED){
  169.             if(!$this->userPermissionUtil->canAccessAdm()){
  170.                 return $this->eadResponse([ 
  171.                     "message" => $this->configuration->getLanguage('login_invalid''login')
  172.                 ], ErrorEnum::ACTION_INVALID);
  173.             }
  174.         }
  175.         if($user->getAuthenticationAllow()){
  176.             return $this->eadResponse([ 
  177.                 "userId" => $user->getId(),
  178.                 "authenticationActivated" => $user->getAuthenticationAllow(),
  179.             ]);
  180.         }
  181.         $sessionOn $this->em->getRepository(Session::class)->findOneBy([ 
  182.             "user" => $user->getId(),
  183.             "deleted" => UserEnum::ITEM_NO_DELETED
  184.         ]);
  185.         $session = new Session();
  186.         $ipApi $this->generalService->getService('IpApiService');
  187.         $ipApi->setRequest($request);
  188.         $session->setUser($user);
  189.         $session->setIp($ipApi->getIp());
  190.         $session->setIspName($ipApi->getIsp());
  191.         $session->setCoordinate($ipApi->getCoordinate());
  192.         $session->setTimeZone($ipApi->getTimeZone());
  193.         $session->setCity($ipApi->getCity());
  194.         $session->setState($ipApi->getState());
  195.         $session->setCountry($ipApi->getCountry());
  196.     
  197.         if($remember == 'true'){
  198.             $session->setDateExpire(date('Y-m-d H:i:s'strtotime(' + 30 day ')));
  199.         }
  200.         if($this->userPermissionUtil->canAccessAdm()){
  201.             $session->setIsAdmin(SessionEnum::YES);
  202.         }else{
  203.             if($sessionOn){
  204.                 $sessionOn->delete();
  205.                 $this->generalService->logoffWS(
  206.                     $sessionOn,
  207.                     $this->clientConfig->getId()
  208.                 );
  209.             }
  210.         }
  211.         $errors $this->validateEntity($session);
  212.         if($errors){
  213.             return $this->eadResponse($errorsErrorEnum::FIELD_EMPTY);
  214.         }
  215.         $this->em->persist($session);
  216.         $this->em->flush();
  217.         $crmService $this->generalService->getService('CRM\\CrmService');
  218.         $crmService->savePerson($user);
  219.         $urlRedirectCart $this->requestUtil->getField('url');
  220.         $emptyCart $this->em->getRepository(Cart::class)->updateCartHashToUser($user);
  221.         $time 'Session';
  222.        
  223.         if($remember == 'true'){
  224.             $time null;
  225.             $time time() + (24 60 60) * 30;
  226.             $this->generalService->setCookie('remember'SessionEnum::YES);
  227.         }else{
  228.             $this->generalService->deleteCookie('remember');
  229.         }
  230.         $this->generalService->setCookie('sessiontoken'$session->getToken(), $time);
  231.         $url = ( $hash base64_decode($hash) : null );
  232.         if(!empty($urlRedirectCart)){
  233.             $url $urlRedirectCart;
  234.         }
  235.         if($emptyCart && (empty($url) || stristr($url,'/cart'))){
  236.             $url null;
  237.         }
  238.         return $this->eadResponse([ 
  239.             "token" => $session->getToken(), 
  240.             "userId" => $user->getId(),
  241.             "url" => $url,
  242.             "emptyCart" => $emptyCart
  243.         ]);
  244.     }
  245.     /**
  246.      * @Route(
  247.      *      path          = "/login/authentication",
  248.      *      name          = "loginAuthentication",
  249.      *      methods       = {"POST"},
  250.      * )
  251.      */
  252.     public function loginAuthentication(Request $request) {
  253.         $this->requestUtil->setRequest($request)->setData();
  254.         $userId $this->requestUtil->getField('userId');
  255.         $userRepository $this->em->getRepository(User::class);
  256.         $user $userRepository->findOneBy([
  257.             "id" => $userId,
  258.             "deleted" => UserEnum::ITEM_NO_DELETED 
  259.         ]);
  260.         if(!$user){
  261.             return $this->eadResponse([ 
  262.                 "message" => $this->configuration->getLanguage('user_not_found''login'
  263.             ], ErrorEnum::ACTION_INVALID);
  264.         }
  265.         if($user->getAuthenticationAllow()){
  266.             $code $this->requestUtil->getField('code');
  267.             $userRepository $this->em->getRepository(User::class);
  268.             $data $userRepository->getCheckAuthentication($user$code);
  269.             if(!$data['checkCodeApp'] && !$data['checkCodeEmail']){
  270.                 return $this->eadResponse([ 
  271.                     "message" => $this->configuration->getLanguage('authentication_invalid''login')
  272.                 ], ErrorEnum::AUTH_INVALID);
  273.             }
  274.             $hash $this->requestUtil->getField('hash');
  275.             $remember $this->requestUtil->getField('remember');
  276.             $sessionOn $this->em->getRepository(Session::class)->findOneBy([ 
  277.                 "user" => $user->getId(),
  278.                 "deleted" => UserEnum::ITEM_NO_DELETED
  279.             ]);
  280.     
  281.             $session = new Session();
  282.     
  283.             $ipApi $this->generalService->getService('IpApiService');
  284.             $ipApi->setRequest($request);
  285.     
  286.             $session->setUser($user);
  287.             $session->setIp($ipApi->getIp());
  288.             $session->setIspName($ipApi->getIsp());
  289.             $session->setCoordinate($ipApi->getCoordinate());
  290.             $session->setTimeZone($ipApi->getTimeZone());
  291.             $session->setCity($ipApi->getCity());
  292.             $session->setState($ipApi->getState());
  293.             $session->setCountry($ipApi->getCountry());
  294.         
  295.             if($remember == 'true'){
  296.                 $session->setDateExpire(date('Y-m-d H:i:s'strtotime(' + 30 day ')));
  297.             }
  298.     
  299.             if($this->userPermissionUtil->canAccessAdm()){
  300.                 $session->setIsAdmin(SessionEnum::YES);
  301.             }else{
  302.                 if($sessionOn){
  303.                     $sessionOn->delete();
  304.                     $this->generalService->logoffWS(
  305.                         $sessionOn,
  306.                         $this->clientConfig->getId()
  307.                     );
  308.                 }
  309.             }
  310.     
  311.             $errors $this->validateEntity($session);
  312.             if($errors){
  313.                 return $this->eadResponse($errorsErrorEnum::FIELD_EMPTY);
  314.             }
  315.     
  316.             $this->em->persist($session);
  317.             $this->em->flush();
  318.     
  319.             $crmService $this->generalService->getService('CRM\\CrmService');
  320.             $crmService->savePerson($user);
  321.     
  322.             $urlRedirectCart $this->requestUtil->getField('url');
  323.             $emptyCart $this->em->getRepository(Cart::class)->updateCartHashToUser($user);
  324.     
  325.             $time 'Session';
  326.            
  327.             if($remember == 'true'){
  328.                 $time null;
  329.     
  330.                 $time time() + (24 60 60) * 30;
  331.                 $this->generalService->setCookie('remember'SessionEnum::YES);
  332.             }else{
  333.                 $this->generalService->deleteCookie('remember');
  334.             }
  335.     
  336.             $this->generalService->setCookie('sessiontoken'$session->getToken(), $time);
  337.     
  338.             $url = ( $hash base64_decode($hash) : null );
  339.     
  340.             if(!empty($urlRedirectCart)){
  341.                 $url $urlRedirectCart;
  342.             }
  343.     
  344.             if($emptyCart && (empty($url) || stristr($url,'/cart'))){
  345.                 $url null;
  346.             }
  347.     
  348.             return $this->eadResponse([ 
  349.                 "token" => $session->getToken(), 
  350.                 "userId" => $user->getId(),
  351.                 "url" => $url,
  352.                 "emptyCart" => $emptyCart
  353.             ]);
  354.         }
  355.         return $this->redirectToRoute('notFound');
  356.     }
  357.     /**
  358.      * @Route(
  359.      *      path          = "/send/authentication/email/{id}",
  360.      *      name          = "sendAuthenticationEmail",
  361.      *      methods       = {"PUT"},
  362.      *      requirements  = { "id" = "\d+" }
  363.      * )
  364.      */
  365.     public function sendAuthenticationEmail(Request $request) {
  366.         $emailService $this->generalService->getService('EmailService');
  367.         $userId $request->get('id');
  368.         $userRepository $this->em->getRepository(User::class);
  369.         $user $userRepository->findOneBy([
  370.             "id" => $userId,
  371.             "deleted" => UserEnum::ITEM_NO_DELETED 
  372.         ]);
  373.         if(!$user){
  374.             return $this->eadResponse([ 
  375.                 "message" => $this->configuration->getLanguage('user_not_found''login'
  376.             ], ErrorEnum::ACTION_INVALID);
  377.         }
  378.         
  379.         if($emailService->checkUserToSend($user)){
  380.             $generateCode $this->stringUtil->randomText(6);
  381.             $emailService->setToEmail($user->getEmail());
  382.             $emailService->setToName($user->getName());
  383.             $subText $this->configuration->getLanguage('login_authentication.subject''email');
  384.             $subject "{$subText} - {$this->client->getBrand()}";
  385.             $emailService->setSubject($subject);
  386.             
  387.             $domain $this->client->getDomainPrimary();
  388.             $emailService->setData([
  389.                 "userName" => $user->getName(),
  390.                 "code" => $generateCode
  391.             ]);
  392.             $emailService->setTemplateBody("login_authentication");
  393.             $send $emailService->send();
  394.             $dateExpireAuthentication date('Y-m-d H:i:s'strtotime(' + 5 minute '));
  395.             $user->setAuthenticationCodeEmail($generateCode);
  396.             $user->setAuthenticationEmailDateExpire($dateExpireAuthentication);
  397.             
  398.             $errors $this->validateEntity($user);
  399.             if($errors){
  400.                 return $this->eadResponse($errorsErrorEnum::FIELD_EMPTY);
  401.             }
  402.             $this->em->flush();
  403.             return $this->eadResponse([ "success" => ]);
  404.         }
  405.         return $this->eadResponse([ 
  406.             "message" => $this->configuration->getLanguage('authentication_email_not_found''login'
  407.         ], ErrorEnum::ACTION_INVALID);
  408.     }
  409.     /**
  410.      * @Route(
  411.      *      path          = "/confirm/{hash}",
  412.      *      name          = "loginConfirm",
  413.      *      methods       = {"GET"},
  414.      *      requirements  = { "hash" = "([a-zA-Z0-9_-]+)" }
  415.      * )
  416.      */
  417.     public function loginConfirm(Request $request) {
  418.         if($this->user){
  419.             
  420.             $sessionOn $this->user->getSession();
  421.             $sessionOn->delete();
  422.             $this->configuration->getSessionSym()->set('session'null);
  423.             $this->em->flush();
  424.             $this->generalService->deleteCookie('remember');
  425.             $this->generalService->deleteCookie('sessiontoken');
  426.             $this->generalService->logoffWS(
  427.                 $sessionOn,
  428.                 $this->clientConfig->getId()
  429.             );
  430.         }
  431.         $this->requestUtil->setRequest($request)->setData();
  432.         $hash $request->get('hash');
  433.         $user $this->em->getRepository(User::class)->findOneBy([ 
  434.             "hashIdentify" => $hash,
  435.             "deleted" => UserEnum::ITEM_NO_DELETED
  436.         ]);
  437.         $this->data["hash"] = base64_encode($this->domain);
  438.         $this->data['remember'] = $this->generalService->getCookie('remember');
  439.          
  440.         if(!$user){
  441.             $this->data["confirmMsg"] = $this->configuration->getLanguage(
  442.                 'user_not_found''login'
  443.             );
  444.             $this->data["confirmStatus"] = UserEnum::NO;
  445.         
  446.             return $this->renderEAD('login/login.html.twig');
  447.         }
  448.         if($user->getStatus() != UserEnum::WAITING){
  449.             $this->data["confirmMsg"] = $this->configuration->getLanguage(
  450.                 'invalid_action''login'
  451.             );
  452.             $this->data["confirmStatus"] = UserEnum::NO;
  453.             return $this->renderEAD('login/login.html.twig');
  454.         }
  455.         $user->setStatus(UserEnum::ACTIVE);
  456.         $user->setConfirmationDate(date('Y-m-d H:i:s'));
  457.         $user->setValidEmail(UserEnum::DELIVERABLE);
  458.         $pixelService $this->generalService->getService('Marketing\\PixelService');
  459.         $pixelService->sendConversion('CompleteRegistration');
  460.         $marketingService $this->generalService->getService('Marketing\\MarketingService');
  461.         $marketingService->setTag(TagsMarketingEnum::TAG_CONFIRM_REGISTER);
  462.         $marketingService->setUser($user);
  463.         $marketingService->send();
  464.         $this->em->flush();
  465.         $dataObj= (object)[
  466.             "user" => (object)[        
  467.                 "id" => (string)$user->getId(),
  468.                 "name" => $user->getName(),
  469.                 "email" => $user->getEmail(),
  470.                 "phone" => $user->getPhone(),
  471.                 "status" => $user->getStatus(),
  472.                 "dates" => (object)[
  473.                     "signup" => $user->getDateRegister(),
  474.                     "confirmation" => $user->getConfirmationDate(),
  475.                 ],
  476.             ], 
  477.         ];
  478.         $webhookService $this->generalService->getService('WebhookService');
  479.         $webhookService->addItemList(WebhookEnum::USER$dataObj);
  480.         $this->data["confirmMsg"] = $this->configuration->getLanguage(
  481.             'user_confirmed'
  482.             'login'
  483.         );
  484.         $this->data["confirmStatus"] = UserEnum::YES;
  485.         
  486.         return $this->renderEAD('login/login.html.twig');
  487.     }
  488.     /**
  489.      * @Route(
  490.      *      path          = "/stopEmail/{hash}",
  491.      *      name          = "stopEmail",
  492.      *      methods       = {"GET"},
  493.      *      requirements  = { "hash" = "([a-zA-Z0-9_-]+)" }
  494.      * )
  495.      */
  496.     public function stopEmail(Request $request) {
  497.         $this->requestUtil->setRequest($request)->setData();
  498.         $hash $request->get('hash');
  499.         
  500.         $user $this->em->getRepository(User::class)->findOneBy([ 
  501.             "hashIdentify" => $hash,
  502.             "deleted" => UserEnum::ITEM_NO_DELETED
  503.         ]);
  504.        
  505.         if(!$user){
  506.             $this->data["confirmMsg"] = $this->configuration->getLanguage('user_not_found''login');
  507.             $this->data["confirmStatus"] = UserEnum::NO;
  508.             $this->data["hash"] = null;
  509.         
  510.             return $this->renderEAD('login/login.html.twig');
  511.         }
  512.         $this->data["hash"] = $hash;
  513.         $sessionOn $user->getSession();
  514.         if($sessionOn){
  515.             $sessionOn->delete();
  516.             $this->configuration->getSessionSym()->set('session'null);
  517.             $this->em->flush();
  518.             $this->generalService->deleteCookie('remember');
  519.             $this->generalService->deleteCookie('sessiontoken');
  520.             $this->generalService->logoffWS(
  521.                 $sessionOn,
  522.                 $this->clientConfig->getId()
  523.             );
  524.         }
  525.         
  526.         $user->delete();
  527.         $this->em->flush();
  528.         
  529.         $this->data["confirmMsg"] = $this->configuration->getLanguage('user_deleted''login');
  530.         $this->data["confirmStatus"] = UserEnum::YES;
  531.         return $this->renderEAD('login/login.html.twig');
  532.     }
  533.     /**
  534.      * @Route(
  535.      *      path          = "/stopNotification/{hash}",
  536.      *      name          = "stopNotification",
  537.      *      methods       = {"GET"},
  538.      *      requirements  = { "hash" = "([a-zA-Z0-9_-]+)" }
  539.      * )
  540.      */
  541.     public function stopNotification(Request $request) {
  542.         $this->requestUtil->setRequest($request)->setData();
  543.         $hash $request->get('hash');
  544.         $user $this->em->getRepository(User::class)->findOneBy([ 
  545.             "hashIdentify" => $hash,
  546.             "deleted" => UserEnum::ITEM_NO_DELETED
  547.         ]);
  548.         
  549.         if(!$user){
  550.             $this->data["confirmMsg"] = $this->configuration->getLanguage(
  551.                 'user_not_found''login'
  552.             );
  553.             
  554.             $this->data["hash"] = null;
  555.         
  556.             return $this->renderEAD('login/login.html.twig');
  557.         }
  558.         
  559.         $this->data['user'] = $user;
  560.         return $this->renderEAD('login/stop-notification.html.twig');
  561.     }
  562.     /**
  563.      * @Route(
  564.      *      path          = "user/notification/{hash}",
  565.      *      name          = "updateUserNotification",
  566.      *      methods       = {"PUT"},
  567.      *      requirements  = { "hash" = "([a-zA-Z0-9_-]+)" }
  568.      * )
  569.      */
  570.     public function updateUserNotification(Request $request) {
  571.         $this->requestUtil->setRequest($request)->setData();
  572.         $hash $request->get('hash');
  573.         $user $this->em->getRepository(User::class)->findOneBy([ 
  574.             "hashIdentify" => $hash,
  575.             "deleted" => UserEnum::ITEM_NO_DELETED
  576.         ]);
  577.         $data = [];
  578.         
  579.         if(!$user){
  580.             $this->data["confirmMsg"] = $this->configuration->getLanguage(
  581.                 'user_not_found'
  582.                 'login'
  583.             );
  584.             $this->data["hash"] = null;
  585.         
  586.             return $this->renderEAD('login/login.html.twig');
  587.         }
  588.         if($this->requestUtil->issetField('allowNotifyNewLesson')){
  589.             $user->setAllowNotifyNewLesson(
  590.                 $this->requestUtil->getField('allowNotifyNewLesson')
  591.             );
  592.         }
  593.         if($this->requestUtil->issetField('allowNotifyNewExam')){
  594.             $user->setAllowNotifyNewExam(
  595.                 $this->requestUtil->getField('allowNotifyNewExam')
  596.             );
  597.         }
  598.         if($this->requestUtil->issetField('allowNotifyNewSupportMessage')){
  599.             $user->setAllowNotifyNewSupportMessage(
  600.                 $this->requestUtil->getField('allowNotifyNewSupportMessage')
  601.             );
  602.         }
  603.         if($this->requestUtil->issetField('allowNotifyNewSupportAnswer')){
  604.             $user->setAllowNotifyNewSupportAnswer(
  605.                 $this->requestUtil->getField('allowNotifyNewSupportAnswer')
  606.             );
  607.         }
  608.         if($this->requestUtil->issetField('allowNotifyNewMessage')){
  609.             $user->setAllowNotifyNewMessage(
  610.                 $this->requestUtil->getField('allowNotifyNewMessage')
  611.             );
  612.         }
  613.         if($this->requestUtil->issetField('allowNotifyNewGroupMessage')){
  614.             $user->setAllowNotifyNewGroupMessage(
  615.                 $this->requestUtil->getField('allowNotifyNewGroupMessage')
  616.             );
  617.         }
  618.         if($this->requestUtil->issetField('allowNotifyCart')){
  619.             $user->setAllowNotifyCart($this->requestUtil->getField('allowNotifyCart'));
  620.         }
  621.         $this->em->flush();
  622.         $data $user->toReturn();
  623.         $this->userLogService->logUpdate("user"$user->getId(), $data);
  624.         return $this->eadResponse([ "message" => "Success" ]);
  625.     }
  626.     /**
  627.      * @Route(
  628.      *      path          = "/login/fast/{pass}",
  629.      *      name          = "loginFast",
  630.      *      methods       = {"GET"}
  631.      * )
  632.     */
  633.     public function loginFast(Request $request) {
  634.        
  635.         $this->requestUtil->setRequest($request)->setData();
  636.         $session = new Session();
  637.         $pass $request->get('pass');
  638.         $pass $this->stringUtil->decryptArray($pass);
  639.         
  640.         if(empty($pass)){
  641.             return $this->redirectToRoute('notFound');
  642.         }
  643.         
  644.         $userId $pass['userId'];
  645.         $time $pass['time'];
  646.         $urlRedirect null;
  647.             
  648.         if(!empty($pass['urlRedirect'])){
  649.             $urlRedirect $pass['urlRedirect'];
  650.         }
  651.         $urlRedirectCart $this->requestUtil->getField('url');
  652.         if(!empty($urlRedirectCart)){
  653.             $urlRedirect $urlRedirectCart;
  654.         }
  655.         
  656.         $user $this->em->getRepository(User::class)->findOneBy([
  657.             "id" => $userId,
  658.             "deleted" => UserEnum::ITEM_NO_DELETED ,
  659.         ]);
  660.            
  661.         if(!$user){
  662.             return $this->redirectToRoute('notFound');
  663.         }
  664.         
  665.         if($user->getStatus() == UserEnum::BLOCK){
  666.             return $this->redirectToRoute('notFound');
  667.         }
  668.         $this->userPermissionUtil->setUser($user);
  669.         
  670.         $platformStatus $this->clientConfig->getPlatformStatus();
  671.         if($platformStatus == ClientEnum::PLATFORM_STATUS_FREEZED){
  672.             if(!$this->userPermissionUtil->canAccessAdm()){
  673.                 return $this->redirectToRoute('notFound');
  674.             }
  675.         }
  676.         $ipApi $this->generalService->getService('IpApiService');
  677.         
  678.         $ipApi->setRequest($request);
  679.         $session->setUser($user);
  680.         $session->setIp($ipApi->getIp());
  681.         $session->setIspName($ipApi->getIsp());
  682.         $session->setCoordinate($ipApi->getCoordinate());
  683.         $session->setTimeZone($ipApi->getTimeZone());
  684.         $session->setCity($ipApi->getCity());
  685.         $session->setState($ipApi->getState());
  686.         $session->setCountry($ipApi->getCountry());
  687.         $sessionOn $this->em->getRepository(Session::class)->findOneBy([ 
  688.             "user" => $user->getId(),
  689.             "deleted" => UserEnum::ITEM_NO_DELETED
  690.         ]);
  691.         if($user->getType() != UserEnum::STUDENT){
  692.             $session->setIsAdmin(SessionEnum::YES);
  693.         }else{
  694.             if($sessionOn){
  695.                 $sessionOn->delete();
  696.                 $this->generalService->logoffWS(
  697.                     $sessionOn,
  698.                     $this->clientConfig->getId()
  699.                 );
  700.             }
  701.         }
  702.         $errors $this->validateEntity($session);
  703.         if($errors){
  704.             return $this->eadResponse($errorsErrorEnum::FIELD_EMPTY);
  705.         }
  706.         $this->em->persist($session);
  707.         $this->em->flush();
  708.         $crmService $this->generalService->getService('CRM\\CrmService');
  709.         $crmService->savePerson($user);
  710.         $emptyCart $this->em->getRepository(Cart::class)->updateCartHashToUser($user);
  711.         $time 'Session';
  712.         $this->generalService->setCookie('sessiontoken'$session->getToken(), $time);
  713.         if(is_null($urlRedirect)){
  714.             return $this->redirectToRoute('resume');
  715.         }
  716.         if(
  717.             $emptyCart &&
  718.             (empty($urlRedirect) || stristr($urlRedirect,'/cart'))
  719.             && !stristr($urlRedirect,'/indvidual')
  720.         ){
  721.             return $this->redirectToRoute('resume');
  722.         }
  723.         
  724.         return $this->redirect($urlRedirect301);
  725.     }
  726.     /**
  727.      * @Route(
  728.      *      path          = "/login/api/{pass}",
  729.      *      name          = "loginApi",
  730.      *      methods       = {"GET"}
  731.      * )
  732.     */
  733.     public function loginApi(Request $request) {
  734.        
  735.         $this->requestUtil->setRequest($request)->setData();
  736.         $ipApi $this->generalService->getService('IpApiService');
  737.         $session = new Session();
  738.         $pass $request->get('pass');
  739.         $pass $this->stringUtil->decryptArray($pass);
  740.         
  741.         if(empty($pass)){
  742.             return $this->eadResponse([ 
  743.                 "message" => $this->configuration->getLanguage('login_invalid''login')
  744.             ], ErrorEnum::ACTION_INVALID);
  745.         }
  746.         
  747.         $userId $pass['userId'];
  748.         $time $pass['time'];
  749.         $urlRedirect null;
  750.         
  751.         if(!empty($pass['urlRedirect'])){
  752.             $urlRedirect $pass['urlRedirect'];
  753.         }
  754.         
  755.         $user $this->em->getRepository(User::class)->findOneBy([
  756.             "id" => $userId,
  757.             "status" => UserEnum::ACTIVE,
  758.             "deleted" => UserEnum::ITEM_NO_DELETED 
  759.         ]);
  760.            
  761.         if(!$user){
  762.             return $this->eadResponse([ 
  763.                 "message" => $this->configuration->getLanguage('login_invalid''login'
  764.             ], ErrorEnum::ACTION_INVALID);
  765.         }
  766.         if($user->getStatus() == UserEnum::BLOCK){
  767.             return $this->eadResponse([ 
  768.                 "message" => $this->configuration->getLanguage('login_invalid''login'
  769.             ], ErrorEnum::ACTION_INVALID);
  770.         }
  771.         $platformStatus $this->clientConfig->getPlatformStatus();
  772.         if($platformStatus == ClientEnum::PLATFORM_STATUS_FREEZED){
  773.             if(!$this->userPermissionUtil->canAccessAdm()){
  774.                 return $this->eadResponse([ 
  775.                     "message" => $this->configuration->getLanguage('login_invalid''login'
  776.                 ], ErrorEnum::ACTION_INVALID);
  777.             }
  778.         }
  779.         $ipApi->setRequest($request);
  780.         $session->setUser($user);
  781.         $session->setIp($ipApi->getIp());
  782.         $session->setIspName($ipApi->getIsp());
  783.         $session->setCoordinate($ipApi->getCoordinate());
  784.         $session->setTimeZone($ipApi->getTimeZone());
  785.         $session->setCity($ipApi->getCity());
  786.         $session->setState($ipApi->getState());
  787.         $session->setCountry($ipApi->getCountry());
  788.         $sessionOn $this->em->getRepository(Session::class)->findOneBy([ 
  789.             "user" => $user->getId(),
  790.             "deleted" => UserEnum::ITEM_NO_DELETED
  791.         ]);
  792.         if($user->getType() != UserEnum::STUDENT){
  793.             $session->setIsAdmin(SessionEnum::YES);
  794.         }else{
  795.             if($sessionOn){
  796.                 $sessionOn->delete();
  797.                 $this->generalService->logoffWS(
  798.                     $sessionOn,
  799.                     $this->clientConfig->getId()
  800.                 );
  801.             }
  802.         }
  803.         $errors $this->validateEntity($session);
  804.         if($errors){
  805.             return $this->eadResponse($errorsErrorEnum::FIELD_EMPTY);
  806.         }
  807.         $this->em->persist($session);
  808.         $this->em->flush();
  809.         $this->generalService->setCookie('sessiontoken'$session->getToken());
  810.         if(is_null($urlRedirect)){
  811.             return $this->redirectToRoute('resume');
  812.         }
  813.     
  814.         return $this->redirect($urlRedirect301);
  815.     }
  816.     /**
  817.      * @Route(
  818.      *      path          = "/recover/password",
  819.      *      name          = "recoverPassword",
  820.      *      methods       = {"POST"},
  821.      * )
  822.      */
  823.     public function recoverPassword(Request $request) {
  824.         $this->requestUtil->setRequest($request)->setData();
  825.         
  826.         $email $this->requestUtil->getField('email');
  827.        
  828.         $user $this->em->getRepository(User::class)->findOneBy([ 
  829.             "email" => $email,
  830.             "deleted" => UserEnum::ITEM_NO_DELETED
  831.         ]);
  832.         
  833.         if(!$user){
  834.             return $this->eadResponse([
  835.                 "message" => "Success"
  836.             ]);
  837.         }
  838.         $emailService $this->generalService->getService('EmailService');
  839.         $hashData = [
  840.             "today" => date('Ymd'),
  841.             "userId" => $user->getId()
  842.         ];
  843.         $hash $this->stringUtil->encodeHex(base64_encode(json_encode($hashData)));
  844.         $domain $this->client->getDomainPrimary();
  845.         if(
  846.             $emailService->checkUserToSend($user) ||
  847.             $this->configuration->get("allow_send_email_user") == UserEnum::YES
  848.         ){
  849.             $emailService->setToEmail($user->getEmail());
  850.             $emailService->setToName($user->getName());
  851.             $subText $this->configuration->getLanguage('recover_password.subject''email');
  852.             $subject "{$subText} - {$this->client->getBrand()}";
  853.             $emailService->setSubject($subject);
  854.             
  855.             $emailService->setData([
  856.                 "userName" => $user->getName(),
  857.                 "btnLink" => "https://{$domain}/change/password/{$hash}"
  858.             ]);
  859.             $emailService->setTemplateBody("recover_password");
  860.             $emailService->send();
  861.         }
  862.         return $this->eadResponse([ "message" => "Success" ]);
  863.     }
  864.     /**
  865.      * @Route(
  866.      *      path          = "/change/password/{hash}",
  867.      *      name          = "changePasswordPost",
  868.      *      methods       = {"GET"},
  869.      *      requirements  = { "hash" = "([a-zA-Z0-9_-]+)" }
  870.      * )
  871.     */
  872.     public function changePassword(Request $request) {
  873.         $hash $request->get("hash");
  874.         $hashData json_decode(base64_decode($this->stringUtil->decodeHex($hash)));
  875.         $this->data['hash'] = $hash;
  876.         $this->data['remember'] = $this->generalService->getCookie('remember');
  877.         if($hashData->today != date("Ymd")){
  878.             $this->data["confirmMsg"] = $this->configuration->getLanguage('invalid_hash''login');
  879.             $this->data["confirmStatus"] = UserEnum::NO;
  880.         
  881.             return $this->renderEAD('login/login.html.twig');
  882.         }
  883.         
  884.         return $this->renderEAD('login/recover-password.html.twig');
  885.     }
  886.     /**
  887.      * @Route(
  888.      *      path          = "/reset/password",
  889.      *      name          = "resetPasswordPost",
  890.      *      methods       = {"POST"}
  891.      * )
  892.      */
  893.     public function resetPasswordPost(Request $request) {
  894.         $this->requestUtil->setRequest($request)->setData();
  895.         $hash $this->requestUtil->getField("hash");
  896.         $hashData json_decode(base64_decode($this->stringUtil->decodeHex($hash)));
  897.         
  898.         if($hashData->today != date("Ymd")){
  899.             return $this->eadResponse([ 
  900.                 "message" => $this->configuration->getLanguage('invalid_hash''login'
  901.             ], ErrorEnum::ACTION_INVALID);
  902.         }
  903.         $user $this->em->getRepository(User::class)->findOneBy([
  904.             "id" => $hashData->userId,
  905.             "deleted" => UserEnum::ITEM_NO_DELETED
  906.         ]);
  907.         if(!$user){
  908.             return $this->eadResponse([ 
  909.                 "message" => $this->configuration->getLanguage('user_not_found''login'
  910.             ], ErrorEnum::ACTION_INVALID);
  911.         }
  912.         $password $this->requestUtil->getField('password');
  913.         $confirmPassword $this->requestUtil->getField('confirmPassword');
  914.         
  915.         if($password != $confirmPassword){
  916.             return $this->eadResponse([ 
  917.                 "message" => $this->configuration->getLanguage('passwords_must_be_the_same''login')
  918.             ], ErrorEnum::ACTION_INVALID);
  919.         }
  920.         
  921.         $user->setPassword($password);
  922.         $this->em->flush();
  923.         $this->data["confirmMsg"] = $this->configuration->getLanguage('updated_password''login');
  924.         $this->data["confirmStatus"] = UserEnum::YES;
  925.         
  926.         return $this->eadResponse([ 
  927.             "message" => $this->configuration->getLanguage('updated_password''login'),
  928.             "url" => 'https://'.$this->client->getDomainPrimary()."/login"
  929.         ]);
  930.     }
  931.     /**
  932.      * @Route(
  933.      *      path          = "/logoff/user",
  934.      *      name          = "userLogoff",
  935.      *      methods       = {"GET"}
  936.      * )
  937.      */
  938.     public function logoffUser(Request $request) {
  939.         $sessionOn $this->user->getSession();
  940.         $userOrigin $sessionOn->getUserOrigin();
  941.         $sessionOn->delete();
  942.         
  943.         $this->generalService->logoffWS(
  944.             $sessionOn,
  945.             $this->clientConfig->getId()
  946.         );
  947.         if($userOrigin){
  948.             $sessionOld $this->em->getRepository(Session::class)->findOneBy([ 
  949.                 "user" => $userOrigin->getId(),
  950.                 "deleted" => UserEnum::ITEM_NO_DELETED
  951.             ]);
  952.             $ipApi $this->generalService->getService('IpApiService');
  953.             $ipApi->setRequest($request);
  954.             
  955.             $session = new Session();
  956.             $session->setUser($userOrigin);
  957.             $session->setIp($ipApi->getIp());
  958.             $session->setIspName($ipApi->getIsp());
  959.             $session->setCoordinate($ipApi->getCoordinate());
  960.             $session->setTimeZone($ipApi->getTimeZone());
  961.             $session->setCity($ipApi->getCity());
  962.             $session->setState($ipApi->getState());
  963.             $session->setCountry($ipApi->getCountry());
  964.             $session->setIsAdmin(UserEnum::YES);
  965.             $errors $this->validateEntity($session);
  966.             if($errors){
  967.                 return $this->eadResponse($errorsErrorEnum::FIELD_EMPTY);
  968.             }
  969.             
  970.             if($sessionOld){
  971.                 $session->setToken($sessionOld->getToken());
  972.                 $sessionOld->delete();
  973.                 $time time() + (24 60 60);
  974.                 if($this->generalService->getCookie('remember') == SessionEnum::YES){
  975.                     $time null;
  976.                 }
  977.                 
  978.                 $this->generalService->setCookie(
  979.                     'sessiontoken'
  980.                     $session->getToken(), 
  981.                     $time
  982.                 );
  983.             }
  984.             $this->em->persist($session);
  985.             $this->em->flush();
  986.             /*return $this->eadResponse([ 
  987.                 "token" => $session->getToken(), 
  988.                 "userId" => $userOrigin->getId() 
  989.             ]);*/
  990.         }
  991.         $this->em->flush();
  992.         return $this->redirectToRoute('home');
  993.     }
  994.     /**
  995.      * @Route(
  996.      *      path          = "/logoff",
  997.      *      name          = "logoff",
  998.      *      methods       = {"GET"},
  999.      * )
  1000.      */
  1001.     public function logoff(Request $request) {
  1002.         if($this->user){
  1003.             $sessionOn $this->user->getSession();
  1004.             if($sessionOn){
  1005.                 $sessionOn->delete();
  1006.                 $this->configuration->getSessionSym()->set('session'null);
  1007.                 $this->em->flush();
  1008.                 $this->generalService->deleteCookie('remember');
  1009.                 $this->generalService->deleteCookie('sessiontoken');
  1010.                 $this->generalService->logoffWS(
  1011.                     $sessionOn,
  1012.                     $this->clientConfig->getId()
  1013.                 );
  1014.             }
  1015.         }
  1016.         return $this->redirectToRoute('home');
  1017.     }
  1018. }