Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. <?php
  2.  
  3. namespace SIGMA\PlatformBundle\Controller;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use GuzzleHttp\Client as Client;
  8. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  9. use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
  10. use Symfony\Component\Security\Core\Exception\AccessDeniedException;
  11. // Entidades
  12. use SIGMA\AppBundle\Entity\Cuenta;
  13. //Controladores
  14. use SIGMA\AppBundle\Controller\CuentaController;
  15.  
  16. class SecurityController extends Controller
  17. {
  18.  
  19.   /**
  20.    * Renderiza la página para el login
  21.    * @return type
  22.    */
  23.   public function loginAction() {
  24.     if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
  25.       return $this->redirect($this->generateUrl('homepage'));
  26.     }
  27.     return $this->render('Security/login.html.twig');
  28.   }
  29.  
  30.   /**
  31.    * Redirige el homepage
  32.    * @return type
  33.    */
  34.   public function homepageAction() {
  35.     return $this->render('Security/index.html.twig');
  36.   }
  37.  
  38.   /**
  39.    * Realiza la verificación de Logmod usando un token externo
  40.    * @return type
  41.    * @throws AccessDeniedException
  42.    */
  43.   public function loginCheckAction() {
  44.  
  45.     $request  = Request::createFromGlobals();
  46.     $username = $request->query->get('username');
  47.     $sid      = $request->query->get('sid');
  48.     $this->checkLogmodValidation($username, $sid);
  49.  
  50.     $firewallName = 'main';
  51.     $securityName = "_security_$firewallName";
  52.  
  53.     $em   = $this->getDoctrine()->getManager();
  54.     $user = $em->getRepository(Cuenta::class)->loadUserByUsername($username);
  55.  
  56.     // Deniega el Acceso si no existe el Usuario
  57.     if (!$user) {
  58.       throw new AccessDeniedException();
  59.     }
  60.    
  61.     // Creación del Token para Logeo
  62.     $token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
  63.     $this->get('security.token_storage')->setToken($token);
  64.     $this->get('session')->set($securityName, serialize($token));
  65.  
  66.    
  67.     // Crea el evento de logeo y su configuración manualmente
  68.     $event = new InteractiveLoginEvent($request, $token);
  69.     $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
  70.  
  71.     $this->get(CuentaController::class)->updateLastLogin($user);
  72.    
  73.     // Redirige a la página de Inicio
  74.     return $this->redirectToRoute('homepage');
  75.   }
  76.  
  77.   /**
  78.    * Verificador de Logmod
  79.    * @param String $username
  80.    * @param String $sid
  81.    * @return Boolean
  82.    */
  83.   private function checkLogmodValidation($username, $sid) {
  84.     $linkVerificador = "http://media.fen.uchile.cl/logmod/verificacion.php?username=$username&sid=$sid";
  85.     $client          = new Client();
  86.     $response        = $client->get($linkVerificador);
  87.     $token           = $response->getBody()->getContents();
  88.  
  89.     if ($token != true) {
  90.       throw new AccessDeniedException();
  91.     }
  92.   }
  93.  
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement