<commit_before><?php

namespace Sitioweb\Bundle\ArmyCreatorBundle;

use FOS\UserBundle\Doctrine\UserManager;
use FOS\UserBundle\Security\LoginManager;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Role\SwitchUserRole;

class UserService
{
    private $kernelRootDir;

    private $tokenStorage;

    private $userManager;

    private $loginManager;

    public function __construct($kernelRootDir, TokenStorageInterface $tokenStorage, UserManager $userManager, LoginManager $loginManager)
    {
        $this->kernelRootDir = $kernelRootDir;
        $this->tokenStorage = $tokenStorage;
        $this->userManager = $userManager;
        $this->loginManager = $loginManager;
    }

    /**
     * onKernelController
     *
     * @param FilterControllerEvent $event
     * @access public
     * @return void
     */
    public function onKernelController(FilterControllerEvent $event)
    {
        return;
        $controllerClass = get_class($event->getController()[0]);

        if (strpos($controllerClass, 'ApiBundle') !== false) {
            return;
        }

        $this->getUser();
    }

    /**
     * getUser
     *
     * @access public
     * @return User
     */
    public function getUser()
    {
        // inspired by Symfony\Bundle\FrameworkBundle\Controller\Controller::getUser()
        if (!$this->tokenStorage) {
            throw new \LogicException('The SecurityBundle is not registered in your application.');
        }

        $token = $this->tokenStorage->getToken();

        if (null === $token) {
            return null;
        }

        $forumUser = $token->getUser();
        if (!is_object($forumUser)) {
            $forumUser = null;
        }
        // end of inspiration

        $isOriginalToken = $this->isOriginalToken($token);

        if ($forumUser && $isOriginalToken) {
            $currentUser = $this->userManager->findUserByEmail($forumUser->getEmail());
            if (!$currentUser) {
                $currentUser = $this->userManager->createUser();
            }
            $currentUser->setForumId($forumUser->getId());
            $currentUser->setUsername($forumUser->getUsername());
            $currentUser->setEmail($forumUser->getEmail());
            $currentUser->setPlainPassword($forumUser->getPassword());
            $lastLogin = new \DateTime();
            // $lastLogin->setTimestamp($user->data['user_lastvisit']);
            $currentUser->setLastLogin($lastLogin);
            $currentUser->setEnabled(true);
            // $currentUser->setAvatar($user->data['user_avatar']);

            $this->userManager->updateUser($currentUser);
            $this->loginManager->loginUser('main', $currentUser);


            return $currentUser;
        } elseif (!$isOriginalToken) {
            return $token->getUser();
        } else {
            $anonymousToken = new AnonymousToken('main', 'anon.');
            $this->tokenStorage->setToken($anonymousToken);

            return $token->getUser();
            //$this->get('fos_user.security.login_manager')->loginUser('main', null);
        }


        return;
    }

    /**
     * Gets the original Token from a switched one.
     *
     * @param TokenInterface $token A switched TokenInterface instance
     *
     * @return TokenInterface|false The original TokenInterface instance, false if the current TokenInterface is not switched
     */
    private function getOriginalToken(TokenInterface $token)
    {
        foreach ($token->getRoles() as $role) {
            if ($role instanceof SwitchUserRole) {
                return $role->getSource();
            }
        }

        return false;
    }

    /**
     * isOriginalToken
     *
     * @param TokenInterface $token
     * @access private
     * @return boolean
     */
    private function isOriginalToken(TokenInterface $token)
    {
        $originalToken = $this->getOriginalToken($token);
        //ld($token, $originalToken);

        return $originalToken === false || $token === $originalToken;
    }<commit_msg>UserService is in charge of returning an armycreator user<commit_after><?php

namespace Sitioweb\Bundle\ArmyCreatorBundle;

use FOS\UserBundle\Doctrine\UserManager;
use FOS\UserBundle\Security\LoginManager;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\Role\SwitchUserRole;

class UserService
{
    private $kernelRootDir;

    private $tokenStorage;

    private $userManager;

    private $loginManager;

    public function __construct(
        $kernelRootDir,
        TokenStorageInterface $tokenStorage,
        UserManager $userManager,
        LoginManager $loginManager
    ) {
        $this->kernelRootDir = $kernelRootDir;
        $this->tokenStorage = $tokenStorage;
        $this->userManager = $userManager;
        $this->loginManager = $loginManager;
    }

    /**
     * onKernelController
     *
     * @param FilterControllerEvent $event
     * @access public
     * @return void
     */
    public function onKernelController(FilterControllerEvent $event)
    {
        return;
        $controllerClass = get_class($event->getController()[0]);

        if (strpos($controllerClass, 'ApiBundle') !== false) {
            return;
        }

        $this->getUser();
    }

    /**
     * getUser
     *
     * @access public
     * @return User
     */
    public function getUser()
    {
        // inspired by Symfony\Bundle\FrameworkBundle\Controller\Controller::getUser()
        if (!$this->tokenStorage) {
            throw new \LogicException('The SecurityBundle is not registered in your application.');
        }

        $token = $this->tokenStorage->getToken();

        if (null === $token) {
            return null;
        }

        $forumUser = $token->getUser();
        if (!is_object($forumUser)) {
            $forumUser = null;
        }
        // end of inspiration

        $isOriginalToken = $this->isOriginalToken($token);

        if ($forumUser && $isOriginalToken) {
            $currentUser = $this->userManager
                ->findUserByEmail($forumUser->getEmail());

            if (!$currentUser) {
                $currentUser = $this->userManager->createUser();
            }

            $currentUser->setForumId($forumUser->getId());
            $currentUser->setUsername($forumUser->getUsername());
            $currentUser->setEmail($forumUser->getEmail());
            $currentUser->setPlainPassword($forumUser->getPassword());
            $lastLogin = new \DateTime();
            // $lastLogin->setTimestamp($user->data['user_lastvisit']);
            $currentUser->setLastLogin($lastLogin);
            $currentUser->setEnabled(true);
            // $currentUser->setAvatar($user->data['user_avatar']);

            $this->userManager->updateUser($currentUser);

            return $currentUser;
        } elseif (!$isOriginalToken) { // user is impersonated
            return $token->getUser();
        }

        return null;
    }

    /**
     * Gets the original Token from a switched one.
     *
     * @param TokenInterface $token A switched TokenInterface instance
     *
     * @return TokenInterface|false The original TokenInterface instance, false if the current TokenInterface is not switched
     */
    private function getOriginalToken(TokenInterface $token)
    {
        foreach ($token->getRoles() as $role) {
            if ($role instanceof SwitchUserRole) {
                return $role->getSource();
            }
        }

        return false;
    }

    /**
     * isOriginalToken
     *
     * @param TokenInterface $token
     * @access private
     * @return boolean
     */
    private function isOriginalToken(TokenInterface $token)
    {
        $originalToken = $this->getOriginalToken($token);

        return $originalToken === false || $token === $originalToken;
    }
{'fraction_non_alphanumeric': 0.12133891213389121, 'fraction_numerical': 0.00023909145248057382, 'mean_word_length': 2.4641821946169773, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 82, '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': '6536458', 'n_tokens_mistral': 2228, 'n_tokens_neox': 2170, 'n_words': 424}