Do not prefill email address when it is rejected by email rules or not elegible for self-registration by registration rules

master
Lars Vierbergen 7 years ago
parent 089d8fa487
commit 46dfe73aa9
  1. 32
      EventListener/RegistrationHandlerListener.php
  2. 2
      Resources/config/services.xml

@ -28,6 +28,8 @@ use vierbergenlars\AuthserverOAuthAccountBundle\Entity\TemporaryUser;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use App\Entity\EmailAddress; use App\Entity\EmailAddress;
use vierbergenlars\AuthserverOAuthAccountBundle\ResourceOwner\ResourceOwnerMap; use vierbergenlars\AuthserverOAuthAccountBundle\ResourceOwner\ResourceOwnerMap;
use EmailRulesBundle\EmailHandler\EmailRules;
use Registration\RegistrationHandler\RegistrationRules;
class RegistrationHandlerListener implements EventSubscriberInterface class RegistrationHandlerListener implements EventSubscriberInterface
{ {
@ -50,6 +52,18 @@ class RegistrationHandlerListener implements EventSubscriberInterface
*/ */
private $resourceOwnerMap; private $resourceOwnerMap;
/**
*
* @var EmailRules|null
*/
private $emailRules;
/**
*
* @var RegistrationRules|null
*/
private $registrationRules;
public static function getSubscribedEvents() public static function getSubscribedEvents()
{ {
return [ return [
@ -64,11 +78,13 @@ class RegistrationHandlerListener implements EventSubscriberInterface
]; ];
} }
public function __construct(EntityManagerInterface $em, TokenStorageInterface $tokenStorage, ResourceOwnerMap $resourceOwnerMap) public function __construct(EntityManagerInterface $em, TokenStorageInterface $tokenStorage, ResourceOwnerMap $resourceOwnerMap, EmailRules $emailRules = null, RegistrationRules $registrationRules = null)
{ {
$this->em = $em; $this->em = $em;
$this->tokenStorage = $tokenStorage; $this->tokenStorage = $tokenStorage;
$this->resourceOwnerMap = $resourceOwnerMap; $this->resourceOwnerMap = $resourceOwnerMap;
$this->emailRules = $emailRules;
$this->registrationRules = $registrationRules;
} }
private function getTemporaryUser() private function getTemporaryUser()
@ -86,6 +102,20 @@ class RegistrationHandlerListener implements EventSubscriberInterface
{ {
if ($tempuser = $this->getTemporaryUser()) { if ($tempuser = $this->getTemporaryUser()) {
if ($tempuser->getEmail()) { if ($tempuser->getEmail()) {
if ($this->emailRules) {
$rule = $this->emailRules->getFirstRuleMatching($tempuser->getEmail());
if ($rule && $rule->isReject()) {
// Do not fill in an email address that will get rejected anyways.
return;
}
}
if ($this->registrationRules) {
$rule = $this->registrationRules->getFirstRuleMatching($tempuser->getEmail());
if (!$rule || !$rule->isSelfRegistration()) {
// Do not fill in an email address that is not elegible for self registration.
return;
}
}
$user = $event->getFormBuilder()->getData(); $user = $event->getFormBuilder()->getData();
/* @var $user \App\Entity\User */ /* @var $user \App\Entity\User */
if (!$user->getPrimaryEmailAddress()) if (!$user->getPrimaryEmailAddress())

@ -47,6 +47,8 @@
<argument type="service" id="doctrine.orm.entity_manager" /> <argument type="service" id="doctrine.orm.entity_manager" />
<argument type="service" id="security.token_storage" /> <argument type="service" id="security.token_storage" />
<argument type="service" id="vierbergenlars.authserver_oauth_account.resource_owner_map" /> <argument type="service" id="vierbergenlars.authserver_oauth_account.resource_owner_map" />
<argument type="service" id="email_rules.rules" on-invalid="null" />
<argument type="service" id="registration.rules" on-invalid="null" />
<tag name="kernel.event_subscriber" /> <tag name="kernel.event_subscriber" />
</service> </service>
</services> </services>