-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPortalContextHelper.php
160 lines (132 loc) · 5.34 KB
/
PortalContextHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <francois.pluchino@klipper.dev>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\Portal;
use Doctrine\Persistence\ManagerRegistry;
use Klipper\Component\DoctrineExtra\Util\RepositoryUtils;
use Klipper\Component\Portal\Entity\Repository\PortalUserRepositoryInterface;
use Klipper\Component\Portal\Model\PortalUserInterface;
use Klipper\Component\Security\Model\Traits\OrganizationalInterface;
use Klipper\Component\SecurityExtra\Helper\OrganizationalContextHelper;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* Portal Context helper.
*
* @author François Pluchino <francois.pluchino@klipper.dev>
*/
class PortalContextHelper
{
protected TokenStorageInterface $tokenStorage;
protected PortalManagerInterface $portalManager;
protected PortalContextInterface $context;
protected AuthorizationCheckerInterface $authChecker;
protected PortalUserRepositoryInterface $portalUserRepository;
protected ?OrganizationalContextHelper $orgContextHelper;
protected string $permissionName;
protected ?string $routeParameterName = null;
public function __construct(
TokenStorageInterface $tokenStorage,
ManagerRegistry $doctrine,
PortalManagerInterface $portalManager,
PortalContextInterface $context,
AuthorizationCheckerInterface $authChecker,
string $permissionName = 'back-office',
?OrganizationalContextHelper $orgContextHelper = null
) {
$this->tokenStorage = $tokenStorage;
/** @var PortalUserRepositoryInterface $portalUserRepo */
$portalUserRepo = RepositoryUtils::getRepository($doctrine, PortalUserInterface::class, PortalUserRepositoryInterface::class);
$this->portalUserRepository = $portalUserRepo;
$this->portalManager = $portalManager;
$this->context = $context;
$this->authChecker = $authChecker;
$this->permissionName = $permissionName;
$this->orgContextHelper = $orgContextHelper;
}
public function setRouteParameterName(string $name): void
{
$this->routeParameterName = $name;
}
public function injectContext(Request $request, bool $isPortalContext = false): void
{
if (null === $this->routeParameterName) {
return;
}
$attr = $request->attributes;
$portalName = $attr->get($this->routeParameterName.'_name');
$portal = $attr->get($this->routeParameterName, $portalName);
if (null === $portalName) {
$routeParams = $attr->get('_route_params', []);
$portalName = $routeParams[$portal] ?? false;
}
if ($isPortalContext && false === $portalName && !empty($portals = $this->portalManager->getAvailablePortals())) {
$portalName = $portals[0]->getName();
}
if (false !== $portalName && null !== $this->tokenStorage->getToken()) {
$this->setCurrentPortalUser($portalName);
}
if (0 !== strpos($attr->get('_route', ''), '_')
&& null !== $this->tokenStorage->getToken()
&& (($isPortalContext && null === $this->context->getCurrentPortalUser())
|| (!$isPortalContext && !$this->authChecker->isGranted('perm:'.$this->permissionName)))) {
throw new NotFoundHttpException();
}
if (null !== $this->orgContextHelper && null !== $portalUser = $this->context->getCurrentPortalUser()) {
if ($portalUser instanceof OrganizationalInterface && null !== $org = $portalUser->getOrganization()) {
$request->attributes->set('_organizational_name', $org->getName());
$this->orgContextHelper->injectContext($request);
}
}
}
/**
* Get the current portal user.
*
* @param string $portalName The current portal name
*/
public function getCurrentPortalUser(?string $portalName): ?PortalUserInterface
{
$portalUser = null;
if (null !== $portalName) {
$portalUser = $this->portalUserRepository->findCurrentPortalUserByPortalName(
$portalName,
$this->getUser()
);
}
return $portalUser;
}
/**
* Set the current portal user defined by portal name,
* in security portal context.
*
* @param string $portalName The current portal name
*/
public function setCurrentPortalUser(?string $portalName): void
{
$this->context->setCurrentPortalUser($this->getCurrentPortalUser($portalName));
}
/**
* Get a user from the Security Token Storage.
*
* @see TokenInterface::getUser()
*/
private function getUser(): ?UserInterface
{
$token = $this->tokenStorage->getToken();
$user = null;
if (null !== $token) {
$tUser = $token->getUser();
$user = $tUser instanceof UserInterface ? $tUser : null;
}
return $user;
}
}