Skip to content

Commit 7d20444

Browse files
committed
feat(migration): migrate legacy footer settings to structured policy payload
Signed-off-by: Vitor Mattos <1079143+vitormattos@users.noreply.github.com>
1 parent b03cece commit 7d20444

1 file changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* SPDX-FileCopyrightText: 2026 LibreCode coop and contributors
6+
* SPDX-License-Identifier: AGPL-3.0-or-later
7+
*/
8+
9+
namespace OCA\Libresign\Migration;
10+
11+
use Closure;
12+
use OCA\Libresign\AppInfo\Application;
13+
use OCA\Libresign\Service\Policy\Provider\Footer\SignatureFooterPolicyValue;
14+
use OCP\DB\ISchemaWrapper;
15+
use OCP\Exceptions\AppConfigTypeConflictException;
16+
use OCP\IAppConfig;
17+
use OCP\Migration\IOutput;
18+
use OCP\Migration\SimpleMigrationStep;
19+
20+
class Version18002Date20260410000000 extends SimpleMigrationStep {
21+
private const APP_ID = Application::APP_ID;
22+
23+
public function __construct(
24+
private IAppConfig $appConfig,
25+
) {
26+
}
27+
28+
#[\Override]
29+
public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void {
30+
$this->migrateLegacyFooterSettings();
31+
}
32+
33+
private function migrateLegacyFooterSettings(): void {
34+
$legacyAddFooter = $this->readLegacyValue('add_footer');
35+
$legacyWriteQrCodeOnFooter = $this->readLegacyBool('write_qrcode_on_footer', true);
36+
$legacyValidationSite = $this->readLegacyString('validation_site', '');
37+
$legacyFooterTemplateIsDefault = $this->readLegacyBool('footer_template_is_default', true);
38+
39+
$rawFooterPolicyValue = $legacyAddFooter;
40+
if (!$this->isStructuredFooterPayload($legacyAddFooter)) {
41+
$rawFooterPolicyValue = [
42+
'enabled' => $this->toBool($legacyAddFooter, true),
43+
'writeQrcodeOnFooter' => $legacyWriteQrCodeOnFooter,
44+
'validationSite' => $legacyValidationSite,
45+
'customizeFooterTemplate' => !$legacyFooterTemplateIsDefault,
46+
];
47+
}
48+
49+
$encodedFooterPolicyValue = SignatureFooterPolicyValue::encode(
50+
SignatureFooterPolicyValue::normalize($rawFooterPolicyValue),
51+
);
52+
53+
$this->appConfig->deleteKey(self::APP_ID, 'add_footer');
54+
$this->appConfig->setValueString(self::APP_ID, 'add_footer', $encodedFooterPolicyValue);
55+
}
56+
57+
private function isStructuredFooterPayload(mixed $value): bool {
58+
if (!is_string($value)) {
59+
return false;
60+
}
61+
62+
$decoded = json_decode($value, true);
63+
if (!is_array($decoded)) {
64+
return false;
65+
}
66+
67+
return array_key_exists('enabled', $decoded)
68+
|| array_key_exists('writeQrcodeOnFooter', $decoded)
69+
|| array_key_exists('validationSite', $decoded)
70+
|| array_key_exists('customizeFooterTemplate', $decoded);
71+
}
72+
73+
private function readLegacyValue(string $key): mixed {
74+
try {
75+
return $this->appConfig->getValueString(self::APP_ID, $key, '');
76+
} catch (AppConfigTypeConflictException) {
77+
return $this->appConfig->getValueBool(self::APP_ID, $key, true);
78+
}
79+
}
80+
81+
private function readLegacyBool(string $key, bool $default): bool {
82+
try {
83+
$rawValue = $this->appConfig->getValueString(self::APP_ID, $key, '');
84+
if ($rawValue === '') {
85+
return $default;
86+
}
87+
88+
return in_array(strtolower(trim($rawValue)), ['1', 'true', 'yes', 'on'], true);
89+
} catch (AppConfigTypeConflictException) {
90+
return $this->appConfig->getValueBool(self::APP_ID, $key, $default);
91+
}
92+
}
93+
94+
private function readLegacyString(string $key, string $default): string {
95+
try {
96+
return trim($this->appConfig->getValueString(self::APP_ID, $key, $default));
97+
} catch (AppConfigTypeConflictException) {
98+
return $default;
99+
}
100+
}
101+
102+
private function toBool(mixed $value, bool $default): bool {
103+
if (is_bool($value)) {
104+
return $value;
105+
}
106+
107+
if (is_int($value)) {
108+
return $value === 1;
109+
}
110+
111+
if (is_string($value)) {
112+
$trimmed = trim($value);
113+
if ($trimmed === '') {
114+
return $default;
115+
}
116+
117+
return in_array(strtolower($trimmed), ['1', 'true', 'yes', 'on'], true);
118+
}
119+
120+
return $default;
121+
}
122+
123+
#[\Override]
124+
public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
125+
return null;
126+
}
127+
}

0 commit comments

Comments
 (0)