app/Customize/EventSubscriber/AuthenticationSuccessSubscriber.php line 58

Open in your IDE?
  1. <?php
  2.  
  3. namespace Customize\EventSubscriber;
  4.  
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\Security\Core\AuthenticationEvents;
  7. use Symfony\Component\Security\Core\Event\AuthenticationEvent;
  8. use Symfony\Component\HttpFoundation\Session\Session;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\HttpFoundation\Cookie;
  11. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. //require 'vendor/autoload.php';
  14. use GuzzleHttp\Client;
  15.  
  16. /**
  17.  * ログインしたときに何かする
  18.  *
  19.  */
  20. class AuthenticationSuccessSubscriber implements EventSubscriberInterface {
  21.     /**
  22.      * @var Session
  23.      */
  24.     protected $session;
  25.     /**
  26.      * @var TokenStorage
  27.      */
  28.      protected $tokenStorage;
  29.     /**
  30.      * @param TokenStorageInterface $tokenStorage
  31.      */
  32.      public function __construct(
  33.         TokenStorageInterface $tokenStorage
  34.     ) {
  35.         $this->tokenStorage $tokenStorage;
  36.     }
  37.      /**
  38.      * @param SessionInterface $session
  39.      * @required
  40.      */
  41.     public function setSession(SessionInterface $session)
  42.     {
  43.         $this->session $session;
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             AuthenticationEvents::AUTHENTICATION_SUCCESS => "onAuthenticationSuccess"
  49.         ];
  50.     }
  51.     
  52.     public function onAuthenticationSuccess(AuthenticationEvent $event)
  53.     {
  54.         $token $event->getAuthenticationToken();
  55.         
  56.     //if(!$token->getRoles()) {
  57.     if(!$token->getRoleNames()) {
  58.             return;
  59.         }
  60.  
  61.         switch($token->getProviderKey()) {
  62.             case "customer":
  63.                 // 会員がログインしたときに何かする
  64.                 $User $token->getUser();
  65.                 $client = new Client();
  66.                 $aes_key getenv('API_AES_KEY');
  67.                 $user_id openssl_encrypt($User['id'],'aes-256-ecb',$aes_key);
  68.                 $options = [
  69.                     'headers' => [
  70.                         'Content-Type' => 'application/x-www-form-urlencoded'],
  71.                     'form_params' => [
  72.                         "site_id" => 1,
  73.                         "user_id" => $user_id,
  74.                         "password" => $User['password']
  75.                        ]
  76.                     ];
  77.                 $url getenv('API_KV_LOGIN');
  78.                 $response $client->request('POST'$url$options);
  79.                 
  80.                 $res json_decode($response->getBody());
  81.                 if(!$res->status){
  82.                     // ログアウト処理?
  83.                     //$this->tokenStorage->setToken(null);
  84.                     $message "システムエラーが発生しました、時間をおいて再度お試しください";
  85.                     $this->session->getFlashBag()->add('eccube.front.request.error'$message);
  86.                     header('Location: /logout');
  87.                     exit;
  88.                 }else{
  89.                     setcookie("nooVAJhmuvazsDiDliP",$user_id,time()+60*60*24*28,"/"); //28日
  90.                 }
  91.                 break;
  92.             case "admin":
  93.                 $User $token->getUser();
  94.                 break;
  95.         }
  96.     }
  97.     public function setCookie(Cookie $cookie)
  98.     {
  99.         $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
  100.         $this->headerNames['set-cookie'] = 'Set-Cookie';
  101.     }
  102.  
  103. }