|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Tahoe\Bundle\MultiTenancyBundle\EventSubscriber; |
|
4
|
|
|
|
|
5
|
|
|
use FOS\UserBundle\FOSUserEvents; |
|
6
|
|
|
use FOS\UserBundle\Event\FilterUserResponseEvent; |
|
7
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
8
|
|
|
use FOS\UserBundle\Event\FormEvent; |
|
9
|
|
|
use Symfony\Component\Form\FormInterface; |
|
10
|
|
|
use Symfony\Component\HttpFoundation\RedirectResponse; |
|
11
|
|
|
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
|
12
|
|
|
use Tahoe\Bundle\MultiTenancyBundle\Manager\RegistrationManager; |
|
13
|
|
|
use Tahoe\Bundle\MultiTenancyBundle\Service\TenantAwareRouter; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Class RegistrationSubscriber |
|
17
|
|
|
* |
|
18
|
|
|
* Responsible for creating tenant during registration, it also add just created user as an tenant admin |
|
19
|
|
|
* |
|
20
|
|
|
* @author Konrad Podgórski <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class RegistrationSubscriber implements EventSubscriberInterface, RegistrationSubscriberInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var FormInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $_form; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var RedirectResponse |
|
31
|
|
|
*/ |
|
32
|
|
|
protected $redirectResponse; |
|
33
|
|
|
|
|
34
|
|
|
protected $registrationManager; |
|
35
|
|
|
|
|
36
|
|
|
protected $tenantAwareRouter; |
|
37
|
|
|
|
|
38
|
|
|
function __construct(RegistrationManager $registrationManager) |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
$this->registrationManager = $registrationManager; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
public function setRouter($router) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->tenantAwareRouter = $router; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritDoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function getSubscribedEvents() |
|
52
|
|
|
{ |
|
53
|
|
|
return array( |
|
54
|
|
|
FOSUserEvents::REGISTRATION_SUCCESS => 'onRegistrationSuccess', |
|
55
|
|
|
FOSUserEvents::REGISTRATION_COMPLETED => 'onRegistrationCompleted', |
|
56
|
|
|
); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Used only to get form reference because it's not available in the next event, onRegistrationCompleted |
|
61
|
|
|
* @param FormEvent $event |
|
62
|
|
|
*/ |
|
63
|
|
|
public function onRegistrationSuccess(FormEvent $event) |
|
64
|
|
|
{ |
|
65
|
|
|
/** |
|
66
|
|
|
* Disclaimer: Subscriber does all it's magic in onRegistrationCompleted method, |
|
67
|
|
|
* however in onRegistrationCompleted we don't have access form (so we can get tenant name and subdomain) |
|
68
|
|
|
* and http response (so we can redirect user to his new tenant instance) |
|
69
|
|
|
* |
|
70
|
|
|
* That's why we are using other event that is fired before onRegistrationCompleted and we grab references to |
|
71
|
|
|
* form and response objects that will be used in that next event. |
|
72
|
|
|
*/ |
|
73
|
|
|
|
|
74
|
|
|
$this->_form = $event->getForm(); |
|
75
|
|
|
|
|
76
|
|
|
// we get reference to the redirect response that will be used in another event |
|
77
|
|
|
$this->redirectResponse = new RedirectResponse('dummy'); |
|
78
|
|
|
// FOS User Registration controller check if response is set in event, if so it will just use it. |
|
79
|
|
|
$event->setResponse($this->redirectResponse); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function onRegistrationCompleted(FilterUserResponseEvent $event) |
|
83
|
|
|
{ |
|
84
|
|
|
$user = $event->getUser(); |
|
85
|
|
|
$tenantName = $this->_form->get('tenantName')->getData(); |
|
86
|
|
|
$tenantSubdomain = $this->_form->get('tenantSubdomain')->getData(); |
|
87
|
|
|
|
|
88
|
|
|
$tenant = $this->registrationManager->createTenant($user, $tenantName, $tenantSubdomain); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
// this referenced redirect response will be used |
|
91
|
|
|
$this->redirectResponse->setTargetUrl($this->tenantAwareRouter->generateUrl($tenant)); |
|
92
|
|
|
|
|
93
|
|
|
unset($this->_form); |
|
94
|
|
|
} |
|
95
|
|
|
} |
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.