diff --git a/changelog/58_UNRELEASED_2020-xx-xx.md b/changelog/58_UNRELEASED_2020-xx-xx.md index 8c109dbf..35f24106 100644 --- a/changelog/58_UNRELEASED_2020-xx-xx.md +++ b/changelog/58_UNRELEASED_2020-xx-xx.md @@ -28,12 +28,12 @@ - Fixed that the "Share/Integrate calendar (iCal)" button did not work (thanks @tsia) ### API improvements/fixes -- Fixed that CORS was broken (there was no response to preflight OPTIONS requests) +- New endpoint `/user/settings` to get all user settings of the currently logged in user (key/value pairs) +- New endpoint `/system/config` to get all config settings (`config.php`) (key/value pairs) - The endpoint `/stock/products/{productId}/locations` now also returns the current stock amount of the product in that loctation (new field/property `amount`) (thanks @Forceu) +- Fixed that CORS was broken (there was no response to preflight OPTIONS requests) ### General & other improvements/fixes -- Fixed that all number inputs are always prefilled in the browser locale number format -- Fixed (again) that the `update.sh` file had wrong line endings (DOS instead of Unix) - Optimized that sometimes the corresponding form was not validated when selecting a date from the datetimepicker - New `config.php` setting `FEATURE_FLAG_STOCK_BEST_BEFORE_DATE_FIELD_NUMBER_PAD` which activates the number pad for best-before-date fields on (supported) mobile browsers (useful because of [shorthands](https://github.com/grocy/grocy#input-shorthands-for-date-fields)) (defaults to `true`) (thanks @Mik-) - Enhancements for the camera barcode scanner (thanks @Mik-) @@ -44,6 +44,8 @@ - Improved the the overview pages on mobile devices (main column was hidden) (thanks @Mik-) - Optimized the handling of settings provided by `data/settingoverrides` files (thanks @dacto) - Optimized the update script (`update.sh`) to create the backup tar archive before writing to it (was a problem on Btrfs file systems) (thanks @shane-kerr) +- Fixed that all number inputs are always prefilled in the browser locale number format +- Fixed (again) that the `update.sh` file had wrong line endings (DOS instead of Unix) - New translations: (thanks all the translators) - Japanese (demo available at https://ja.demo.grocy.info) - Chinese (Taiwan) (demo available at https://zh-tw.demo.grocy.info) diff --git a/controllers/SystemApiController.php b/controllers/SystemApiController.php index a293fd80..a28634d8 100644 --- a/controllers/SystemApiController.php +++ b/controllers/SystemApiController.php @@ -16,6 +16,35 @@ class SystemApiController extends BaseApiController )); } + public function GetConfig(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) + { + try + { + $constants = get_defined_constants(); + + // Some GROCY_* constants are not really config settings and therefore should not be exposed + unset($constants['GROCY_AUTHENTICATED']); + unset($constants['GROCY_DATAPATH']); + unset($constants['GROCY_IS_EMBEDDED_INSTALL']); + unset($constants['GROCY_USER_ID']); + + $returnArray = array(); + foreach ($constants as $constant => $value) + { + if (substr($constant, 0, 6) === 'GROCY_') + { + $returnArray[substr($constant, 6)] = $value; + } + } + + return $this->ApiResponse($response, $returnArray); + } + catch (\Exception $ex) + { + return $this->GenericErrorResponse($response, $ex->getMessage()); + } + } + public function LogMissingLocalization(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { if (GROCY_MODE === 'dev') diff --git a/controllers/UsersApiController.php b/controllers/UsersApiController.php index 00625bf2..c7f21cf0 100644 --- a/controllers/UsersApiController.php +++ b/controllers/UsersApiController.php @@ -69,6 +69,18 @@ class UsersApiController extends BaseApiController } } + public function GetUserSettings(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) + { + try + { + return $this->ApiResponse($response, $this->getUsersService()->GetUserSettings(GROCY_USER_ID)); + } + catch (\Exception $ex) + { + return $this->GenericErrorResponse($response, $ex->getMessage()); + } + } + public function GetUserSetting(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { try diff --git a/grocy.openapi.json b/grocy.openapi.json index 3797efd3..c0e30dd8 100644 --- a/grocy.openapi.json +++ b/grocy.openapi.json @@ -115,6 +115,37 @@ } } }, + "/system/config": { + "get": { + "summary": "Returns all config settings", + "tags": [ + "System" + ], + "responses": { + "200": { + "description": "Key/value pairs of config settings", + "content": { + "application/json": { + "schema": { + "type": "object", + "description": "Key/value pairs of config settings" + } + } + } + }, + "400": { + "description": "The operation was not successful", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/GenericErrorResponse" + } + } + } + } + } + } + }, "/system/log-missing-localization": { "post": { "summary": "Logs a missing localization string", @@ -586,12 +617,12 @@ ], "responses": { "200": { - "description": "An entity object", + "description": "Key/value pairs of userfields", "content": { "application/json": { "schema": { "type": "object", - "description": "Just key/value pairs of userfields" + "description": "Key/value pairs of userfields" } } } @@ -639,7 +670,7 @@ "content": { "application/json": { "schema": { - "description": "Just key/value pairs of userfields" + "description": "Key/value pairs of userfields" } } } @@ -978,9 +1009,40 @@ } } }, + "/user/settings": { + "get": { + "summary": "Returns all settings of the currently logged in user", + "tags": [ + "User settings" + ], + "responses": { + "200": { + "description": "Key/value pairs of user settings", + "content": { + "application/json": { + "schema": { + "type": "object", + "description": "Key/value pairs of user settings" + } + } + } + }, + "400": { + "description": "The operation was not successful", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/GenericErrorResponse" + } + } + } + } + } + } + }, "/user/settings/{settingKey}": { "get": { - "summary": "Gets the given setting of the currently logged in user", + "summary": "Returns the given setting of the currently logged in user", "tags": [ "User settings" ], diff --git a/routes.php b/routes.php index cd65e81e..c432ecee 100644 --- a/routes.php +++ b/routes.php @@ -143,6 +143,7 @@ $app->group('/api', function(RouteCollectorProxy $group) // System $group->get('/system/info', '\Grocy\Controllers\SystemApiController:GetSystemInfo'); $group->get('/system/db-changed-time', '\Grocy\Controllers\SystemApiController:GetDbChangedTime'); + $group->get('/system/config', '\Grocy\Controllers\SystemApiController:GetConfig'); $group->post('/system/log-missing-localization', '\Grocy\Controllers\SystemApiController:LogMissingLocalization'); // Generic entity interaction @@ -167,6 +168,7 @@ $app->group('/api', function(RouteCollectorProxy $group) $group->delete('/users/{userId}', '\Grocy\Controllers\UsersApiController:DeleteUser'); // User + $group->get('/user/settings', '\Grocy\Controllers\UsersApiController:GetUserSettings'); $group->get('/user/settings/{settingKey}', '\Grocy\Controllers\UsersApiController:GetUserSetting'); $group->put('/user/settings/{settingKey}', '\Grocy\Controllers\UsersApiController:SetUserSetting'); diff --git a/services/UsersService.php b/services/UsersService.php index 14810db0..3f08e4a6 100644 --- a/services/UsersService.php +++ b/services/UsersService.php @@ -50,6 +50,19 @@ class UsersService extends BaseService return $returnUsers; } + public function GetUserSettinsg($userId) + { + $settingRow = $this->getDatabase()->user_settings()->where('user_id = :1 AND key = :2', $userId, $settingKey)->fetch(); + if ($settingRow !== null) + { + return $settingRow->value; + } + else + { + return null; + } + } + public function GetUserSetting($userId, $settingKey) { $settingRow = $this->getDatabase()->user_settings()->where('user_id = :1 AND key = :2', $userId, $settingKey)->fetch();