<?php
namespace Customize\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\AuthenticationEvents;
use Symfony\Component\Security\Core\Event\AuthenticationEvent;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
//require 'vendor/autoload.php';
use GuzzleHttp\Client;
/**
* ログインしたときに何かする
*
*/
class AuthenticationSuccessSubscriber implements EventSubscriberInterface {
/**
* @var Session
*/
protected $session;
/**
* @var TokenStorage
*/
protected $tokenStorage;
/**
* @param TokenStorageInterface $tokenStorage
*/
public function __construct(
TokenStorageInterface $tokenStorage
) {
$this->tokenStorage = $tokenStorage;
}
/**
* @param SessionInterface $session
* @required
*/
public function setSession(SessionInterface $session)
{
$this->session = $session;
}
public static function getSubscribedEvents(): array
{
return [
AuthenticationEvents::AUTHENTICATION_SUCCESS => "onAuthenticationSuccess"
];
}
public function onAuthenticationSuccess(AuthenticationEvent $event)
{
$token = $event->getAuthenticationToken();
//if(!$token->getRoles()) {
if(!$token->getRoleNames()) {
return;
}
switch($token->getProviderKey()) {
case "customer":
// 会員がログインしたときに何かする
$User = $token->getUser();
$client = new Client();
$aes_key = getenv('API_AES_KEY');
$user_id = openssl_encrypt($User['id'],'aes-256-ecb',$aes_key);
$options = [
'headers' => [
'Content-Type' => 'application/x-www-form-urlencoded'],
'form_params' => [
"site_id" => 1,
"user_id" => $user_id,
"password" => $User['password']
]
];
$url = getenv('API_KV_LOGIN');
$response = $client->request('POST', $url, $options);
$res = json_decode($response->getBody());
if(!$res->status){
// ログアウト処理?
//$this->tokenStorage->setToken(null);
$message = "システムエラーが発生しました、時間をおいて再度お試しください";
$this->session->getFlashBag()->add('eccube.front.request.error', $message);
header('Location: /logout');
exit;
}else{
setcookie("nooVAJhmuvazsDiDliP",$user_id,time()+60*60*24*28,"/"); //28日
}
break;
case "admin":
$User = $token->getUser();
break;
}
}
public function setCookie(Cookie $cookie)
{
$this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
$this->headerNames['set-cookie'] = 'Set-Cookie';
}
}