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
|
|
|
|
15
|
|
|
namespace SWP\Bundle\MultiTenancyBundle\EventListener; |
16
|
|
|
|
17
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
18
|
|
|
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents; |
19
|
|
|
use SWP\Component\MultiTenancy\Context\TenantContextInterface; |
20
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
21
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* TenantableListener class. |
25
|
|
|
* |
26
|
|
|
* It makes sure all SELECT queries are tenant aware. |
27
|
|
|
*/ |
28
|
|
|
class TenantableListener implements EventSubscriberInterface |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var TenantContextInterface |
32
|
|
|
*/ |
33
|
|
|
protected $tenantContext; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var EntityManagerInterface |
37
|
|
|
*/ |
38
|
|
|
protected $entityManager; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Construct. |
42
|
|
|
* |
43
|
|
|
* @param EntityManagerInterface $entityManager |
44
|
|
|
* @param TenantContextInterface $tenantContext |
45
|
|
|
*/ |
46
|
|
|
public function __construct(EntityManagerInterface $entityManager, TenantContextInterface $tenantContext) |
47
|
|
|
{ |
48
|
|
|
$this->entityManager = $entityManager; |
49
|
|
|
$this->tenantContext = $tenantContext; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Enables tenantable filter on kernel.request. |
54
|
|
|
*/ |
55
|
|
|
public function enable() |
56
|
|
|
{ |
57
|
|
|
$tenant = $this->tenantContext->getTenant(); |
58
|
|
|
|
59
|
|
|
if ($tenant && $tenant->getId()) { |
60
|
|
|
$this->entityManager |
61
|
|
|
->getFilters() |
62
|
|
|
->enable('tenantable') |
63
|
|
|
->setParameter('tenantCode', $tenant->getCode()); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Disabled tenantable filter. |
69
|
|
|
*/ |
70
|
|
|
public function disable() |
71
|
|
|
{ |
72
|
|
|
$filters = $this->entityManager->getFilters(); |
73
|
|
|
|
74
|
|
|
if ($filters->isEnabled('tenantable')) { |
75
|
|
|
$filters->disable('tenantable'); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
public static function getSubscribedEvents() |
83
|
|
|
{ |
84
|
|
|
return [ |
85
|
|
|
KernelEvents::REQUEST => 'enable', |
86
|
|
|
MultiTenancyEvents::TENANTABLE_ENABLE => 'enable', |
87
|
|
|
MultiTenancyEvents::TENANTABLE_DISABLE => 'disable', |
88
|
|
|
]; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|