Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
91.30% |
21 / 23 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| TemplateLoader | |
91.30% |
21 / 23 |
|
75.00% |
3 / 4 |
9.05 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
5.15 | |||
| getInitialStatePayload | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| canRequestSign | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | /** |
| 5 | * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors |
| 6 | * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | */ |
| 8 | |
| 9 | namespace OCA\Libresign\Files; |
| 10 | |
| 11 | use OCA\Files\Event\LoadSidebar; |
| 12 | use OCA\Libresign\AppInfo\Application; |
| 13 | use OCA\Libresign\Exception\LibresignException; |
| 14 | use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory; |
| 15 | use OCA\Libresign\Service\AccountService; |
| 16 | use OCA\Libresign\Service\IdentifyMethodService; |
| 17 | use OCA\Libresign\Service\Policy\PolicyService; |
| 18 | use OCA\Libresign\Service\Validation\SigningRequestValidator; |
| 19 | use OCP\App\IAppManager; |
| 20 | use OCP\AppFramework\Services\IInitialState; |
| 21 | use OCP\EventDispatcher\Event; |
| 22 | use OCP\EventDispatcher\IEventListener; |
| 23 | use OCP\IRequest; |
| 24 | use OCP\IUserSession; |
| 25 | |
| 26 | /** |
| 27 | * @template-implements IEventListener<Event> |
| 28 | */ |
| 29 | class TemplateLoader implements IEventListener { |
| 30 | public function __construct( |
| 31 | private IRequest $request, |
| 32 | private IUserSession $userSession, |
| 33 | private AccountService $accountService, |
| 34 | private IInitialState $initialState, |
| 35 | private SigningRequestValidator $signingRequestValidator, |
| 36 | private IdentifyMethodService $identifyMethodService, |
| 37 | private CertificateEngineFactory $certificateEngineFactory, |
| 38 | private PolicyService $policyService, |
| 39 | private IAppManager $appManager, |
| 40 | private TemplateLoaderAssets $assets, |
| 41 | ) { |
| 42 | } |
| 43 | |
| 44 | #[\Override] |
| 45 | public function handle(Event $event): void { |
| 46 | if (!($event instanceof LoadSidebar)) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | if (!$this->appManager->isEnabledForUser('libresign')) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | $initialStatePayload = $this->getInitialStatePayload(); |
| 55 | foreach ($initialStatePayload as $key => $value) { |
| 56 | $this->initialState->provideInitialState($key, $value); |
| 57 | } |
| 58 | |
| 59 | if ($initialStatePayload['certificate_ok'] === true) { |
| 60 | $this->assets->addInitScript(Application::APP_ID, 'libresign-init'); |
| 61 | } |
| 62 | |
| 63 | $this->assets->addScript(Application::APP_ID, 'libresign-tab'); |
| 64 | $this->assets->addStyle(Application::APP_ID, 'libresign-tab'); |
| 65 | } |
| 66 | |
| 67 | protected function getInitialStatePayload(): array { |
| 68 | return [ |
| 69 | 'certificate_ok' => $this->certificateEngineFactory->getEngine()->isSetupOk(), |
| 70 | 'effective_policies' => [ |
| 71 | 'policies' => $this->policyService->resolveKnownPolicyStates(), |
| 72 | ], |
| 73 | 'can_request_sign' => $this->canRequestSign(), |
| 74 | ]; |
| 75 | } |
| 76 | |
| 77 | private function canRequestSign(): bool { |
| 78 | try { |
| 79 | $this->signingRequestValidator->canRequestSign($this->userSession->getUser()); |
| 80 | return true; |
| 81 | } catch (LibresignException) { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | } |