src/Controller/Admin/LoginController.php line 55

Open in your IDE?
  1. <?php
  2. namespace EADPlataforma\Controller\Admin;
  3. use EADPlataforma\Entity\User;
  4. use EADPlataforma\Enum\UserEnum;
  5. use EADPlataforma\Error\ActionInvalidException;
  6. use EADPlataforma\Error\FieldException;
  7. use EADPlataforma\Error\NotFoundException;
  8. use EADPlataforma\Requests\Login\AuthenticationRequest;
  9. use EADPlataforma\Requests\Login\LoginRequest;
  10. use EADPlataforma\Requests\Login\ResetPasswordRequest;
  11. use EADPlataforma\Requests\Login\SendEmailAuthenticationRequest;
  12. use EADPlataforma\Requests\Login\SendEmailRecoverPasswordRequest;
  13. use EADPlataforma\Requests\Login\UpdateUserNotificationRequest;
  14. use EADPlataforma\Response\HttpNoContent;
  15. use EADPlataforma\Response\HttpOk;
  16. use EADPlataforma\Services\EntityServices\LoginService;
  17. use EADPlataforma\Services\EntityServices\PermissionService;
  18. use EADPlataforma\Services\EntityServices\SessionService;
  19. use EADPlataforma\Services\EntityServices\UserService;
  20. use EADPlataforma\Services\EntityServices\ConfigurationIpService;
  21. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
  22. use Symfony\Component\HttpFoundation\JsonResponse;
  23. use Symfony\Component\HttpFoundation\Request;
  24. use Symfony\Component\Routing\Annotation\Route;
  25. /**
  26.  * @Route(
  27.  *      path          = "/auth",
  28.  *      schemes         = {"http|https"}
  29.  * )
  30.  * @Cache(
  31.  *      maxage          = "0",
  32.  *      smaxage         = "0",
  33.  *      expires         = "now",
  34.  *      public          = false
  35.  * )
  36.  */
  37. class LoginController extends AbstractController {
  38.     public function getEntityClass(): string
  39.     {
  40.         return User::class;
  41.     }
  42.     /**
  43.      * @Route(
  44.      *      name          = "authLogin",
  45.      *      methods       = {"POST"}
  46.      * )
  47.      * 
  48.      * @throws ActionInvalidException
  49.      */
  50.     public function authLogin(
  51.         LoginRequest $request,
  52.         LoginService $loginService
  53.         PermissionService $permissionService,
  54.         SessionService $sessionService,
  55.         ConfigurationIpService $configurationIpService
  56.     ): JsonResponse
  57.     {
  58.         if(!$loginService->verifyUserAgentValid($request)){
  59.             throw new ActionInvalidException(
  60.                 $this->configuration->getLanguage('login_invalid''login')
  61.             );
  62.         }
  63.         $user $loginService->getUserLogin($request);
  64.         if(!$user){
  65.             throw new ActionInvalidException(
  66.                 $this->configuration->getLanguage('login_invalid''login')
  67.             );
  68.         }
  69.         if(!$configurationIpService->isValidIp($request->request->getClientIp())){
  70.             throw new ActionInvalidException(
  71.                 $this->configuration->getLanguage('login_invalid''login')
  72.             );
  73.         }
  74.         if($user->getId() != UserEnum::YES && $user->getPassword() == UserEnum::PASSWORD_RESET){
  75.             throw new ActionInvalidException(
  76.                 $this->configuration->getLanguage('password_reset''login')
  77.             );
  78.         }
  79.         if(!$loginService->verifyUserLoginValid($user$request->password)){
  80.             throw new ActionInvalidException(
  81.                 $this->configuration->getLanguage('login_invalid''login')
  82.             );
  83.         }
  84.         $platformStatus $this->clientConfig->getPlatformStatus();
  85.         if(!$loginService->verifyPlataformStatus($user$platformStatus)){
  86.             throw new ActionInvalidException(
  87.                 $this->configuration->getLanguage('login_invalid''login')
  88.             );
  89.         }
  90.         if($user->getAuthenticationAllow()){
  91.             $signId $this->generalService->signDataWithExpiration($user->getId(), 300);
  92.             
  93.             $data = [
  94.                 "userHash" => $signId,
  95.                 "authenticationActivated" => $user->getAuthenticationAllow()
  96.             ];
  97.             return new HttpOk($data);
  98.         }
  99.         $data $sessionService->registerSession($request$user);
  100.         if(!$data) {
  101.             throw new ActionInvalidException("ExecutionTypeInvalid");
  102.         }
  103.         return new HttpOk($data);
  104.     }
  105.     /**
  106.      * @Route(
  107.      *      path          = "/mfa",
  108.      *      name          = "authMFA",
  109.      *      methods       = {"POST"},
  110.      * )
  111.      * 
  112.      * @throws ActionInvalidException
  113.      * @throws NotFoundException
  114.      */
  115.     public function authMFA(
  116.         AuthenticationRequest $request,
  117.         UserService $userService,
  118.         LoginService $loginService,
  119.         SessionService $sessionService
  120.     ): JsonResponse
  121.     {
  122.         $userHash $request->data['userHash'];
  123.         $userId $this->generalService->verifySignedDataExpire($userHash);
  124.         if(!$userId){
  125.             throw new ActionInvalidException(
  126.                 $this->configuration->getLanguage('user_not_found''login')
  127.             );
  128.         }
  129.         $user $userService->searchUser($userId);
  130.         if(!$user){
  131.             throw new NotFoundException(
  132.                 $this->configuration->getLanguage('user_not_found''login')
  133.             ); 
  134.         }
  135.         if(!$user->getAuthenticationAllow()){
  136.             throw new NotFoundException(
  137.                 $this->configuration->getLanguage('authentication_not_enabled''login')
  138.             ); 
  139.         }
  140.         if(!$loginService->verifyAuthenticationValid($request$user)){
  141.             throw new ActionInvalidException(
  142.                 $this->configuration->getLanguage('authentication_invalid''login')
  143.             );
  144.         }
  145.         $data $sessionService->registerSession($request$user);
  146.         if(!$data) {
  147.             throw new ActionInvalidException("ExecutionTypeInvalid");
  148.         }
  149.         return new HttpOk($data);
  150.     }
  151.     /**
  152.      * @Route(
  153.      *      path          = "/email/mfa/{userHash}",
  154.      *      name          = "sendEmailAuthentication",
  155.      *      methods       = {"PUT"}
  156.      * )
  157.      * 
  158.      * @throws ActionInvalidException
  159.      * @throws NotFoundException
  160.      */
  161.     public function sendEmailAuthentication(
  162.         SendEmailAuthenticationRequest $request,
  163.         UserService $userService,
  164.         LoginService $loginService
  165.     ): JsonResponse
  166.     {
  167.         $userHash $request->request->get('userHash');
  168.         $userId $this->generalService->verifySignedDataExpire($userHash);
  169.         if(!$userId){
  170.             throw new ActionInvalidException(
  171.                 $this->configuration->getLanguage('email_invalid''login')
  172.             );
  173.         }
  174.         $user $userService->searchUser($userId);
  175.         if (!$user) {
  176.             throw new NotFoundException($this->configuration->getLanguage('user_not_found''login'));
  177.         }
  178.         if(!$loginService->sendEmailAuthentication($user)){
  179.             throw new ActionInvalidException(
  180.                 $this->configuration->getLanguage('email_invalid''login')
  181.             );
  182.         }
  183.         return new HttpNoContent;
  184.     }
  185.     /**
  186.      * @Route(
  187.      *      path          = "/user/notification/{hash}",
  188.      *      name          = "updateUserNotification",
  189.      *      methods       = {"PUT"},
  190.      *      requirements  = { "hash" = "([a-zA-Z0-9_-]+)" }
  191.      * )
  192.      * 
  193.      * @throws ActionInvalidException
  194.      * @throws NotFoundException
  195.      */
  196.     public function updateUserNotification(
  197.         UpdateUserNotificationRequest $request,
  198.         UserService $userService,
  199.         LoginService $loginService
  200.     ): JsonResponse
  201.     {
  202.         $hash $request->request->get('hash');
  203.         $user $userService->searchUserByHash($hash);
  204.         if(!$user){
  205.             throw new NotFoundException($this->configuration->getLanguage('user_not_found''login'));
  206.         }
  207.         $userService->updateUserNotification($request->data$user);
  208.         return new HttpNoContent;
  209.     }
  210.     /**
  211.      * @Route(
  212.      *      path          = "/email/recover/password",
  213.      *      name          = "sendEmailRecoverPassword",
  214.      *      methods       = {"POST"}
  215.      * )
  216.      * 
  217.      * @throws ActionInvalidException
  218.      * @throws NotFoundException
  219.      */
  220.     public function sendEmailRecoverPassword(
  221.         SendEmailRecoverPasswordRequest $request,
  222.         UserService $userService,
  223.         LoginService $loginService
  224.     ): JsonResponse
  225.     {
  226.         $email $request->data["email"];
  227.         $user $userService->searchUserByEmail($emailUserEnum::ITEM_NO_DELETED);
  228.         if(!$user){
  229.             throw new NotFoundException($this->configuration->getLanguage('user_not_found''login')); 
  230.         }
  231.         if(!$loginService->sendEmailRecoverPassword($user)){
  232.             throw new ActionInvalidException(
  233.                 $this->configuration->getLanguage('email_invalid''login')
  234.             );
  235.         }
  236.         return new HttpNoContent;
  237.     }
  238.     /**
  239.      * @Route(
  240.      *      path          = "/reset/password",
  241.      *      name          = "resetPassword",
  242.      *      methods       = {"POST"}
  243.      * )
  244.      * 
  245.      * @throws ActionInvalidException
  246.      */
  247.     public function resetPassword(
  248.         ResetPasswordRequest $request,
  249.         UserService $userService,
  250.         LoginService $loginService
  251.     ): JsonResponse
  252.     {
  253.         $errorMessage $loginService->verifyResetPassword($request->data);
  254.         if(!empty($errorMessage)){
  255.             throw new ActionInvalidException($errorMessage);
  256.         }
  257.         $data = [ 
  258.             "message" => $this->configuration->getLanguage('updated_password''login'),
  259.             "status" => UserEnum::YES,
  260.             "url" => "https://{$this->client->getDomainPrimary()}/login"
  261.         ];
  262.         return new HttpOk($data);
  263.     }
  264.     
  265. }