You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
tos/EventListener/RegistrationListener.php

75 lines
2.1 KiB

<?php
namespace vierbergenlars\AuthserverTosBundle\EventListener;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Registration\RegistrationEvents;
use Registration\Event\RegistrationFormEvent;
use Registration\Event\RegistrationHandleEvent;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Validator\Constraints\IsTrue;
use vierbergenlars\AuthserverTosBundle\Entity\UserTos;
use Doctrine\ORM\EntityManagerInterface;
class RegistrationListener implements EventSubscriberInterface
{
private $tosUrl;
private $tosVersion;
/**
*
* @var EntityManagerInterface
*/
private $em;
public static function getSubscribedEvents()
{
return [
RegistrationEvents::BUILD_FORM => [
'onBuildForm',
-200
],
RegistrationEvents::HANDLE_FORM => [
'onHandleForm',
-20 // After persisting user
]
];
}
public function __construct($tosUrl, $tosVersion, EntityManagerInterface $em)
{
$this->tosUrl = $tosUrl;
$this->tosVersion = $tosVersion;
$this->em = $em;
}
public function onBuildForm(RegistrationFormEvent $event)
{
$event->getFormBuilder()->add('vl_tos_accept', CheckboxType::class, [
'label' => 'I accept the <a href="' . htmlentities($this->tosUrl, ENT_HTML5 | ENT_QUOTES) . '">terms of service</a>',
'mapped' => false,
'attr' => [
'align_with_widget' => true
],
'constraints' => [
new IsTrue([
'message' => 'You must accept the terms of service.'
])
]
]);
}
public function onHandleForm(RegistrationHandleEvent $event)
{
if ($event->getForm()
->get('vl_tos_accept')
->getData()) {
$user = $event->getForm()->getData();
/* @var $user \App\Entity\User */
$tosUser = new UserTos($user);
$tosUser->setAcceptedVersion($this->tosVersion);
$this->em->persist($tosUser);
}
}
}