1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
7
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace OCA\TwoFactorGateway\Provider\Gateway; |
11
|
|
|
|
12
|
|
|
use OCA\TwoFactorGateway\AppInfo\Application; |
13
|
|
|
use OCA\TwoFactorGateway\Exception\ConfigurationException; |
14
|
|
|
use OCP\IAppConfig; |
15
|
|
|
|
16
|
|
|
trait TConfigurable { |
17
|
|
|
/** |
18
|
|
|
* @return string|static |
19
|
|
|
* @throws ConfigurationException |
20
|
|
|
*/ |
21
|
|
|
public function __call(string $name, array $args) { |
22
|
|
|
if (!preg_match('/^(?<operation>get|set|delete)(?<field>[A-Z][A-Za-z0-9_]*)$/', $name, $matches)) { |
23
|
|
|
throw new ConfigurationException('Invalid method ' . $name); |
24
|
|
|
} |
25
|
|
|
$field = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $matches['field'])); |
26
|
|
|
$key = $this->keyFromField($field); |
27
|
|
|
|
28
|
|
|
switch ($matches['operation']) { |
29
|
|
|
case 'get': |
30
|
|
|
$val = (string)$this->getAppConfig()->getValueString(Application::APP_ID, $key, ''); |
31
|
|
|
if ($val === '') { |
32
|
|
|
throw new ConfigurationException('No value set for ' . $field); |
33
|
|
|
} |
34
|
|
|
return $val; |
35
|
|
|
|
36
|
|
|
case 'set': |
37
|
|
|
$this->getAppConfig()->setValueString(Application::APP_ID, $key, (string)($args[0] ?? '')); |
38
|
|
|
return $this; |
39
|
|
|
|
40
|
|
|
case 'delete': |
41
|
|
|
$this->getAppConfig()->deleteKey(Application::APP_ID, $key); |
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
throw new ConfigurationException('Invalid operation ' . $matches['operation']); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @throws ConfigurationException |
49
|
|
|
*/ |
50
|
|
|
private function keyFromField(string $field): string { |
51
|
|
|
$fields = array_column($this->getSchemaFields(), 'field'); |
52
|
|
|
if (!in_array($field, $fields, true)) { |
53
|
|
|
throw new ConfigurationException('Invalid configuration field: ' . $field . ', check SCHEMA at ' . static::class); |
54
|
|
|
} |
55
|
|
|
return $this->getProviderId() . '_' . $field; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @throws ConfigurationException |
60
|
|
|
*/ |
61
|
|
|
private function getAppConfig(): IAppConfig { |
62
|
|
|
if (!isset($this->appConfig)) { |
63
|
|
|
throw new ConfigurationException('No app config set'); |
64
|
|
|
} |
65
|
|
|
return $this->appConfig; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return array |
70
|
|
|
* @throws ConfigurationException |
71
|
|
|
*/ |
72
|
|
|
protected function getSchemaFields(): array { |
73
|
|
|
return static::getSchema()['fields']; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @return array |
78
|
|
|
* @throws ConfigurationException |
79
|
|
|
*/ |
80
|
|
|
public static function getSchema(): array { |
81
|
|
|
if (!defined(static::class . '::SCHEMA')) { |
82
|
|
|
throw new ConfigurationException('No SCHEMA defined'); |
83
|
|
|
} |
84
|
|
|
return static::SCHEMA; |
|
|
|
|
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|