1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Tahoe\Bundle\MultiTenancyBundle\Controller; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\Controller; |
8
|
|
|
use Symfony\Component\HttpFoundation\Request; |
9
|
|
|
use Tahoe\Bundle\MultiTenancyBundle\Model\MultiTenantTenantInterface; |
10
|
|
|
use Tahoe\Bundle\MultiTenancyBundle\Model\MultiTenantUserInterface; |
11
|
|
|
use Tahoe\Bundle\MultiTenancyBundle\Service\TenantResolver; |
12
|
|
|
|
13
|
|
|
class TenantController extends Controller |
14
|
|
|
{ |
15
|
|
|
public function switchDropdownAction() |
16
|
|
|
{ |
17
|
|
|
$tenantRepository = $this->container->get('tahoe.multi_tenancy.tenant_user_repository'); |
18
|
|
|
|
19
|
|
|
$tenant = $this->container->get('tahoe.multi_tenancy.tenant_resolver')->getTenant(); |
20
|
|
|
|
21
|
|
|
$tenantUsers = $tenantRepository->findBy( |
22
|
|
|
array( |
23
|
|
|
'user' => $this->getUser() |
24
|
|
|
) |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
return $this->render( |
28
|
|
|
'TahoeMultiTenancyBundle:Tenant:_switch_dropdown.html.twig', |
29
|
|
|
array( |
30
|
|
|
'tenant' => $tenant, |
31
|
|
|
'tenantUsers' => $tenantUsers |
32
|
|
|
) |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function switchTenantAction($subdomain) |
37
|
|
|
{ |
38
|
|
|
/** @var MultiTenantUserInterface $user */ |
39
|
|
|
$user = $this->getUser(); |
40
|
|
|
$filters = $this->getDoctrine()->getManager()->getFilters(); |
|
|
|
|
41
|
|
|
|
42
|
|
|
// for a moment we need to disable tenant aware filter to fetch invitations from all tenants |
43
|
|
|
$filters->disable("tenantAware"); |
44
|
|
|
$tenant = $this->container->get('tenant_repository')->findOneBy(array( |
45
|
|
|
'subdomain' => $subdomain |
46
|
|
|
)); |
47
|
|
|
$filters->enable("tenantAware"); |
48
|
|
|
|
49
|
|
|
if ($this->get('tahoe.multi_tenancy.tenant_resolver')->getStrategy() == TenantResolver::STRATEGY_FIXED_SUBDOMAIN) { |
50
|
|
|
// we update the tenant in the user and we redirect to the dashboard |
51
|
|
|
$user->setActiveTenant($tenant); |
52
|
|
|
$this->get('doctrine.orm.entity_manager')->flush(); |
53
|
|
|
|
54
|
|
|
return $this->redirect($this->generateUrl('dashboard_index')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$url = $this->container |
58
|
|
|
->get('tahoe.multi_tenancy.tenant_aware_router')->generateUrl($tenant, 'dashboard_index'); |
59
|
|
|
|
60
|
|
|
return $this->redirect($url); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: