From 4b1766ead08e5dc85685a18bf115015230f95ced Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Thu, 10 Dec 2020 18:02:24 +0100 Subject: [PATCH] Return default user setting if not configured for API endpoint /user/settings/{settingKey} (fixes #1169) --- changelog/60_UNRELEASED_2020-xx-xx.md | 1 + services/UsersService.php | 13 ++++++++++--- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/changelog/60_UNRELEASED_2020-xx-xx.md b/changelog/60_UNRELEASED_2020-xx-xx.md index d4507419..cb442c6a 100644 --- a/changelog/60_UNRELEASED_2020-xx-xx.md +++ b/changelog/60_UNRELEASED_2020-xx-xx.md @@ -217,5 +217,6 @@ - Fixed that the endpoint `/stock/volatile` didn't include products which expire today (thanks @fipwmaqzufheoxq92ebc) - Fixed that the endpoint `/objects/{entity}` did not include Userfields for Userentities (so the effective endpoint `/objects/userobjects`) - Fixed that the endpoint `/stock/consume` returned the response code `200` and an empty response body when `stock_entry_id` was set (consuming a specific stock entry) but invalid (now returns the response code `400`) (thanks @fipwmaqzufheoxq92ebc) +- Fixed that the endpoint `/user/settings/{settingKey}` didn't return the default setting if it was not configured for the current user (same behavior as the endpoint `/user/settings` now) - Endpoint `/calendar/ical`: Fixed that "Track date only"-chores were always set to happen at 12am (are treated as all-day events now) - Fixed (again) that CORS was broken diff --git a/services/UsersService.php b/services/UsersService.php index a8a67b26..2dd4c4f6 100644 --- a/services/UsersService.php +++ b/services/UsersService.php @@ -53,14 +53,22 @@ class UsersService extends BaseService public function GetUserSetting($userId, $settingKey) { $settingRow = $this->getDatabase()->user_settings()->where('user_id = :1 AND key = :2', $userId, $settingKey)->fetch(); - if ($settingRow !== null) { return $settingRow->value; } else { - return null; + // Use the configured default values for a missing setting, otherwise return NULL + global $GROCY_DEFAULT_USER_SETTINGS; + if (array_key_exists($settingKey, $GROCY_DEFAULT_USER_SETTINGS)) + { + return $GROCY_DEFAULT_USER_SETTINGS[$settingKey]; + } + else + { + return null; + } } } @@ -69,7 +77,6 @@ class UsersService extends BaseService $settings = []; $settingRows = $this->getDatabase()->user_settings()->where('user_id = :1', $userId)->fetchAll(); - foreach ($settingRows as $settingRow) { $settings[$settingRow->key] = $settingRow->value;