-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthPresenterTrait.php
More file actions
190 lines (163 loc) · 6.2 KB
/
Copy pathAuthPresenterTrait.php
File metadata and controls
190 lines (163 loc) · 6.2 KB
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<?php
namespace ADT\FancyAdmin\UI\Presenters;
use ADT\FancyAdmin\DI\Injects\AccountQueryFactoryInject;
use ADT\FancyAdmin\DI\Injects\AuthenticatorInject;
use ADT\FancyAdmin\DI\Injects\EntityManagerInject;
use ADT\FancyAdmin\DI\Injects\FancyAdminInject;
use ADT\FancyAdmin\DI\Injects\LinkGeneratorInject;
use ADT\FancyAdmin\DI\Injects\SecurityUserInject;
use ADT\FancyAdmin\Model\Entities\File;
use ADT\FancyAdmin\Model\Menu\NavbarMenuFactory;
use ADT\FancyAdmin\Model\Menu\UserMenuFactory;
use ADT\FancyAdmin\UI\Components\Forms\SelectAccount\SelectAccountForm;
use ADT\FancyAdmin\UI\Components\Forms\SelectAccount\SelectAccountFormFactory;
use Nette\Application\AbortException;
use Nette\Application\Attributes\Persistent;
use Nette\Application\ForbiddenRequestException;
use Nette\Application\UI\InvalidLinkException;
use Nette\Http\FileUpload;
use Nette\Security\AuthenticationException;
use ReflectionClass;
use ReflectionException;
trait AuthPresenterTrait
{
use PresenterTrait;
use EntityManagerInject;
use LinkGeneratorInject;
use AccountQueryFactoryInject;
use FancyAdminInject;
use AuthenticatorInject;
use SecurityUserInject;
#[Persistent]
public ?int $selectedAccount = null;
#[Persistent]
public ?string $gridFilterClass = null;
#[Persistent]
public array $gridFilterParameters = [];
/**
* @throws InvalidLinkException
*/
protected function startup(): void
{
parent::startup();
if ($token = $this->getParameter('token')) {
try {
$this->_securityUser->login($token, context: 'cashdesk'); // TODO enum
$this->redirect('this');
} catch (AuthenticationException) {}
}
if (!$this->getUser()->isLoggedIn()) {
$parameters = array_merge($this->request->getParameters());
unset($parameters['token']);
$this->request->setParameters(array_merge($this->request->getParameters()));
$this->redirect(':Portal:Sign:in', ['backlink' => $this->storeRequest()]);
}
if ($this->getParameter('selectedAccount')) {
$this->getUser()->getIdentity()->setSelectedAccount($this->_accountQueryFactory->create()->disableAccountFilter()->byId($this->getParameter('selectedAccount'))->fetchOne());
$this->em->flush();
} elseif ($this->getUser()->isAllowed($this->_fancyAdmin->getBackofficeAclResource())) {
$this->getUser()->getIdentity()->setSelectedAccount(null);
$this->em->flush();
} elseif ($this->getUser()->getIdentity()->getSelectedAccount()) {
// TODO odstranit
try {
$this->redirect(':PortalCustomer:Home:', ['selectedAccount' => $this->getUser()->getIdentity()->getSelectedAccount()->getId()]);
} catch (InvalidLinkException) {
$this->redirect(':Portal:Home:', ['selectedAccount' => $this->getUser()->getIdentity()->getSelectedAccount()->getId()]);
}
} else {
// TODO odstranit
try {
$this->redirect(':PortalCustomer:Home:', ['selectedAccount' => $this->getUser()->getIdentity()->getAccounts()[0]->getId()]);
} catch (InvalidLinkException) {
$this->redirect(':Portal:Home:', ['selectedAccount' => $this->getUser()->getIdentity()->getAccounts()[0]->getId()]);
}
}
// TODO delame kvuli ublaboo datagridu ktery potrebuje sessionu uz pri vykresleni
$this->getSession()->start();
$this->primaryTemplate = true;
}
/**
* @throws ReflectionException
* @throws ForbiddenRequestException
*/
public function checkRequirements($element): void
{
parent::checkRequirements($element);
if ($this->getUser()->isLoggedIn()) {
$this->validateSecurityAttributes();
}
}
/**
* @throws ReflectionException
* @throws ForbiddenRequestException
*/
private function validateSecurityAttributes(): void
{
$reflection = new ReflectionClass($this->getPresenter()::class);
$reflectionMethod = $reflection->getMethod(static::ActionKey . ucfirst($this->getAction()));
foreach ($reflectionMethod->getAttributes() as $attribute) {
if ($attribute->getName() === SecurityCheckAttribute::class) {
$attributeInstance = $attribute->newInstance();
$aclResourceName = $attributeInstance->getResourceName();
if (!$this->getUser()->isAllowed($aclResourceName)) {
throw new ForbiddenRequestException();
}
}
}
}
public function handleEditGridFilter(): void
{
$this->redrawSidePanel('gridFilter');
}
public function handleRemoveGridFilter(): void
{
if ($gridFilter = $this->gridFilterQueryFactory->create()->byId($this->getParameter('removeId'))->fetchOneOrNull()) {
$this->_em->remove($gridFilter);
$this->_em->flush();
}
}
public function handleSummernoteUpload(): void
{
/** @var FileUpload[] $files */
$files = $this->getHttpRequest()->getFiles();
if (!isset($files['file'])) {
$this->payload->status = false;
$this->sendPayload();
}
$fileEntityClass = $this->_em->findEntityClassByInterface(File::class);
/** @var File $fileEntity */
$fileEntity = (new $fileEntityClass)
->setTemporaryFile($files['file']->getTemporaryFile(), $files['file']->getUntrustedName());;
$this->_em->persist($fileEntity);
$this->_em->flush();
$this->payload->status = true;
$this->payload->filename = $fileEntity->getUrl();
}
public function beforeRender(): void
{
parent::beforeRender();
$submodule = str_replace('Portal', '', explode(':', $this->name)[0]);
$className = "\\App\\UI\\Portal\\{$submodule}\\Presenters\\NavbarMenuFactory";
$userMenuClassName = "\\App\\UI\\Portal\\{$submodule}\\Presenters\\UserMenuFactory";
/** @var NavbarMenuFactory $className */
$navbarMenuFactory = new $className();
/** @var UserMenuFactory $userMenuFactory */
$userMenuFactory = new $userMenuClassName();
$this->getTemplate()->navbarMenu = $navbarMenuFactory->create()->setLinkGenerator($this->_linkGenerator);
$this->getTemplate()->userMenu = $userMenuFactory->create()->setLinkGenerator($this->_linkGenerator);
$this->getTemplate()->summernoteUpload = $this->getPresenter()->link('summernoteUpload!');
}
/**
* @throws AbortException
*/
public function redrawSidePanel(?string $name = null): never
{
$this->getPresenter()->payload->snippets[$this->getSnippetId('sidePanel')] = $this[$name ? $name . ucfirst('sidePanel') : 'sidePanel']->renderToString();
$this->getPresenter()->sendPayload();
}
public function createComponentSelectAccountForm(SelectAccountFormFactory $factory): SelectAccountForm
{
return $factory->create();
}
}