Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace SIGMA\PlatformBundle\Controller;
- use Symfony\Bundle\FrameworkBundle\Controller\Controller;
- use Symfony\Component\HttpFoundation\Request;
- use GuzzleHttp\Client as Client;
- use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
- use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
- use Symfony\Component\Security\Core\Exception\AccessDeniedException;
- // Entidades
- use SIGMA\AppBundle\Entity\Cuenta;
- //Controladores
- use SIGMA\AppBundle\Controller\CuentaController;
- class SecurityController extends Controller
- {
- /**
- * Renderiza la página para el login
- * @return type
- */
- public function loginAction() {
- if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
- return $this->redirect($this->generateUrl('homepage'));
- }
- return $this->render('Security/login.html.twig');
- }
- /**
- * Redirige el homepage
- * @return type
- */
- public function homepageAction() {
- return $this->render('Security/index.html.twig');
- }
- /**
- * Realiza la verificación de Logmod usando un token externo
- * @return type
- * @throws AccessDeniedException
- */
- public function loginCheckAction() {
- $request = Request::createFromGlobals();
- $username = $request->query->get('username');
- $sid = $request->query->get('sid');
- $this->checkLogmodValidation($username, $sid);
- $firewallName = 'main';
- $securityName = "_security_$firewallName";
- $em = $this->getDoctrine()->getManager();
- $user = $em->getRepository(Cuenta::class)->loadUserByUsername($username);
- // Deniega el Acceso si no existe el Usuario
- if (!$user) {
- throw new AccessDeniedException();
- }
- // Creación del Token para Logeo
- $token = new UsernamePasswordToken($user, null, $firewallName, $user->getRoles());
- $this->get('security.token_storage')->setToken($token);
- $this->get('session')->set($securityName, serialize($token));
- // Crea el evento de logeo y su configuración manualmente
- $event = new InteractiveLoginEvent($request, $token);
- $this->get("event_dispatcher")->dispatch("security.interactive_login", $event);
- $this->get(CuentaController::class)->updateLastLogin($user);
- // Redirige a la página de Inicio
- return $this->redirectToRoute('homepage');
- }
- /**
- * Verificador de Logmod
- * @param String $username
- * @param String $sid
- * @return Boolean
- */
- private function checkLogmodValidation($username, $sid) {
- $linkVerificador = "http://media.fen.uchile.cl/logmod/verificacion.php?username=$username&sid=$sid";
- $client = new Client();
- $response = $client->get($linkVerificador);
- $token = $response->getBody()->getContents();
- if ($token != true) {
- throw new AccessDeniedException();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement