Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
96.30% covered (success)
96.30%
26 / 27
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
Admin
96.30% covered (success)
96.30%
26 / 27
80.00% covered (warning)
80.00%
4 / 5
6
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getForm
100.00% covered (success)
100.00%
20 / 20
100.00% covered (success)
100.00%
1 / 1
1
 getSection
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPriority
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getSignatureEngineInitialState
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2020-2024 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Settings;
10
11use OCA\Libresign\AppInfo\Application;
12use OCA\Libresign\Handler\CertificateEngine\CertificateEngineFactory;
13use OCA\Libresign\Service\AccountService;
14use OCA\Libresign\Service\CertificatePolicyService;
15use OCA\Libresign\Service\Policy\PolicyService;
16use OCP\AppFramework\Http\ContentSecurityPolicy;
17use OCP\AppFramework\Http\TemplateResponse;
18use OCP\AppFramework\Services\IInitialState;
19use OCP\IAppConfig;
20use OCP\IUserSession;
21use OCP\Settings\ISettings;
22use OCP\Util;
23
24class Admin implements ISettings {
25    public const PASSWORD_PLACEHOLDER = '••••••••';
26
27    public function __construct(
28        private IInitialState $initialState,
29        private AccountService $accountService,
30        private IUserSession $userSession,
31        private CertificateEngineFactory $certificateEngineFactory,
32        private CertificatePolicyService $certificatePolicyService,
33        private IAppConfig $appConfig,
34        private PolicyService $policyService,
35    ) {
36    }
37    #[\Override]
38    public function getForm(): TemplateResponse {
39        Util::addScript(Application::APP_ID, 'libresign-settings');
40        Util::addStyle(Application::APP_ID, 'libresign-settings');
41        $this->initialState->provideInitialState('config', $this->accountService->getConfig($this->userSession->getUser()));
42        $this->initialState->provideInitialState('certificate_engine', $this->certificateEngineFactory->getEngine()->getName());
43        $this->initialState->provideInitialState('certificate_policies_oid', $this->certificatePolicyService->getOid());
44        $this->initialState->provideInitialState('certificate_policies_cps', $this->certificatePolicyService->getCps());
45        $this->initialState->provideInitialState('config_path', $this->appConfig->getValueString(Application::APP_ID, 'config_path'));
46        $this->initialState->provideInitialState('signature_engine', $this->getSignatureEngineInitialState());
47        $this->initialState->provideInitialState('effective_policies', [
48            'policies' => $this->policyService->resolveKnownPolicyStatesWithRuleCounts(
49                $this->policyService->getAllRuleCounts(),
50            ),
51        ]);
52        $this->initialState->provideInitialState('ldap_extension_available', function_exists('ldap_connect'));
53
54        $response = new TemplateResponse(Application::APP_ID, 'admin_settings');
55        $policy = new ContentSecurityPolicy();
56        $policy->addAllowedWorkerSrcDomain("'self'");
57        $policy->addAllowedWorkerSrcDomain('blob:');
58        $response->setContentSecurityPolicy($policy);
59
60        return $response;
61    }
62
63    /**
64     * @psalm-return 'libresign'
65     */
66    #[\Override]
67    public function getSection(): string {
68        return Application::APP_ID;
69    }
70
71    /**
72     * @psalm-return 100
73     */
74    #[\Override]
75    public function getPriority(): int {
76        return 100;
77    }
78
79    private function getSignatureEngineInitialState(): string {
80        $engine = $this->appConfig->getValueString(Application::APP_ID, 'signature_engine', 'JSignPdf');
81        if ($engine === 'PhpNative') {
82            return $engine;
83        }
84        return 'JSignPdf';
85    }
86
87}