<commit_before><?php
/**
 * @copyright: DotKernel
 * @library: dotkernel/dot-user
 * @author: n3vrax
 * Date: 6/21/2016
 * Time: 10:45 PM
 */

namespace Dot\User\Listener;

use Dot\Authentication\AuthenticationInterface;
use Dot\Authentication\AuthenticationResult;
use Dot\Authentication\Web\Action\LoginAction;
use Dot\Authentication\Web\Action\LogoutAction;
use Dot\Authentication\Web\Event\AuthenticationEvent;
use Dot\FlashMessenger\FlashMessengerInterface;
use Dot\User\Entity\UserEntityInterface;
use Dot\User\Options\MessagesOptions;
use Dot\User\Options\UserOptions;
use Dot\User\Service\UserServiceInterface;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\Form\Form;

/**
 * Class AuthenticationListener
 * @package Dot\User\Listener
 */
class AuthenticationListener extends AbstractListenerAggregate
{
    /** @var  Form */
    protected $loginForm;

    /** @var  FlashMessengerInterface */
    protected $flashMessenger;

    /** @var  UserServiceInterface */
    protected $userService;

    /** @var  UserOptions */
    protected $options;

    public function __construct(
        Form $form,
        FlashMessengerInterface $flashMessenger,
        UserServiceInterface $userService,
        UserOptions $options
    ) {
        $this->loginForm = $form;
        $this->flashMessenger = $flashMessenger;
        $this->userService = $userService;
        $this->options = $options;
    }

    /**
     * @param EventManagerInterface $events
     * @param int $priority
     */
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $sharedEvents = $events->getSharedManager();

        //this will be called after the default prepare listener and the actual authentication call
        //use it for additional checks and data insertions into template
        $this->listeners[] = $sharedEvents->attach(
            LoginAction::class,
            AuthenticationEvent::EVENT_AUTHENTICATION_AUTHENTICATE,
            [$this, 'injectData'],
            500);

        $this->listeners[] = $sharedEvents->attach(
            LoginAction::class,
            AuthenticationEvent::EVENT_AUTHENTICATION_AUTHENTICATE,
            [$this, 'preAuthentication'],
            400);

        $this->listeners[] = $sharedEvents->attach(
            LoginAction::class,
            AuthenticationEvent::EVENT_AUTHENTICATION_AUTHENTICATE,
            [$this, 'postAuthentication'],
            -50
        );

        $this->listeners[] = $sharedEvents->attach(
            LogoutAction::class,
            AuthenticationEvent::EVENT_AUTHENTICATION_LOGOUT,
            [$this, 'onLogout'],
            100
        );
    }

    /**
     * We'll use this to inject our form into the event, consequently into the template
     *
     * @param AuthenticationEvent $e
     */
    public function injectData(AuthenticationEvent $e)
    {
        $form = $this->loginForm;
        $data = $this->flashMessenger->getData('loginFormData') ?: [];
        $messages = $this->flashMessenger->getData('loginFormMessages') ?: [];

        $form->setData($data);
        $form->setMessages($messages);

        $e->setParam('form', $form);
    }

    /**
     * Pre authentication listener that happens before the default authentication
     * Can be used to prepare some data for the form and pre-validate data
     *
     * @param AuthenticationEvent $e
     */
    public function preAuthentication(AuthenticationEvent $e)
    {
        //we can leverage the default authentication flow
        //we will inject the form into the template, and validate data on POST
        //in case of errors, we set them on the event object
        $request = $e->getRequest();
        $form = $e->getParam('form', null);

        if ($request->getMethod() === 'POST') {
            $data = $request->getParsedBody();

            if ($form instanceof Form) {
                $form->setData($data);

                if (!$form->isValid()) {
                    $messages = [];
                    foreach ($form->getMessages() as $message) {
                        $messages[] = $message;
                    }
                    $e->setError($messages);

                    $this->flashMessenger->addData('loginFormData', $data);
                    $this->flashMessenger->addData('loginFormMessages', $form->getMessages());
                    return;
                }

                $data = array_merge($e->getParams(), $data, $form->getData());
                $e->setParams($data);
            }
        }
    }

    /**
     * In case authentication result is invalid, store form data and messages in session
     *
     * @param AuthenticationEvent $e<commit_msg>minor fix<commit_after><?php
/**
 * @copyright: DotKernel
 * @library: dotkernel/dot-user
 * @author: n3vrax
 * Date: 6/21/2016
 * Time: 10:45 PM
 */

namespace Dot\User\Listener;

use Dot\Authentication\AuthenticationInterface;
use Dot\Authentication\AuthenticationResult;
use Dot\Authentication\Web\Action\LoginAction;
use Dot\Authentication\Web\Action\LogoutAction;
use Dot\Authentication\Web\Event\AuthenticationEvent;
use Dot\FlashMessenger\FlashMessengerInterface;
use Dot\User\Entity\UserEntityInterface;
use Dot\User\Options\MessagesOptions;
use Dot\User\Options\UserOptions;
use Dot\User\Service\UserServiceInterface;
use Zend\EventManager\AbstractListenerAggregate;
use Zend\EventManager\EventManagerInterface;
use Zend\Form\Form;

/**
 * Class AuthenticationListener
 * @package Dot\User\Listener
 */
class AuthenticationListener extends AbstractListenerAggregate
{
    /** @var  Form */
    protected $loginForm;

    /** @var  FlashMessengerInterface */
    protected $flashMessenger;

    /** @var  UserServiceInterface */
    protected $userService;

    /** @var  UserOptions */
    protected $options;

    public function __construct(
        Form $form,
        FlashMessengerInterface $flashMessenger,
        UserServiceInterface $userService,
        UserOptions $options
    ) {
        $this->loginForm = $form;
        $this->flashMessenger = $flashMessenger;
        $this->userService = $userService;
        $this->options = $options;
    }

    /**
     * @param EventManagerInterface $events
     * @param int $priority
     */
    public function attach(EventManagerInterface $events, $priority = 1)
    {
        $sharedEvents = $events->getSharedManager();

        //this will be called after the default prepare listener and the actual authentication call
        //use it for additional checks and data insertions into template
        $this->listeners[] = $sharedEvents->attach(
            LoginAction::class,
            AuthenticationEvent::EVENT_AUTHENTICATION_AUTHENTICATE,
            [$this, 'injectData'],
            500);

        $this->listeners[] = $sharedEvents->attach(
            LoginAction::class,
            AuthenticationEvent::EVENT_AUTHENTICATION_AUTHENTICATE,
            [$this, 'preAuthentication'],
            400);

        $this->listeners[] = $sharedEvents->attach(
            LoginAction::class,
            AuthenticationEvent::EVENT_AUTHENTICATION_AUTHENTICATE,
            [$this, 'postAuthentication'],
            -50
        );

        $this->listeners[] = $sharedEvents->attach(
            LogoutAction::class,
            AuthenticationEvent::EVENT_AUTHENTICATION_LOGOUT,
            [$this, 'onLogout'],
            100
        );
    }

    /**
     * We'll use this to inject our form into the event, consequently into the template
     *
     * @param AuthenticationEvent $e
     */
    public function injectData(AuthenticationEvent $e)
    {
        $form = $this->loginForm;
        $data = $this->flashMessenger->getData('loginFormData') ?: [];
        $messages = $this->flashMessenger->getData('loginFormMessages') ?: [];

        $form->setData($data);
        $form->setMessages($messages);

        $e->setParam('form', $form);
    }

    /**
     * Pre authentication listener that happens before the default authentication
     * Can be used to prepare some data for the form and pre-validate data
     *
     * @param AuthenticationEvent $e
     */
    public function preAuthentication(AuthenticationEvent $e)
    {
        //we can leverage the default authentication flow
        //we will inject the form into the template, and validate data on POST
        //in case of errors, we set them on the event object
        $request = $e->getRequest();
        $form = $e->getParam('form', null);

        if ($request->getMethod() === 'POST') {
            $data = $request->getParsedBody();

            if ($form instanceof Form) {
                $form->setData($data);

                if (!$form->isValid()) {
                    $messages = [];
                    foreach ($form->getMessages() as $message) {
                        $messages[] = current($message);
                    }
                    $e->setError($messages);

                    $this->flashMessenger->addData('loginFormData', $data);
                    $this->flashMessenger->addData('loginFormMessages', $form->getMessages());
                    return;
                }

                $data = array_merge($e->getParams(), $data, $form->getData());
                $e->setParams($data);
            }
        }
    }

    /**
     * In case authentication result is invalid, store form data and messages in session
     *
     * @param AuthenticationEvent $e
{'fraction_non_alphanumeric': 0.11578614362543499, 'fraction_numerical': 0.00506168933881683, 'mean_word_length': 2.3774928774928776, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 77, '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': '5550028', 'n_tokens_mistral': 2582, 'n_tokens_neox': 2414, 'n_words': 582}