Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DefaultUserFolderPolicy
100.00% covered (success)
100.00%
42 / 42
100.00% covered (success)
100.00%
2 / 2
13
100.00% covered (success)
100.00%
1 / 1
 keys
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
39 / 39
100.00% covered (success)
100.00%
1 / 1
12
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\DefaultUserFolder;
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 DefaultUserFolderPolicy implements IPolicyDefinitionProvider {
20    public const KEY = 'default_user_folder';
21    public const SYSTEM_APP_CONFIG_KEY = 'default_user_folder';
22    public const DEFAULT_FOLDER = 'LibreSign';
23
24    #[\Override]
25    public function keys(): array {
26        return [
27            self::KEY,
28        ];
29    }
30
31    #[\Override]
32    public function get(string|\BackedEnum $policyKey): IPolicyDefinition {
33        return match (PolicyKeyNormalizer::normalize($policyKey)) {
34            self::KEY => new PolicySpec(
35                key: self::KEY,
36                defaultSystemValue: self::DEFAULT_FOLDER,
37                allowedValues: [],
38                normalizer: static function (mixed $rawValue): string {
39                    $candidate = trim((string)$rawValue);
40                    return $candidate !== '' ? $candidate : self::DEFAULT_FOLDER;
41                },
42                appConfigKey: self::SYSTEM_APP_CONFIG_KEY,
43                supportsRequestOverride: true,
44                requestLifecycle: PolicySpec::LIFECYCLE_RUNTIME,
45                groupPolicyManager: static function (PolicyContext $context, ?PolicyLayer $systemPolicy, array $groupLayers): bool {
46                    $actorRole = $context->getActorRole();
47                    if ($actorRole->canManageSystemPolicies) {
48                        return true;
49                    }
50
51                    if (!$actorRole->canManageGroupPolicies) {
52                        return false;
53                    }
54
55                    if ($actorRole->manageableGroupCount < 1) {
56                        return false;
57                    }
58
59                    return DelegationLayerHelper::hasExplicitGlobalDelegation($systemPolicy)
60                        || DelegationLayerHelper::hasSystemCreatedGroupDelegation($groupLayers);
61                },
62                systemCreatedGroupRuleEditor: static function (PolicyContext $context, ?PolicyLayer $systemPolicy, PolicyLayer $existingPolicy): bool {
63                    $actorRole = $context->getActorRole();
64                    if ($actorRole->canManageSystemPolicies) {
65                        return true;
66                    }
67
68                    if (!$actorRole->canManageGroupPolicies) {
69                        return false;
70                    }
71
72                    if (!$existingPolicy->isVisibleToChild() || !$existingPolicy->isAllowChildOverride() || $existingPolicy->getValue() === null) {
73                        return false;
74                    }
75
76                    if (DelegationLayerHelper::hasExplicitGlobalDelegation($systemPolicy)) {
77                        return true;
78                    }
79
80                    return $existingPolicy->isCreatedBySystemAdmin();
81                },
82                supportsGroupAdminDelegation: true,
83            ),
84            default => throw new \InvalidArgumentException('Unknown policy key: ' . PolicyKeyNormalizer::normalize($policyKey)),
85        };
86    }
87
88}