Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.62% covered (warning)
84.62%
33 / 39
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
LegalInformationPolicy
84.62% covered (warning)
84.62%
33 / 39
50.00% covered (danger)
50.00%
1 / 2
12.52
0.00% covered (danger)
0.00%
0 / 1
 keys
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 get
83.33% covered (warning)
83.33%
30 / 36
0.00% covered (danger)
0.00%
0 / 1
11.56
1<?php
2
3declare(strict_types=1);
4/**
5 * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6 * SPDX-License-Identifier: AGPL-3.0-or-later
7 */
8
9namespace OCA\Libresign\Service\Policy\Provider\LegalInformation;
10
11use OCA\Libresign\Service\Policy\Contract\IPolicyDefinition;
12use OCA\Libresign\Service\Policy\Contract\IPolicyDefinitionProvider;
13use OCA\Libresign\Service\Policy\Model\PolicyContext;
14use OCA\Libresign\Service\Policy\Model\PolicyLayer;
15use OCA\Libresign\Service\Policy\Model\PolicySpec;
16use OCA\Libresign\Service\Policy\Provider\Helper\DelegationLayerHelper;
17use OCA\Libresign\Service\Policy\Provider\Helper\PolicyKeyNormalizer;
18
19final class LegalInformationPolicy implements IPolicyDefinitionProvider {
20    public const KEY = 'legal_information';
21    public const SYSTEM_APP_CONFIG_KEY = 'legal_information';
22
23    #[\Override]
24    public function keys(): array {
25        return [
26            self::KEY,
27        ];
28    }
29
30    #[\Override]
31    public function get(string|\BackedEnum $policyKey): IPolicyDefinition {
32        return match (PolicyKeyNormalizer::normalize($policyKey)) {
33            self::KEY => new PolicySpec(
34                key: self::KEY,
35                defaultSystemValue: '',
36                allowedValues: [],
37                normalizer: static fn (mixed $rawValue): string => (string)$rawValue,
38                appConfigKey: self::SYSTEM_APP_CONFIG_KEY,
39                supportsRequestOverride: true,
40                requestLifecycle: PolicySpec::LIFECYCLE_REQUEST_SNAPSHOT,
41                groupPolicyManager: static function (PolicyContext $context, ?PolicyLayer $systemPolicy, array $groupLayers): bool {
42                    $actorRole = $context->getActorRole();
43                    if ($actorRole->canManageSystemPolicies) {
44                        return true;
45                    }
46
47                    if (!$actorRole->canManageGroupPolicies) {
48                        return false;
49                    }
50
51                    if ($actorRole->manageableGroupCount < 1) {
52                        return false;
53                    }
54
55                    return DelegationLayerHelper::hasExplicitGlobalDelegation($systemPolicy)
56                        || DelegationLayerHelper::hasSystemCreatedGroupDelegation($groupLayers);
57                },
58                systemCreatedGroupRuleEditor: static function (PolicyContext $context, ?PolicyLayer $systemPolicy, PolicyLayer $existingPolicy): bool {
59                    $actorRole = $context->getActorRole();
60                    if ($actorRole->canManageSystemPolicies) {
61                        return true;
62                    }
63
64                    if (!$actorRole->canManageGroupPolicies) {
65                        return false;
66                    }
67
68                    if (!$existingPolicy->isVisibleToChild() || !$existingPolicy->isAllowChildOverride() || $existingPolicy->getValue() === null) {
69                        return false;
70                    }
71
72                    if (DelegationLayerHelper::hasExplicitGlobalDelegation($systemPolicy)) {
73                        return true;
74                    }
75
76                    return $existingPolicy->isCreatedBySystemAdmin();
77                },
78                supportsGroupAdminDelegation: true,
79            ),
80            default => throw new \InvalidArgumentException('Unknown policy key: ' . PolicyKeyNormalizer::normalize($policyKey)),
81        };
82    }
83
84}