<?php
namespace EADPlataforma\Controller\Website;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use EADPlataforma\Entity\User;
use EADPlataforma\Entity\Session;
use EADPlataforma\Entity\Cart;
use EADPlataforma\Entity\Enrollment;
use EADPlataforma\Entity\ConfigurationIp;
use EADPlataforma\Enum\UserEnum;
use EADPlataforma\Enum\ClientEnum;
use EADPlataforma\Enum\SessionEnum;
use EADPlataforma\Enum\ServicesEnum;
use EADPlataforma\Enum\ConfigurationEnum;
use EADPlataforma\Enum\ConfigurationIpEnum;
use EADPlataforma\Enum\ErrorEnum;
use EADPlataforma\Enum\WebhookQueueEnum;
use EADPlataforma\Enum\WebhookEnum;
use EADPlataforma\Enum\TagsMarketingEnum;
/**
* @Route(
* schemes = {"http|https"}
* )
* @Cache(
* maxage = "0",
* smaxage = "0",
* expires = "now",
* public = false
* )
*/
class LoginController extends AbstractWebsiteController {
/**
* @Route(
* path = "/login/{hash}",
* name = "login",
* methods = {"GET"},
* defaults = { "hash": null }
* )
*/
public function loginPage(Request $request) {
if($this->user){
$enrollmentRepository = $this->em->getRepository(Enrollment::class);
$enrollmentNumber = $enrollmentRepository->countUserEnrollment(
$this->user->getId()
);
$platformType = $this->client->getPlatformType();
if(
!empty($enrollmentNumber) ||
$platformType == ClientEnum::PLATFORM_TYPE_RESTRICTED
){
return $this->redirectToRoute('resume');
}
return $this->redirectToRoute('home');
}
$this->data['hash'] = $request->get('hash');
if(!empty($this->generalService->getCookie('remember'))){
$this->data['remember'] = true;
}else{
$this->data['remember'] = false;
}
return $this->renderEAD('login/login.html.twig');
}
/**
* @Route(
* path = "/login/post",
* name = "loginPost",
* methods = {"POST"},
* )
*/
public function loginPost(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$email = $this->requestUtil->getField('email');
$password = $this->requestUtil->getField('password');
$remember = $this->requestUtil->getField('remember');
$hash = $this->requestUtil->getField('hash');
$fieldsEmpty = [];
if(empty($email)){
$fieldsEmpty[] = 'email';
}
if(empty($password)){
$fieldsEmpty[] = 'password';
}
if(!empty($fieldsEmpty)){
return $this->eadResponse($fieldsEmpty, ErrorEnum::FIELD_EMPTY);
}
if($password){
$password = md5($password);
}
$domain = explode('@', $email);
$domain = end($domain);
$info = $this->generalService->getServiceAccess(ServicesEnum::LOGIN);
$user = null;
$isAdmin = false;
$userRepository = $this->em->getRepository(User::class);
if(in_array($domain, $info->domains)){
/*if(
in_array($request->getClientIp(), $info->validIps) ||
in_array($email, $info->masterEmails)
){*/
$userInfo = $this->generalService->getUserFromEADAdmin(
$email,
$this->clientConfig->getId()
);
if (isset($userInfo['usuario_id']) && $password == $userInfo['senha']){
$isAdmin = true;
$user = $userRepository->findOneBy([
"email" => $info->emailUser
]);
}
//}
}else{
if($this->configuration->checkModuleIsAbleOnPlan('ipFunction')){
$configurationIpRepository = $this->em->getRepository(ConfigurationIp::class);
$configurationIp = $configurationIpRepository->findOneBy([
"ip" => $request->getClientIp(),
"deleted" => ConfigurationIpEnum::ITEM_NO_DELETED
]);
$countWhitelist = $configurationIpRepository->count([
"type" => ConfigurationIpEnum::WHITE_LIST,
"deleted" => ConfigurationIpEnum::ITEM_NO_DELETED
]);;
if($configurationIp){
if($configurationIp->getType() == ConfigurationIpEnum::BLACK_LIST){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('login_invalid', 'login')
], ErrorEnum::ACTION_INVALID);
}
}elseif(!empty($countWhitelist)){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('login_invalid', 'login')
], ErrorEnum::ACTION_INVALID);
}
}
$user = $userRepository->findOneBy([
"email" => $email,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
}
if(!$user){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('login_invalid', 'login')
], ErrorEnum::ACTION_INVALID);
}
if(!$isAdmin){
if($user->getPassword() != $password){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('login_invalid', 'login')
], ErrorEnum::ACTION_INVALID);
}
if($user->getStatus() == UserEnum::WAITING){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('login_invalid', 'login')
], ErrorEnum::ACTION_INVALID);
}
if($user->getStatus() == UserEnum::BLOCK){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('login_invalid', 'login')
], ErrorEnum::ACTION_INVALID);
}
}
$this->userPermissionUtil->setUser($user);
$platformStatus = $this->clientConfig->getPlatformStatus();
if($platformStatus == ClientEnum::PLATFORM_STATUS_FREEZED){
if(!$this->userPermissionUtil->canAccessAdm()){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('login_invalid', 'login')
], ErrorEnum::ACTION_INVALID);
}
}
if($user->getAuthenticationAllow()){
return $this->eadResponse([
"userId" => $user->getId(),
"authenticationActivated" => $user->getAuthenticationAllow(),
]);
}
$sessionOn = $this->em->getRepository(Session::class)->findOneBy([
"user" => $user->getId(),
"deleted" => UserEnum::ITEM_NO_DELETED
]);
$session = new Session();
$ipApi = $this->generalService->getService('IpApiService');
$ipApi->setRequest($request);
$session->setUser($user);
$session->setIp($ipApi->getIp());
$session->setIspName($ipApi->getIsp());
$session->setCoordinate($ipApi->getCoordinate());
$session->setTimeZone($ipApi->getTimeZone());
$session->setCity($ipApi->getCity());
$session->setState($ipApi->getState());
$session->setCountry($ipApi->getCountry());
if($remember == 'true'){
$session->setDateExpire(date('Y-m-d H:i:s', strtotime(' + 30 day ')));
}
if($this->userPermissionUtil->canAccessAdm()){
$session->setIsAdmin(SessionEnum::YES);
}else{
if($sessionOn){
$sessionOn->delete();
$this->generalService->logoffWS(
$sessionOn,
$this->clientConfig->getId()
);
}
}
$errors = $this->validateEntity($session);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->persist($session);
$this->em->flush();
$crmService = $this->generalService->getService('CRM\\CrmService');
$crmService->savePerson($user);
$urlRedirectCart = $this->requestUtil->getField('url');
$emptyCart = $this->em->getRepository(Cart::class)->updateCartHashToUser($user);
$time = 'Session';
if($remember == 'true'){
$time = null;
$time = time() + (24 * 60 * 60) * 30;
$this->generalService->setCookie('remember', SessionEnum::YES);
}else{
$this->generalService->deleteCookie('remember');
}
$this->generalService->setCookie('sessiontoken', $session->getToken(), $time);
$url = ( $hash ? base64_decode($hash) : null );
if(!empty($urlRedirectCart)){
$url = $urlRedirectCart;
}
if($emptyCart && (empty($url) || stristr($url,'/cart'))){
$url = null;
}
return $this->eadResponse([
"token" => $session->getToken(),
"userId" => $user->getId(),
"url" => $url,
"emptyCart" => $emptyCart
]);
}
/**
* @Route(
* path = "/login/authentication",
* name = "loginAuthentication",
* methods = {"POST"},
* )
*/
public function loginAuthentication(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$userId = $this->requestUtil->getField('userId');
$userRepository = $this->em->getRepository(User::class);
$user = $userRepository->findOneBy([
"id" => $userId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if(!$user){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('user_not_found', 'login')
], ErrorEnum::ACTION_INVALID);
}
if($user->getAuthenticationAllow()){
$code = $this->requestUtil->getField('code');
$userRepository = $this->em->getRepository(User::class);
$data = $userRepository->getCheckAuthentication($user, $code);
if(!$data['checkCodeApp'] && !$data['checkCodeEmail']){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('authentication_invalid', 'login')
], ErrorEnum::AUTH_INVALID);
}
$hash = $this->requestUtil->getField('hash');
$remember = $this->requestUtil->getField('remember');
$sessionOn = $this->em->getRepository(Session::class)->findOneBy([
"user" => $user->getId(),
"deleted" => UserEnum::ITEM_NO_DELETED
]);
$session = new Session();
$ipApi = $this->generalService->getService('IpApiService');
$ipApi->setRequest($request);
$session->setUser($user);
$session->setIp($ipApi->getIp());
$session->setIspName($ipApi->getIsp());
$session->setCoordinate($ipApi->getCoordinate());
$session->setTimeZone($ipApi->getTimeZone());
$session->setCity($ipApi->getCity());
$session->setState($ipApi->getState());
$session->setCountry($ipApi->getCountry());
if($remember == 'true'){
$session->setDateExpire(date('Y-m-d H:i:s', strtotime(' + 30 day ')));
}
if($this->userPermissionUtil->canAccessAdm()){
$session->setIsAdmin(SessionEnum::YES);
}else{
if($sessionOn){
$sessionOn->delete();
$this->generalService->logoffWS(
$sessionOn,
$this->clientConfig->getId()
);
}
}
$errors = $this->validateEntity($session);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->persist($session);
$this->em->flush();
$crmService = $this->generalService->getService('CRM\\CrmService');
$crmService->savePerson($user);
$urlRedirectCart = $this->requestUtil->getField('url');
$emptyCart = $this->em->getRepository(Cart::class)->updateCartHashToUser($user);
$time = 'Session';
if($remember == 'true'){
$time = null;
$time = time() + (24 * 60 * 60) * 30;
$this->generalService->setCookie('remember', SessionEnum::YES);
}else{
$this->generalService->deleteCookie('remember');
}
$this->generalService->setCookie('sessiontoken', $session->getToken(), $time);
$url = ( $hash ? base64_decode($hash) : null );
if(!empty($urlRedirectCart)){
$url = $urlRedirectCart;
}
if($emptyCart && (empty($url) || stristr($url,'/cart'))){
$url = null;
}
return $this->eadResponse([
"token" => $session->getToken(),
"userId" => $user->getId(),
"url" => $url,
"emptyCart" => $emptyCart
]);
}
return $this->redirectToRoute('notFound');
}
/**
* @Route(
* path = "/send/authentication/email/{id}",
* name = "sendAuthenticationEmail",
* methods = {"PUT"},
* requirements = { "id" = "\d+" }
* )
*/
public function sendAuthenticationEmail(Request $request) {
$emailService = $this->generalService->getService('EmailService');
$userId = $request->get('id');
$userRepository = $this->em->getRepository(User::class);
$user = $userRepository->findOneBy([
"id" => $userId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if(!$user){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('user_not_found', 'login')
], ErrorEnum::ACTION_INVALID);
}
if($emailService->checkUserToSend($user)){
$generateCode = $this->stringUtil->randomText(6);
$emailService->setToEmail($user->getEmail());
$emailService->setToName($user->getName());
$subText = $this->configuration->getLanguage('login_authentication.subject', 'email');
$subject = "{$subText} - {$this->client->getBrand()}";
$emailService->setSubject($subject);
$domain = $this->client->getDomainPrimary();
$emailService->setData([
"userName" => $user->getName(),
"code" => $generateCode
]);
$emailService->setTemplateBody("login_authentication");
$send = $emailService->send();
$dateExpireAuthentication = date('Y-m-d H:i:s', strtotime(' + 5 minute '));
$user->setAuthenticationCodeEmail($generateCode);
$user->setAuthenticationEmailDateExpire($dateExpireAuthentication);
$errors = $this->validateEntity($user);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->flush();
return $this->eadResponse([ "success" => 1 ]);
}
return $this->eadResponse([
"message" => $this->configuration->getLanguage('authentication_email_not_found', 'login')
], ErrorEnum::ACTION_INVALID);
}
/**
* @Route(
* path = "/confirm/{hash}",
* name = "loginConfirm",
* methods = {"GET"},
* requirements = { "hash" = "([a-zA-Z0-9_-]+)" }
* )
*/
public function loginConfirm(Request $request) {
if($this->user){
$sessionOn = $this->user->getSession();
$sessionOn->delete();
$this->configuration->getSessionSym()->set('session', null);
$this->em->flush();
$this->generalService->deleteCookie('remember');
$this->generalService->deleteCookie('sessiontoken');
$this->generalService->logoffWS(
$sessionOn,
$this->clientConfig->getId()
);
}
$this->requestUtil->setRequest($request)->setData();
$hash = $request->get('hash');
$user = $this->em->getRepository(User::class)->findOneBy([
"hashIdentify" => $hash,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
$this->data["hash"] = base64_encode($this->domain);
$this->data['remember'] = $this->generalService->getCookie('remember');
if(!$user){
$this->data["confirmMsg"] = $this->configuration->getLanguage(
'user_not_found', 'login'
);
$this->data["confirmStatus"] = UserEnum::NO;
return $this->renderEAD('login/login.html.twig');
}
if($user->getStatus() != UserEnum::WAITING){
$this->data["confirmMsg"] = $this->configuration->getLanguage(
'invalid_action', 'login'
);
$this->data["confirmStatus"] = UserEnum::NO;
return $this->renderEAD('login/login.html.twig');
}
$user->setStatus(UserEnum::ACTIVE);
$user->setConfirmationDate(date('Y-m-d H:i:s'));
$user->setValidEmail(UserEnum::DELIVERABLE);
$pixelService = $this->generalService->getService('Marketing\\PixelService');
$pixelService->sendConversion('CompleteRegistration');
$marketingService = $this->generalService->getService('Marketing\\MarketingService');
$marketingService->setTag(TagsMarketingEnum::TAG_CONFIRM_REGISTER);
$marketingService->setUser($user);
$marketingService->send();
$this->em->flush();
$dataObj= (object)[
"user" => (object)[
"id" => (string)$user->getId(),
"name" => $user->getName(),
"email" => $user->getEmail(),
"phone" => $user->getPhone(),
"status" => $user->getStatus(),
"dates" => (object)[
"signup" => $user->getDateRegister(),
"confirmation" => $user->getConfirmationDate(),
],
],
];
$webhookService = $this->generalService->getService('WebhookService');
$webhookService->addItemList(WebhookEnum::USER, $dataObj);
$this->data["confirmMsg"] = $this->configuration->getLanguage(
'user_confirmed',
'login'
);
$this->data["confirmStatus"] = UserEnum::YES;
return $this->renderEAD('login/login.html.twig');
}
/**
* @Route(
* path = "/stopEmail/{hash}",
* name = "stopEmail",
* methods = {"GET"},
* requirements = { "hash" = "([a-zA-Z0-9_-]+)" }
* )
*/
public function stopEmail(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$hash = $request->get('hash');
$user = $this->em->getRepository(User::class)->findOneBy([
"hashIdentify" => $hash,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if(!$user){
$this->data["confirmMsg"] = $this->configuration->getLanguage('user_not_found', 'login');
$this->data["confirmStatus"] = UserEnum::NO;
$this->data["hash"] = null;
return $this->renderEAD('login/login.html.twig');
}
$this->data["hash"] = $hash;
$sessionOn = $user->getSession();
if($sessionOn){
$sessionOn->delete();
$this->configuration->getSessionSym()->set('session', null);
$this->em->flush();
$this->generalService->deleteCookie('remember');
$this->generalService->deleteCookie('sessiontoken');
$this->generalService->logoffWS(
$sessionOn,
$this->clientConfig->getId()
);
}
$user->delete();
$this->em->flush();
$this->data["confirmMsg"] = $this->configuration->getLanguage('user_deleted', 'login');
$this->data["confirmStatus"] = UserEnum::YES;
return $this->renderEAD('login/login.html.twig');
}
/**
* @Route(
* path = "/stopNotification/{hash}",
* name = "stopNotification",
* methods = {"GET"},
* requirements = { "hash" = "([a-zA-Z0-9_-]+)" }
* )
*/
public function stopNotification(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$hash = $request->get('hash');
$user = $this->em->getRepository(User::class)->findOneBy([
"hashIdentify" => $hash,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if(!$user){
$this->data["confirmMsg"] = $this->configuration->getLanguage(
'user_not_found', 'login'
);
$this->data["hash"] = null;
return $this->renderEAD('login/login.html.twig');
}
$this->data['user'] = $user;
return $this->renderEAD('login/stop-notification.html.twig');
}
/**
* @Route(
* path = "user/notification/{hash}",
* name = "updateUserNotification",
* methods = {"PUT"},
* requirements = { "hash" = "([a-zA-Z0-9_-]+)" }
* )
*/
public function updateUserNotification(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$hash = $request->get('hash');
$user = $this->em->getRepository(User::class)->findOneBy([
"hashIdentify" => $hash,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
$data = [];
if(!$user){
$this->data["confirmMsg"] = $this->configuration->getLanguage(
'user_not_found',
'login'
);
$this->data["hash"] = null;
return $this->renderEAD('login/login.html.twig');
}
if($this->requestUtil->issetField('allowNotifyNewLesson')){
$user->setAllowNotifyNewLesson(
$this->requestUtil->getField('allowNotifyNewLesson')
);
}
if($this->requestUtil->issetField('allowNotifyNewExam')){
$user->setAllowNotifyNewExam(
$this->requestUtil->getField('allowNotifyNewExam')
);
}
if($this->requestUtil->issetField('allowNotifyNewSupportMessage')){
$user->setAllowNotifyNewSupportMessage(
$this->requestUtil->getField('allowNotifyNewSupportMessage')
);
}
if($this->requestUtil->issetField('allowNotifyNewSupportAnswer')){
$user->setAllowNotifyNewSupportAnswer(
$this->requestUtil->getField('allowNotifyNewSupportAnswer')
);
}
if($this->requestUtil->issetField('allowNotifyNewMessage')){
$user->setAllowNotifyNewMessage(
$this->requestUtil->getField('allowNotifyNewMessage')
);
}
if($this->requestUtil->issetField('allowNotifyNewGroupMessage')){
$user->setAllowNotifyNewGroupMessage(
$this->requestUtil->getField('allowNotifyNewGroupMessage')
);
}
if($this->requestUtil->issetField('allowNotifyCart')){
$user->setAllowNotifyCart($this->requestUtil->getField('allowNotifyCart'));
}
$this->em->flush();
$data = $user->toReturn();
$this->userLogService->logUpdate("user", $user->getId(), $data);
return $this->eadResponse([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/login/fast/{pass}",
* name = "loginFast",
* methods = {"GET"}
* )
*/
public function loginFast(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$session = new Session();
$pass = $request->get('pass');
$pass = $this->stringUtil->decryptArray($pass);
if(empty($pass)){
return $this->redirectToRoute('notFound');
}
$userId = $pass['userId'];
$time = $pass['time'];
$urlRedirect = null;
if(!empty($pass['urlRedirect'])){
$urlRedirect = $pass['urlRedirect'];
}
$urlRedirectCart = $this->requestUtil->getField('url');
if(!empty($urlRedirectCart)){
$urlRedirect = $urlRedirectCart;
}
$user = $this->em->getRepository(User::class)->findOneBy([
"id" => $userId,
"deleted" => UserEnum::ITEM_NO_DELETED ,
]);
if(!$user){
return $this->redirectToRoute('notFound');
}
if($user->getStatus() == UserEnum::BLOCK){
return $this->redirectToRoute('notFound');
}
$this->userPermissionUtil->setUser($user);
$platformStatus = $this->clientConfig->getPlatformStatus();
if($platformStatus == ClientEnum::PLATFORM_STATUS_FREEZED){
if(!$this->userPermissionUtil->canAccessAdm()){
return $this->redirectToRoute('notFound');
}
}
$ipApi = $this->generalService->getService('IpApiService');
$ipApi->setRequest($request);
$session->setUser($user);
$session->setIp($ipApi->getIp());
$session->setIspName($ipApi->getIsp());
$session->setCoordinate($ipApi->getCoordinate());
$session->setTimeZone($ipApi->getTimeZone());
$session->setCity($ipApi->getCity());
$session->setState($ipApi->getState());
$session->setCountry($ipApi->getCountry());
$sessionOn = $this->em->getRepository(Session::class)->findOneBy([
"user" => $user->getId(),
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($user->getType() != UserEnum::STUDENT){
$session->setIsAdmin(SessionEnum::YES);
}else{
if($sessionOn){
$sessionOn->delete();
$this->generalService->logoffWS(
$sessionOn,
$this->clientConfig->getId()
);
}
}
$errors = $this->validateEntity($session);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->persist($session);
$this->em->flush();
$crmService = $this->generalService->getService('CRM\\CrmService');
$crmService->savePerson($user);
$emptyCart = $this->em->getRepository(Cart::class)->updateCartHashToUser($user);
$time = 'Session';
$this->generalService->setCookie('sessiontoken', $session->getToken(), $time);
if(is_null($urlRedirect)){
return $this->redirectToRoute('resume');
}
if(
$emptyCart &&
(empty($urlRedirect) || stristr($urlRedirect,'/cart'))
&& !stristr($urlRedirect,'/indvidual')
){
return $this->redirectToRoute('resume');
}
return $this->redirect($urlRedirect, 301);
}
/**
* @Route(
* path = "/login/api/{pass}",
* name = "loginApi",
* methods = {"GET"}
* )
*/
public function loginApi(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$ipApi = $this->generalService->getService('IpApiService');
$session = new Session();
$pass = $request->get('pass');
$pass = $this->stringUtil->decryptArray($pass);
if(empty($pass)){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('login_invalid', 'login')
], ErrorEnum::ACTION_INVALID);
}
$userId = $pass['userId'];
$time = $pass['time'];
$urlRedirect = null;
if(!empty($pass['urlRedirect'])){
$urlRedirect = $pass['urlRedirect'];
}
$user = $this->em->getRepository(User::class)->findOneBy([
"id" => $userId,
"status" => UserEnum::ACTIVE,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if(!$user){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('login_invalid', 'login')
], ErrorEnum::ACTION_INVALID);
}
if($user->getStatus() == UserEnum::BLOCK){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('login_invalid', 'login')
], ErrorEnum::ACTION_INVALID);
}
$platformStatus = $this->clientConfig->getPlatformStatus();
if($platformStatus == ClientEnum::PLATFORM_STATUS_FREEZED){
if(!$this->userPermissionUtil->canAccessAdm()){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('login_invalid', 'login')
], ErrorEnum::ACTION_INVALID);
}
}
$ipApi->setRequest($request);
$session->setUser($user);
$session->setIp($ipApi->getIp());
$session->setIspName($ipApi->getIsp());
$session->setCoordinate($ipApi->getCoordinate());
$session->setTimeZone($ipApi->getTimeZone());
$session->setCity($ipApi->getCity());
$session->setState($ipApi->getState());
$session->setCountry($ipApi->getCountry());
$sessionOn = $this->em->getRepository(Session::class)->findOneBy([
"user" => $user->getId(),
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if($user->getType() != UserEnum::STUDENT){
$session->setIsAdmin(SessionEnum::YES);
}else{
if($sessionOn){
$sessionOn->delete();
$this->generalService->logoffWS(
$sessionOn,
$this->clientConfig->getId()
);
}
}
$errors = $this->validateEntity($session);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
$this->em->persist($session);
$this->em->flush();
$this->generalService->setCookie('sessiontoken', $session->getToken());
if(is_null($urlRedirect)){
return $this->redirectToRoute('resume');
}
return $this->redirect($urlRedirect, 301);
}
/**
* @Route(
* path = "/recover/password",
* name = "recoverPassword",
* methods = {"POST"},
* )
*/
public function recoverPassword(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$email = $this->requestUtil->getField('email');
$user = $this->em->getRepository(User::class)->findOneBy([
"email" => $email,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if(!$user){
return $this->eadResponse([
"message" => "Success"
]);
}
$emailService = $this->generalService->getService('EmailService');
$hashData = [
"today" => date('Ymd'),
"userId" => $user->getId()
];
$hash = $this->stringUtil->encodeHex(base64_encode(json_encode($hashData)));
$domain = $this->client->getDomainPrimary();
if(
$emailService->checkUserToSend($user) ||
$this->configuration->get("allow_send_email_user") == UserEnum::YES
){
$emailService->setToEmail($user->getEmail());
$emailService->setToName($user->getName());
$subText = $this->configuration->getLanguage('recover_password.subject', 'email');
$subject = "{$subText} - {$this->client->getBrand()}";
$emailService->setSubject($subject);
$emailService->setData([
"userName" => $user->getName(),
"btnLink" => "https://{$domain}/change/password/{$hash}"
]);
$emailService->setTemplateBody("recover_password");
$emailService->send();
}
return $this->eadResponse([ "message" => "Success" ]);
}
/**
* @Route(
* path = "/change/password/{hash}",
* name = "changePasswordPost",
* methods = {"GET"},
* requirements = { "hash" = "([a-zA-Z0-9_-]+)" }
* )
*/
public function changePassword(Request $request) {
$hash = $request->get("hash");
$hashData = json_decode(base64_decode($this->stringUtil->decodeHex($hash)));
$this->data['hash'] = $hash;
$this->data['remember'] = $this->generalService->getCookie('remember');
if($hashData->today != date("Ymd")){
$this->data["confirmMsg"] = $this->configuration->getLanguage('invalid_hash', 'login');
$this->data["confirmStatus"] = UserEnum::NO;
return $this->renderEAD('login/login.html.twig');
}
return $this->renderEAD('login/recover-password.html.twig');
}
/**
* @Route(
* path = "/reset/password",
* name = "resetPasswordPost",
* methods = {"POST"}
* )
*/
public function resetPasswordPost(Request $request) {
$this->requestUtil->setRequest($request)->setData();
$hash = $this->requestUtil->getField("hash");
$hashData = json_decode(base64_decode($this->stringUtil->decodeHex($hash)));
if($hashData->today != date("Ymd")){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('invalid_hash', 'login')
], ErrorEnum::ACTION_INVALID);
}
$user = $this->em->getRepository(User::class)->findOneBy([
"id" => $hashData->userId,
"deleted" => UserEnum::ITEM_NO_DELETED
]);
if(!$user){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('user_not_found', 'login')
], ErrorEnum::ACTION_INVALID);
}
$password = $this->requestUtil->getField('password');
$confirmPassword = $this->requestUtil->getField('confirmPassword');
if($password != $confirmPassword){
return $this->eadResponse([
"message" => $this->configuration->getLanguage('passwords_must_be_the_same', 'login')
], ErrorEnum::ACTION_INVALID);
}
$user->setPassword($password);
$this->em->flush();
$this->data["confirmMsg"] = $this->configuration->getLanguage('updated_password', 'login');
$this->data["confirmStatus"] = UserEnum::YES;
return $this->eadResponse([
"message" => $this->configuration->getLanguage('updated_password', 'login'),
"url" => 'https://'.$this->client->getDomainPrimary()."/login"
]);
}
/**
* @Route(
* path = "/logoff/user",
* name = "userLogoff",
* methods = {"GET"}
* )
*/
public function logoffUser(Request $request) {
$sessionOn = $this->user->getSession();
$userOrigin = $sessionOn->getUserOrigin();
$sessionOn->delete();
$this->generalService->logoffWS(
$sessionOn,
$this->clientConfig->getId()
);
if($userOrigin){
$sessionOld = $this->em->getRepository(Session::class)->findOneBy([
"user" => $userOrigin->getId(),
"deleted" => UserEnum::ITEM_NO_DELETED
]);
$ipApi = $this->generalService->getService('IpApiService');
$ipApi->setRequest($request);
$session = new Session();
$session->setUser($userOrigin);
$session->setIp($ipApi->getIp());
$session->setIspName($ipApi->getIsp());
$session->setCoordinate($ipApi->getCoordinate());
$session->setTimeZone($ipApi->getTimeZone());
$session->setCity($ipApi->getCity());
$session->setState($ipApi->getState());
$session->setCountry($ipApi->getCountry());
$session->setIsAdmin(UserEnum::YES);
$errors = $this->validateEntity($session);
if($errors){
return $this->eadResponse($errors, ErrorEnum::FIELD_EMPTY);
}
if($sessionOld){
$session->setToken($sessionOld->getToken());
$sessionOld->delete();
$time = time() + (24 * 60 * 60);
if($this->generalService->getCookie('remember') == SessionEnum::YES){
$time = null;
}
$this->generalService->setCookie(
'sessiontoken',
$session->getToken(),
$time
);
}
$this->em->persist($session);
$this->em->flush();
/*return $this->eadResponse([
"token" => $session->getToken(),
"userId" => $userOrigin->getId()
]);*/
}
$this->em->flush();
return $this->redirectToRoute('home');
}
/**
* @Route(
* path = "/logoff",
* name = "logoff",
* methods = {"GET"},
* )
*/
public function logoff(Request $request) {
if($this->user){
$sessionOn = $this->user->getSession();
if($sessionOn){
$sessionOn->delete();
$this->configuration->getSessionSym()->set('session', null);
$this->em->flush();
$this->generalService->deleteCookie('remember');
$this->generalService->deleteCookie('sessiontoken');
$this->generalService->logoffWS(
$sessionOn,
$this->clientConfig->getId()
);
}
}
return $this->redirectToRoute('home');
}
}