Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.00% covered (warning)
84.00%
21 / 25
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ApprovalGroupsPolicy
84.00% covered (warning)
84.00%
21 / 25
0.00% covered (danger)
0.00%
0 / 2
4.07
0.00% covered (danger)
0.00%
0 / 1
 keys
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 get
95.45% covered (success)
95.45%
21 / 22
0.00% covered (danger)
0.00%
0 / 1
3
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\ApprovalGroups;
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\PolicySpec;
15use OCA\Libresign\Service\Policy\Provider\Helper\PolicyKeyNormalizer;
16
17final class ApprovalGroupsPolicy implements IPolicyDefinitionProvider {
18    public const KEY = 'approval_group';
19    public const SYSTEM_APP_CONFIG_KEY = self::KEY;
20
21    #[\Override]
22    public function keys(): array {
23        return [
24            self::KEY,
25        ];
26    }
27
28    #[\Override]
29    public function get(string|\BackedEnum $policyKey): IPolicyDefinition {
30        return match (PolicyKeyNormalizer::normalize($policyKey)) {
31            self::KEY => new PolicySpec(
32                key: self::KEY,
33                defaultSystemValue: ApprovalGroupsPolicyValue::encode(ApprovalGroupsPolicyValue::DEFAULT_GROUPS),
34                allowedValues: static fn (PolicyContext $context): array => [],
35                normalizer: static fn (mixed $rawValue): mixed => ApprovalGroupsPolicyValue::encode($rawValue),
36                validator: static function (mixed $value): void {
37                    if (!is_string($value)) {
38                        throw new \InvalidArgumentException('Invalid value for ' . self::KEY);
39                    }
40
41                    $decoded = ApprovalGroupsPolicyValue::decode($value);
42                    if ($decoded === []) {
43                        throw new \InvalidArgumentException('At least one authorized group is required for ' . self::KEY);
44                    }
45                },
46                appConfigKey: self::SYSTEM_APP_CONFIG_KEY,
47                supportsUserPreference: false,
48                supportsRequestOverride: false,
49                requestLifecycle: PolicySpec::LIFECYCLE_RUNTIME,
50                supportedScopes: [PolicySpec::SCOPE_SYSTEM],
51                backendOnly: true,
52            ),
53            default => throw new \InvalidArgumentException('Unknown policy key: ' . PolicyKeyNormalizer::normalize($policyKey)),
54        };
55    }
56}