|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Superdesk Web Publisher MultiTenancy Bundle. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright 2016 Sourcefabric z.u. and contributors. |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please see the |
|
9
|
|
|
* AUTHORS and LICENSE files distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* @copyright 2016 Sourcefabric z.ú. |
|
12
|
|
|
* @license http://www.superdesk.org/license |
|
13
|
|
|
*/ |
|
14
|
|
|
namespace SWP\MultiTenancyBundle\EventListener; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
17
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
18
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
19
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
20
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
|
21
|
|
|
use SWP\Component\MultiTenancy\Resolver\TenantResolverInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Tenant Listener class. |
|
25
|
|
|
* |
|
26
|
|
|
* It makes sure there is always tenant set in context and queries. |
|
27
|
|
|
* It resolves current tenant based on subdomain and sets the resolved |
|
28
|
|
|
* tenant in context so it can be available everywhere in the system. |
|
29
|
|
|
* Additionally it makes sure all SELECT queries are tenant aware. |
|
30
|
|
|
*/ |
|
31
|
|
|
class TenantListener implements EventSubscriberInterface |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var TenantContextInterface |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $tenantContext; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var TenantResolverInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $tenantResolver; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var EntityManagerInterface |
|
45
|
|
|
*/ |
|
46
|
|
|
protected $entityManager; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Construct. |
|
50
|
|
|
* |
|
51
|
|
|
* @param EntityManagerInterface $entityManager |
|
52
|
|
|
* @param TenantContextInterface $tenantContext |
|
53
|
|
|
* @param TenantResolverInterface $tenantResolver |
|
54
|
|
|
*/ |
|
55
|
|
|
public function __construct( |
|
56
|
|
|
EntityManagerInterface $entityManager, |
|
57
|
|
|
TenantContextInterface $tenantContext, |
|
58
|
|
|
TenantResolverInterface $tenantResolver |
|
59
|
|
|
) { |
|
60
|
|
|
$this->entityManager = $entityManager; |
|
61
|
|
|
$this->tenantContext = $tenantContext; |
|
62
|
|
|
$this->tenantResolver = $tenantResolver; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Resolve and set tenant on kernel.request. |
|
67
|
|
|
* |
|
68
|
|
|
* @param GetResponseEvent $event |
|
69
|
|
|
*/ |
|
70
|
|
|
public function onKernelRequest(GetResponseEvent $event) |
|
71
|
|
|
{ |
|
72
|
|
|
$tenant = $this->tenantContext->getTenant(); |
|
73
|
|
|
$tenantId = $tenant->getId(); |
|
74
|
|
|
if ($tenantId) { |
|
75
|
|
|
$this->entityManager |
|
76
|
|
|
->getFilters() |
|
77
|
|
|
->enable('tenantable') |
|
78
|
|
|
->setParameter('tenantId', $tenantId); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (null !== $tenantId) { |
|
82
|
|
|
return; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
$request = $event->getRequest(); |
|
86
|
|
|
$this->tenantContext->setTenant( |
|
87
|
|
|
$this->tenantResolver->resolve($request->getHost()) |
|
88
|
|
|
); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public static function getSubscribedEvents() |
|
95
|
|
|
{ |
|
96
|
|
|
return [ |
|
97
|
|
|
KernelEvents::REQUEST => 'onKernelRequest', |
|
98
|
|
|
]; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|