From 92ae06962942f985822f2ba888cfabbb70f74223 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 7 May 2024 12:06:35 +0100 Subject: [PATCH 01/15] Disable button in UI if user cannot be deleted Signed-off-by: snipe --- resources/views/users/view.blade.php | 67 ++++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/resources/views/users/view.blade.php b/resources/views/users/view.blade.php index 3dbd9ff531..7a9d0f5793 100755 --- a/resources/views/users/view.blade.php +++ b/resources/views/users/view.blade.php @@ -91,7 +91,7 @@ @if ($user->managedLocations()->count() >= 0 )
  • - +
  • @endif - @can('update', $user) + @if ($user->managesUsers()->count() >= 0 ) +
  • + + + +
  • + @endif + + + @can('update', $user) - @if ($user->managedLocations()->count() >= 0 ) + @if ($user->managedLocations->count() >= 0 )
  • @endif - @if ($user->managesUsers()->count() >= 0 ) + @if ($user->managesUsers->count() >= 0 )
  • + From 386b2839e860e3e5de22154a02b804e3faf145c7 Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 7 May 2024 18:59:24 +0100 Subject: [PATCH 13/15] Added tests Signed-off-by: snipe --- tests/Feature/Api/Users/UsersDeleteTest.php | 42 +++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 tests/Feature/Api/Users/UsersDeleteTest.php diff --git a/tests/Feature/Api/Users/UsersDeleteTest.php b/tests/Feature/Api/Users/UsersDeleteTest.php new file mode 100644 index 0000000000..cbdba83278 --- /dev/null +++ b/tests/Feature/Api/Users/UsersDeleteTest.php @@ -0,0 +1,42 @@ +create(['first_name' => 'Manager', 'last_name' => 'McManagerson']); + User::factory()->create(['first_name' => 'Lowly', 'last_name' => 'Worker', 'manager_id' => $manager->id]); + $this->actingAs(User::factory()->deleteUsers()->create())->assertFalse($manager->isDeletable()); + } + + public function testDisallowUserDeletionIfStillManagingLocations() + { + $manager = User::factory()->create(['first_name' => 'Manager', 'last_name' => 'McManagerson']); + Location::factory()->create(['manager_id' => $manager->id]); + $this->actingAs(User::factory()->deleteUsers()->create())->assertFalse($manager->isDeletable()); + } + + public function testAllowUserDeletionIfNotManagingLocations() + { + $manager = User::factory()->create(['first_name' => 'Manager', 'last_name' => 'McManagerson']); + $this->actingAs(User::factory()->deleteUsers()->create())->assertTrue($manager->isDeletable()); + } + + public function testDisallowUserDeletionIfNoDeletePermissions() + { + $manager = User::factory()->create(['first_name' => 'Manager', 'last_name' => 'McManagerson']); + Location::factory()->create(['manager_id' => $manager->id]); + $this->actingAs(User::factory()->editUsers()->create())->assertFalse($manager->isDeletable()); + } + + +} From 8c327e6523fe170606124b823abdcbe85e92b41a Mon Sep 17 00:00:00 2001 From: snipe Date: Tue, 7 May 2024 19:16:56 +0100 Subject: [PATCH 14/15] Handle user not found properly Signed-off-by: snipe --- app/Http/Controllers/Api/UsersController.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index f2f2f28b80..0faa541243 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -416,11 +416,14 @@ class UsersController extends Controller $this->authorize('view', User::class); $user = User::withCount('assets as assets_count', 'licenses as licenses_count', 'accessories as accessories_count', 'consumables as consumables_count', 'managesUsers as manages_users_count', 'managedLocations as manages_locations_count'); - $user = Company::scopeCompanyables($user)->find($id); - $this->authorize('view', $user); + if ($user = Company::scopeCompanyables($user)->find($id)) { + $this->authorize('view', $user); + return (new UsersTransformer)->transformUser($user); + } + + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/users/message.user_not_found', compact('id')))); - return (new UsersTransformer)->transformUser($user); } From b2a5d86e30c54a51d14b6c04658dd9ec09de6338 Mon Sep 17 00:00:00 2001 From: Tobias Regnery Date: Wed, 8 May 2024 09:34:35 +0200 Subject: [PATCH 15/15] Fixes #14701 - wrong total asset count The total asset count in the sidenav shows the ready to deploy count instead of the total count. Fix this by adjusting the query to all assets. Also respect the setting for archived assets. Add a default value for total assets, since we are now using the settings-variable, which is not available during the setup process. While at it, move the block for total assets before the ready to deploy assets to match the ordering of the sidenav. Signed-off-by: Tobias Regnery --- app/Http/Middleware/AssetCountForSidebar.php | 24 ++++++++++++-------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/app/Http/Middleware/AssetCountForSidebar.php b/app/Http/Middleware/AssetCountForSidebar.php index e529f75efa..9d81eca93f 100644 --- a/app/Http/Middleware/AssetCountForSidebar.php +++ b/app/Http/Middleware/AssetCountForSidebar.php @@ -21,25 +21,36 @@ class AssetCountForSidebar /** * This needs to be set for the /setup process, since the tables might not exist yet */ + $total_assets = 0; $total_due_for_checkin = 0; $total_overdue_for_checkin = 0; $total_due_for_audit = 0; $total_overdue_for_audit = 0; try { - $total_rtd_sidebar = Asset::RTD()->count(); - view()->share('total_rtd_sidebar', $total_rtd_sidebar); + $settings = Setting::getSettings(); + view()->share('settings', $settings); } catch (\Exception $e) { \Log::debug($e); } try { - $total_assets = Asset::RTD()->count(); + $total_assets = Asset::all()->count(); + if ($settings->show_archived_in_list != '1') { + $total_assets -= Asset::Archived()->count(); + } view()->share('total_assets', $total_assets); } catch (\Exception $e) { \Log::debug($e); } + try { + $total_rtd_sidebar = Asset::RTD()->count(); + view()->share('total_rtd_sidebar', $total_rtd_sidebar); + } catch (\Exception $e) { + \Log::debug($e); + } + try { $total_deployed_sidebar = Asset::Deployed()->count(); view()->share('total_deployed_sidebar', $total_deployed_sidebar); @@ -75,13 +86,6 @@ class AssetCountForSidebar \Log::debug($e); } - try { - $settings = Setting::getSettings(); - view()->share('settings', $settings); - } catch (\Exception $e) { - \Log::debug($e); - } - try { $total_due_for_audit = Asset::DueForAudit($settings)->count(); view()->share('total_due_for_audit', $total_due_for_audit);