src/AppBundle/Controller/DefaultController.php line 86

Open in your IDE?
  1. <?php
  2. namespace AppBundle\Controller;
  3. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  4. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpFoundation\RedirectResponse;
  8. use Symfony\Component\Routing\RouterInterface;
  9. // firebase
  10. use Kreait\Firebase\Factory;
  11. use Kreait\Firebase\ServiceAccount;
  12. // login action
  13. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  14. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  15. // utils  
  16. use Symfony\Component\Console\Output\ConsoleOutput;
  17. use AppBundle\AppInterfaces\InitializerInterface;
  18. class DefaultController extends Controller implements InitializerInterface {
  19.   private $em;
  20.   private $active_user_name;
  21.   public function initialize(Request $request) {
  22.       // salida de datos en consola
  23.     $this->Output = new ConsoleOutput();
  24.   }
  25.   
  26.   /* @Route("/{prefix}", requirements={"prefix": "quiniapp|quiniapp/"})
  27.     public function appAction(Request $request) {
  28.       return $this->render("@app/index.html");
  29.    }
  30.    */
  31.   /**
  32.    * @Route("/", name="home")
  33.    */ public function indexAction(Request $request) {
  34.     
  35.     $device $this->get('mobile_detect.mobile_detector');
  36.     $User $this->getUser();
  37.     if($User) {
  38.       $userRoles $User->getRoles();
  39.       $constants $this->getParameter('constants');
  40.       $url '';
  41.       // redirecció a la home del admin
  42.       if(in_array($constants['roles.admin'], $userRoles)) {
  43.         $url $this->generateUrl('admin.home');
  44.       } 
  45.       // redirección a la home del usuario
  46.       else if(in_array($constants['roles.standard_user'], $userRoles)) {
  47.         
  48.         //$active_week = $this->get('quinibar.seasson')->getActive()->getActiveWeek();
  49.         /*
  50.         $template = ":webApp/pages:home.html.twig";
  51.         if($request->isXmlHttpRequest()){
  52.           $template = ":webApp/iphone:home.html.twig"; 
  53.         }
  54.         return $this->render($template,[
  55.           'user'=>$User,
  56.           'week'=>$active_week
  57.           ]);
  58.       */
  59.           return $this->render("@pwa/index.html");
  60.       } else if(in_array($constants['roles.establishment_owner'], $userRoles)) {
  61.         $url $this->generateUrl('owner.home');
  62.       }
  63.   
  64.       return new RedirectResponse($url);
  65.     }
  66.     
  67.     // si no tenim usuari registrat enviem a la app de login
  68.     // return $this->render("@loginApp/index.html");
  69.     return $this->render(":webApp/pages:public.html.twig");
  70.     // return $this->render(':public:index.html.twig');
  71.     
  72.   
  73.   /**
  74.    * @Route("/firebase", name="firebase")
  75.    */ public function firebase(Request $request) {
  76.     
  77.     $serviceAccount ServiceAccount::fromJsonFile($this->get('kernel')->getRootDir() . '/config/firebase-credentials.json');
  78.     $firebase = (new Factory)
  79.     ->withServiceAccount($serviceAccount)
  80.     ->create();
  81.     $auth $firebase->getAuth();
  82.     
  83.     $firebase_id $request->query->get('firebase_id');
  84.     // recuperem el usuari de firebase a partir de la id
  85.     $user $auth->getUser('firebase-id');
  86.     /*
  87.     register a user
  88.     $userProperties = [
  89.     'email' => 'marc@topops.es',
  90.     'emailVerified' => false,
  91.     'phoneNumber' => '+34678768798',
  92.     'password' => 'andromed4',
  93.     'displayName' => 'Marc',
  94.     'photoUrl' => 'http://www.example.com/12345678/photo.png',
  95.     'disabled' => false,  
  96.     ];
  97.     $createdUser = $auth->createUser($userProperties);
  98.     */
  99.     
  100.     // recuperem un usuari per email un cop hem comprovat que està logejat a firebase:
  101.     $user $this->getDoctrine()->getManager()->getRepository("AppBundle:User")->findOneBy(array('email' => $firebase_mail));
  102.     // creamos el token
  103.     $token = new UsernamePasswordToken($usernull'main'$user->getRoles());
  104.     // guardamos el token
  105.     $this->get('security.token_storage')->setToken($token);
  106.     // If the firewall name is not main, then the set value would be instead:
  107.         // $this->get('session')->set('_security_XXXFIREWALLNAMEXXX', serialize($token));
  108.     // establecemos la variable de sesión
  109.         $this->get('session')->set('_security_main'serialize($token));
  110.         
  111.     // Fire the login event manually
  112.         $event = new InteractiveLoginEvent($request$token);
  113.     // lanzamos el evento
  114.         $this->get("event_dispatcher")->dispatch("security.interactive_login"$event);
  115.     return $this->render("::firebase.html.twig",[
  116.       'var'=>$auth]
  117.       );
  118.     // return $this->render(':public:index.html.twig');
  119.   
  120. }