From 6081b8ee67585ca85a5e1fe597d71f0dd6109c5d Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Sat, 4 Aug 2018 07:45:24 +0200 Subject: [PATCH] Fix some form validation problems (closes #36) --- app.php | 2 +- middleware/ApiKeyAuthMiddleware.php | 10 +++++++++- public/viewjs/batterytracking.js | 1 + public/viewjs/habittracking.js | 1 + services/HabitsService.php | 7 ++++--- services/StockService.php | 8 +++++++- views/components/productpicker.blade.php | 3 ++- views/shoppinglistform.blade.php | 3 ++- 8 files changed, 27 insertions(+), 8 deletions(-) diff --git a/app.php b/app.php index 2fcc813a..df81207c 100644 --- a/app.php +++ b/app.php @@ -36,7 +36,7 @@ else // Load composer dependencies require_once __DIR__ . '/vendor/autoload.php'; -// Load config fils +// Load config files require_once GROCY_DATAPATH . '/config.php'; require_once __DIR__ . '/config-dist.php'; //For not in own config defined values we use the default ones diff --git a/middleware/ApiKeyAuthMiddleware.php b/middleware/ApiKeyAuthMiddleware.php index 4e1266ee..793a81ad 100644 --- a/middleware/ApiKeyAuthMiddleware.php +++ b/middleware/ApiKeyAuthMiddleware.php @@ -49,12 +49,20 @@ class ApiKeyAuthMiddleware extends BaseMiddleware define('GROCY_AUTHENTICATED', false); $response = $response->withStatus(401); } - else + elseif ($validApiKey) { $user = $apiKeyService->GetUserByApiKey($request->getHeaderLine($this->ApiKeyHeaderName)); define('GROCY_AUTHENTICATED', true); define('GROCY_USER_ID', $user->id); + $response = $next($request, $response); + } + elseif ($validSession) + { + $user = $sessionService->GetUserBySessionKey($_COOKIE[$this->SessionCookieName]); + define('GROCY_AUTHENTICATED', true); + define('GROCY_USER_ID', $user->id); + $response = $next($request, $response); } } diff --git a/public/viewjs/batterytracking.js b/public/viewjs/batterytracking.js index b53c8d1a..1b73144b 100644 --- a/public/viewjs/batterytracking.js +++ b/public/viewjs/batterytracking.js @@ -44,6 +44,7 @@ $('#battery_id').on('change', function(e) { Grocy.Components.BatteryCard.Refresh(batteryId); $('#tracked_time').find('input').focus(); + Grocy.FrontendHelpers.ValidateForm('batterytracking-form'); } }); diff --git a/public/viewjs/habittracking.js b/public/viewjs/habittracking.js index 853a235e..b73ef2f2 100644 --- a/public/viewjs/habittracking.js +++ b/public/viewjs/habittracking.js @@ -43,6 +43,7 @@ $('#habit_id').on('change', function(e) { Grocy.Components.HabitCard.Refresh(habitId); Grocy.Components.DateTimePicker.GetInputElement().focus(); + Grocy.FrontendHelpers.ValidateForm('habittracking-form'); } }); diff --git a/services/HabitsService.php b/services/HabitsService.php index 7bcca2d1..153bacf7 100644 --- a/services/HabitsService.php +++ b/services/HabitsService.php @@ -45,12 +45,13 @@ class HabitsService extends BaseService $habitTrackedCount = $this->Database->habits_log()->where('habit_id', $habitId)->count(); $habitLastTrackedTime = $this->Database->habits_log()->where('habit_id', $habitId)->max('tracked_time'); - $doneByUserId = $this->Database->habits_log()->where('habit_id = :1 AND tracked_time = :2', $habitId, $habitLastTrackedTime)->fetch()->done_by_user_id; - if ($doneByUserId !== null && !empty($doneByUserId)) + $lastHabitLogRow = $this->Database->habits_log()->where('habit_id = :1 AND tracked_time = :2', $habitId, $habitLastTrackedTime)->fetch(); + $lastDoneByUser = null; + if ($lastHabitLogRow !== null && !empty($lastHabitLogRow)) { $usersService = new UsersService(); $users = $usersService->GetUsersAsDto(); - $lastDoneByUser = FindObjectInArrayByPropertyValue($users, 'id', $doneByUserId); + $lastDoneByUser = FindObjectInArrayByPropertyValue($users, 'id', $lastHabitLogRow->done_by_user_id); } return array( diff --git a/services/StockService.php b/services/StockService.php index 5e717673..1c5bf024 100644 --- a/services/StockService.php +++ b/services/StockService.php @@ -33,7 +33,13 @@ class StockService extends BaseService $productLastUsed = $this->Database->stock_log()->where('product_id', $productId)->where('transaction_type', self::TRANSACTION_TYPE_CONSUME)->max('used_date'); $quPurchase = $this->Database->quantity_units($product->qu_id_purchase); $quStock = $this->Database->quantity_units($product->qu_id_stock); - $lastPrice = $this->Database->stock_log()->where('product_id = :1 AND transaction_type = :2', $productId, self::TRANSACTION_TYPE_PURCHASE)->orderBy('row_created_timestamp', 'DESC')->limit(1)->fetch()->price; + + $lastPrice = null; + $lastLogRow = $this->Database->stock_log()->where('product_id = :1 AND transaction_type = :2', $productId, self::TRANSACTION_TYPE_PURCHASE)->orderBy('row_created_timestamp', 'DESC')->limit(1)->fetch(); + if ($lastLogRow !== null && !empty($lastLogRow)) + { + $lastPrice = $lastLogRow->price; + } return array( 'product' => $product, diff --git a/views/components/productpicker.blade.php b/views/components/productpicker.blade.php index 98d20e82..fbc250e1 100644 --- a/views/components/productpicker.blade.php +++ b/views/components/productpicker.blade.php @@ -4,10 +4,11 @@ @php if(empty($disallowAddProductWorkflows)) { $disallowAddProductWorkflows = false; } @endphp @php if(empty($prefillByName)) { $prefillByName = ''; } @endphp +@php if(!isset($isRequired)) { $isRequired = true; } @endphp
- @foreach($products as $product) diff --git a/views/shoppinglistform.blade.php b/views/shoppinglistform.blade.php index d698d25c..de50216d 100644 --- a/views/shoppinglistform.blade.php +++ b/views/shoppinglistform.blade.php @@ -23,7 +23,8 @@ @include('components.productpicker', array( 'products' => $products, - 'nextInputSelector' => '#amount' + 'nextInputSelector' => '#amount', + 'isRequired' => false ))