<commit_before><?php
/**
 * Authserver, an OAuth2-based single-signon authentication provider written in PHP.
 *
 * Copyright (C) $today.date  <NAME>
 *
 * his program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace Registration\EventListener;

use Registration\RegistrationEvents;
use Registration\RegistrationHandler\RegistrationRules;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Mail\PrimedTwigMailer;
use Doctrine\ORM\EntityManagerInterface;
use Braincrafted\Bundle\BootstrapBundle\Session\FlashMessage;
use Registration\Event\RegistrationHandleEvent;

class RegistrationHandlerListener implements EventSubscriberInterface
{

    /**
     *
     * @var RegistrationRules
     */
    private $registrationRules;

    /**
     *
     * @var PrimedTwigMailer
     */
    private $mailer;

    /**
     *
     * @var EntityManagerInterface
     */
    private $em;

    /**
     *
     * @var FlashMessage
     */
    private $flashMessage;

    public static function getSubscribedEvents()
    {
        return [
            RegistrationEvents::HANDLE_FORM => [
                [
                    'handleFormRegistrationRules',
                    100
                ],
                [
                    'handleFormPersist',
                    -10
                ],
                [
                    'handleFormMail',
                    -100
                ],
                [
                    'handleFormFlushData',
                    -200
                ]

            ]
        ];
    }

    public function __construct(RegistrationRules $registrationRules, PrimedTwigMailer $mailer, EntityManagerInterface $em, FlashMessage $flashMessage)
    {
        $this->registrationRules = $registrationRules;
        $this->mailer = $mailer;
        $this->em = $em;
        $this->flashMessage = $flashMessage;
    }

    public function handleFormRegistrationRules(RegistrationHandleEvent $event)
    {
        if ($event->isFailed())
            return;
        $user = $event->getForm()->getData();
        if (!$user)
            return;
        /* @var $user User */
        $emailAddress = $user->getPrimaryEmailAddress()->getEmail();
        $registrationRule = $this->registrationRules->getFirstRuleMatching($emailAddress);
        if (!$registrationRule || !$registrationRule->isSelfRegistration()) {
            $this->flashMessage->error('Self-registration is not allowed with this email address.');
            $event->setFailed();
        }

        $user->setEnabled($registrationRule->isAutoActivate());
    }

    public function handleFormPersist(RegistrationHandleEvent $event)
    {
        if ($event->isFailed())
            return;
        $user = $event->getForm()->getData();
        if (!$user)
            return;
        /* @var $user User */

        $this->em->persist($user);
        $this->em->flush();
    }<commit_msg>[Registration] Stop processing when registration rule refuses self registration<commit_after><?php
/**
 * Authserver, an OAuth2-based single-signon authentication provider written in PHP.
 *
 * Copyright (C) $today.date  <NAME>
 *
 * his program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
namespace Registration\EventListener;

use Registration\RegistrationEvents;
use Registration\RegistrationHandler\RegistrationRules;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use App\Mail\PrimedTwigMailer;
use Doctrine\ORM\EntityManagerInterface;
use Braincrafted\Bundle\BootstrapBundle\Session\FlashMessage;
use Registration\Event\RegistrationHandleEvent;

class RegistrationHandlerListener implements EventSubscriberInterface
{

    /**
     *
     * @var RegistrationRules
     */
    private $registrationRules;

    /**
     *
     * @var PrimedTwigMailer
     */
    private $mailer;

    /**
     *
     * @var EntityManagerInterface
     */
    private $em;

    /**
     *
     * @var FlashMessage
     */
    private $flashMessage;

    public static function getSubscribedEvents()
    {
        return [
            RegistrationEvents::HANDLE_FORM => [
                [
                    'handleFormRegistrationRules',
                    100
                ],
                [
                    'handleFormPersist',
                    -10
                ],
                [
                    'handleFormMail',
                    -100
                ],
                [
                    'handleFormFlushData',
                    -200
                ]

            ]
        ];
    }

    public function __construct(RegistrationRules $registrationRules, PrimedTwigMailer $mailer, EntityManagerInterface $em, FlashMessage $flashMessage)
    {
        $this->registrationRules = $registrationRules;
        $this->mailer = $mailer;
        $this->em = $em;
        $this->flashMessage = $flashMessage;
    }

    public function handleFormRegistrationRules(RegistrationHandleEvent $event)
    {
        if ($event->isFailed())
            return;
        $user = $event->getForm()->getData();
        if (!$user)
            return;
        /* @var $user User */
        $emailAddress = $user->getPrimaryEmailAddress()->getEmail();
        $registrationRule = $this->registrationRules->getFirstRuleMatching($emailAddress);
        if (!$registrationRule || !$registrationRule->isSelfRegistration()) {
            $this->flashMessage->error('Self-registration is not allowed with this email address.');
            $event->setFailed();
            return;
        }

        $user->setEnabled($registrationRule->isAutoActivate());
    }

    public function handleFormPersist(RegistrationHandleEvent $event)
    {
        if ($event->isFailed())
            return;
        $user = $event->getForm()->getData();
        if (!$user)
            return;
        /* @var $user User */

        $this->em->persist($user);
        $this->em->flush();
    }
{'fraction_non_alphanumeric': 0.09994377284228283, 'fraction_numerical': 0.003654765251616531, 'mean_word_length': 2.193447037701975, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '137066', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1738, 'n_words': 475}