<commit_before><?php

namespace Persona\Hris\Core\Security\Subscriber;

use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTExpiredEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Persona\Hris\Core\Manager\ManagerFactory;
use Persona\Hris\Core\Security\Model\UserInterface;
use Persona\Hris\Core\Security\Model\UserRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;

/**
 * @author <NAME> <<EMAIL>>
 */
final class UserSessionSubscriber implements EventSubscriberInterface
{
    /**
     * @var SessionInterface
     */
    private $session;

    /**
     * @var RequestStack
     */
    private $requestStack;

    /**
     * @var UserRepositoryInterface
     */
    private $userRepository;

    /**
     * @var ManagerFactory
     */
    private $managerFactory;

    /**
     * @var KernelInterface
     */
    private $kernel;

    /**
     * UserSessionSubscriber constructor.
     *
     * @param KernelInterface         $kernel
     * @param ManagerFactory          $managerFactory
     * @param UserRepositoryInterface $userRepository
     * @param SessionInterface        $session
     * @param RequestStack            $requestStack
     */
    public function __construct(
        KernelInterface $kernel,
        ManagerFactory $managerFactory,
        UserRepositoryInterface $userRepository,
        SessionInterface $session,
        RequestStack $requestStack
    ) {
        $this->kernel = $kernel;
        $this->managerFactory = $managerFactory;
        $this->userRepository = $userRepository;
        $this->session = $session;
        $this->requestStack = $requestStack;
    }

    /**
     * @param JWTExpiredEvent $event
     */
    public function resetSession(JWTExpiredEvent $event)
    {
        $user = $this->userRepository->findUserBySessionId((string) $this->session->get(UserInterface::SESSION_KEY));
        if (!$user instanceof UserInterface) {
            return;
        }

        $user->setSessionId('');
        $user->setLoggedIn(false);

        $this->session->remove(UserInterface::SESSION_KEY);

        $persister = $this->managerFactory->getWriteManager();
        $persister->persist($user);
        $persister->flush();
    }

    /**
     * @param AuthenticationSuccessEvent $event
     */
    public function startSession(AuthenticationSuccessEvent $event)
    {
        $user = $event->getUser();
        if (!$user instanceof UserInterface) {
            return;
        }

        $this->session->set(UserInterface::SESSION_KEY, sha1(date('YmdHis')));

        $user->setLoggedIn(true);
        $user->setSessionId($this->session->get(UserInterface::SESSION_KEY));

        $persister = $this->managerFactory->getWriteManager();
        $persister->persist($user);
        $persister->flush();
    }

    /**
     * @param InteractiveLoginEvent $event
     */
    public function checkSession(InteractiveLoginEvent $event)
    {
        if ('prod' !== $this->kernel->getEnvironment()) {
            return;
        }

        $user = $event->getAuthenticationToken()->getUser();
        if (!$user instanceof UserInterface) {
            return;
        }

        $userPersist = $this->userRepository->find($user->getId());
        if ($userPersist->isLoggedIn($user, $this->session)) {
            throw new AuthenticationException();
        }
    }

    /**
     * @param JWTCreatedEvent $event
     */
    public function createToken(JWTCreatedEvent $event)
    {
        $expiration = new \DateTime();
        if ($this->requestStack->getCurrentRequest()->get('_remember_me')) {
            $interval = new \DateInterval('P1M7DT7H9M17S');
        } else {
            $interval = new \DateInterval('PT7H9M17S');
        }
        $expiration->add($interval);

        $payload = $event->getData();
        $payload['exp'] = $expiration->getTimestamp();

        $event->setData($payload);
    }

    /**
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return [
            Events::AUTHENTICATION_SUCCESS => ['startSession', 1],
            Events::JWT_EXPIRED => ['resetSession', 1],
            Events::JWT_CREATED => ['createToken', 1],
            SecurityEvents::INTERACTIVE_LOGIN => ['checkSession', 1],
        ];
    }
}<commit_msg>bugfix remember me<commit_after><?php

namespace Persona\Hris\Core\Security\Subscriber;

use Lexik\Bundle\JWTAuthenticationBundle\Event\AuthenticationSuccessEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTCreatedEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Event\JWTExpiredEvent;
use Lexik\Bundle\JWTAuthenticationBundle\Events;
use Persona\Hris\Core\Manager\ManagerFactory;
use Persona\Hris\Core\Security\Model\UserInterface;
use Persona\Hris\Core\Security\Model\UserRepositoryInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;

/**
 * @author <NAME> <<EMAIL>>
 */
final class UserSessionSubscriber implements EventSubscriberInterface
{
    /**
     * @var SessionInterface
     */
    private $session;

    /**
     * @var RequestStack
     */
    private $requestStack;

    /**
     * @var UserRepositoryInterface
     */
    private $userRepository;

    /**
     * @var ManagerFactory
     */
    private $managerFactory;

    /**
     * @var KernelInterface
     */
    private $kernel;

    /**
     * UserSessionSubscriber constructor.
     *
     * @param KernelInterface         $kernel
     * @param ManagerFactory          $managerFactory
     * @param UserRepositoryInterface $userRepository
     * @param SessionInterface        $session
     * @param RequestStack            $requestStack
     */
    public function __construct(
        KernelInterface $kernel,
        ManagerFactory $managerFactory,
        UserRepositoryInterface $userRepository,
        SessionInterface $session,
        RequestStack $requestStack
    ) {
        $this->kernel = $kernel;
        $this->managerFactory = $managerFactory;
        $this->userRepository = $userRepository;
        $this->session = $session;
        $this->requestStack = $requestStack;
    }

    /**
     * @param JWTExpiredEvent $event
     */
    public function resetSession(JWTExpiredEvent $event)
    {
        $user = $this->userRepository->findUserBySessionId((string) $this->session->get(UserInterface::SESSION_KEY));
        if (!$user instanceof UserInterface) {
            return;
        }

        $user->setSessionId('');
        $user->setLoggedIn(false);

        $this->session->remove(UserInterface::SESSION_KEY);

        $persister = $this->managerFactory->getWriteManager();
        $persister->persist($user);
        $persister->flush();
    }

    /**
     * @param AuthenticationSuccessEvent $event
     */
    public function startSession(AuthenticationSuccessEvent $event)
    {
        $user = $event->getUser();
        if (!$user instanceof UserInterface) {
            return;
        }

        $this->session->set(UserInterface::SESSION_KEY, sha1(date('YmdHis')));

        $user->setLoggedIn(true);
        $user->setSessionId($this->session->get(UserInterface::SESSION_KEY));

        $persister = $this->managerFactory->getWriteManager();
        $persister->persist($user);
        $persister->flush();
    }

    /**
     * @param InteractiveLoginEvent $event
     */
    public function checkSession(InteractiveLoginEvent $event)
    {
        if ('prod' !== $this->kernel->getEnvironment()) {
            return;
        }

        $user = $event->getAuthenticationToken()->getUser();
        if (!$user instanceof UserInterface) {
            return;
        }

        $userPersist = $this->userRepository->find($user->getId());
        if ($userPersist->isLoggedIn($user, $this->session)) {
            throw new AuthenticationException();
        }
    }

    /**
     * @param JWTCreatedEvent $event
     */
    public function createToken(JWTCreatedEvent $event)
    {
        $expiration = new \DateTime();
        if (1 === (int) $this->requestStack->getCurrentRequest()->get('_remember_me', 0)) {
            $interval = new \DateInterval('P1M7DT7H9M17S');
        } else {
            $interval = new \DateInterval('PT7H9M17S');
        }
        $expiration->add($interval);

        $payload = $event->getData();
        $payload['exp'] = $expiration->getTimestamp();

        $event->setData($payload);
    }

    /**
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return [
            Events::AUTHENTICATION_SUCCESS => ['startSession', 1],
            Events::JWT_EXPIRED => ['resetSession', 1],
            Events::JWT_CREATED => ['createToken', 1],
            SecurityEvents::INTERACTIVE_LOGIN => ['checkSession', 1],
        ];
    }
}
{'fraction_non_alphanumeric': 0.12713987473903968, 'fraction_numerical': 0.003340292275574113, 'mean_word_length': 2.7499021526418788, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 105, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6956593', 'n_tokens_mistral': 2653, 'n_tokens_neox': 2604, 'n_words': 431}