From db53a00d3fa53b8625a87f2d2670b5b766f803d6 Mon Sep 17 00:00:00 2001 From: Ivan Nieto Vivanco Date: Thu, 11 Aug 2022 17:01:52 -0500 Subject: [PATCH] Prevent to delete a user if still has consumables associated to them --- app/Http/Controllers/Api/UsersController.php | 4 ++++ app/Http/Controllers/Users/UsersController.php | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 92d763dd23..027435e4d5 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -441,6 +441,10 @@ class UsersController extends Controller return response()->json(Helper::formatStandardApiResponse('error', null, 'This user still has '.$user->accessories->count().' accessories associated with them.')); } + if (($user->consumables) && ($user->consumables->count() > 0)) { + return response()->json(Helper::formatStandardApiResponse('error', null, 'This user still has '.$user->consumables->count().' consumables associated with them.')); + } + if (($user->managedLocations()) && ($user->managedLocations()->count() > 0)) { return response()->json(Helper::formatStandardApiResponse('error', null, 'This user still has '.$user->managedLocations()->count().' locations that they manage.')); } diff --git a/app/Http/Controllers/Users/UsersController.php b/app/Http/Controllers/Users/UsersController.php index 7cbbbdb10b..10a5d8ba57 100755 --- a/app/Http/Controllers/Users/UsersController.php +++ b/app/Http/Controllers/Users/UsersController.php @@ -348,6 +348,12 @@ class UsersController extends Controller ->with('error', 'This user still has '.$accessoriesCount.' accessories associated with them.'); } + if (($user->consumables()) && (($consumablesCount = $user->consumables()->count())) > 0) { + // Redirect to the user management page + return redirect()->route('users.index') + ->with('error', 'This user still has '.$consumablesCount.' consumables associated with them.'); + } + if (($user->managedLocations()) && (($managedLocationsCount = $user->managedLocations()->count())) > 0) { // Redirect to the user management page return redirect()->route('users.index')