src/Knitink/CoreBundle/Controller/DefaultController.php line 58

Open in your IDE?
  1. <?php
  2. namespace Knitink\CoreBundle\Controller;
  3. use Knitink\CoreBundle\Domain\UserManager;
  4. use Knitink\ManagementBundle\Domain\ClientManager;
  5. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Cookie;
  8. use Symfony\Component\HttpFoundation\JsonResponse;
  9. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  10. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  11. class DefaultController extends AbstractController
  12. {
  13.     public function login($tokenRequest $requestClientManager $clientManager)
  14.     {
  15.         /*The user needs to be registered */
  16.         $client $clientManager->searchOneBy(["token"=>$token]);
  17.         $user null;
  18.         if($client){
  19.             $user $client->getTokenUser();
  20.         }
  21.         // Check if the user exists !
  22.         if(!$user){
  23.             throw $this->createAccessDeniedException('Token doesnt exists');
  24.             /*
  25.             return new Response(
  26.                 'Token doesnt exists',
  27.                 Response::HTTP_UNAUTHORIZED
  28.                 ['Content-type' => 'application/json']
  29.             );
  30.             */
  31.         }
  32.         //Handle getting or creating the user entity likely with a posted form
  33.         // The third parameter "main" can change according to the name of your firewall in security.yml
  34.         $token = new UsernamePasswordToken($usernull'main'$user->getRoles());
  35.         $this->get('security.token_storage')->setToken($token);
  36.         // If the firewall name is not main, then the set value would be instead:
  37.         // $this->get('session')->set('_security_XXXFIREWALLNAMEXXX', serialize($token));
  38.         $this->get('session')->set('_security_main'serialize($token));
  39.         // Fire the login event manually
  40.         $event = new InteractiveLoginEvent($request$token);
  41.         $this->get("event_dispatcher")->dispatch("security.interactive_login"$event);
  42.         /*
  43.          * Now the user is authenticated !!!!
  44.          * Do what you need to do now, like render a view, redirect to route etc.
  45.          */
  46.         return $this->redirectToRoute("knk_core_index");
  47.     }
  48.     public function index(Request $request)
  49.     {
  50.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED'); //  IS_AUTHENTICATED_FULLY
  51.         $user $this->getUser();
  52.         if($user){
  53.             // Seteamos el idioma
  54.             $locale = (method_exists($user'getLocale')?$user->getLocale():null);
  55.             if($locale != null) {
  56.                 $this->get('session')->set('_locale'$locale);
  57.             }
  58.             // Redirigimos al dashboard
  59.             return $this->redirectToRoute("knk_core_dashboard");
  60.         }else{
  61.             //return $this->redirectToRoute('globunet_user_security_login');
  62.         }
  63.     }
  64.     public function dashboard(UserManager $userManager)
  65.     {
  66.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
  67.         $response null;
  68.         if ($this->isGranted('ROLE_KNK_UNIV') and $this->isGranted('ROLE_KNK_MNGT')){
  69.             $users = [];
  70.             if ($this->isGranted('ROLE_ALLOWED_TO_SWITCH')) {
  71.                 $users $userManager->cgetByUser($this->getUser());
  72.             }
  73.             $response $this->render('knitink/core_bundle/default/index.html.twig', ['users' => $users]);
  74.         } else if($this->isGranted('ROLE_KNK_MNGT_DEALER') or $this->isGranted('ROLE_KNK_MNGT_INSTALLER')){
  75.             $url 'knk_core_management';
  76.             $response $this->redirectToRoute($url);
  77.         }else if($this->isGranted('ROLE_KNK_MNGT_SUPPORT')){
  78.             $url 'knk_core_control';
  79.             $response $this->redirectToRoute($url);
  80.         }else{
  81.             $url 'knk_core_universe';
  82.             $response $this->redirectToRoute($url);
  83.         }
  84.         return $response;
  85.     }
  86.     public function universe()
  87.     {
  88.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
  89.         $user $this->getUser();
  90.         if($user){
  91.             $count 0;
  92.             if($user instanceof \Knitink\CoreBundle\Entity\User){
  93.                 $count $user->getClients()->count();
  94.             }
  95.             if($this->isGranted('ROLE_KNK_UNIV_SUPER_ADMIN') || $count ){
  96.                 $url 'knk_univ_clients';
  97.             }else{
  98.                 $this->get('session')->set('database'null);
  99.                 $this->get('session')->set('client'null);
  100.                 $url 'knk_univ_home';
  101.             }
  102.             return $this->redirectToRoute($url);
  103.         }else{
  104.             return $this->redirectToRoute('globunet_user_security_login');
  105.         }
  106.     }
  107.     public function management()
  108.     {
  109.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
  110.         $user $this->getUser();
  111.         if($user){
  112.             $url 'knk_mngt_home';
  113.             return $this->redirectToRoute($url);
  114.         }else{
  115.             return $this->redirectToRoute('globunet_user_security_login');
  116.         }
  117.     }
  118.     public function control()
  119.     {
  120.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
  121.         $user $this->getUser();
  122.         if($user){
  123.             $url 'knk_ctrl_home';
  124.             return $this->redirectToRoute($url);
  125.         }else{
  126.             return $this->redirectToRoute('globunet_user_security_login');
  127.         }
  128.     }
  129.     public function locale(Request $request$locale)
  130.     {
  131.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
  132.         //$locale = $request->getLocale();
  133.         $this->get('session')->set('_locale'$locale);
  134.         $referer $request->headers->get('referer');
  135.         return $this->redirect($referer);
  136.         /*
  137.         $user = $this->getUser();
  138.         if($user){
  139.             return $this->redirectToRoute('knk_univ_home');
  140.         }else{
  141.             return $this->redirectToRoute('globunet_user_security_login');
  142.         }
  143.         */
  144.     }
  145.     public function legal()
  146.     {
  147.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
  148.         return $this->render('knitink/core_bundle/default/legal.html.twig', []);
  149.     }
  150.     public function dataProtectionPolicy()
  151.     {
  152.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
  153.         return $this->render('knitink/core_bundle/default/data-protection-policy.html.twig', []);
  154.     }
  155.     public function cookies()
  156.     {
  157.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_REMEMBERED');
  158.         return $this->render('knitink/core_bundle/default/cookies-policy.html.twig', []);
  159.     }
  160.     public function cookiesAccept()
  161.     {
  162.         $request Request::createFromGlobals();
  163.         $content json_decode($request->getContent(), true);
  164.         if (isset($content['cookies-consent']) && $content['cookies-consent'] == 'ok') {
  165.             $expires = new \DateTime("now");
  166.             $dateInterval = new \DateInterval(sprintf('P%dD'365));
  167.             $expires->add($dateInterval);
  168.             $cookie = new Cookie('knitink_accept_cookies_policy_consent','ok'$expires);
  169.             $jsonResponse = new JsonResponse(['success' => true]);
  170.             $jsonResponse->headers->setCookie($cookie);
  171.             return $jsonResponse;
  172.         } else {
  173.             return new JsonResponse(['success' => false], 400);
  174.         }
  175.     }
  176. }