src/Controller/AutorizacionUsuarioController.php line 84

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Usuario;
  4. use App\Repository\CuentaRepository;
  5. use App\Repository\UsuarioRepository;
  6. use App\Service\UsuarioService;
  7. use FOS\RestBundle\Controller\AbstractFOSRestController;
  8. use FOS\RestBundle\Controller\Annotations as Rest;
  9. use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTTokenManagerInterface;
  10. use phpDocumentor\Reflection\Types\Object_;
  11. use Symfony\Component\HttpFoundation\Response;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\Security\Core\Security;
  14. use Symfony\Component\Serializer\SerializerInterface;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17.  * @Rest\Route("/api/autenticacion/")
  18.  *
  19.  */
  20. class AutorizacionUsuarioController extends AbstractFOSRestController
  21. {
  22.     private $repository;
  23.     private $cuentaRepository;
  24.     private $serviceUsuario;
  25.     private $JWTTokenManager;
  26.     public function __construct(SerializerInterface $serializer,
  27.                                 UsuarioRepository $usuarioRepository,
  28.                                 CuentaRepository $cuentaRepository,
  29.                                 UsuarioService $serviceUsuario,
  30.                                 JWTTokenManagerInterface $JWTTokenManager
  31.     ){
  32.         $this->serializer $serializer;
  33.         $this->repository $usuarioRepository;
  34.         $this->cuentaRepository  $cuentaRepository;
  35.         $this-> serviceUsuario $serviceUsuario;
  36.         $this->JWTTokenManager   $JWTTokenManager;
  37.     }
  38.     /**
  39.      * @Rest\Post("login", name="login")
  40.      * @param Request $request
  41.      * @return Response
  42.      */
  43.     public function autentificarUsuario(Request $request)
  44.     {
  45.         $nombrePublico $request->request->get("nombre");
  46.         $clavePublica  $request->request->get("pass");
  47.         $user  $this->repository->findOneBy(["nombre"=>$nombrePublico,"password"=>md5($clavePublica),"cuenta"=>1"activo"=>1]);
  48.         //dd($user);
  49.         if(!$user) {
  50.             //data['error'] = $this->createNotFoundException();
  51.             $response = new Response("usuario no encontrado",Response::HTTP_UNAUTHORIZED);
  52.             return $response;
  53.         }
  54.         // $token = $this->JWTTokenManager->create($user);
  55.         $token $this->JWTTokenManager->create($user->getCuenta());
  56.         $data = array("token" => $token"tipoUser" => $user ->getTipo());
  57.         // $data = $token;
  58.         $response =  new Response(json_encode($data),Response::HTTP_OK);
  59.         $response->prepare($request);
  60.         return $response;
  61.     }
  62.     /**
  63.      * @Rest\Post("loginCuenta", name="login_account")
  64.      * @param email string
  65.      * @param pass string
  66.      */
  67.     public function autentificarCuenta(Request $request)
  68.     {
  69.         $token null;
  70.         $appVersion $request->request->get("version");
  71.         if ($request->request->has("email") && $request->request->has("pass")) {
  72.             $email $request->request->get("email");
  73.             $clave $request->request->get("pass");
  74.             if ($clave != '') {
  75.                 $cuenta $this->cuentaRepository->findOneBy(['email' => $email'password' => md5($clave), 'activo' => 1]);
  76.                 if ($cuenta && !empty($cuenta->getToken())) {
  77.                     $token $cuenta->getToken();
  78.                 }
  79.             }
  80.         } else {
  81.             if ($request->request->has("token")) {
  82.                 $cuenta $this->cuentaRepository->findOneBy(['token' => $request->request->get("token"), "activo" => 1]);
  83.             }
  84.         }
  85.         if (empty($cuenta)) {
  86.             $response = new Response(json_encode(["mensaje" => "cuenta no encontrada""err_code" => 1]), Response::HTTP_UNAUTHORIZED);
  87.             return $response;
  88.         }
  89.         if ($token == null) {
  90.             $token $this->JWTTokenManager->create($cuenta);
  91.             $cuenta->setToken($token);
  92.         }
  93.         $cuenta->setFechaLogin(new \DateTime());
  94.         if ($appVersion == null) {
  95.             $cuenta->setVersionApp('');
  96.         } else {
  97.             $cuenta->setVersionApp($appVersion);
  98.         }
  99.         $this->cuentaRepository->save($cuenta);
  100.         $this->cuentaRepository->commit();
  101.         $data= ['token'=> $token,
  102.             'tipo'=>$cuenta->getTipoCuenta()->getId(),
  103.             'parentId'=>$cuenta->getCuentaPadre()->getId(),
  104.             'parentNombre'=>$cuenta->getCuentaPadre()->getEmpresa(),
  105.             'disclaimerAccept'=>$cuenta->getAceptacionDisclaimer(),
  106.             'laboratorio'=>$cuenta->getLaboratorio(),
  107.             'emailCuenta'=>$cuenta->getEmail(),
  108.             'nombre_tipo'=>$cuenta->getTipoCuenta()->getNombre(),
  109.             'comparte_tarifa'=> $cuenta->getCuentaPadre()->getCompartirTarifa(),
  110.             'empresa' => $cuenta->getEmpresa()];
  111.         $response = new \Symfony\Component\HttpFoundation\JsonResponse($data);
  112.         $response->setEncodingOptions(JSON_UNESCAPED_UNICODE);
  113.         return $response;
  114.         // return new Response($this->serializer->serialize($data,'json'), Response::HTTP_OK);
  115.     }
  116.     /**
  117.      * @Rest\Post("loginUsuario", name="login_account_user")
  118.      * @param email string
  119.      * @param pass string
  120.      * @param token
  121.      */
  122.     public function autentificarUsuarioCuenta(Request $request)
  123.     {
  124.         $nombrePublico $request->request->get("nombre");
  125.         $clavePublica  $request->request->get("pass");
  126.        // $token = $request->headers->get("authorization"); //$request->request->get("token");
  127.         $token =  substr($request->headers->get("authorization"),strlen("Bearer "));
  128.         if(!empty($token)) {
  129.             $cuenta $this->cuentaRepository->findOneBy(['token' => $token'activo' => 1]);
  130.         }
  131.         if(empty($cuenta)) {
  132.             $response = new Response(json_encode(["mensaje"=>"cuenta no encontrada","err_code"=>1]), Response::HTTP_UNAUTHORIZED);
  133.             return $response;
  134.         }
  135.         $user  $this->repository->findOneBy(["nombre"=>$nombrePublico,"password"=>md5($clavePublica),'cuenta'=>$cuenta->getId()]);
  136.         if(!$user) {
  137.             $response = new Response(json_encode(["mensaje"=>"usuario no encontrado","err_code"=>2]),Response::HTTP_UNAUTHORIZED);
  138.             return $response;
  139.         }
  140.         $token $this->JWTTokenManager->create($user);
  141.         $data = array("token" => $token"tipoUser" => $user ->getTipo());
  142.         $response =  new Response(json_encode($data),Response::HTTP_OK);
  143.         $response->prepare($request);
  144.         return $response;
  145.     }
  146.     /**
  147.      *  @Rest\Post("logoutCuenta")
  148.      */
  149.     public function logoutCuenta(Request $request)
  150.     {
  151.         $response = new Response(json_encode(["mensaje"=>"cuenta no encontrada","err_code"=>1]),Response::HTTP_UNAUTHORIZED);
  152.         if(!$request->request->has('token'))
  153.             return $response;
  154.         $token $request->request->get('token');
  155.         $cuenta $this->cuentaRepository->findOneBy(['token'=>$token]);
  156.         if(empty($cuenta))
  157.             return $response;
  158.         $cuenta->setToken('');
  159.         $this->cuentaRepository->save($cuenta);
  160.         $response = new Response("",Response::HTTP_OK);
  161.         return $response;
  162.     }
  163. }