fix: user settings
Signed-off-by: Sebastian Krupinski <krupinski01@gmail.com>
This commit is contained in:
@@ -249,7 +249,7 @@ class UserAccountsStore
|
||||
// Settings Operations
|
||||
// =========================================================================
|
||||
|
||||
public function fetchSettings(string $tenant, string $uid, array $settings = []): ?array
|
||||
public function fetchSettings(string $tenant, string $uid, array $settings = [], bool $flatten = false): ?array
|
||||
{
|
||||
// Only fetch the settings field from the database
|
||||
$user = $this->store->selectCollection('user_accounts')->findOne(
|
||||
@@ -264,12 +264,39 @@ class UserAccountsStore
|
||||
$userSettings = $user['settings'] ?? [];
|
||||
|
||||
if (empty($settings)) {
|
||||
return $userSettings;
|
||||
return $flatten ? $this->flattenSettings($userSettings) : $userSettings;
|
||||
}
|
||||
|
||||
// Specific keys are always returned in dot-notation (already flat)
|
||||
$result = [];
|
||||
foreach ($settings as $key) {
|
||||
$result[$key] = $userSettings[$key] ?? null;
|
||||
$parts = explode('.', $key);
|
||||
$value = $userSettings;
|
||||
foreach ($parts as $part) {
|
||||
if (!is_array($value) || !array_key_exists($part, $value)) {
|
||||
$value = null;
|
||||
break;
|
||||
}
|
||||
$value = $value[$part];
|
||||
}
|
||||
$result[$key] = $value;
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Recursively flatten a nested settings array into dot-notation keys.
|
||||
*/
|
||||
private function flattenSettings(array $settings, string $prefix = ''): array
|
||||
{
|
||||
$result = [];
|
||||
foreach ($settings as $key => $value) {
|
||||
$fullKey = $prefix !== '' ? "{$prefix}.{$key}" : (string) $key;
|
||||
if (is_array($value)) {
|
||||
$result = array_merge($result, $this->flattenSettings($value, $fullKey));
|
||||
} else {
|
||||
$result[$fullKey] = $value;
|
||||
}
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user