|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2026 LibreCode coop and contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Libresign\Migration; |
| 11 | + |
| 12 | +use Closure; |
| 13 | +use OCA\Libresign\AppInfo\Application; |
| 14 | +use OCP\Exceptions\AppConfigTypeConflictException; |
| 15 | +use OCP\IAppConfig; |
| 16 | +use OCP\IDBConnection; |
| 17 | +use OCP\Migration\IOutput; |
| 18 | +use OCP\Migration\SimpleMigrationStep; |
| 19 | + |
| 20 | +class Version18002Date20260511000000 extends SimpleMigrationStep { |
| 21 | + public function __construct( |
| 22 | + private readonly IAppConfig $appConfig, |
| 23 | + private readonly IDBConnection $connection, |
| 24 | + ) { |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @param IOutput $output |
| 29 | + * @param Closure $schemaClosure |
| 30 | + * @param array $options |
| 31 | + */ |
| 32 | + #[\Override] |
| 33 | + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { |
| 34 | + $this->ensureArrayConfig('groups_request_sign', ['admin']); |
| 35 | + $this->ensureArrayConfig('approval_group', ['admin']); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * @param list<string> $default |
| 40 | + */ |
| 41 | + private function ensureArrayConfig(string $key, array $default): void { |
| 42 | + $configValue = $this->getRawConfigValue($key); |
| 43 | + if ($configValue === null) { |
| 44 | + $this->appConfig->setValueArray(Application::APP_ID, $key, $default); |
| 45 | + return; |
| 46 | + } |
| 47 | + |
| 48 | + $normalized = $this->normalizeConfigValue($configValue, $default); |
| 49 | + $this->forceArrayType($key); |
| 50 | + |
| 51 | + try { |
| 52 | + $this->appConfig->setValueArray(Application::APP_ID, $key, $normalized); |
| 53 | + } catch (AppConfigTypeConflictException) { |
| 54 | + $this->forceArrayType($key); |
| 55 | + $this->appConfig->setValueArray(Application::APP_ID, $key, $normalized); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + private function getRawConfigValue(string $key): ?string { |
| 60 | + $qb = $this->connection->getQueryBuilder(); |
| 61 | + $qb->select('configvalue') |
| 62 | + ->from('appconfig') |
| 63 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter(Application::APP_ID))) |
| 64 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))) |
| 65 | + ->setMaxResults(1); |
| 66 | + |
| 67 | + $result = $qb->executeQuery()->fetchOne(); |
| 68 | + if ($result === false || $result === null) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + |
| 72 | + return (string)$result; |
| 73 | + } |
| 74 | + |
| 75 | + private function forceArrayType(string $key): void { |
| 76 | + $qb = $this->connection->getQueryBuilder(); |
| 77 | + $qb->update('appconfig') |
| 78 | + ->set('type', $qb->createNamedParameter(IAppConfig::VALUE_ARRAY)) |
| 79 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter(Application::APP_ID))) |
| 80 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter($key))) |
| 81 | + ->executeStatement(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @param list<string> $default |
| 86 | + * @return list<string> |
| 87 | + */ |
| 88 | + private function normalizeConfigValue(string $raw, array $default): array { |
| 89 | + $decoded = json_decode($raw, true); |
| 90 | + if (is_array($decoded)) { |
| 91 | + return array_values(array_map(static fn (mixed $item): string => (string)$item, $decoded)); |
| 92 | + } |
| 93 | + |
| 94 | + if ($raw === '') { |
| 95 | + return []; |
| 96 | + } |
| 97 | + |
| 98 | + return $default; |
| 99 | + } |
| 100 | +} |
0 commit comments