src/Controller/UserController.php line 54

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\User;
  4. use App\Service\DairyOffice;
  5. use JsonException;
  6. use Psr\Container\{ContainerExceptionInterfaceNotFoundExceptionInterface};
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. use Symfony\Component\HttpClient\HttpClient;
  9. use Symfony\Component\HttpFoundation\{RequestResponseSession\Session};
  10. use Symfony\Component\Routing\Annotation\Route;
  11. use Symfony\Contracts\HttpClient\Exception\{ClientExceptionInterface,
  12.     DecodingExceptionInterface,
  13.     RedirectionExceptionInterface,
  14.     ServerExceptionInterface,
  15.     TransportExceptionInterface};
  16. class UserController extends AbstractController
  17. {
  18.     /**
  19.      * @Route("/security", name="security")
  20.      */
  21.     public function index(): Response
  22.     {
  23.         return $this->render('security/index.html.twig', [
  24.             'controller_name' => 'UserController',
  25.         ]);
  26.     }
  27.     /**
  28.      * @Route("/login", name="api_login")
  29.      * @param Request $request
  30.      *
  31.      * @return Response
  32.      * @throws ClientExceptionInterface
  33.      * @throws JsonException
  34.      * @throws RedirectionExceptionInterface
  35.      * @throws ServerExceptionInterface
  36.      * @throws TransportExceptionInterface
  37.      * @throws ContainerExceptionInterface
  38.      * @throws NotFoundExceptionInterface
  39.      * @throws DecodingExceptionInterface
  40.      */
  41.     public function login(Request $request): Response
  42.     {
  43.         $req      $request->request;
  44.         $password $req->get('password');
  45.         $username $req->get('username');
  46.         $token     '';
  47.         $email     '';
  48.         $logged_in false;
  49.         $user      = new User($username, [ 'ROLE_BASE_USER' ], $email$password);
  50.         $session $this->container->get("session", function () {
  51.             $session = new Session();
  52.             $session->start();
  53.             return $session;
  54.         });
  55.         $session->set('user'$user);
  56.         $content $user->callUserApi(
  57.             'https://authorize.dairyoffice.com/oauth2/token',
  58.             $username,
  59.             $password,
  60.             $this->getParameter('jwt.client_id'),
  61.             $this->getParameter('jwt.secret_key')
  62.         );
  63.         if (array_key_exists(0$content) && ! $content[0]) {
  64.             if (array_key_exists(1$content) && is_array($content[1])) {
  65.                 $message $content[1]['message'];
  66.             } else if (array_key_exists(1$content) && is_array($content[1])) {
  67.                 $message $content[1];
  68.             } else {
  69.                 $message $content;
  70.             }
  71.             $this->addFlash'danger'$message );
  72.         } else {
  73.             $token   $content['access_token'];
  74.             $email   $content['email'];
  75.             $decodedJwtToken json_decode(
  76.                 base64_decode(str_replace('_''/'str_replace(
  77.                     '-',
  78.                     '+',
  79.                     explode('.'$token)[1]
  80.                 ))),
  81.                 true,
  82.                 512,
  83.                 JSON_THROW_ON_ERROR
  84.             );
  85.             $user->setEmail($email)
  86.                  ->setUsername($decodedJwtToken['name'])
  87.                  ->setApiToken($token);
  88.             $logged_in $decodedJwtToken['exp'] - microtime(true) > 0;
  89.         }
  90.         $do = new DairyOffice(['token' => $token]);
  91.         $data $do->getDairyOfficeLinks();
  92.         return $this->render('default/index.html.twig', [
  93.             'data'      => $data,
  94.             'user'      => $user,
  95.             'logged_in' => $logged_in,
  96.         ]);
  97.     }
  98.     /**
  99.      * @Route("/logout", name="api_logout")
  100.      */
  101.     public function logout(): Response {
  102.         /** TODO: See about logging off from the API */
  103.         $session $this->container->get("session", function () {
  104.             $session = new Session();
  105.             $session->invalidate();
  106.             return $session;
  107.         });
  108.         return $this->redirectToRoute('home');
  109.     }
  110. }