From 0771d58fe74573e5fb5d1d37ebeb1a53e1abccac Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Tue, 17 Sep 2019 17:49:58 +0200 Subject: [PATCH] Fixed that `/api/system/db-changed-time` always returned the current time --- changelog/52_UNRELEASED_2019-xx-xx.md | 1 + services/ApiKeyService.php | 5 +++++ services/DatabaseService.php | 5 +++++ services/SessionService.php | 5 +++++ 4 files changed, 16 insertions(+) diff --git a/changelog/52_UNRELEASED_2019-xx-xx.md b/changelog/52_UNRELEASED_2019-xx-xx.md index b797e82f..6bc19dcd 100644 --- a/changelog/52_UNRELEASED_2019-xx-xx.md +++ b/changelog/52_UNRELEASED_2019-xx-xx.md @@ -31,4 +31,5 @@ - When adding a product (through `stock/product/{productId}/add` or `stock/product/{productId}/inventory`) with omitted best before date and if the given product has "Default best before days" set, the best before date is calculated based on that (so far always today was used which is still the case when no date is supplied and also the product has no "Default best before days set) (thanks @Forceu) - Field `stock_amount` of endpoint `/stock/products/{productId}´ now returns `0` instead of `null` when the given product is not in stock (thanks @Forceu) - `​/stock​/products​/{productId}` returns additional fields for the aggregated amount(s): `stock_amount_aggregated` and `stock_amount_opened_aggregated` - contains the same for "normal" products, `is_aggregated_amount` indicates if aggregation has happened + - Fixed that `/system/db-changed-time` always returned the current time (more or less) due to that that time is the database file modification time and the database is effectively changed on each request because of session information tracking - which now explicitly does not change the database file modification time, so this should work again to determine if any data changes happened - It's now also possible to provide the API key via a query parameter (same name as the header, so `GROCY-API-KEY`) diff --git a/services/ApiKeyService.php b/services/ApiKeyService.php index 36def7c9..08d3b7af 100644 --- a/services/ApiKeyService.php +++ b/services/ApiKeyService.php @@ -21,9 +21,14 @@ class ApiKeyService extends BaseService $apiKeyRow = $this->Database->api_keys()->where('api_key = :1 AND expires > :2 AND key_type = :3', $apiKey, date('Y-m-d H:i:s', time()), $keyType)->fetch(); if ($apiKeyRow !== null) { + // This should not change the database file modification time at this is used + // to determine if REALLY something has changed + $dbModTime = $this->DatabaseService->GetDbChangedTime(); $apiKeyRow->update(array( 'last_used' => date('Y-m-d H:i:s', time()) )); + $this->DatabaseService->SetDbChangedTime($dbModTime); + return true; } else diff --git a/services/DatabaseService.php b/services/DatabaseService.php index e488de79..bbfaa4ce 100644 --- a/services/DatabaseService.php +++ b/services/DatabaseService.php @@ -68,4 +68,9 @@ class DatabaseService { return date('Y-m-d H:i:s', filemtime(GROCY_DATAPATH . '/grocy.db')); } + + public function SetDbChangedTime($dateTime) + { + touch(GROCY_DATAPATH . '/grocy.db', strtotime($dateTime)); + } } diff --git a/services/SessionService.php b/services/SessionService.php index 244d5c25..2d3ae687 100644 --- a/services/SessionService.php +++ b/services/SessionService.php @@ -18,9 +18,14 @@ class SessionService extends BaseService $sessionRow = $this->Database->sessions()->where('session_key = :1 AND expires > :2', $sessionKey, date('Y-m-d H:i:s', time()))->fetch(); if ($sessionRow !== null) { + // This should not change the database file modification time at this is used + // to determine if REALLY something has changed + $dbModTime = $this->DatabaseService->GetDbChangedTime(); $sessionRow->update(array( 'last_used' => date('Y-m-d H:i:s', time()) )); + $this->DatabaseService->SetDbChangedTime($dbModTime); + return true; } else