<commit_before><?php

/**
 * LoginWidgetType.php
 *
 * @since 23/09/16
 * @author gseidel
 */

namespace Enhavo\Bundle\UserBundle\Widget;

use Enhavo\Bundle\AppBundle\Widget\AbstractWidgetType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class LoginWidgetType extends AbstractWidgetType
{
    /**
     * @inheritDoc
     */
    public function getType()
    {
        return 'login';
    }

    public function createViewData($options, $resource = null)
    {
        if(!$options['csrf_token']) {
            $token = $options['csrf_token'];
        } else {
            $token = $this->getCsrfToken();
        }

        if(!$options['last_username']) {
            $lastUsername = $options['last_username'];
        } else {
            $lastUsername = $this->getLastUserName();
        }

        if(!$options['error']) {
            $error = $options['error'];
        } else {
            $error = $this->getError();
        }
        
        return [
            'csrf_token' => $token,
            'last_username' => $lastUsername,
            'error' => $error,
            'failurePath' => $options['failurePath']
        ];
    }

    public function configureOptions(OptionsResolver $optionsResolver)
    {
        parent::configureOptions($optionsResolver);

        $optionsResolver->setDefaults([
            'template' => 'theme/widget/login.html.twig',
            'csrf_token' => null,
            'last_username' => null,
            'error' => null,
            'failurePath' => null,
        ]);
    }

    private function getCsrfToken()
    {
        return $this->container->get('security.csrf.token_manager')->getToken('authenticate')->getValue();
    }

    private function getError()
    {
        $request = $this->container->get('request_stack')->getMasterRequest();
        $session = $request->getSession();
        $authErrorKey = Security::AUTHENTICATION_ERROR;

        if ($request->attributes->has($authErrorKey)) {
            $error = $request->attributes->get($authErrorKey);
        } elseif (null !== $session && $session->has($authErrorKey)) {
            $error = $session->get($authErrorKey);
            $session->remove($authErrorKey);
        } else {
            $error = null;
        }

        if (!$error instanceof AuthenticationException) {
            $error = null;
        }
        return $error;
    }

    private function getLastUserName()
    {
        $session = $this->container->get('session');
        $lastUsernameKey = Security::LAST_USERNAME;
        $lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey);
        return $lastUsername;
    }
}
<commit_msg>fix login widget options logic<commit_after><?php

/**
 * LoginWidgetType.php
 *
 * @since 23/09/16
 * @author gseidel
 */

namespace Enhavo\Bundle\UserBundle\Widget;

use Enhavo\Bundle\AppBundle\Widget\AbstractWidgetType;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class LoginWidgetType extends AbstractWidgetType
{
    /**
     * @inheritDoc
     */
    public function getType()
    {
        return 'login';
    }

    public function createViewData($options, $resource = null)
    {
        if($options['csrf_token']) {
            $token = $options['csrf_token'];
        } else {
            $token = $this->getCsrfToken();
        }

        if($options['last_username']) {
            $lastUsername = $options['last_username'];
        } else {
            $lastUsername = $this->getLastUserName();
        }

        if($options['error']) {
            $error = $options['error'];
        } else {
            $error = $this->getError();
        }
        
        return [
            'csrf_token' => $token,
            'last_username' => $lastUsername,
            'error' => $error,
            'failurePath' => $options['failurePath']
        ];
    }

    public function configureOptions(OptionsResolver $optionsResolver)
    {
        parent::configureOptions($optionsResolver);

        $optionsResolver->setDefaults([
            'template' => 'theme/widget/login.html.twig',
            'csrf_token' => null,
            'last_username' => null,
            'error' => null,
            'failurePath' => null,
        ]);
    }

    private function getCsrfToken()
    {
        return $this->container->get('security.csrf.token_manager')->getToken('authenticate')->getValue();
    }

    private function getError()
    {
        $request = $this->container->get('request_stack')->getMasterRequest();
        $session = $request->getSession();
        $authErrorKey = Security::AUTHENTICATION_ERROR;

        if ($request->attributes->has($authErrorKey)) {
            $error = $request->attributes->get($authErrorKey);
        } elseif (null !== $session && $session->has($authErrorKey)) {
            $error = $session->get($authErrorKey);
            $session->remove($authErrorKey);
        } else {
            $error = null;
        }

        if (!$error instanceof AuthenticationException) {
            $error = null;
        }
        return $error;
    }

    private function getLastUserName()
    {
        $session = $this->container->get('session');
        $lastUsernameKey = Security::LAST_USERNAME;
        $lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey);
        return $lastUsername;
    }
}

{'fraction_non_alphanumeric': 0.1442169540229885, 'fraction_numerical': 0.0021551724137931034, 'mean_word_length': 2.2701115678214916, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 65, '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': '1044047', 'n_tokens_mistral': 1552, 'n_tokens_neox': 1525, 'n_words': 250}