vendor/symfony/security-core/Authentication/Provider/UserAuthenticationProvider.php line 67

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Security\Core\Authentication\Provider;
  11. use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
  12. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  14. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  15. use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
  16. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  17. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  18. use Symfony\Component\Security\Core\Role\SwitchUserRole;
  19. use Symfony\Component\Security\Core\User\UserCheckerInterface;
  20. use Symfony\Component\Security\Core\User\UserInterface;
  21. use Mysqli;
  22. /**
  23.  * UserProviderInterface retrieves users for UsernamePasswordToken tokens.
  24.  *
  25.  * @author Fabien Potencier <fabien@symfony.com>
  26.  */
  27. abstract class UserAuthenticationProvider implements AuthenticationProviderInterface
  28. {
  29.     private $hideUserNotFoundExceptions;
  30.     private $userChecker;
  31.     private $providerKey;
  32.     /**
  33.      * @throws \InvalidArgumentException
  34.      */
  35.     public function __construct(UserCheckerInterface $userCheckerstring $providerKeybool $hideUserNotFoundExceptions true)
  36.     {
  37.         if (empty($providerKey)) {
  38.             throw new \InvalidArgumentException('$providerKey must not be empty.');
  39.         }
  40.         $this->userChecker $userChecker;
  41.         $this->providerKey $providerKey;
  42.         $this->hideUserNotFoundExceptions $hideUserNotFoundExceptions;
  43.     }
  44.     /**
  45.      * {@inheritdoc}
  46.      */
  47.     public function authenticate(TokenInterface $token)
  48.     {
  49.         if (!$this->supports($token)) {
  50.             throw new AuthenticationException('The token is not supported by this authentication provider.');
  51.         }
  52.         $username $token->getUsername();
  53.         if ('' === $username || null === $username) {
  54.             $username AuthenticationProviderInterface::USERNAME_NONE_PROVIDED;
  55.         }
  56.      
  57.         try {
  58.             $user $this->retrieveUser($username$token);
  59.         } catch (UsernameNotFoundException $e) {
  60.             if ($this->hideUserNotFoundExceptions) {
  61.                 throw new BadCredentialsException('Bad credentials.'0$e);
  62.             }
  63.             $e->setUsername($username);
  64.             throw $e;
  65.         }
  66.         if (!$user instanceof UserInterface) {
  67.             throw new AuthenticationServiceException('retrieveUser() must return a UserInterface.');
  68.         }
  69.         try {
  70.             $this->userChecker->checkPreAuth($user);
  71.             $this->checkAuthentication($user$token);
  72.             $this->userChecker->checkPostAuth($user);
  73.         } catch (BadCredentialsException $e) {
  74.             if ($this->hideUserNotFoundExceptions) {
  75.                 throw new BadCredentialsException('Bad credentials.'0$e);
  76.             }
  77.             throw $e;
  78.         }
  79.         if ($token instanceof SwitchUserToken) {
  80.             $authenticatedToken = new SwitchUserToken($user$token->getCredentials(), $this->providerKey$this->getRoles($user$token), $token->getOriginalToken());
  81.         } else {
  82.             $authenticatedToken = new UsernamePasswordToken($user$token->getCredentials(), $this->providerKey$this->getRoles($user$token));
  83.         }
  84.         $roles $user->getRoles();
  85.         if (in_array('ROLE_ADMIN'$roles) || in_array('ROLE_SUPER_ADMIN'$roles) || in_array('ROLE_AGENT'$roles)) {
  86.             $mysqli $this->conectionTKT();
  87.             $consulta "INSERT INTO agent_login (agent_id,date,auth_type) VALUES ('".$user->getId()."','".date('Y-m-d H:i:s')."', 'login')";
  88.     
  89.             if ($mysqli->query($consulta) === TRUE) {
  90.                 echo 'ok';
  91.             } else {
  92.                 echo json_encode($mysqli->error);
  93.             }
  94.     
  95.             $mysqli->close();
  96.     
  97.         }
  98.        
  99.         $authenticatedToken->setAttributes($token->getAttributes());
  100.         return $authenticatedToken;
  101.     }
  102.     public function conectionTKT(){
  103.         $mysqli = new mysqli("database-plesk.cfcc6wi065dc.us-east-1.rds.amazonaws.com""ticketera""Qn0is837Zo102""admin_tkt2");
  104.         //$mysqli = new mysqli("localhost", "root", "root", "ticketerasg");
  105.         /* comprobar la conexión */
  106.         if ($mysqli->connect_errno) {
  107.             printf("Falló la conexión: %s\n"$mysqli->connect_error);
  108.             exit();
  109.         }
  110.         return $mysqli;
  111.     }
  112.     /**
  113.      * {@inheritdoc}
  114.      */
  115.     public function supports(TokenInterface $token)
  116.     {
  117.         return $token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey();
  118.     }
  119.     /**
  120.      * Retrieves roles from user and appends SwitchUserRole if original token contained one.
  121.      *
  122.      * @return array The user roles
  123.      */
  124.     private function getRoles(UserInterface $userTokenInterface $token)
  125.     {
  126.         $roles $user->getRoles();
  127.         foreach ($token->getRoles(false) as $role) {
  128.             if ($role instanceof SwitchUserRole) {
  129.                 $roles[] = $role;
  130.                 break;
  131.             }
  132.         }
  133.         return $roles;
  134.     }
  135.     /**
  136.      * Retrieves the user from an implementation-specific location.
  137.      *
  138.      * @param string                $username The username to retrieve
  139.      * @param UsernamePasswordToken $token    The Token
  140.      *
  141.      * @return UserInterface The user
  142.      *
  143.      * @throws AuthenticationException if the credentials could not be validated
  144.      */
  145.     abstract protected function retrieveUser($usernameUsernamePasswordToken $token);
  146.     /**
  147.      * Does additional checks on the user and token (like validating the
  148.      * credentials).
  149.      *
  150.      * @throws AuthenticationException if the credentials could not be validated
  151.      */
  152.     abstract protected function checkAuthentication(UserInterface $userUsernamePasswordToken $token);
  153. }