<commit_before><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\UserBundle\Security\User;

use Darvin\UserBundle\Entity\BaseUser;
use Darvin\UserBundle\Repository\UserRepository;
use Darvin\UserBundle\Security\OAuth\Exception\BadResponseException;
use Darvin\UserBundle\Security\OAuth\Response\DarvinAuthResponse;
use Darvin\UserBundle\User\UserFactory;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\EntityManager;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

/**
 * OAuth user provider
 */
class OAuthUserProvider implements OAuthAwareUserProviderInterface, UserProviderInterface
{
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    protected $em;

    /**
     * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
     */
    protected $session;

    /**
     * @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
     */
    protected $tokenStorage;

    /**
     * @var \Darvin\UserBundle\User\UserFactory
     */
    protected $userFactory;

    /**
     * @var \Darvin\UserBundle\Repository\UserRepository
     */
    protected $userRepository;

    /**
     * @param \Doctrine\ORM\EntityManager                                                         $em             Entity manager
     * @param \Symfony\Component\HttpFoundation\Session\SessionInterface                          $session        Session
     * @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $tokenStorage   Authentication token storage
     * @param \Darvin\UserBundle\User\UserFactory                                                 $userFactory    User factory
     * @param \Darvin\UserBundle\Repository\UserRepository                                        $userRepository User entity repository
     */
    public function __construct(
        EntityManager $em,
        SessionInterface $session,
        TokenStorageInterface $tokenStorage,
        UserFactory $userFactory,
        UserRepository $userRepository
    ) {
        $this->em = $em;
        $this->session = $session;
        $this->tokenStorage = $tokenStorage;
        $this->userFactory = $userFactory;
        $this->userRepository = $userRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
    {
        // hack to error invalid_grant
        if (!$response instanceof DarvinAuthResponse) {
            throw new BadResponseException($response);
        }
        if ($response->getError()) {
            $this->session->invalidate();
            $this->tokenStorage->setToken(null);

            return null;
        }

        return $this->loadUserByUsername($response->getNickname());
    }

    /**
     * {@inheritdoc}
     */
    public function loadUserByUsername($email)
    {
        $user = $this->userRepository->findOneBy(array(
            'email' => $email,
        ));

        if (!empty($user)) {
            return $user;
        }

        $user = $this->createUser($email);

        $this->em->persist($user);
        $this->em->flush();

        return $user;
    }

    /**
     * {@inheritdoc}
     */
    public function refreshUser(UserInterface $user)
    {
        if (!$this->supportsClass(ClassUtils::getClass($user))) {
            throw new UnsupportedUserException(sprintf('User class "%s" is not supported.', ClassUtils::getClass($user)));
        }

        return $this->loadUserByUsername($user->getUsername());
    }

    /**
     * {@inheritdoc}
     */
    public function supportsClass($class)
    {
        return BaseUser::BASE_USER_CLASS === $class || is_subclass_of($class, BaseUser::BASE_USER_CLASS);
    }

    /**
     * @param string $email User e-mail
     *
     * @return \Darvin\UserBundle\Entity\BaseUser
     */
    protected function createUser($email)
    {
        return $this->userFactory->createUser()
            ->setEmail($email)
            ->generateRandomPlainPassword();
    }
}<commit_msg>OAuth user provider: enable and unlock new user.<commit_after><?php
/**
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright (c) 2015, Darvin Studio
 * @link      https://www.darvin-studio.ru
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Darvin\UserBundle\Security\User;

use Darvin\UserBundle\Entity\BaseUser;
use Darvin\UserBundle\Repository\UserRepository;
use Darvin\UserBundle\Security\OAuth\Exception\BadResponseException;
use Darvin\UserBundle\Security\OAuth\Response\DarvinAuthResponse;
use Darvin\UserBundle\User\UserFactory;
use Doctrine\Common\Util\ClassUtils;
use Doctrine\ORM\EntityManager;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\OAuthAwareUserProviderInterface;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;

/**
 * OAuth user provider
 */
class OAuthUserProvider implements OAuthAwareUserProviderInterface, UserProviderInterface
{
    /**
     * @var \Doctrine\ORM\EntityManager
     */
    protected $em;

    /**
     * @var \Symfony\Component\HttpFoundation\Session\SessionInterface
     */
    protected $session;

    /**
     * @var \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
     */
    protected $tokenStorage;

    /**
     * @var \Darvin\UserBundle\User\UserFactory
     */
    protected $userFactory;

    /**
     * @var \Darvin\UserBundle\Repository\UserRepository
     */
    protected $userRepository;

    /**
     * @param \Doctrine\ORM\EntityManager                                                         $em             Entity manager
     * @param \Symfony\Component\HttpFoundation\Session\SessionInterface                          $session        Session
     * @param \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface $tokenStorage   Authentication token storage
     * @param \Darvin\UserBundle\User\UserFactory                                                 $userFactory    User factory
     * @param \Darvin\UserBundle\Repository\UserRepository                                        $userRepository User entity repository
     */
    public function __construct(
        EntityManager $em,
        SessionInterface $session,
        TokenStorageInterface $tokenStorage,
        UserFactory $userFactory,
        UserRepository $userRepository
    ) {
        $this->em = $em;
        $this->session = $session;
        $this->tokenStorage = $tokenStorage;
        $this->userFactory = $userFactory;
        $this->userRepository = $userRepository;
    }

    /**
     * {@inheritdoc}
     */
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
    {
        // hack to error invalid_grant
        if (!$response instanceof DarvinAuthResponse) {
            throw new BadResponseException($response);
        }
        if ($response->getError()) {
            $this->session->invalidate();
            $this->tokenStorage->setToken(null);

            return null;
        }

        return $this->loadUserByUsername($response->getNickname());
    }

    /**
     * {@inheritdoc}
     */
    public function loadUserByUsername($email)
    {
        $user = $this->userRepository->findOneBy(array(
            'email' => $email,
        ));

        if (!empty($user)) {
            return $user;
        }

        $user = $this->createUser($email);

        $this->em->persist($user);
        $this->em->flush();

        return $user;
    }

    /**
     * {@inheritdoc}
     */
    public function refreshUser(UserInterface $user)
    {
        if (!$this->supportsClass(ClassUtils::getClass($user))) {
            throw new UnsupportedUserException(sprintf('User class "%s" is not supported.', ClassUtils::getClass($user)));
        }

        return $this->loadUserByUsername($user->getUsername());
    }

    /**
     * {@inheritdoc}
     */
    public function supportsClass($class)
    {
        return BaseUser::BASE_USER_CLASS === $class || is_subclass_of($class, BaseUser::BASE_USER_CLASS);
    }

    /**
     * @param string $email User e-mail
     *
     * @return \Darvin\UserBundle\Entity\BaseUser
     */
    protected function createUser($email)
    {
        return $this->userFactory->createUser()
            ->setEmail($email)
            ->setEnabled(true)
            ->setLocked(false)
            ->generateRandomPlainPassword();
    }
}
{'fraction_non_alphanumeric': 0.11457562625515273, 'fraction_numerical': 0.0008455765775288025, 'mean_word_length': 2.513553657630895, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 65, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2251986', 'n_tokens_mistral': 2534, 'n_tokens_neox': 2487, 'n_words': 451}