From 89f01d75d7879a6dc05e3a3792c1e9609027fad7 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 4 Jul 2024 19:59:10 +0100 Subject: [PATCH 01/42] Updated test names, added tests Signed-off-by: snipe --- ...toreTest.php => CreateAssetModelsTest.php} | 5 +- ...IndexTest.php => IndexAssetModelsTest.php} | 2 +- .../AssetModels/Ui/UpdateAssetModelsTest.php | 73 +++++++++++++++++++ 3 files changed, 77 insertions(+), 3 deletions(-) rename tests/Feature/AssetModels/Ui/{AssetModelStoreTest.php => CreateAssetModelsTest.php} (89%) rename tests/Feature/AssetModels/Ui/{AssetModelIndexTest.php => IndexAssetModelsTest.php} (91%) create mode 100644 tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php diff --git a/tests/Feature/AssetModels/Ui/AssetModelStoreTest.php b/tests/Feature/AssetModels/Ui/CreateAssetModelsTest.php similarity index 89% rename from tests/Feature/AssetModels/Ui/AssetModelStoreTest.php rename to tests/Feature/AssetModels/Ui/CreateAssetModelsTest.php index 81b07237c0dd..86710dab122b 100644 --- a/tests/Feature/AssetModels/Ui/AssetModelStoreTest.php +++ b/tests/Feature/AssetModels/Ui/CreateAssetModelsTest.php @@ -7,9 +7,9 @@ use App\Models\User; use Tests\TestCase; -class AssetModelStoreTest extends TestCase +class CreateAssetModelsTest extends TestCase { - public function testPermissionRequiredToStoreAssetModel() + public function testPermissionRequiredToCreateAssetModel() { $this->actingAs(User::factory()->create()) ->post(route('models.store'), [ @@ -32,4 +32,5 @@ public function testUserCanCreateAssetModels() $this->assertTrue(AssetModel::where('name', 'Test Model')->exists()); } + } diff --git a/tests/Feature/AssetModels/Ui/AssetModelIndexTest.php b/tests/Feature/AssetModels/Ui/IndexAssetModelsTest.php similarity index 91% rename from tests/Feature/AssetModels/Ui/AssetModelIndexTest.php rename to tests/Feature/AssetModels/Ui/IndexAssetModelsTest.php index a73d425723d5..495de30f0a31 100644 --- a/tests/Feature/AssetModels/Ui/AssetModelIndexTest.php +++ b/tests/Feature/AssetModels/Ui/IndexAssetModelsTest.php @@ -5,7 +5,7 @@ use App\Models\User; use Tests\TestCase; -class AssetModelIndexTest extends TestCase +class IndexAssetModelsTest extends TestCase { public function testPermissionRequiredToViewAssetModelList() { diff --git a/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php b/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php new file mode 100644 index 000000000000..e03e6c5762a8 --- /dev/null +++ b/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php @@ -0,0 +1,73 @@ +actingAs(User::factory()->create()) + ->post(route('models.store'), [ + 'name' => 'Test Model', + 'category_id' => Category::factory()->create()->id + ]) + ->assertForbidden(); + } + + public function testUserCanCreateAssetModels() + { + $this->assertFalse(AssetModel::where('name', 'Test Model')->exists()); + + $this->actingAs(User::factory()->superuser()->create()) + ->post(route('models.store'), [ + 'name' => 'Test Model', + 'category_id' => Category::factory()->create()->id + ]) + ->assertRedirect(route('models.index')); + + $this->assertTrue(AssetModel::where('name', 'Test Model')->exists()); + } + + public function testUserCanEditAssetModels() + { + $model = AssetModel::factory()->create(['name' => 'Test Model', 'category_id' => Category::factory()->create()->id]); + $this->assertTrue(AssetModel::where('name', 'Test Model')->exists()); + + $response = $this->actingAs(User::factory()->superuser()->create()) + ->put(route('models.update', ['model' => $model]), [ + 'name' => 'Test Model Edited', + 'category_id' => $model->category_id, + ]) + ->assertStatus(302) + ->assertSessionHasNoErrors() + ->assertRedirect(route('models.index')); + + $this->followRedirects($response)->assertSee('Success'); + $this->assertTrue(AssetModel::where('name', 'Test Model Edited')->exists()); + + } + + public function testUserCannotChangeAssetModelCategoryType() + { + $model = AssetModel::factory()->create(['name' => 'Test Model', 'category_id' => Category::factory()->forAssets()->create()->id]); + $this->assertTrue(AssetModel::where('name', 'Test Model')->exists()); + + $response = $this->actingAs(User::factory()->superuser()->create()) + ->put(route('models.update', ['model' => $model]), [ + 'name' => 'Test Model Edited', + 'category_id' => Category::factory()->forAccessories()->create()->id, + ]) + ->assertStatus(302) + ->assertSessionHasNoErrors() + ->assertRedirect(route('models.index')); + + $this->followRedirects($response)->assertSee(trans('general.error')); + $this->assertFalse(AssetModel::where('name', 'Test Model Edited')->exists()); + + } +} From e98284fff5f859a4c582ddcc98bb4b8fdbb7aa11 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 4 Jul 2024 20:48:35 +0100 Subject: [PATCH 02/42] Modernized helpers and use statements Signed-off-by: snipe --- .../Accessories/AccessoriesController.php | 12 ++++++------ .../Accessories/AccessoryCheckinController.php | 4 ++-- .../Accessories/AccessoryCheckoutController.php | 4 ++-- .../Controllers/Account/AcceptanceController.php | 8 ++++---- app/Http/Controllers/Api/AccessoriesController.php | 2 +- app/Http/Controllers/Api/AssetFilesController.php | 2 +- app/Http/Controllers/Api/AssetsController.php | 8 ++++---- app/Http/Controllers/Api/ComponentsController.php | 4 ++-- app/Http/Controllers/Api/ConsumablesController.php | 4 ++-- app/Http/Controllers/Api/CustomFieldsController.php | 2 +- .../Controllers/Api/CustomFieldsetsController.php | 4 ++-- app/Http/Controllers/Api/DepartmentsController.php | 2 +- app/Http/Controllers/Api/GroupsController.php | 2 +- app/Http/Controllers/Api/LabelsController.php | 4 ++-- app/Http/Controllers/Api/LicenseSeatsController.php | 2 +- app/Http/Controllers/Api/ManufacturersController.php | 2 +- app/Http/Controllers/Api/ProfileController.php | 10 +++++----- app/Http/Controllers/Api/ReportsController.php | 2 +- app/Http/Controllers/Api/SettingsController.php | 8 ++++---- app/Http/Controllers/Api/UsersController.php | 12 ++++++------ 20 files changed, 49 insertions(+), 49 deletions(-) diff --git a/app/Http/Controllers/Accessories/AccessoriesController.php b/app/Http/Controllers/Accessories/AccessoriesController.php index 81d5d692c5a1..f335ccf37a39 100755 --- a/app/Http/Controllers/Accessories/AccessoriesController.php +++ b/app/Http/Controllers/Accessories/AccessoriesController.php @@ -27,7 +27,7 @@ class AccessoriesController extends Controller * @author [A. Gianotto] [] * @see AccessoriesController::getDatatable() method that generates the JSON response * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function index() @@ -41,7 +41,7 @@ public function index() * Returns a view with a form to create a new Accessory. * * @author [A. Gianotto] [] - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function create() @@ -80,7 +80,7 @@ public function store(ImageUploadRequest $request) $accessory->purchase_date = request('purchase_date'); $accessory->purchase_cost = request('purchase_cost'); $accessory->qty = request('qty'); - $accessory->user_id = Auth::user()->id; + $accessory->user_id = auth()->id(); $accessory->supplier_id = request('supplier_id'); $accessory->notes = request('notes'); @@ -100,7 +100,7 @@ public function store(ImageUploadRequest $request) * * @author [A. Gianotto] [] * @param int $accessoryId - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function edit($accessoryId = null) @@ -122,7 +122,7 @@ public function edit($accessoryId = null) * @author [J. Vinsmoke] * @param int $accessoryId * @since [v6.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function getClone($accessoryId = null) { @@ -243,7 +243,7 @@ public function destroy($accessoryId) * @param int $accessoryID * @see AccessoriesController::getDataView() method that generates the JSON response * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function show($accessoryID = null) diff --git a/app/Http/Controllers/Accessories/AccessoryCheckinController.php b/app/Http/Controllers/Accessories/AccessoryCheckinController.php index 29c60650572b..a1af20594560 100644 --- a/app/Http/Controllers/Accessories/AccessoryCheckinController.php +++ b/app/Http/Controllers/Accessories/AccessoryCheckinController.php @@ -19,7 +19,7 @@ class AccessoryCheckinController extends Controller * @param Request $request * @param int $accessoryUserId * @param string $backto - * @return View + * @return \Illuminate\Contracts\View\View * @internal param int $accessoryId * @throws \Illuminate\Auth\Access\AuthorizationException */ @@ -70,7 +70,7 @@ public function store(Request $request, $accessoryUserId = null, $backto = null) if (DB::table('accessories_users')->where('id', '=', $accessory_user->id)->delete()) { $return_to = e($accessory_user->assigned_to); - event(new CheckoutableCheckedIn($accessory, User::find($return_to), Auth::user(), $request->input('note'), $checkin_at)); + event(new CheckoutableCheckedIn($accessory, User::find($return_to), auth()->user(), $request->input('note'), $checkin_at)); return redirect()->route('accessories.show', $accessory->id)->with('success', trans('admin/accessories/message.checkin.success')); } diff --git a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php index 679b8d42511e..c980e29efd27 100644 --- a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php +++ b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php @@ -19,7 +19,7 @@ class AccessoryCheckoutController extends Controller * * @author [A. Gianotto] [] * @param int $id - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function create($id) @@ -95,7 +95,7 @@ public function store(Request $request, $accessoryId) DB::table('accessories_users')->where('assigned_to', '=', $accessory->assigned_to)->where('accessory_id', '=', $accessory->id)->first(); - event(new CheckoutableCheckedOut($accessory, $user, Auth::user(), $request->input('note'))); + event(new CheckoutableCheckedOut($accessory, $user, auth()->user(), $request->input('note'))); // Redirect to the new accessory page return redirect()->route('accessories.index')->with('success', trans('admin/accessories/message.checkout.success')); diff --git a/app/Http/Controllers/Account/AcceptanceController.php b/app/Http/Controllers/Account/AcceptanceController.php index fafb6aeda53d..6ea00fd9d0aa 100644 --- a/app/Http/Controllers/Account/AcceptanceController.php +++ b/app/Http/Controllers/Account/AcceptanceController.php @@ -36,11 +36,11 @@ class AcceptanceController extends Controller /** * Show a listing of pending checkout acceptances for the current user * - * @return View + * @return \Illuminate\Contracts\View\View */ public function index() { - $acceptances = CheckoutAcceptance::forUser(Auth::user())->pending()->get(); + $acceptances = CheckoutAcceptance::forUser(auth()->user())->pending()->get(); return view('account/accept.index', compact('acceptances')); } @@ -64,7 +64,7 @@ public function create($id) return redirect()->route('account.accept')->with('error', trans('admin/users/message.error.asset_already_accepted')); } - if (! $acceptance->isCheckedOutTo(Auth::user())) { + if (! $acceptance->isCheckedOutTo(auth()->user())) { return redirect()->route('account.accept')->with('error', trans('admin/users/message.error.incorrect_user_accepted')); } @@ -94,7 +94,7 @@ public function store(Request $request, $id) return redirect()->route('account.accept')->with('error', trans('admin/users/message.error.asset_already_accepted')); } - if (! $acceptance->isCheckedOutTo(Auth::user())) { + if (! $acceptance->isCheckedOutTo(auth()->user())) { return redirect()->route('account.accept')->with('error', trans('admin/users/message.error.incorrect_user_accepted')); } diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index eb74a9e0f060..ad8995e4dd4a 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -303,7 +303,7 @@ public function checkout(Request $request, $accessoryId) 'note' => $request->get('note'), ]); - event(new CheckoutableCheckedOut($accessory, $user, Auth::user(), $request->input('note'))); + event(new CheckoutableCheckedOut($accessory, $user, auth()->user(), $request->input('note'))); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/accessories/message.checkout.success'))); } diff --git a/app/Http/Controllers/Api/AssetFilesController.php b/app/Http/Controllers/Api/AssetFilesController.php index d8bc5e384136..500c241f9f64 100644 --- a/app/Http/Controllers/Api/AssetFilesController.php +++ b/app/Http/Controllers/Api/AssetFilesController.php @@ -15,7 +15,7 @@ use App\Models\Actionlog; use \Illuminate\Support\Facades\Auth; use Carbon\Carbon; -use DB; +use Illuminate\Support\Facades\DB; use Illuminate\Http\Request; use App\Http\Requests\UploadFileRequest; use Illuminate\Support\Facades\Log; diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index dc20588b75d5..41f2e9bf2b2d 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -638,7 +638,7 @@ public function store(StoreAssetRequest $request): JsonResponse $target = Location::find(request('assigned_location')); } if (isset($target)) { - $asset->checkOut($target, Auth::user(), date('Y-m-d H:i:s'), '', 'Checked out on asset creation', e($request->get('name'))); + $asset->checkOut($target, auth()->user(), date('Y-m-d H:i:s'), '', 'Checked out on asset creation', e($request->get('name'))); } if ($asset->image) { @@ -729,7 +729,7 @@ public function update(ImageUploadRequest $request, $id) } if (isset($target)) { - $asset->checkOut($target, Auth::user(), date('Y-m-d H:i:s'), '', 'Checked out on asset update', e($request->get('name')), $location); + $asset->checkOut($target, auth()->user(), date('Y-m-d H:i:s'), '', 'Checked out on asset update', e($request->get('name')), $location); } if ($asset->image) { @@ -895,7 +895,7 @@ public function checkout(AssetCheckoutRequest $request, $asset_id) // $asset->location_id = $target->rtd_location_id; // } - if ($asset->checkOut($target, Auth::user(), $checkout_at, $expected_checkin, $note, $asset_name, $asset->location_id)) { + if ($asset->checkOut($target, auth()->user(), $checkout_at, $expected_checkin, $note, $asset_name, $asset->location_id)) { return response()->json(Helper::formatStandardApiResponse('success', ['asset'=> e($asset->asset_tag)], trans('admin/hardware/message.checkout.success'))); } @@ -976,7 +976,7 @@ function (Builder $query) use ($asset) { }); if ($asset->save()) { - event(new CheckoutableCheckedIn($asset, $target, Auth::user(), $request->input('note'), $checkin_at, $originalValues)); + event(new CheckoutableCheckedIn($asset, $target, auth()->user(), $request->input('note'), $checkin_at, $originalValues)); return response()->json(Helper::formatStandardApiResponse('success', [ 'asset_tag'=> e($asset->asset_tag), diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php index 4cf8173eac92..fc2ad9cfcac5 100644 --- a/app/Http/Controllers/Api/ComponentsController.php +++ b/app/Http/Controllers/Api/ComponentsController.php @@ -277,7 +277,7 @@ public function checkout(Request $request, $componentId) 'component_id' => $component->id, 'created_at' => \Carbon::now(), 'assigned_qty' => $request->get('assigned_qty', 1), - 'user_id' => \Auth::id(), + 'user_id' => auth()->id(), 'asset_id' => $request->get('assigned_to'), 'note' => $request->get('note'), ]); @@ -346,7 +346,7 @@ public function checkin(Request $request, $component_asset_id) $asset = Asset::find($component_assets->asset_id); - event(new CheckoutableCheckedIn($component, $asset, \Auth::user(), $request->input('note'), \Carbon::now())); + event(new CheckoutableCheckedIn($component, $asset, auth()->user(), $request->input('note'), \Carbon::now())); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.checkin.success'))); diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php index 956a176580e0..65033e000241 100644 --- a/app/Http/Controllers/Api/ConsumablesController.php +++ b/app/Http/Controllers/Api/ConsumablesController.php @@ -252,7 +252,7 @@ public function getDataView($consumableId) * @author [A. Gutierrez] [] * @param int $id * @since [v4.9.5] - * @return JsonResponse + * @return \Illuminate\Http\JsonResponse */ public function checkout(Request $request, $id) { @@ -293,7 +293,7 @@ public function checkout(Request $request, $id) ] ); - event(new CheckoutableCheckedOut($consumable, $user, Auth::user(), $request->input('note'))); + event(new CheckoutableCheckedOut($consumable, $user, auth()->user(), $request->input('note'))); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/consumables/message.checkout.success'))); diff --git a/app/Http/Controllers/Api/CustomFieldsController.php b/app/Http/Controllers/Api/CustomFieldsController.php index 470d672ccc22..ca00d00077cc 100644 --- a/app/Http/Controllers/Api/CustomFieldsController.php +++ b/app/Http/Controllers/Api/CustomFieldsController.php @@ -33,7 +33,7 @@ public function index() * @author [V. Cordes] [] * @param int $id * @since [v4.1.10] - * @return View + * @return \Illuminate\Contracts\View\View */ public function show($id) { diff --git a/app/Http/Controllers/Api/CustomFieldsetsController.php b/app/Http/Controllers/Api/CustomFieldsetsController.php index 2fa22f549115..0034ec038624 100644 --- a/app/Http/Controllers/Api/CustomFieldsetsController.php +++ b/app/Http/Controllers/Api/CustomFieldsetsController.php @@ -30,7 +30,7 @@ class CustomFieldsetsController extends Controller * @author [Josh Gibson] * @param int $id * @since [v1.8] - * @return View + * @return \Illuminate\Contracts\View\View */ public function index() { @@ -46,7 +46,7 @@ public function index() * @author [Josh Gibson] * @param int $id * @since [v1.8] - * @return View + * @return \Illuminate\Contracts\View\View */ public function show($id) { diff --git a/app/Http/Controllers/Api/DepartmentsController.php b/app/Http/Controllers/Api/DepartmentsController.php index 485e614b25af..662051439aba 100644 --- a/app/Http/Controllers/Api/DepartmentsController.php +++ b/app/Http/Controllers/Api/DepartmentsController.php @@ -100,7 +100,7 @@ public function store(ImageUploadRequest $request) $department->fill($request->all()); $department = $request->handleImages($department); - $department->user_id = Auth::user()->id; + $department->user_id = auth()->id(); $department->manager_id = ($request->filled('manager_id') ? $request->input('manager_id') : null); if ($department->save()) { diff --git a/app/Http/Controllers/Api/GroupsController.php b/app/Http/Controllers/Api/GroupsController.php index f1eafd49dcff..9b5e822b3f2a 100644 --- a/app/Http/Controllers/Api/GroupsController.php +++ b/app/Http/Controllers/Api/GroupsController.php @@ -67,7 +67,7 @@ public function store(Request $request) $groupPermissions = Helper::selectedPermissionsArray($permissions, $permissions); $group->name = $request->input('name'); - $group->created_by = Auth::user()->id; + $group->created_by = auth()->id(); $group->permissions = json_encode($request->input('permissions', $groupPermissions)); if ($group->save()) { diff --git a/app/Http/Controllers/Api/LabelsController.php b/app/Http/Controllers/Api/LabelsController.php index 126fe99438a1..9366161c8ec8 100644 --- a/app/Http/Controllers/Api/LabelsController.php +++ b/app/Http/Controllers/Api/LabelsController.php @@ -16,7 +16,7 @@ class LabelsController extends Controller * Returns JSON listing of all labels. * * @author Grant Le Roux - * @return JsonResponse + * @return \Illuminate\Http\JsonResponse */ public function index(Request $request) { @@ -50,7 +50,7 @@ public function index(Request $request) * * @author Grant Le Roux * @param string $labelName - * @return JsonResponse + * @return \Illuminate\Http\JsonResponse */ public function show(string $labelName) { diff --git a/app/Http/Controllers/Api/LicenseSeatsController.php b/app/Http/Controllers/Api/LicenseSeatsController.php index 317b09876898..f15792cdcd1d 100644 --- a/app/Http/Controllers/Api/LicenseSeatsController.php +++ b/app/Http/Controllers/Api/LicenseSeatsController.php @@ -110,7 +110,7 @@ public function update(Request $request, $licenseId, $seatId) // attempt to update the license seat $licenseSeat->fill($request->all()); - $licenseSeat->user_id = Auth::user()->id; + $licenseSeat->user_id = auth()->id(); // check if this update is a checkin operation // 1. are relevant fields touched at all? diff --git a/app/Http/Controllers/Api/ManufacturersController.php b/app/Http/Controllers/Api/ManufacturersController.php index 6b5a130229b7..36fbe5354cfc 100644 --- a/app/Http/Controllers/Api/ManufacturersController.php +++ b/app/Http/Controllers/Api/ManufacturersController.php @@ -186,7 +186,7 @@ public function restore($id) $logaction->item_type = Manufacturer::class; $logaction->item_id = $manufacturer->id; $logaction->created_at = date('Y-m-d H:i:s'); - $logaction->user_id = Auth::user()->id; + $logaction->user_id = auth()->id(); $logaction->logaction('restore'); return response()->json(Helper::formatStandardApiResponse('success', trans('admin/manufacturers/message.restore.success')), 200); diff --git a/app/Http/Controllers/Api/ProfileController.php b/app/Http/Controllers/Api/ProfileController.php index b6752dd1ede8..41adbd58458b 100644 --- a/app/Http/Controllers/Api/ProfileController.php +++ b/app/Http/Controllers/Api/ProfileController.php @@ -47,7 +47,7 @@ public function __construct(TokenRepository $tokenRepository, ValidationFactory */ public function requestedAssets() { - $checkoutRequests = CheckoutRequest::where('user_id', '=', Auth::user()->id)->get(); + $checkoutRequests = CheckoutRequest::where('user_id', '=', auth()->id())->get(); $results = array(); $show_field = array(); @@ -106,10 +106,10 @@ public function createApiToken(Request $request) { $accessTokenName = $request->input('name', 'Auth Token'); - if ($accessToken = Auth::user()->createToken($accessTokenName)->accessToken) { + if ($accessToken = auth()->user()->createToken($accessTokenName)->accessToken) { // Get the ID so we can return that with the payload - $token = DB::table('oauth_access_tokens')->where('user_id', '=', Auth::user()->id)->where('name','=',$accessTokenName)->orderBy('created_at', 'desc')->first(); + $token = DB::table('oauth_access_tokens')->where('user_id', '=', auth()->id())->where('name','=',$accessTokenName)->orderBy('created_at', 'desc')->first(); $accessTokenData['id'] = $token->id; $accessTokenData['token'] = $accessToken; $accessTokenData['name'] = $accessTokenName; @@ -135,7 +135,7 @@ public function deleteApiToken($tokenId) { } $token = $this->tokenRepository->findForUser( - $tokenId, Auth::user()->getAuthIdentifier() + $tokenId, auth()->user()->getAuthIdentifier() ); if (is_null($token)) { @@ -163,7 +163,7 @@ public function showApiTokens(Request $request) { abort(403); } - $tokens = $this->tokenRepository->forUser(Auth::user()->getAuthIdentifier()); + $tokens = $this->tokenRepository->forUser(auth()->user()->getAuthIdentifier()); $token_values = $tokens->load('client')->filter(function ($token) { return $token->client->personal_access_client && ! $token->revoked; })->values(); diff --git a/app/Http/Controllers/Api/ReportsController.php b/app/Http/Controllers/Api/ReportsController.php index 8761c9442b0a..61e777febc07 100644 --- a/app/Http/Controllers/Api/ReportsController.php +++ b/app/Http/Controllers/Api/ReportsController.php @@ -14,7 +14,7 @@ class ReportsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function index(Request $request) { diff --git a/app/Http/Controllers/Api/SettingsController.php b/app/Http/Controllers/Api/SettingsController.php index d9059988f24c..5be5a72afba2 100644 --- a/app/Http/Controllers/Api/SettingsController.php +++ b/app/Http/Controllers/Api/SettingsController.php @@ -148,7 +148,7 @@ public function ldaptestlogin(Request $request) * * @author [A. Gianotto] [] * @since [v3.0] - * @return JsonResponse + * @return \Illuminate\Http\JsonResponse */ public function ajaxTestEmail() { @@ -170,7 +170,7 @@ public function ajaxTestEmail() * * @author [A. Gianotto] [] * @since [v5.0.0] - * @return JsonResponse + * @return \Illuminate\Http\JsonResponse */ public function purgeBarcodes() { @@ -276,7 +276,7 @@ public function listBackups() { * exhausts memory on larger files. * * @author [A. Gianotto] - * @return JsonResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse + * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse */ public function downloadBackup($file) { @@ -296,7 +296,7 @@ public function downloadBackup($file) { * * @author [A. Gianotto] * @since [v6.3.1] - * @return JsonResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse + * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse */ public function downloadLatestBackup() { diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 6a96fb7282cc..2f60f84826d3 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -365,13 +365,13 @@ public function store(SaveUserRequest $request) $user = new User; $user->fill($request->all()); - $user->created_by = Auth::user()->id; + $user->created_by = auth()->id(); if ($request->has('permissions')) { $permissions_array = $request->input('permissions'); // Strip out the superuser permission if the API user isn't a superadmin - if (! Auth::user()->isSuperUser()) { + if (! auth()->user()->isSuperUser()) { unset($permissions_array['superuser']); } $user->permissions = $permissions_array; @@ -468,7 +468,7 @@ public function update(SaveUserRequest $request, $id) $permissions_array = $request->input('permissions'); // Strip out the individual superuser permission if the API user isn't a superadmin - if (!Auth::user()->isSuperUser()) { + if (!auth()->user()->isSuperUser()) { unset($permissions_array['superuser']); } @@ -486,7 +486,7 @@ public function update(SaveUserRequest $request, $id) if ($user->save()) { // Check if the request has groups passed and has a value, AND that the user us a superuser - if (($request->has('groups')) && (Auth::user()->isSuperUser())) { + if (($request->has('groups')) && (auth()->user()->isSuperUser())) { $validator = Validator::make($request->only('groups'), [ 'groups.*' => 'integer|exists:permission_groups,id', @@ -710,7 +710,7 @@ public function postTwoFactorReset(Request $request) $logaction->item_type = User::class; $logaction->item_id = $user->id; $logaction->created_at = date('Y-m-d H:i:s'); - $logaction->user_id = Auth::user()->id; + $logaction->user_id = auth()->id(); $logaction->logaction('2FA reset'); return response()->json(['message' => trans('admin/settings/general.two_factor_reset_success')], 200); @@ -762,7 +762,7 @@ public function restore($userId) $logaction->item_type = User::class; $logaction->item_id = $user->id; $logaction->created_at = date('Y-m-d H:i:s'); - $logaction->user_id = Auth::user()->id; + $logaction->user_id = auth()->id(); $logaction->logaction('restore'); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/users/message.success.restored')), 200); From ae9085b11faddbd9d08f15f527ebc4b01826f178 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 4 Jul 2024 20:49:22 +0100 Subject: [PATCH 03/42] Modernize use statements, switch to auth() Signed-off-by: snipe --- .../AssetMaintenancesController.php | 37 +++---- .../AssetModelsFilesController.php | 4 +- .../Assets/AssetCheckinController.php | 4 +- .../Assets/AssetCheckoutController.php | 4 +- .../Assets/AssetFilesController.php | 4 +- .../Controllers/Assets/AssetsController.php | 28 +++--- .../Assets/BulkAssetsController.php | 10 +- app/Http/Controllers/Auth/LoginController.php | 10 +- .../Controllers/BulkAssetModelsController.php | 5 +- app/Http/Controllers/CategoriesController.php | 5 +- app/Http/Controllers/CompaniesController.php | 2 +- .../Components/ComponentCheckinController.php | 2 +- .../ComponentCheckoutController.php | 4 +- .../ConsumableCheckoutController.php | 4 +- app/Http/Controllers/Controller.php | 2 +- .../Controllers/CustomFieldsController.php | 6 +- .../Controllers/CustomFieldsetsController.php | 10 +- app/Http/Controllers/DashboardController.php | 6 +- .../Controllers/DepartmentsController.php | 21 +++- .../Controllers/DepreciationsController.php | 4 +- app/Http/Controllers/GroupsController.php | 6 +- app/Http/Controllers/HealthController.php | 7 +- .../Kits/CheckoutKitController.php | 2 +- .../Kits/PredefinedKitsController.php | 28 +++--- app/Http/Controllers/LabelsController.php | 5 +- .../Licenses/LicenseCheckinController.php | 2 +- .../Licenses/LicenseCheckoutController.php | 4 +- app/Http/Controllers/LocationsController.php | 8 +- .../Controllers/ManufacturersController.php | 6 +- app/Http/Controllers/ModalController.php | 2 +- app/Http/Controllers/ProfileController.php | 18 ++-- app/Http/Controllers/ReportsController.php | 12 +-- app/Http/Controllers/SettingsController.php | 99 ++++++++++--------- .../Controllers/StatuslabelsController.php | 2 +- app/Http/Controllers/SuppliersController.php | 4 +- .../Controllers/Users/BulkUsersController.php | 4 +- .../Controllers/Users/UsersController.php | 12 +-- app/Http/Controllers/ViewAssetsController.php | 17 ++-- app/Http/Middleware/CheckForDebug.php | 2 +- app/Http/Middleware/CheckForTwoFactor.php | 6 +- app/Http/Requests/DeleteUserRequest.php | 4 +- app/Http/Transformers/AssetsTransformer.php | 6 +- app/Importer/AssetImporter.php | 2 +- app/Importer/LocationImporter.php | 2 +- app/Listeners/CheckoutableListener.php | 4 +- app/Livewire/OauthClients.php | 18 ++-- app/Livewire/PersonalAccessTokens.php | 6 +- app/Models/Actionlog.php | 8 +- app/Models/Asset.php | 2 +- app/Models/Company.php | 22 ++--- app/Models/Loggable.php | 16 +-- app/Services/PredefinedKitCheckoutService.php | 2 +- database/factories/CategoryFactory.php | 28 ++++++ resources/lang/en-US/mail.php | 1 + .../forms/edit/company-select.blade.php | 1 - 55 files changed, 286 insertions(+), 254 deletions(-) diff --git a/app/Http/Controllers/AssetMaintenancesController.php b/app/Http/Controllers/AssetMaintenancesController.php index c9ffea9a4c48..02be1e606121 100644 --- a/app/Http/Controllers/AssetMaintenancesController.php +++ b/app/Http/Controllers/AssetMaintenancesController.php @@ -2,17 +2,14 @@ namespace App\Http\Controllers; -use App\Helpers\Helper; use App\Models\Asset; use App\Models\AssetMaintenance; use App\Models\Company; use Illuminate\Support\Facades\Auth; use Carbon\Carbon; use Illuminate\Http\Request; -use Slack; -use Str; -use TCPDF; -use View; +use \Illuminate\Contracts\View\View; +use \Illuminate\Http\RedirectResponse; /** * This controller handles all actions related to Asset Maintenance for @@ -29,9 +26,8 @@ class AssetMaintenancesController extends Controller * @author Vincent Sposato * @version v1.0 * @since [v1.8] - * @return View */ - private static function getInsufficientPermissionsRedirect() + private static function getInsufficientPermissionsRedirect(): RedirectResponse { return redirect()->route('maintenances.index') ->with('error', trans('general.insufficient_permissions')); @@ -46,9 +42,8 @@ private static function getInsufficientPermissionsRedirect() * @author Vincent Sposato * @version v1.0 * @since [v1.8] - * @return View */ - public function index() + public function index() : View { $this->authorize('view', Asset::class); return view('asset_maintenances/index'); @@ -63,7 +58,7 @@ public function index() * @since [v1.8] * @return mixed */ - public function create() + public function create() : View { $this->authorize('update', Asset::class); $asset = null; @@ -92,9 +87,8 @@ public function create() * @author Vincent Sposato * @version v1.0 * @since [v1.8] - * @return mixed */ - public function store(Request $request) + public function store(Request $request) : RedirectResponse { $this->authorize('update', Asset::class); // create a new model instance @@ -144,9 +138,8 @@ public function store(Request $request) * @param int $assetMaintenanceId * @version v1.0 * @since [v1.8] - * @return mixed */ - public function edit($assetMaintenanceId = null) + public function edit($assetMaintenanceId = null) : View | RedirectResponse { $this->authorize('update', Asset::class); // Check if the asset maintenance exists @@ -162,14 +155,9 @@ public function edit($assetMaintenanceId = null) return static::getInsufficientPermissionsRedirect(); } - // Prepare Improvement Type List - $assetMaintenanceType = [ - '' => 'Select an improvement type', - ] + AssetMaintenance::getImprovementOptions(); + $assetMaintenanceType = ['' => 'Select an improvement type'] + AssetMaintenance::getImprovementOptions(); - // Get Supplier List - // Render the view return view('asset_maintenances/edit') ->with('selectedAsset', null) ->with('assetMaintenanceType', $assetMaintenanceType) @@ -183,11 +171,10 @@ public function edit($assetMaintenanceId = null) * @author Vincent Sposato * @param Request $request * @param int $assetMaintenanceId - * @return mixed * @version v1.0 * @since [v1.8] */ - public function update(Request $request, $assetMaintenanceId = null) + public function update(Request $request, $assetMaintenanceId = null) : View | RedirectResponse { $this->authorize('update', Asset::class); // Check if the asset maintenance exists @@ -255,9 +242,8 @@ public function update(Request $request, $assetMaintenanceId = null) * @param int $assetMaintenanceId * @version v1.0 * @since [v1.8] - * @return mixed */ - public function destroy($assetMaintenanceId) + public function destroy($assetMaintenanceId) : RedirectResponse { $this->authorize('update', Asset::class); // Check if the asset maintenance exists @@ -284,9 +270,8 @@ public function destroy($assetMaintenanceId) * @param int $assetMaintenanceId * @version v1.0 * @since [v1.8] - * @return View */ - public function show($assetMaintenanceId) + public function show($assetMaintenanceId) : View | RedirectResponse { $this->authorize('view', Asset::class); diff --git a/app/Http/Controllers/AssetModelsFilesController.php b/app/Http/Controllers/AssetModelsFilesController.php index c8247ffc154e..8e0512801b83 100644 --- a/app/Http/Controllers/AssetModelsFilesController.php +++ b/app/Http/Controllers/AssetModelsFilesController.php @@ -54,7 +54,7 @@ public function store(UploadFileRequest $request, $modelId = null) * @param int $modelId * @param int $fileId * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function show($modelId = null, $fileId = null) @@ -101,7 +101,7 @@ public function show($modelId = null, $fileId = null) * @param int $modelId * @param int $fileId * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function destroy($modelId = null, $fileId = null) diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.php index 73f4838a6a61..10aa95794214 100644 --- a/app/Http/Controllers/Assets/AssetCheckinController.php +++ b/app/Http/Controllers/Assets/AssetCheckinController.php @@ -26,7 +26,7 @@ class AssetCheckinController extends Controller * @author [A. Gianotto] [] * @param int $assetId * @param string $backto - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException * @since [v1.0] */ @@ -136,7 +136,7 @@ function (Builder $query) use ($asset) { // Was the asset updated? if ($asset->save()) { - event(new CheckoutableCheckedIn($asset, $target, Auth::user(), $request->input('note'), $checkin_at, $originalValues)); + event(new CheckoutableCheckedIn($asset, $target, auth()->user(), $request->input('note'), $checkin_at, $originalValues)); return Helper::getRedirectOption($asset, $assetId, 'Assets'); } // Redirect to the asset management page with error diff --git a/app/Http/Controllers/Assets/AssetCheckoutController.php b/app/Http/Controllers/Assets/AssetCheckoutController.php index 36f00f0fc609..dfc11f1fc76e 100644 --- a/app/Http/Controllers/Assets/AssetCheckoutController.php +++ b/app/Http/Controllers/Assets/AssetCheckoutController.php @@ -23,7 +23,7 @@ class AssetCheckoutController extends Controller * @author [A. Gianotto] [] * @param int $assetId * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function create($assetId) { @@ -72,7 +72,7 @@ public function store(AssetCheckoutRequest $request, $assetId) return redirect()->route('hardware.show', $asset->id)->with('error', trans('admin/hardware/general.model_invalid_fix')); } - $admin = Auth::user(); + $admin = auth()->user(); $target = $this->determineCheckoutTarget(); diff --git a/app/Http/Controllers/Assets/AssetFilesController.php b/app/Http/Controllers/Assets/AssetFilesController.php index 01ff0288efd4..fcd3375f8dc0 100644 --- a/app/Http/Controllers/Assets/AssetFilesController.php +++ b/app/Http/Controllers/Assets/AssetFilesController.php @@ -54,7 +54,7 @@ public function store(UploadFileRequest $request, $assetId = null) * @param int $assetId * @param int $fileId * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function show($assetId = null, $fileId = null) @@ -105,7 +105,7 @@ public function show($assetId = null, $fileId = null) * @param int $assetId * @param int $fileId * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function destroy($assetId = null, $fileId = null) diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php index 82c68eb01a55..ec258b88ec9f 100755 --- a/app/Http/Controllers/Assets/AssetsController.php +++ b/app/Http/Controllers/Assets/AssetsController.php @@ -55,7 +55,7 @@ public function __construct() * @see AssetController::getDatatable() method that generates the JSON response * @since [v1.0] * @param Request $request - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function index(Request $request) @@ -72,7 +72,7 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v1.0] * @param Request $request - * @return View + * @return \Illuminate\Contracts\View\View * @internal param int $model_id */ public function create(Request $request) @@ -198,7 +198,7 @@ public function store(ImageUploadRequest $request) } if (isset($target)) { - $asset->checkOut($target, Auth::user(), date('Y-m-d H:i:s'), $request->input('expected_checkin', null), 'Checked out on asset creation', $request->get('name'), $location); + $asset->checkOut($target, auth()->user(), date('Y-m-d H:i:s'), $request->input('expected_checkin', null), 'Checked out on asset creation', $request->get('name'), $location); } $success = true; @@ -229,7 +229,7 @@ public function getOptionCookie(Request $request){ * @author [A. Gianotto] [] * @param int $assetId * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function edit($assetId = null) { @@ -252,7 +252,7 @@ public function edit($assetId = null) * @author [A. Gianotto] [] * @param int $assetId * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function show($assetId = null) { @@ -561,7 +561,7 @@ public function getBarCode($assetId = null) * * @author [L. Swartzendruber] [ * @param int $assetId - * @return View + * @return \Illuminate\Contracts\View\View */ public function getLabel($assetId = null) { @@ -585,7 +585,7 @@ public function getLabel($assetId = null) * @author [A. Gianotto] [] * @param int $assetId * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function getClone($assetId = null) { @@ -614,7 +614,7 @@ public function getClone($assetId = null) * * @author [A. Gianotto] [] * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function getImportHistory() { @@ -636,7 +636,7 @@ public function getImportHistory() * * @author [A. Gianotto] [] * @since [v3.3] - * @return View + * @return \Illuminate\Contracts\View\View */ public function postImportHistory(Request $request) { @@ -730,8 +730,8 @@ public function postImportHistory(Request $request) Actionlog::firstOrCreate([ 'item_id' => $asset->id, 'item_type' => Asset::class, - 'user_id' => Auth::user()->id, - 'note' => 'Checkout imported by '.Auth::user()->present()->fullName().' from history importer', + 'user_id' => auth()->id(), + 'note' => 'Checkout imported by '.auth()->user()->present()->fullName().' from history importer', 'target_id' => $item[$asset_tag][$batch_counter]['user_id'], 'target_type' => User::class, 'created_at' => $item[$asset_tag][$batch_counter]['checkout_date'], @@ -758,8 +758,8 @@ public function postImportHistory(Request $request) Actionlog::firstOrCreate([ 'item_id' => $item[$asset_tag][$batch_counter]['asset_id'], 'item_type' => Asset::class, - 'user_id' => Auth::user()->id, - 'note' => 'Checkin imported by '.Auth::user()->present()->fullName().' from history importer', + 'user_id' => auth()->id(), + 'note' => 'Checkin imported by '.auth()->user()->present()->fullName().' from history importer', 'target_id' => null, 'created_at' => $checkin_date, 'action_type' => 'checkin', @@ -796,7 +796,7 @@ public function sortByName(array $recordA, array $recordB): int * @author [A. Gianotto] [] * @param int $assetId * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function getRestore($assetId = null) { diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index ffbf81944b51..c2b5c7369640 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -36,7 +36,7 @@ class BulkAssetsController extends Controller * action would make a lot more sense here and make things a lot more clear. * * @author [A. Gianotto] [] - * @return View + * @return \Illuminate\Contracts\View\View * @internal param int $assetId * @since [v2.0] * @throws \Illuminate\Auth\Access\AuthorizationException @@ -471,7 +471,7 @@ protected function conditionallyAddItem($field) * * @author [A. Gianotto] [] * @param Request $request - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException * @internal param array $assets * @since [v2.0] @@ -505,7 +505,7 @@ public function destroy(Request $request) /** * Show Bulk Checkout Page - * @return View View to checkout multiple assets + * @return \Illuminate\Contracts\View\View View to checkout multiple assets */ public function showCheckout() { @@ -517,7 +517,7 @@ public function showCheckout() /** * Process Multiple Checkout Request - * @return View + * @return \Illuminate\Contracts\View\View */ public function storeCheckout(AssetCheckoutRequest $request) { @@ -525,7 +525,7 @@ public function storeCheckout(AssetCheckoutRequest $request) $this->authorize('checkout', Asset::class); try { - $admin = Auth::user(); + $admin = auth()->user(); $target = $this->determineCheckoutTarget(); diff --git a/app/Http/Controllers/Auth/LoginController.php b/app/Http/Controllers/Auth/LoginController.php index ac19a57a7284..9ac976b43ce8 100644 --- a/app/Http/Controllers/Auth/LoginController.php +++ b/app/Http/Controllers/Auth/LoginController.php @@ -127,7 +127,7 @@ private function loginViaSaml(Request $request) $saml->clearData(); } - if ($user = Auth::user()) { + if ($user = auth()->user()) { $user->last_login = \Carbon::now(); $user->saveQuietly(); } @@ -326,7 +326,7 @@ public function login(Request $request) } } - if ($user = Auth::user()) { + if ($user = auth()->user()) { $user->last_login = \Carbon::now(); $user->activated = 1; $user->saveQuietly(); @@ -350,7 +350,7 @@ public function getTwoFactorEnroll() } $settings = Setting::getSettings(); - $user = Auth::user(); + $user = auth()->user(); // We wouldn't normally see this page if 2FA isn't enforced via the // \App\Http\Middleware\CheckForTwoFactor middleware AND if a device isn't enrolled, @@ -398,7 +398,7 @@ public function getTwoFactorAuth() return redirect()->route('login')->with('error', trans('auth/general.login_prompt')); } - $user = Auth::user(); + $user = auth()->user(); // Check whether there is a device enrolled. // This *should* be handled via the \App\Http\Middleware\CheckForTwoFactor middleware @@ -427,7 +427,7 @@ public function postTwoFactorAuth(Request $request) return redirect()->route('two-factor')->with('error', trans('auth/message.two_factor.code_required')); } - $user = Auth::user(); + $user = auth()->user(); $secret = $request->input('two_factor_secret'); if (Google2FA::verifyKey($user->two_factor_secret, $secret)) { diff --git a/app/Http/Controllers/BulkAssetModelsController.php b/app/Http/Controllers/BulkAssetModelsController.php index 5437791aa833..672fc30079cf 100644 --- a/app/Http/Controllers/BulkAssetModelsController.php +++ b/app/Http/Controllers/BulkAssetModelsController.php @@ -5,7 +5,6 @@ use App\Helpers\Helper; use App\Models\AssetModel; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Redirect; class BulkAssetModelsController extends Controller @@ -16,7 +15,7 @@ class BulkAssetModelsController extends Controller * @author [A. Gianotto] [] * @since [v1.7] * @param Request $request - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function edit(Request $request) { @@ -61,7 +60,7 @@ public function edit(Request $request) * @author [A. Gianotto] [] * @since [v1.7] * @param Request $request - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function update(Request $request) { diff --git a/app/Http/Controllers/CategoriesController.php b/app/Http/Controllers/CategoriesController.php index d7e83b43d814..c6a51f1b03df 100755 --- a/app/Http/Controllers/CategoriesController.php +++ b/app/Http/Controllers/CategoriesController.php @@ -7,7 +7,6 @@ use App\Models\Category as Category; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; -use Str; /** * This class controls all actions related to Categories for @@ -91,7 +90,7 @@ public function store(ImageUploadRequest $request) * @see CategoriesController::postEdit() method saves the data * @param int $categoryId * @since [v1.0] - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function edit($categoryId = null) @@ -178,7 +177,7 @@ public function destroy($categoryId) * @author [A. Gianotto] [] * @see CategoriesController::getDataView() method that generates the JSON response * @param $id - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException * @since [v1.8] */ diff --git a/app/Http/Controllers/CompaniesController.php b/app/Http/Controllers/CompaniesController.php index 63205a598d6f..646f2c438423 100644 --- a/app/Http/Controllers/CompaniesController.php +++ b/app/Http/Controllers/CompaniesController.php @@ -81,7 +81,7 @@ public function store(ImageUploadRequest $request) * @author [Abdullah Alansari] [] * @since [v1.8] * @param int $companyId - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function edit($companyId) diff --git a/app/Http/Controllers/Components/ComponentCheckinController.php b/app/Http/Controllers/Components/ComponentCheckinController.php index 9f4724e35315..b59237a5d0c8 100644 --- a/app/Http/Controllers/Components/ComponentCheckinController.php +++ b/app/Http/Controllers/Components/ComponentCheckinController.php @@ -95,7 +95,7 @@ public function store(Request $request, $component_asset_id, $backto = null) $asset = Asset::find($component_assets->asset_id); - event(new CheckoutableCheckedIn($component, $asset, Auth::user(), $request->input('note'), Carbon::now())); + event(new CheckoutableCheckedIn($component, $asset, auth()->user(), $request->input('note'), Carbon::now())); if ($backto == 'asset'){ return redirect()->route('hardware.show', $asset->id)->with('success', trans('admin/components/message.checkin.success')); diff --git a/app/Http/Controllers/Components/ComponentCheckoutController.php b/app/Http/Controllers/Components/ComponentCheckoutController.php index 79ff57e7df65..fc319b47deab 100644 --- a/app/Http/Controllers/Components/ComponentCheckoutController.php +++ b/app/Http/Controllers/Components/ComponentCheckoutController.php @@ -100,14 +100,14 @@ public function store(Request $request, $componentId) $component->asset_id = $request->input('asset_id'); $component->assets()->attach($component->id, [ 'component_id' => $component->id, - 'user_id' => Auth::user(), + 'user_id' => auth()->user(), 'created_at' => date('Y-m-d H:i:s'), 'assigned_qty' => $request->input('assigned_qty'), 'asset_id' => $request->input('asset_id'), 'note' => $request->input('note'), ]); - event(new CheckoutableCheckedOut($component, $asset, Auth::user(), $request->input('note'))); + event(new CheckoutableCheckedOut($component, $asset, auth()->user(), $request->input('note'))); return redirect()->route('components.index')->with('success', trans('admin/components/message.checkout.success')); } diff --git a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php index d838ee5696e9..919cd1698fab 100644 --- a/app/Http/Controllers/Consumables/ConsumableCheckoutController.php +++ b/app/Http/Controllers/Consumables/ConsumableCheckoutController.php @@ -82,7 +82,7 @@ public function store(Request $request, $consumableId) return redirect()->route('consumables.index')->with('error', trans('admin/consumables/message.checkout.unavailable')); } - $admin_user = Auth::user(); + $admin_user = auth()->user(); $assigned_to = e($request->input('assigned_to')); // Check if the user exists @@ -102,7 +102,7 @@ public function store(Request $request, $consumableId) 'note' => $request->input('note'), ]); } - event(new CheckoutableCheckedOut($consumable, $user, Auth::user(), $request->input('note'))); + event(new CheckoutableCheckedOut($consumable, $user, auth()->user(), $request->input('note'))); // Redirect to the new consumable page return redirect()->route('consumables.index')->with('success', trans('admin/consumables/message.checkout.success')); diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index c716fff368a6..74fff19a3733 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -35,6 +35,6 @@ abstract class Controller extends BaseController public function __construct() { view()->share('signedIn', Auth::check()); - view()->share('user', Auth::user()); + view()->share('user', auth()->user()); } } diff --git a/app/Http/Controllers/CustomFieldsController.php b/app/Http/Controllers/CustomFieldsController.php index 94b45552fdf7..43d2fbfab161 100644 --- a/app/Http/Controllers/CustomFieldsController.php +++ b/app/Http/Controllers/CustomFieldsController.php @@ -25,7 +25,7 @@ class CustomFieldsController extends Controller * * @author [Brady Wetherington] [] * @since [v1.8] - * @return \Illuminate\Support\Facades\View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function index() @@ -60,7 +60,7 @@ public function show() * @see CustomFieldsController::storeField() * @author [Brady Wetherington] [] * @since [v1.8] - * @return \Illuminate\Support\Facades\View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function create(Request $request) @@ -202,7 +202,7 @@ public function destroy($field_id) * @author [A. Gianotto] [] * @param int $id * @since [v4.0] - * @return \Illuminate\Support\Facades\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function edit(Request $request, $id) diff --git a/app/Http/Controllers/CustomFieldsetsController.php b/app/Http/Controllers/CustomFieldsetsController.php index e4382a31ea9a..64ba9dfb1c2b 100644 --- a/app/Http/Controllers/CustomFieldsetsController.php +++ b/app/Http/Controllers/CustomFieldsetsController.php @@ -34,7 +34,7 @@ public function index() * * @author [Brady Wetherington] [] * @param int $id - * @return \Illuminate\Support\Facades\View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException * @since [v1.8] */ @@ -70,7 +70,7 @@ public function show($id) * * @author [Brady Wetherington] [] * @since [v1.8] - * @return \Illuminate\Support\Facades\View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function create() @@ -95,7 +95,7 @@ public function store(Request $request) $fieldset = new CustomFieldset([ 'name' => $request->get('name'), - 'user_id' => Auth::user()->id, + 'user_id' => auth()->id(), ]); $validator = Validator::make($request->all(), $fieldset->rules); @@ -175,7 +175,7 @@ public function update(Request $request, $id) * @author [Brady Wetherington] [] * @param int $id * @since [v1.8] - * @return View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function destroy($id) @@ -203,7 +203,7 @@ public function destroy($id) * * @author [Brady Wetherington] [] * @since [v1.8] - * @return View + * @return \Illuminate\Contracts\View\View */ public function associate(Request $request, $id) { diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 89283a7c21ff..2366503c64e2 100755 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -21,12 +21,12 @@ class DashboardController extends Controller * * @author [A. Gianotto] [] * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function index() { // Show the page - if (Auth::user()->hasAccess('admin')) { + if (auth()->user()->hasAccess('admin')) { $asset_stats = null; $counts['asset'] = \App\Models\Asset::count(); @@ -34,7 +34,7 @@ public function index() $counts['license'] = \App\Models\License::assetcount(); $counts['consumable'] = \App\Models\Consumable::count(); $counts['component'] = \App\Models\Component::count(); - $counts['user'] = \App\Models\Company::scopeCompanyables(Auth::user())->count(); + $counts['user'] = \App\Models\Company::scopeCompanyables(auth()->user())->count(); $counts['grand_total'] = $counts['asset'] + $counts['accessory'] + $counts['license'] + $counts['consumable']; if ((! file_exists(storage_path().'/oauth-private.key')) || (! file_exists(storage_path().'/oauth-public.key'))) { diff --git a/app/Http/Controllers/DepartmentsController.php b/app/Http/Controllers/DepartmentsController.php index 62823098baa3..51de30c88ee6 100644 --- a/app/Http/Controllers/DepartmentsController.php +++ b/app/Http/Controllers/DepartmentsController.php @@ -4,6 +4,7 @@ use App\Http\Requests\ImageUploadRequest; use App\Models\Department; +use App\Models\Company; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; @@ -25,7 +26,7 @@ public function __construct() * @see AssetController::getDatatable() method that generates the JSON response * @since [v4.0] * @param Request $request - * @return \Illuminate\Support\Facades\View + * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ public function index(Request $request) @@ -45,7 +46,7 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param ImageUploadRequest $request - * @return \Illuminate\Http\Response + * @return \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function store(ImageUploadRequest $request) @@ -53,7 +54,7 @@ public function store(ImageUploadRequest $request) $this->authorize('create', Department::class); $department = new Department; $department->fill($request->all()); - $department->user_id = Auth::user()->id; + $department->user_id = auth()->id(); $department->manager_id = ($request->filled('manager_id') ? $request->input('manager_id') : null); $department->location_id = ($request->filled('location_id') ? $request->input('location_id') : null); $department->company_id = ($request->filled('company_id') ? $request->input('company_id') : null); @@ -73,7 +74,7 @@ public function store(ImageUploadRequest $request) * @author [A. Gianotto] [] * @param int $id * @since [v4.0] - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function show($id) @@ -145,7 +146,7 @@ public function destroy($id) * @see LocationsController::postCreate() method that validates and stores * @param int $departmentId * @since [v1.0] - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function edit($departmentId = null) @@ -159,6 +160,16 @@ public function edit($departmentId = null) return view('departments/edit', compact('item')); } + /** + * Save updated Department information. + * + * @author [A. Gianotto] [] + * @see LocationsController::postCreate() method that validates and stores + * @param int $departmentId + * @since [v1.0] + * @return \Illuminate\Http\RedirectResponse + * @throws \Illuminate\Auth\Access\AuthorizationException + */ public function update(ImageUploadRequest $request, $id) { if (is_null($department = Department::find($id))) { diff --git a/app/Http/Controllers/DepreciationsController.php b/app/Http/Controllers/DepreciationsController.php index 70bfb78cb543..f453e4998e7b 100755 --- a/app/Http/Controllers/DepreciationsController.php +++ b/app/Http/Controllers/DepreciationsController.php @@ -87,7 +87,7 @@ public function store(Request $request) * @see DepreciationsController::postEdit() * @param int $depreciationId * @since [v1.0] - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function edit($depreciationId = null) @@ -175,7 +175,7 @@ public function destroy($depreciationId) * @see DepreciationsController::postEdit() * @param int $depreciationId * @since [v1.0] - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function show($id) diff --git a/app/Http/Controllers/GroupsController.php b/app/Http/Controllers/GroupsController.php index bfc89075248f..9cb68e3a7a9a 100755 --- a/app/Http/Controllers/GroupsController.php +++ b/app/Http/Controllers/GroupsController.php @@ -64,7 +64,7 @@ public function store(Request $request) $group = new Group(); $group->name = $request->input('name'); $group->permissions = json_encode($request->input('permission')); - $group->created_by = Auth::user()->id; + $group->created_by = auth()->id(); if ($group->save()) { return redirect()->route('groups.index')->with('success', trans('admin/groups/message.success.create')); @@ -80,7 +80,7 @@ public function store(Request $request) * @see GroupsController::postEdit() * @param int $id * @since [v1.0] - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function edit($id) { @@ -154,7 +154,7 @@ public function destroy($id) * * @author [A. Gianotto] [] * @param $id - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @since [v4.0.11] */ public function show($id) diff --git a/app/Http/Controllers/HealthController.php b/app/Http/Controllers/HealthController.php index 456f6b6f9459..c75b903b0bcf 100644 --- a/app/Http/Controllers/HealthController.php +++ b/app/Http/Controllers/HealthController.php @@ -5,10 +5,13 @@ use Illuminate\Routing\Controller as BaseController; /** - * This controller provide the healthz route for + * This controller provide the health route for * the Snipe-IT Asset Management application. * - * @version v1.0 + * @version v1.0 + * + * @return \Illuminate\Http\JsonResponse + */ class HealthController extends BaseController { diff --git a/app/Http/Controllers/Kits/CheckoutKitController.php b/app/Http/Controllers/Kits/CheckoutKitController.php index d806f7d170f9..c75e4ea8f0cc 100644 --- a/app/Http/Controllers/Kits/CheckoutKitController.php +++ b/app/Http/Controllers/Kits/CheckoutKitController.php @@ -33,7 +33,7 @@ public function __construct(PredefinedKitCheckoutService $kitService) * Show Bulk Checkout Page * * @author [D. Minaev.] [] - * @return View View to checkout + * @return \Illuminate\Contracts\View\View View to checkout */ public function showCheckout($kit_id) { diff --git a/app/Http/Controllers/Kits/PredefinedKitsController.php b/app/Http/Controllers/Kits/PredefinedKitsController.php index 67d148b5eea2..187f5aad14cd 100644 --- a/app/Http/Controllers/Kits/PredefinedKitsController.php +++ b/app/Http/Controllers/Kits/PredefinedKitsController.php @@ -73,7 +73,7 @@ public function store(ImageUploadRequest $request) * @author [D. Minaev] [] * @since [v1.0] * @param int $kit_id - * @return View + * @return \Illuminate\Contracts\View\View */ public function edit($kit_id = null) { @@ -150,7 +150,7 @@ public function destroy($kit_id) * @author [D. Minaev] [] * @since [v1.0] * @param int $modelId - * @return View + * @return \Illuminate\Contracts\View\View */ public function show($kit_id = null) { @@ -162,7 +162,7 @@ public function show($kit_id = null) * * @author [D. Minaev] [] * @param int $kit_id - * @return View + * @return \Illuminate\Contracts\View\View */ public function editModel($kit_id, $model_id) { @@ -184,7 +184,7 @@ public function editModel($kit_id, $model_id) * * @author [D. Minaev] [] * @param int $modelId - * @return View + * @return \Illuminate\Contracts\View\View */ public function updateModel(Request $request, $kit_id, $model_id) { @@ -214,7 +214,7 @@ public function updateModel(Request $request, $kit_id, $model_id) * * @author [D. Minaev] [] * @param int $modelId - * @return View + * @return \Illuminate\Contracts\View\View */ public function detachModel($kit_id, $model_id) { @@ -237,7 +237,7 @@ public function detachModel($kit_id, $model_id) * @author [D. Minaev] [] * @param int $kit_id * @param int $license_id - * @return View + * @return \Illuminate\Contracts\View\View */ public function editLicense($kit_id, $license_id) { @@ -262,7 +262,7 @@ public function editLicense($kit_id, $license_id) * @author [D. Minaev] [] * @param int $kit_id * @param int $license_id - * @return View + * @return \Illuminate\Contracts\View\View */ public function updateLicense(Request $request, $kit_id, $license_id) { @@ -293,7 +293,7 @@ public function updateLicense(Request $request, $kit_id, $license_id) * @author [D. Minaev] [] * @param int $kit_id * @param int $license_id - * @return View + * @return \Illuminate\Contracts\View\View */ public function detachLicense($kit_id, $license_id) { @@ -316,7 +316,7 @@ public function detachLicense($kit_id, $license_id) * @author [D. Minaev] [] * @param int $kit_id * @param int $accessoryId - * @return View + * @return \Illuminate\Contracts\View\View */ public function editAccessory($kit_id, $accessory_id) { @@ -341,7 +341,7 @@ public function editAccessory($kit_id, $accessory_id) * @author [D. Minaev] [] * @param int $kit_id * @param int $accessory_id - * @return View + * @return \Illuminate\Contracts\View\View */ public function updateAccessory(Request $request, $kit_id, $accessory_id) { @@ -371,7 +371,7 @@ public function updateAccessory(Request $request, $kit_id, $accessory_id) * * @author [D. Minaev] [] * @param int $accessory_id - * @return View + * @return \Illuminate\Contracts\View\View */ public function detachAccessory($kit_id, $accessory_id) { @@ -394,7 +394,7 @@ public function detachAccessory($kit_id, $accessory_id) * @author [D. Minaev] [] * @param int $kit_id * @param int $consumable_id - * @return View + * @return \Illuminate\Contracts\View\View */ public function editConsumable($kit_id, $consumable_id) { @@ -419,7 +419,7 @@ public function editConsumable($kit_id, $consumable_id) * @author [D. Minaev] [] * @param int $kit_id * @param int $consumableId - * @return View + * @return \Illuminate\Contracts\View\View */ public function updateConsumable(Request $request, $kit_id, $consumable_id) { @@ -449,7 +449,7 @@ public function updateConsumable(Request $request, $kit_id, $consumable_id) * * @author [D. Minaev] [] * @param int $consumable_id - * @return View + * @return \Illuminate\Contracts\View\View */ public function detachConsumable($kit_id, $consumable_id) { diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index 1c5715b35f52..98b030d85ca5 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -14,8 +14,6 @@ use App\Models\Supplier; use App\Models\User; use App\View\Label as LabelView; -use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Storage; class LabelsController extends Controller { @@ -23,7 +21,7 @@ class LabelsController extends Controller * Returns the Label view with test data * * @param string $labelName - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @author Grant Le Roux */ public function show(string $labelName) @@ -96,6 +94,5 @@ public function show(string $labelName) ->with('bulkedit', false) ->with('count', 0); - return redirect()->route('home')->with('error', trans('admin/labels/message.does_not_exist')); } } diff --git a/app/Http/Controllers/Licenses/LicenseCheckinController.php b/app/Http/Controllers/Licenses/LicenseCheckinController.php index be71dfc4d2f1..e863aa860efb 100644 --- a/app/Http/Controllers/Licenses/LicenseCheckinController.php +++ b/app/Http/Controllers/Licenses/LicenseCheckinController.php @@ -102,7 +102,7 @@ public function store(Request $request, $seatId = null, $backTo = null) // Was the asset updated? if ($licenseSeat->save()) { - event(new CheckoutableCheckedIn($licenseSeat, $return_to, Auth::user(), $request->input('notes'))); + event(new CheckoutableCheckedIn($licenseSeat, $return_to, auth()->user(), $request->input('notes'))); if ($backTo == 'user') { return redirect()->route('users.show', $return_to->id)->with('success', trans('admin/licenses/message.checkin.success')); diff --git a/app/Http/Controllers/Licenses/LicenseCheckoutController.php b/app/Http/Controllers/Licenses/LicenseCheckoutController.php index d3887d365cf4..83bcb52a7cd1 100644 --- a/app/Http/Controllers/Licenses/LicenseCheckoutController.php +++ b/app/Http/Controllers/Licenses/LicenseCheckoutController.php @@ -119,7 +119,7 @@ protected function checkoutToAsset($licenseSeat) $licenseSeat->assigned_to = $target->assigned_to; } if ($licenseSeat->save()) { - event(new CheckoutableCheckedOut($licenseSeat, $target, Auth::user(), request('notes'))); + event(new CheckoutableCheckedOut($licenseSeat, $target, auth()->user(), request('notes'))); return true; } @@ -136,7 +136,7 @@ protected function checkoutToUser($licenseSeat) $licenseSeat->assigned_to = request('assigned_to'); if ($licenseSeat->save()) { - event(new CheckoutableCheckedOut($licenseSeat, $target, Auth::user(), request('notes'))); + event(new CheckoutableCheckedOut($licenseSeat, $target, auth()->user(), request('notes'))); return true; } diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php index 938c76fe1228..dcdd937429f7 100755 --- a/app/Http/Controllers/LocationsController.php +++ b/app/Http/Controllers/LocationsController.php @@ -100,7 +100,7 @@ public function store(ImageUploadRequest $request) * @see LocationsController::postCreate() method that validates and stores * @param int $locationId * @since [v1.0] - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function edit($locationId = null) @@ -202,7 +202,7 @@ public function destroy($locationId) * @author [A. Gianotto] [] * @param int $id * @since [v1.0] - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function show($id = null) { @@ -240,7 +240,7 @@ public function print_assigned($id) * @author [A. Gianotto] [] * @param int $locationId * @since [v6.0.14] - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function getClone($locationId = null) { @@ -282,7 +282,7 @@ public function print_all_assigned($id) * * @author [A. Gianotto] [] * @since [v6.3.1] - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function postBulkDelete(Request $request) { diff --git a/app/Http/Controllers/ManufacturersController.php b/app/Http/Controllers/ManufacturersController.php index 833c5b6977e7..78b7a04407d3 100755 --- a/app/Http/Controllers/ManufacturersController.php +++ b/app/Http/Controllers/ManufacturersController.php @@ -92,7 +92,7 @@ public function store(ImageUploadRequest $request) * @see ManufacturersController::update() * @param int $manufacturerId * @since [v1.0] - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function edit($manufacturerId = null) @@ -197,7 +197,7 @@ public function destroy($manufacturerId) * @author [A. Gianotto] [] * @param int $manufacturerId * @since [v1.0] - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function show($manufacturerId = null) @@ -238,7 +238,7 @@ public function restore($id) $logaction->item_type = Manufacturer::class; $logaction->item_id = $manufacturer->id; $logaction->created_at = date('Y-m-d H:i:s'); - $logaction->user_id = Auth::user()->id; + $logaction->user_id = auth()->id(); $logaction->logaction('restore'); // Redirect them to the deleted page if there are more, otherwise the section index diff --git a/app/Http/Controllers/ModalController.php b/app/Http/Controllers/ModalController.php index 6f6b39dd1230..fab491a5f444 100644 --- a/app/Http/Controllers/ModalController.php +++ b/app/Http/Controllers/ModalController.php @@ -15,7 +15,7 @@ class ModalController extends Controller * @version v5.3.7-pre * @author [Brady Wetherington] [] * @author [A. Gianotto] [authorize('self.profile'); - $user = Auth::user(); + $user = auth()->user(); return view('account/profile', compact('user')); } @@ -43,7 +43,7 @@ public function getIndex() public function postIndex(ImageUploadRequest $request) { $this->authorize('self.profile'); - $user = Auth::user(); + $user = auth()->user(); $user->first_name = $request->input('first_name'); $user->last_name = $request->input('last_name'); $user->website = $request->input('website'); @@ -97,11 +97,11 @@ public function api(): \Illuminate\Contracts\View\View /** * User change email page. * - * @return View + * @return \Illuminate\Contracts\View\View */ public function password() { - $user = Auth::user(); + $user = auth()->user(); return view('account/change-password', compact('user')); } @@ -117,7 +117,7 @@ public function passwordSave(Request $request) return redirect()->route('account.password.index')->with('error', trans('admin/users/table.lock_passwords')); } - $user = Auth::user(); + $user = auth()->user(); if ($user->ldap_import == '1') { return redirect()->route('account.password.index')->with('error', trans('admin/users/message.error.password_ldap')); } @@ -178,7 +178,7 @@ public function passwordSave(Request $request) * * @author [A. Gianotto] [] * @since [v4.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function getMenuState(Request $request) { @@ -199,10 +199,10 @@ public function getMenuState(Request $request) */ public function printInventory() { - $show_user = Auth::user(); + $show_user = auth()->user(); return view('users/print') - ->with('assets', Auth::user()->assets) + ->with('assets', auth()->user()->assets) ->with('licenses', $show_user->licenses()->get()) ->with('accessories', $show_user->accessories()->get()) ->with('consumables', $show_user->consumables()->get()) @@ -220,7 +220,7 @@ public function printInventory() public function emailAssetList() { - if (!$user = User::find(Auth::user()->id)) { + if (!$user = User::find(auth()->id())) { return redirect()->back() ->with('error', trans('admin/users/message.user_not_found', ['id' => $id])); } diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index 67a73d0dc0e2..16c2c338937e 100644 --- a/app/Http/Controllers/ReportsController.php +++ b/app/Http/Controllers/ReportsController.php @@ -47,7 +47,7 @@ public function __construct() * * @author [A. Gianotto] [] * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function getAccessoryReport() { @@ -104,7 +104,7 @@ public function exportAccessoryReport() * * @author [A. Gianotto] [] * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function getDeprecationReport() { @@ -197,7 +197,7 @@ public function exportDeprecationReport() * * @author [A. Gianotto] [] * @since [v4.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function audit() { @@ -212,7 +212,7 @@ public function audit() * * @author [A. Gianotto] [] * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function getActivityReport() { @@ -328,7 +328,7 @@ public function postActivityReport(Request $request) * * @author [A. Gianotto] [] * @since [v1.0] - * @return View + * @return \Illuminate\Contracts\View\View */ public function getLicenseReport() { @@ -1015,7 +1015,7 @@ public function postCustom(CustomAssetReportRequest $request) /** * getImprovementsReport * - * @return View + * @return \Illuminate\Contracts\View\View * @author Vincent Sposato * @version v1.0 */ diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 7fefbf909022..0fa308410934 100755 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -19,7 +19,7 @@ use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Storage; use Illuminate\Validation\Rule; -use Redirect; +use Illuminate\View\View; use Illuminate\Support\Str; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Auth; @@ -47,7 +47,7 @@ class SettingsController extends Controller * * @since [v3.0] * - * @return View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\Response */ public function getSetupIndex() { @@ -215,7 +215,7 @@ public function postSaveFirstAdmin(SetupUserRequest $request) * * @since [v3.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getSetupUser() { @@ -231,7 +231,7 @@ public function getSetupUser() * * @since [v3.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getSetupDone() { @@ -248,7 +248,7 @@ public function getSetupDone() * * @since [v3.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getSetupMigrate() { @@ -271,7 +271,7 @@ public function getSetupMigrate() * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function index() { @@ -287,7 +287,7 @@ public function index() * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getEdit() { @@ -303,7 +303,7 @@ public function getEdit() * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getSettings() { @@ -319,7 +319,7 @@ public function getSettings() * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function postSettings(Request $request) { @@ -376,7 +376,7 @@ public function postSettings(Request $request) * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getBranding() { @@ -494,7 +494,7 @@ public function postBranding(ImageUploadRequest $request) * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getSecurity() { @@ -510,7 +510,7 @@ public function getSecurity() * * @since [v1.0] * - * @return View + * @return \Illuminate\Http\RedirectResponse */ public function postSecurity(Request $request) { @@ -568,7 +568,7 @@ public function postSecurity(Request $request) * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getLocalization() { @@ -584,7 +584,7 @@ public function getLocalization() * * @since [v1.0] * - * @return View + * @return \Illuminate\Http\RedirectResponse */ public function postLocalization(Request $request) { @@ -616,7 +616,7 @@ public function postLocalization(Request $request) * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getAlerts() { @@ -632,7 +632,7 @@ public function getAlerts() * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function postAlerts(Request $request) { @@ -689,7 +689,7 @@ public function postAlerts(Request $request) * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getSlack() { @@ -705,7 +705,7 @@ public function getSlack() * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getAssetTags() { @@ -721,7 +721,7 @@ public function getAssetTags() * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function postAssetTags(Request $request) { @@ -749,7 +749,7 @@ public function postAssetTags(Request $request) * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getBarcodes() { @@ -766,7 +766,7 @@ public function getBarcodes() * * @since [v1.0] * - * @return View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function postBarcodes(Request $request) { @@ -795,7 +795,7 @@ public function postBarcodes(Request $request) * * @since [v4.0] * - * @return View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function getPhpInfo() { @@ -814,7 +814,7 @@ public function getPhpInfo() * * @since [v4.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getLabels() { @@ -830,7 +830,7 @@ public function getLabels() * * @since [v4.0] * - * @return View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function postLabels(Request $request) { @@ -907,7 +907,7 @@ public function postLabels(Request $request) * * @since [v4.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getLdapSettings() { @@ -942,7 +942,7 @@ public function getLdapSettings() * * @since [v4.0] * - * @return View + * @return \Illuminate\Http\RedirectResponse */ public function postLdapSettings(Request $request) { @@ -1001,7 +1001,7 @@ public function postLdapSettings(Request $request) * * @since v5.0.0 * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getSamlSettings() { @@ -1017,7 +1017,7 @@ public function getSamlSettings() * * @since v5.0.0 * - * @return View + * @return \Illuminate\Http\RedirectResponse */ public function postSamlSettings(SettingsSamlRequest $request) { @@ -1061,7 +1061,7 @@ public static function getPDFBranding() * * @author [A. Gianotto] [] * @since [v6.1.1] - * @return View + * @return \Illuminate\Contracts\View\View */ public function getGoogleLoginSettings() { @@ -1074,7 +1074,7 @@ public function getGoogleLoginSettings() * * @author [A. Gianotto] [] * @since [v6.1.1] - * @return View + * @return \Illuminate\Http\RedirectResponse */ public function postGoogleLoginSettings(Request $request) { @@ -1104,7 +1104,7 @@ public function postGoogleLoginSettings(Request $request) * * @since [v1.8] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function getBackups() { @@ -1179,7 +1179,7 @@ public function postBackups() * * @since [v1.8] * - * @return Storage + * @return \Illuminate\Http\RedirectResponse */ public function downloadFile($filename = null) { @@ -1205,7 +1205,7 @@ public function downloadFile($filename = null) * * @since [v1.8] * - * @return View + * @return \Illuminate\Http\RedirectResponse */ public function deleteFile($filename = null) { @@ -1231,7 +1231,7 @@ public function deleteFile($filename = null) } // Hell to the no - Log::warning('User ID '.Auth::user()->id.' is attempting to delete backup file '.$filename.' and is not authorized to.'); + Log::warning('User ID '.auth()->id().' is attempting to delete backup file '.$filename.' and is not authorized to.'); return redirect()->route('settings.backups.index')->with('error', trans('general.backup_delete_not_allowed')); } @@ -1281,7 +1281,7 @@ public function postUploadBackup(Request $request) { * * @since [v6.0] * - * @return View + * @return \Illuminate\Http\RedirectResponse */ public function postRestore($filename = null) { @@ -1292,7 +1292,7 @@ public function postRestore($filename = null) if (Storage::exists($path.'/'.$filename)) { // grab the user's info so we can make sure they exist in the system - $user = User::find(Auth::user()->id); + $user = User::find(auth()->id()); // TODO: run a backup @@ -1352,12 +1352,12 @@ public function postRestore($filename = null) * * @since [v4.0] * - * @return View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ public function getPurge() { - Log::warning('User '.Auth::user()->username.' (ID'.Auth::user()->id.') is attempting a PURGE'); + Log::warning('User '.auth()->user()->username.' (ID'.auth()->id().') is attempting a PURGE'); if (config('app.allow_purge')=='true') { return view('settings.purge-form'); @@ -1373,11 +1373,11 @@ public function getPurge() * * @since [v3.0] * - * @return View + * @return \Illuminate\Http\RedirectResponse */ public function postPurge(Request $request) { - Log::warning('User '.Auth::user()->username.' (ID'.Auth::user()->id.') is attempting a PURGE'); + Log::warning('User '.auth()->user()->username.' (ID'.auth()->id().') is attempting a PURGE'); if (config('app.allow_purge')=='true') { Log::debug('Purging is not allowed via the .env'); @@ -1386,7 +1386,7 @@ public function postPurge(Request $request) if ($request->input('confirm_purge')=='DELETE') { - Log::warning('User ID ' . Auth::user()->id . ' initiated a PURGE!'); + Log::warning('User ID ' . auth()->id() . ' initiated a PURGE!'); // Run a backup immediately before processing Artisan::call('backup:run'); Artisan::call('snipeit:purge', ['--force' => 'true', '--no-interaction' => true]); @@ -1404,7 +1404,7 @@ public function postPurge(Request $request) } } - Log::error('User '.Auth::user()->username.' (ID'.Auth::user()->id.') is attempting to purge deleted data and is not authorized to.'); + Log::error('User '.auth()->user()->username.' (ID'.auth()->id().') is attempting to purge deleted data and is not authorized to.'); // Nope. @@ -1422,7 +1422,7 @@ public function postPurge(Request $request) * * @since [v4.0] * - * @return View + * @return \Illuminate\Contracts\View\View */ public function api() { @@ -1436,7 +1436,7 @@ public function api() * * @since [v3.0] * - * @return \Illuminate\Http\RedirectResponse + * @return \Illuminate\Http\JsonResponse */ public function ajaxTestEmail() { @@ -1446,12 +1446,21 @@ public function ajaxTestEmail() 'email' => config('mail.from.address'), ])->notify(new MailTest()); - return response()->json(Helper::formatStandardApiResponse('success', null, 'Maiol sent!')); + return response()->json(Helper::formatStandardApiResponse('success', null, trans('mail_sent.mail_sent'))); } catch (\Exception $e) { return response()->json(Helper::formatStandardApiResponse('success', null, $e->getMessage())); } } + + + /** + * Get login attempts view + * + * @author [A. Gianotto] [] + * + * @return \Illuminate\Contracts\View\View + */ public function getLoginAttempts() { return view('settings.logins'); diff --git a/app/Http/Controllers/StatuslabelsController.php b/app/Http/Controllers/StatuslabelsController.php index 0c559560e03e..aba678d47c9b 100755 --- a/app/Http/Controllers/StatuslabelsController.php +++ b/app/Http/Controllers/StatuslabelsController.php @@ -96,7 +96,7 @@ public function store(Request $request) * Statuslabel update. * * @param int $statuslabelId - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function edit($statuslabelId = null) diff --git a/app/Http/Controllers/SuppliersController.php b/app/Http/Controllers/SuppliersController.php index b901224714e9..86b6147bd45d 100755 --- a/app/Http/Controllers/SuppliersController.php +++ b/app/Http/Controllers/SuppliersController.php @@ -82,7 +82,7 @@ public function store(ImageUploadRequest $request) * Supplier update. * * @param int $supplierId - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ public function edit($supplierId = null) @@ -174,7 +174,7 @@ public function destroy($supplierId) * Get the asset information to present to the supplier view page * * @param null $supplierId - * @return \Illuminate\Contracts\View\View + * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @internal param int $assetId */ public function show($supplierId = null) diff --git a/app/Http/Controllers/Users/BulkUsersController.php b/app/Http/Controllers/Users/BulkUsersController.php index efac5a8d334e..b0683e2cbc62 100644 --- a/app/Http/Controllers/Users/BulkUsersController.php +++ b/app/Http/Controllers/Users/BulkUsersController.php @@ -102,7 +102,7 @@ public function update(Request $request) // Remove the user from any updates. $user_raw_array = array_diff($user_raw_array, [Auth::id()]); $manager_conflict = false; - $users = User::whereIn('id', $user_raw_array)->where('id', '!=', Auth::user()->id)->get(); + $users = User::whereIn('id', $user_raw_array)->where('id', '!=', auth()->id())->get(); $return_array = [ 'success' => trans('admin/users/message.success.update_bulk'), @@ -318,7 +318,7 @@ public function merge(Request $request) // Get the users $merge_into_user = User::find($request->input('merge_into_id')); $users_to_merge = User::whereIn('id', $user_ids_to_merge)->with('assets', 'manager', 'userlog', 'licenses', 'consumables', 'accessories', 'managedLocations','uploads', 'acceptances')->get(); - $admin = User::find(Auth::user()->id); + $admin = User::find(auth()->id()); // Walk users foreach ($users_to_merge as $user_to_merge) { diff --git a/app/Http/Controllers/Users/UsersController.php b/app/Http/Controllers/Users/UsersController.php index 0b16dd0a958e..7cff29fe59f0 100755 --- a/app/Http/Controllers/Users/UsersController.php +++ b/app/Http/Controllers/Users/UsersController.php @@ -117,7 +117,7 @@ public function store(SaveUserRequest $request) $user->zip = $request->input('zip', null); $user->remote = $request->input('remote', 0); $user->website = $request->input('website', null); - $user->created_by = Auth::user()->id; + $user->created_by = auth()->id(); $user->start_date = $request->input('start_date', null); $user->end_date = $request->input('end_date', null); $user->autoassign_licenses = $request->input('autoassign_licenses', 0); @@ -125,7 +125,7 @@ public function store(SaveUserRequest $request) // Strip out the superuser permission if the user isn't a superadmin $permissions_array = $request->input('permission'); - if (! Auth::user()->isSuperUser()) { + if (! auth()->user()->isSuperUser()) { unset($permissions_array['superuser']); } $user->permissions = json_encode($permissions_array); @@ -176,7 +176,7 @@ private function filterDisplayable($permissions) * @author [A. Gianotto] [] * @since [v1.0] * @param $permissions - * @return View + * @return \Illuminate\Contracts\View\View * @internal param int $id * @throws \Illuminate\Auth\Access\AuthorizationException */ @@ -246,7 +246,7 @@ public function update(SaveUserRequest $request, $id = null) } // Only save groups if the user is a superuser - if (Auth::user()->isSuperUser()) { + if (auth()->user()->isSuperUser()) { $user->groups()->sync($request->input('groups')); } @@ -300,7 +300,7 @@ public function update(SaveUserRequest $request, $id = null) // Strip out the superuser permission if the user isn't a superadmin - if (! Auth::user()->isSuperUser()) { + if (! auth()->user()->isSuperUser()) { unset($permissions_array['superuser']); $permissions_array['superuser'] = $orig_superuser; } @@ -379,7 +379,7 @@ public function getRestore($id = null) $logaction->item_type = User::class; $logaction->item_id = $user->id; $logaction->created_at = date('Y-m-d H:i:s'); - $logaction->user_id = Auth::user()->id; + $logaction->user_id = auth()->id(); $logaction->logaction('restore'); // Redirect them to the deleted page if there are more, otherwise the section index diff --git a/app/Http/Controllers/ViewAssetsController.php b/app/Http/Controllers/ViewAssetsController.php index cbff99937d4d..87a993fa1f72 100755 --- a/app/Http/Controllers/ViewAssetsController.php +++ b/app/Http/Controllers/ViewAssetsController.php @@ -36,7 +36,7 @@ public function getIndex() 'consumables', 'accessories', 'licenses', - )->find(Auth::user()->id); + )->find(auth()->id()); $field_array = array(); @@ -92,7 +92,7 @@ public function getRequestItem(Request $request, $itemType, $itemId = null, $can } $item = call_user_func([$fullItemType, 'find'], $itemId); - $user = Auth::user(); + $user = auth()->user(); $logaction = new Actionlog(); $logaction->item_id = $data['asset_id'] = $item->id; @@ -102,14 +102,15 @@ public function getRequestItem(Request $request, $itemType, $itemId = null, $can if ($user->location_id) { $logaction->location_id = $user->location_id; } - $logaction->target_id = $data['user_id'] = Auth::user()->id; + + $logaction->target_id = $data['user_id'] = auth()->id(); $logaction->target_type = User::class; $data['item_quantity'] = $request->has('request-quantity') ? e($request->input('request-quantity')) : 1; $data['requested_by'] = $user->present()->fullName(); $data['item'] = $item; $data['item_type'] = $itemType; - $data['target'] = Auth::user(); + $data['target'] = auth()->user(); if ($fullItemType == Asset::class) { $data['item_url'] = route('hardware.show', $item->id); @@ -147,7 +148,7 @@ public function getRequestItem(Request $request, $itemType, $itemId = null, $can */ public function getRequestAsset($assetId = null) { - $user = Auth::user(); + $user = auth()->user(); // Check if the asset exists and is requestable if (is_null($asset = Asset::RequestableAssets()->find($assetId))) { @@ -160,7 +161,7 @@ public function getRequestAsset($assetId = null) } $data['item'] = $asset; - $data['target'] = Auth::user(); + $data['target'] = auth()->user(); $data['item_quantity'] = 1; $settings = Setting::getSettings(); @@ -172,11 +173,11 @@ public function getRequestAsset($assetId = null) if ($user->location_id) { $logaction->location_id = $user->location_id; } - $logaction->target_id = $data['user_id'] = Auth::user()->id; + $logaction->target_id = $data['user_id'] = auth()->id(); $logaction->target_type = User::class; // If it's already requested, cancel the request. - if ($asset->isRequestedBy(Auth::user())) { + if ($asset->isRequestedBy(auth()->user())) { $asset->cancelRequest(); $asset->decrement('requests_counter', 1); diff --git a/app/Http/Middleware/CheckForDebug.php b/app/Http/Middleware/CheckForDebug.php index 85311cf5390c..3bf919f1f2fc 100644 --- a/app/Http/Middleware/CheckForDebug.php +++ b/app/Http/Middleware/CheckForDebug.php @@ -18,7 +18,7 @@ public function handle($request, Closure $next) { view()->share('debug_in_production', false); - if (((Auth::check() && (Auth::user()->isSuperUser()))) && (app()->environment() == 'production') && (config('app.warn_debug') === true) && (config('app.debug') === true)) { + if (((Auth::check() && (auth()->user()->isSuperUser()))) && (app()->environment() == 'production') && (config('app.warn_debug') === true) && (config('app.debug') === true)) { view()->share('debug_in_production', true); } diff --git a/app/Http/Middleware/CheckForTwoFactor.php b/app/Http/Middleware/CheckForTwoFactor.php index 7846d00ade83..b94423d20590 100644 --- a/app/Http/Middleware/CheckForTwoFactor.php +++ b/app/Http/Middleware/CheckForTwoFactor.php @@ -38,17 +38,17 @@ public function handle($request, Closure $next) if ($settings = Setting::getSettings()) { if (Auth::check() && ($settings->two_factor_enabled != '')) { // This user is already 2fa-authed - if ($request->session()->get('2fa_authed')==Auth::user()->id) { + if ($request->session()->get('2fa_authed')==auth()->id()) { return $next($request); } // Two-factor is optional and the user has NOT opted in, let them through - if (($settings->two_factor_enabled == '1') && (Auth::user()->two_factor_optin != '1')) { + if (($settings->two_factor_enabled == '1') && (auth()->user()->two_factor_optin != '1')) { return $next($request); } // Otherwise make sure they're enrolled and show them the 2FA code screen - if ((Auth::user()->two_factor_secret != '') && (Auth::user()->two_factor_enrolled == '1')) { + if ((auth()->user()->two_factor_secret != '') && (auth()->user()->two_factor_enrolled == '1')) { return redirect()->route('two-factor')->with('info', trans('auth/message.two_factor.enter_two_factor_code')); } diff --git a/app/Http/Requests/DeleteUserRequest.php b/app/Http/Requests/DeleteUserRequest.php index 282919d7ae4a..99574950d880 100644 --- a/app/Http/Requests/DeleteUserRequest.php +++ b/app/Http/Requests/DeleteUserRequest.php @@ -29,7 +29,7 @@ public function prepareForValidation(): void if ($user_to_delete) { $this->merge([ 'user' => request()->route('user'), - 'admin_id' => Auth::user()->id, + 'admin_id' => auth()->id(), 'managed_users' => $user_to_delete->managesUsers()->count(), 'managed_locations' => $user_to_delete->managedLocations()->count(), 'assigned_assets' => $user_to_delete->assets()->count(), @@ -48,7 +48,7 @@ public function prepareForValidation(): void public function rules(): array { return [ - 'user' => Rule::notIn([auth()->user()->id]), + 'user' => Rule::notIn([auth()->id()]), 'managed_users' => Rule::in([0]), 'managed_locations' => Rule::in([0]), 'assigned_assets' => Rule::in([0]), diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index eeb10eb0d2ed..d3a19929e1eb 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -231,7 +231,7 @@ public function transformRequestedAsset(Asset $asset) 'expected_checkin' => Helper::getFormattedDateObject($asset->expected_checkin, 'date'), 'location' => ($asset->location) ? e($asset->location->name) : null, 'status'=> ($asset->assetstatus) ? $asset->present()->statusMeta : null, - 'assigned_to_self' => ($asset->assigned_to == \Auth::user()->id), + 'assigned_to_self' => ($asset->assigned_to == auth()->id()), ]; if (($asset->model) && ($asset->model->fieldset) && ($asset->model->fieldset->fields->count() > 0)) { @@ -258,8 +258,8 @@ public function transformRequestedAsset(Asset $asset) $permissions_array['available_actions'] = [ - 'cancel' => ($asset->isRequestedBy(\Auth::user())) ? true : false, - 'request' => ($asset->isRequestedBy(\Auth::user())) ? false : true, + 'cancel' => ($asset->isRequestedBy(auth()->user())) ? true : false, + 'request' => ($asset->isRequestedBy(auth()->user())) ? false : true, ]; $array += $permissions_array; diff --git a/app/Importer/AssetImporter.php b/app/Importer/AssetImporter.php index 02ffa2077c86..c32a9fb4c66b 100644 --- a/app/Importer/AssetImporter.php +++ b/app/Importer/AssetImporter.php @@ -184,7 +184,7 @@ public function createAssetIfNotExists(array $row) if (isset($target) && ($target !== false)) { if (!is_null($asset->assigned_to)){ if ($asset->assigned_to != $target->id) { - event(new CheckoutableCheckedIn($asset, User::find($asset->assigned_to), Auth::user(), 'Checkin from CSV Importer', $checkin_date)); + event(new CheckoutableCheckedIn($asset, User::find($asset->assigned_to), auth()->user(), 'Checkin from CSV Importer', $checkin_date)); } } diff --git a/app/Importer/LocationImporter.php b/app/Importer/LocationImporter.php index 3b1b65b08f0c..e344b6beaf48 100644 --- a/app/Importer/LocationImporter.php +++ b/app/Importer/LocationImporter.php @@ -65,7 +65,7 @@ public function createLocationIfNotExists(array $row) $this->item['ldap_ou'] = trim($this->findCsvMatch($row, 'ldap_ou')); $this->item['manager'] = trim($this->findCsvMatch($row, 'manager')); $this->item['manager_username'] = trim($this->findCsvMatch($row, 'manager_username')); - $this->item['user_id'] = \Auth::user()->id; + $this->item['user_id'] = auth()->id(); if ($this->findCsvMatch($row, 'parent_location')) { $this->item['parent_id'] = $this->createOrFetchLocation(trim($this->findCsvMatch($row, 'parent_location'))); diff --git a/app/Listeners/CheckoutableListener.php b/app/Listeners/CheckoutableListener.php index 4bc367f02568..63e552233c4a 100644 --- a/app/Listeners/CheckoutableListener.php +++ b/app/Listeners/CheckoutableListener.php @@ -69,9 +69,9 @@ public function onCheckedOut($event) } } } catch (ClientException $e) { - Log::warning("Exception caught during checkout notification: " . $e->getMessage()); + Log::debug("Exception caught during checkout notification: " . $e->getMessage()); } catch (Exception $e) { - Log::warning("Exception caught during checkout notification: " . $e->getMessage()); + Log::debug("Exception caught during checkout notification: " . $e->getMessage()); } } diff --git a/app/Livewire/OauthClients.php b/app/Livewire/OauthClients.php index 02fc77848b0a..fda91260c8e3 100644 --- a/app/Livewire/OauthClients.php +++ b/app/Livewire/OauthClients.php @@ -22,8 +22,8 @@ class OauthClients extends Component public function render() { return view('livewire.oauth-clients', [ - 'clients' => app(ClientRepository::class)->activeForUser(auth()->user()->id), - 'authorized_tokens' => app(TokenRepository::class)->forUser(auth()->user()->id)->where('revoked', false), + 'clients' => app(ClientRepository::class)->activeForUser(auth()->id()), + 'authorized_tokens' => app(TokenRepository::class)->forUser(auth()->id())->where('revoked', false), ]); } @@ -35,7 +35,7 @@ public function createClient(): void ]); app(ClientRepository::class)->create( - auth()->user()->id, + auth()->id(), $this->name, $this->redirect, ); @@ -47,10 +47,10 @@ public function deleteClient(Client $clientId): void { // test for safety // ->delete must be of type Client - thus the model binding - if ($clientId->user_id == auth()->user()->id) { + if ($clientId->user_id == auth()->id()) { app(ClientRepository::class)->delete($clientId); } else { - Log::warning('User ' . auth()->user()->id . ' attempted to delete client ' . $clientId->id . ' which belongs to user ' . $clientId->user_id); + Log::warning('User ' . auth()->id() . ' attempted to delete client ' . $clientId->id . ' which belongs to user ' . $clientId->user_id); $this->authorizationError = 'You are not authorized to delete this client.'; } } @@ -58,10 +58,10 @@ public function deleteClient(Client $clientId): void public function deleteToken($tokenId): void { $token = app(TokenRepository::class)->find($tokenId); - if ($token->user_id == auth()->user()->id) { + if ($token->user_id == auth()->id()) { app(TokenRepository::class)->revokeAccessToken($tokenId); } else { - Log::warning('User ' . auth()->user()->id . ' attempted to delete token ' . $tokenId . ' which belongs to user ' . $token->user_id); + Log::warning('User ' . auth()->id() . ' attempted to delete token ' . $tokenId . ' which belongs to user ' . $token->user_id); $this->authorizationError = 'You are not authorized to delete this token.'; } } @@ -84,12 +84,12 @@ public function updateClient(Client $editClientId): void ]); $client = app(ClientRepository::class)->find($editClientId->id); - if ($client->user_id == auth()->user()->id) { + if ($client->user_id == auth()->id()) { $client->name = $this->editName; $client->redirect = $this->editRedirect; $client->save(); } else { - Log::warning('User ' . auth()->user()->id . ' attempted to edit client ' . $editClientId->id . ' which belongs to user ' . $client->user_id); + Log::warning('User ' . auth()->id() . ' attempted to edit client ' . $editClientId->id . ' which belongs to user ' . $client->user_id); $this->authorizationError = 'You are not authorized to edit this client.'; } diff --git a/app/Livewire/PersonalAccessTokens.php b/app/Livewire/PersonalAccessTokens.php index 69bf1d28a154..79543a609e8b 100644 --- a/app/Livewire/PersonalAccessTokens.php +++ b/app/Livewire/PersonalAccessTokens.php @@ -23,7 +23,7 @@ public function autoFocusModalEvent(): void public function render() { return view('livewire.personal-access-tokens', [ - 'tokens' => Auth::user()->tokens, + 'tokens' => auth()->user()->tokens, ]); } @@ -38,7 +38,7 @@ public function createToken(): void { $this->validate(); - $newToken = Auth::user()->createToken($this->name); + $newToken = auth()->user()->createToken($this->name); $this->newTokenString = $newToken->accessToken; @@ -49,6 +49,6 @@ public function deleteToken($tokenId): void { //this needs safety (though the scope of auth::user might kind of do it...) //seems like it does, test more - Auth::user()->tokens()->find($tokenId)?->delete(); + auth()->user()->tokens()->find($tokenId)?->delete(); } } diff --git a/app/Models/Actionlog.php b/app/Models/Actionlog.php index 90e0e884f8b5..33e9707f2ad2 100755 --- a/app/Models/Actionlog.php +++ b/app/Models/Actionlog.php @@ -4,7 +4,7 @@ use App\Models\Traits\Searchable; use App\Presenters\Presentable; -use Carbon; +use Carbon\Carbon; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Support\Facades\Auth; @@ -81,14 +81,14 @@ public static function boot() parent::boot(); static::creating(function (self $actionlog) { // If the admin is a superadmin, let's see if the target instead has a company. - if (Auth::user() && Auth::user()->isSuperUser()) { + if (auth()->user() && auth()->user()->isSuperUser()) { if ($actionlog->target) { $actionlog->company_id = $actionlog->target->company_id; } elseif ($actionlog->item) { $actionlog->company_id = $actionlog->item->company_id; } - } elseif (Auth::user() && Auth::user()->company) { - $actionlog->company_id = Auth::user()->company_id; + } elseif (auth()->user() && auth()->user()->company) { + $actionlog->company_id = auth()->user()->company_id; } }); } diff --git a/app/Models/Asset.php b/app/Models/Asset.php index a3d603551ff0..5f5e0cd603f3 100644 --- a/app/Models/Asset.php +++ b/app/Models/Asset.php @@ -369,7 +369,7 @@ public function checkOut($target, $admin = null, $checkout_at = null, $expected_ } elseif (get_class($admin) === \App\Models\User::class) { $checkedOutBy = $admin; } else { - $checkedOutBy = Auth::user(); + $checkedOutBy = auth()->user(); } event(new CheckoutableCheckedOut($this, $target, $checkedOutBy, $note, $originalValues)); diff --git a/app/Models/Company.php b/app/Models/Company.php index ea8a28b7e154..657b34390b7a 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -106,7 +106,7 @@ public static function getIdForCurrentUser($unescaped_input) if (! static::isFullMultipleCompanySupportEnabled()) { return static::getIdFromInput($unescaped_input); } else { - $current_user = Auth::user(); + $current_user = auth()->user(); // Super users should be able to set a company to whatever they need if ($current_user->isSuperUser()) { @@ -157,24 +157,24 @@ public static function isCurrentUserHasAccess($companyable) } - if (Auth::user()) { + if (auth()->user()) { Log::warning('Companyable is '.$companyable); - $current_user_company_id = Auth::user()->company_id; + $current_user_company_id = auth()->user()->company_id; $companyable_company_id = $companyable->company_id; - return $current_user_company_id == null || $current_user_company_id == $companyable_company_id || Auth::user()->isSuperUser(); + return $current_user_company_id == null || $current_user_company_id == $companyable_company_id || auth()->user()->isSuperUser(); } } public static function isCurrentUserAuthorized() { - return (! static::isFullMultipleCompanySupportEnabled()) || (Auth::user()->isSuperUser()); + return (! static::isFullMultipleCompanySupportEnabled()) || (auth()->user()->isSuperUser()); } public static function canManageUsersCompanies() { - return ! static::isFullMultipleCompanySupportEnabled() || Auth::user()->isSuperUser() || - Auth::user()->company_id == null; + return ! static::isFullMultipleCompanySupportEnabled() || auth()->user()->isSuperUser() || + auth()->user()->company_id == null; } /** @@ -200,7 +200,7 @@ public function isDeletable() */ public static function getIdForUser($unescaped_input) { - if (! static::isFullMultipleCompanySupportEnabled() || Auth::user()->isSuperUser()) { + if (! static::isFullMultipleCompanySupportEnabled() || auth()->user()->isSuperUser()) { return static::getIdFromInput($unescaped_input); } else { return static::getIdForCurrentUser($unescaped_input); @@ -259,7 +259,7 @@ public function components() public static function scopeCompanyables($query, $column = 'company_id', $table_name = null) { // If not logged in and hitting this, assume we are on the command line and don't scope?' - if (! static::isFullMultipleCompanySupportEnabled() || (Auth::hasUser() && Auth::user()->isSuperUser()) || (! Auth::hasUser())) { + if (! static::isFullMultipleCompanySupportEnabled() || (Auth::hasUser() && auth()->user()->isSuperUser()) || (! Auth::hasUser())) { return $query; } else { return static::scopeCompanyablesDirectly($query, $column, $table_name); @@ -277,7 +277,7 @@ private static function scopeCompanyablesDirectly($query, $column = 'company_id' // Get the company ID of the logged-in user, or set it to null if there is no company associated with the user if (Auth::hasUser()) { - $company_id = Auth::user()->company_id; + $company_id = auth()->user()->company_id; } else { $company_id = null; } @@ -309,7 +309,7 @@ public static function scopeCompanyableChildren(array $companyable_names, $query if (count($companyable_names) == 0) { throw new Exception('No Companyable Children to scope'); - } elseif (! static::isFullMultipleCompanySupportEnabled() || (Auth::hasUser() && Auth::user()->isSuperUser())) { + } elseif (! static::isFullMultipleCompanySupportEnabled() || (Auth::hasUser() && auth()->user()->isSuperUser())) { return $query; } else { $f = function ($q) { diff --git a/app/Models/Loggable.php b/app/Models/Loggable.php index f855b4125373..ae5d554882d5 100644 --- a/app/Models/Loggable.php +++ b/app/Models/Loggable.php @@ -36,8 +36,8 @@ public function logCheckout($note, $target, $action_date = null, $originalValues { $log = new Actionlog; $log = $this->determineLogItemType($log); - if (Auth::user()) { - $log->user_id = Auth::user()->id; + if (auth()->user()) { + $log->user_id = auth()->id(); } if (! isset($target)) { @@ -148,8 +148,8 @@ public function logCheckin($target, $note, $action_date = null, $originalValues $log->action_date = date('Y-m-d H:i:s'); } - if (Auth::user()) { - $log->user_id = Auth::user()->id; + if (auth()->user()) { + $log->user_id = auth()->id(); } $changed = []; @@ -225,7 +225,7 @@ public function logAudit($note, $location_id, $filename = null) } $log->location_id = ($location_id) ? $location_id : null; $log->note = $note; - $log->user_id = Auth::user()->id; + $log->user_id = auth()->id(); $log->filename = $filename; $log->logaction('audit'); @@ -249,8 +249,8 @@ public function logAudit($note, $location_id, $filename = null) public function logCreate($note = null) { $user_id = -1; - if (Auth::user()) { - $user_id = Auth::user()->id; + if (auth()->user()) { + $user_id = auth()->id(); } $log = new Actionlog; if (static::class == LicenseSeat::class) { @@ -284,7 +284,7 @@ public function logUpload($filename, $note) $log->item_type = static::class; $log->item_id = $this->id; } - $log->user_id = Auth::user()->id; + $log->user_id = auth()->id(); $log->note = $note; $log->target_id = null; $log->created_at = date('Y-m-d H:i:s'); diff --git a/app/Services/PredefinedKitCheckoutService.php b/app/Services/PredefinedKitCheckoutService.php index 7ef28c9ee0be..d68387539530 100644 --- a/app/Services/PredefinedKitCheckoutService.php +++ b/app/Services/PredefinedKitCheckoutService.php @@ -54,7 +54,7 @@ public function checkout(Request $request, PredefinedKit $kit, User $user) $expected_checkin = $request->get('expected_checkin'); } - $admin = Auth::user(); + $admin = auth()->user(); $note = e($request->get('note')); diff --git a/database/factories/CategoryFactory.php b/database/factories/CategoryFactory.php index fe6bc255b77c..2a89c128928d 100644 --- a/database/factories/CategoryFactory.php +++ b/database/factories/CategoryFactory.php @@ -178,4 +178,32 @@ public function forAccessories() 'category_type' => 'accessory', ]); } + + public function forAssets() + { + return $this->state([ + 'category_type' => 'asset', + ]); + } + + public function forLicenses() + { + return $this->state([ + 'category_type' => 'license', + ]); + } + + public function forComponents() + { + return $this->state([ + 'category_type' => 'component', + ]); + } + + public function forConsumables() + { + return $this->state([ + 'category_type' => 'consumable', + ]); + } } diff --git a/resources/lang/en-US/mail.php b/resources/lang/en-US/mail.php index 759ff0f5e8b2..56b2c91f11df 100644 --- a/resources/lang/en-US/mail.php +++ b/resources/lang/en-US/mail.php @@ -90,4 +90,5 @@ 'welcome_to' => 'Welcome to :web!', 'your_assets' => 'View Your Assets', 'your_credentials' => 'Your Snipe-IT credentials', + 'mail_sent' => 'Mail sent successfully!', ]; diff --git a/resources/views/partials/forms/edit/company-select.blade.php b/resources/views/partials/forms/edit/company-select.blade.php index f4da23421c3d..59a4748da1e1 100644 --- a/resources/views/partials/forms/edit/company-select.blade.php +++ b/resources/views/partials/forms/edit/company-select.blade.php @@ -33,7 +33,6 @@ {!! $errors->first($fieldname, '
:message
') !!} - {!! $errors->first($fieldname, '
') !!} @endif From cc6f1cd585469b34b03db2e95407f3524afb68b2 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 4 Jul 2024 20:50:11 +0100 Subject: [PATCH 04/42] Added asset model request :( Signed-off-by: snipe --- .../Controllers/AssetModelsController.php | 74 ++++++------------- app/Http/Requests/StoreAssetModelRequest.php | 48 ++++++++++++ 2 files changed, 70 insertions(+), 52 deletions(-) create mode 100644 app/Http/Requests/StoreAssetModelRequest.php diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index a2deb616b07c..17254a6d0df4 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -4,19 +4,20 @@ use App\Helpers\Helper; use App\Http\Requests\ImageUploadRequest; +use App\Http\Requests\StoreAssetModelRequest; use App\Models\Actionlog; -use App\Models\Asset; use App\Models\AssetModel; use App\Models\CustomField; use App\Models\User; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\View; use Illuminate\Support\Facades\Validator; use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; -use Symfony\Component\HttpFoundation\JsonResponse; use Illuminate\Support\Facades\Log; +use \Illuminate\Contracts\View\View; +use \Illuminate\Http\RedirectResponse; + /** * This class controls all actions related to asset models for @@ -33,10 +34,8 @@ class AssetModelsController extends Controller * * @author [A. Gianotto] [] * @since [v1.0] - * @return View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index() + public function index() : View { $this->authorize('index', AssetModel::class); @@ -48,10 +47,8 @@ public function index() * * @author [A. Gianotto] [] * @since [v1.0] - * @return View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create() + public function create() : View { $this->authorize('create', AssetModel::class); @@ -66,16 +63,12 @@ public function create() * @author [A. Gianotto] [] * @since [v1.0] * @param ImageUploadRequest $request - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : RedirectResponse { $this->authorize('create', AssetModel::class); - // Create a new asset model $model = new AssetModel; - // Save the model data $model->eol = $request->input('eol'); $model->depreciation_id = $request->input('depreciation_id'); $model->name = $request->input('name'); @@ -93,7 +86,6 @@ public function store(ImageUploadRequest $request) $model = $request->handleImages($model); - // Was it created? if ($model->save()) { if ($this->shouldAddDefaultValues($request->input())) { if (!$this->assignCustomFieldsDefaultValues($model, $request->input('default_values'))){ @@ -113,18 +105,14 @@ public function store(ImageUploadRequest $request) * @author [A. Gianotto] [] * @since [v1.0] * @param int $modelId - * @return View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit($modelId = null) + public function edit($modelId = null) : View | RedirectResponse { $this->authorize('update', AssetModel::class); if ($item = AssetModel::find($modelId)) { $category_type = 'asset'; - $view = View::make('models/edit', compact('item', 'category_type')); - $view->with('depreciation_list', Helper::depreciationList()); + return view('models/edit', compact('item', 'category_type'))->with('depreciation_list', Helper::depreciationList()); - return $view; } return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist')); @@ -142,12 +130,11 @@ public function edit($modelId = null) * @return \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function update(ImageUploadRequest $request, $modelId = null) + public function update(StoreAssetModelRequest $request, $modelId = null) : RedirectResponse { $this->authorize('update', AssetModel::class); - // Check if the model exists + if (is_null($model = AssetModel::find($modelId))) { - // Redirect to the models management page return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist')); } @@ -173,9 +160,6 @@ public function update(ImageUploadRequest $request, $modelId = null) } } - - - if ($model->save()) { if ($model->wasChanged('eol')) { if ($model->eol > 0) { @@ -200,10 +184,8 @@ public function update(ImageUploadRequest $request, $modelId = null) * @author [A. Gianotto] [] * @since [v1.0] * @param int $modelId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($modelId) + public function destroy($modelId) : RedirectResponse { $this->authorize('delete', AssetModel::class); // Check if the model exists @@ -237,10 +219,8 @@ public function destroy($modelId) * @author [A. Gianotto] [] * @since [v1.0] * @param int $id - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function getRestore($id) + public function getRestore($id) : RedirectResponse { $this->authorize('create', AssetModel::class); @@ -255,7 +235,7 @@ public function getRestore($id) $logaction->item_type = User::class; $logaction->item_id = $model->id; $logaction->created_at = date('Y-m-d H:i:s'); - $logaction->user_id = Auth::user()->id; + $logaction->user_id = auth()->id(); $logaction->logaction('restore'); @@ -282,10 +262,8 @@ public function getRestore($id) * @author [A. Gianotto] [] * @since [v1.0] * @param int $modelId - * @return View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show($modelId = null) + public function show($modelId = null) : View | RedirectResponse { $this->authorize('view', AssetModel::class); $model = AssetModel::withTrashed()->find($modelId); @@ -303,9 +281,8 @@ public function show($modelId = null) * @author [A. Gianotto] [] * @since [v1.0] * @param int $modelId - * @return View */ - public function getClone($modelId = null) + public function getClone($modelId = null) : View | RedirectResponse { $this->authorize('create', AssetModel::class); // Check if the model exists @@ -331,9 +308,8 @@ public function getClone($modelId = null) * @author [B. Wetherington] [] * @since [v2.0] * @param int $modelId - * @return View */ - public function getCustomFields($modelId) + public function getCustomFields($modelId) : View { return view('models.custom_fields_form')->with('model', AssetModel::find($modelId)); } @@ -345,9 +321,8 @@ public function getCustomFields($modelId) * * @author [A. Gianotto] [] * @since [v1.7] - * @return \Illuminate\Contracts\View\View */ - public function postBulkEdit(Request $request) + public function postBulkEdit(Request $request) : View | RedirectResponse { $models_raw_array = $request->input('ids'); @@ -389,9 +364,8 @@ public function postBulkEdit(Request $request) * * @author [A. Gianotto] [] * @since [v1.7] - * @return \Illuminate\Contracts\View\View */ - public function postBulkEditSave(Request $request) + public function postBulkEditSave(Request $request) : RedirectResponse { $models_raw_array = $request->input('ids'); $update_array = []; @@ -429,9 +403,8 @@ public function postBulkEditSave(Request $request) * @author [A. Gianotto] [] * @since [v1.0] * @param int $modelId - * @return \Illuminate\Http\RedirectResponse */ - public function postBulkDelete(Request $request) + public function postBulkDelete(Request $request) : RedirectResponse { $models_raw_array = $request->input('ids'); @@ -470,9 +443,8 @@ public function postBulkDelete(Request $request) * any default values were entered into the form. * * @param array $input - * @return bool */ - private function shouldAddDefaultValues(array $input) + private function shouldAddDefaultValues(array $input) : bool { return ! empty($input['add_default_values']) && ! empty($input['default_values']) @@ -484,7 +456,6 @@ private function shouldAddDefaultValues(array $input) * * @param AssetModel $model * @param array $defaultValues - * @return void */ private function assignCustomFieldsDefaultValues(AssetModel $model, array $defaultValues): bool { @@ -527,9 +498,8 @@ private function assignCustomFieldsDefaultValues(AssetModel $model, array $defau /** * Removes all default values * - * @return void */ - private function removeCustomFieldsDefaultValues(AssetModel $model) + private function removeCustomFieldsDefaultValues(AssetModel $model) : void { $model->defaultValues()->detach(); } diff --git a/app/Http/Requests/StoreAssetModelRequest.php b/app/Http/Requests/StoreAssetModelRequest.php new file mode 100644 index 000000000000..407f06403946 --- /dev/null +++ b/app/Http/Requests/StoreAssetModelRequest.php @@ -0,0 +1,48 @@ +category_type = Category::select('category_type')->find($this->category_id); + + \Log::error('Category Type: '.$this->category_type); + + $this->merge([ + 'model_category_type' => $this->category_type, + ]); + } + + /** + * Get the validation rules that apply to the request. + * + * @return array|string> + */ + public function rules(): array + { + return array_merge( + ['model_category_type' => 'required|in:asset'], + parent::rules(), + ); + } + + public function messages(): array + { + $messages = ['model_category_type.in' => 'The category must be an asset category, dummy.']; + return $messages; + } +} From 066ec8e26ee3484cb43410cd735799e640f13f8a Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 4 Jul 2024 20:50:18 +0100 Subject: [PATCH 05/42] Added (failing) tests Signed-off-by: snipe --- .../AssetModels/Ui/UpdateAssetModelsTest.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php b/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php index e03e6c5762a8..ad5483a39c74 100644 --- a/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php +++ b/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php @@ -35,7 +35,9 @@ public function testUserCanCreateAssetModels() public function testUserCanEditAssetModels() { - $model = AssetModel::factory()->create(['name' => 'Test Model', 'category_id' => Category::factory()->create()->id]); + $category = Category::factory()->forAssets()->create(); + \Log::error('Test Category: '.$category->category_type); + $model = AssetModel::factory()->create(['name' => 'Test Model', 'category_id' => $category->id]); $this->assertTrue(AssetModel::where('name', 'Test Model')->exists()); $response = $this->actingAs(User::factory()->superuser()->create()) @@ -54,7 +56,10 @@ public function testUserCanEditAssetModels() public function testUserCannotChangeAssetModelCategoryType() { - $model = AssetModel::factory()->create(['name' => 'Test Model', 'category_id' => Category::factory()->forAssets()->create()->id]); + $category = Category::factory()->forAssets()->create(); + \Log::error('Test Update Category: '.$category->category_type); + + $model = AssetModel::factory()->create(['name' => 'Test Model', 'category_id' => $category->id]); $this->assertTrue(AssetModel::where('name', 'Test Model')->exists()); $response = $this->actingAs(User::factory()->superuser()->create()) @@ -63,8 +68,8 @@ public function testUserCannotChangeAssetModelCategoryType() 'category_id' => Category::factory()->forAccessories()->create()->id, ]) ->assertStatus(302) - ->assertSessionHasNoErrors() - ->assertRedirect(route('models.index')); + ->assertSessionHasErrors(['model_category_type']) + ->assertRedirect(route('models.update', ['model' => $model])); $this->followRedirects($response)->assertSee(trans('general.error')); $this->assertFalse(AssetModel::where('name', 'Test Model Edited')->exists()); From c570759370ca9a9f0ecaa157c8675a1130929512 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 4 Jul 2024 21:14:58 +0100 Subject: [PATCH 06/42] Fixed tests Signed-off-by: snipe --- app/Http/Controllers/AssetModelsController.php | 2 +- app/Http/Requests/StoreAssetModelRequest.php | 11 ++++------- .../Feature/AssetModels/Ui/UpdateAssetModelsTest.php | 11 ++++++----- 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index 17254a6d0df4..9468e70290d5 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -130,7 +130,7 @@ public function edit($modelId = null) : View | RedirectResponse * @return \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function update(StoreAssetModelRequest $request, $modelId = null) : RedirectResponse + public function update(StoreAssetModelRequest $request, $modelId) : RedirectResponse { $this->authorize('update', AssetModel::class); diff --git a/app/Http/Requests/StoreAssetModelRequest.php b/app/Http/Requests/StoreAssetModelRequest.php index 407f06403946..1457ae71fdd1 100644 --- a/app/Http/Requests/StoreAssetModelRequest.php +++ b/app/Http/Requests/StoreAssetModelRequest.php @@ -18,12 +18,9 @@ public function authorize(): bool public function prepareForValidation(): void { - $this->category_type = Category::select('category_type')->find($this->category_id); - - \Log::error('Category Type: '.$this->category_type); - + $this->category_type = Category::find($this->category_id)->category_type; $this->merge([ - 'model_category_type' => $this->category_type, + 'category_type' => $this->category_type, ]); } @@ -35,14 +32,14 @@ public function prepareForValidation(): void public function rules(): array { return array_merge( - ['model_category_type' => 'required|in:asset'], + ['category_type' => 'required|in:asset'], parent::rules(), ); } public function messages(): array { - $messages = ['model_category_type.in' => 'The category must be an asset category, dummy.']; + $messages = ['category_type.in' => 'The category must be an asset category, dummy.']; return $messages; } } diff --git a/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php b/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php index ad5483a39c74..9d4d64cab2d0 100644 --- a/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php +++ b/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php @@ -16,6 +16,7 @@ public function testPermissionRequiredToStoreAssetModel() 'name' => 'Test Model', 'category_id' => Category::factory()->create()->id ]) + ->assertStatus(403) ->assertForbidden(); } @@ -28,6 +29,8 @@ public function testUserCanCreateAssetModels() 'name' => 'Test Model', 'category_id' => Category::factory()->create()->id ]) + ->assertStatus(302) + ->assertSessionHasNoErrors() ->assertRedirect(route('models.index')); $this->assertTrue(AssetModel::where('name', 'Test Model')->exists()); @@ -36,7 +39,6 @@ public function testUserCanCreateAssetModels() public function testUserCanEditAssetModels() { $category = Category::factory()->forAssets()->create(); - \Log::error('Test Category: '.$category->category_type); $model = AssetModel::factory()->create(['name' => 'Test Model', 'category_id' => $category->id]); $this->assertTrue(AssetModel::where('name', 'Test Model')->exists()); @@ -57,19 +59,18 @@ public function testUserCanEditAssetModels() public function testUserCannotChangeAssetModelCategoryType() { $category = Category::factory()->forAssets()->create(); - \Log::error('Test Update Category: '.$category->category_type); - $model = AssetModel::factory()->create(['name' => 'Test Model', 'category_id' => $category->id]); $this->assertTrue(AssetModel::where('name', 'Test Model')->exists()); $response = $this->actingAs(User::factory()->superuser()->create()) + ->from(route('models.edit', ['model' => $model->id])) ->put(route('models.update', ['model' => $model]), [ 'name' => 'Test Model Edited', 'category_id' => Category::factory()->forAccessories()->create()->id, ]) + ->assertSessionHasErrors(['category_type']) ->assertStatus(302) - ->assertSessionHasErrors(['model_category_type']) - ->assertRedirect(route('models.update', ['model' => $model])); + ->assertRedirect(route('models.edit', ['model' => $model->id])); $this->followRedirects($response)->assertSee(trans('general.error')); $this->assertFalse(AssetModel::where('name', 'Test Model Edited')->exists()); From 0f43b3f8f914ab38e65301cdb9e75faf1540a02e Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 4 Jul 2024 21:52:16 +0100 Subject: [PATCH 07/42] Added another test Signed-off-by: snipe --- .../AssetModels/Ui/CreateAssetModelsTest.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/tests/Feature/AssetModels/Ui/CreateAssetModelsTest.php b/tests/Feature/AssetModels/Ui/CreateAssetModelsTest.php index 86710dab122b..ef35aa5f6ce0 100644 --- a/tests/Feature/AssetModels/Ui/CreateAssetModelsTest.php +++ b/tests/Feature/AssetModels/Ui/CreateAssetModelsTest.php @@ -33,4 +33,22 @@ public function testUserCanCreateAssetModels() $this->assertTrue(AssetModel::where('name', 'Test Model')->exists()); } + public function testUserCannotUseAccessoryCategoryTypeAsAssetModelCategoryType() + { + + $response = $this->actingAs(User::factory()->superuser()->create()) + ->from(route('models.create')) + ->post(route('models.store'), [ + 'name' => 'Test Invalid Model Category', + 'category_id' => Category::factory()->forAccessories()->create()->id + ]); + $response->assertStatus(302); + $response->assertRedirect(route('models.create')); + $response->assertInvalid(['category_type']); + $response->assertSessionHasErrors(['category_type']); + $this->followRedirects($response)->assertSee(trans('general.error')); + $this->assertFalse(AssetModel::where('name', 'Test Invalid Model Category')->exists()); + + } + } From ef56f599d290b7fc8914425e2a850af1c051e785 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 4 Jul 2024 21:52:29 +0100 Subject: [PATCH 08/42] Use asset model request Signed-off-by: snipe --- app/Http/Controllers/Api/AssetModelsController.php | 5 +++-- app/Http/Controllers/AssetModelsController.php | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php index f1fa5369d1d3..e777208d360d 100644 --- a/app/Http/Controllers/Api/AssetModelsController.php +++ b/app/Http/Controllers/Api/AssetModelsController.php @@ -4,6 +4,7 @@ use App\Helpers\Helper; use App\Http\Controllers\Controller; +use App\Http\Requests\StoreAssetModelRequest; use App\Http\Transformers\AssetModelsTransformer; use App\Http\Transformers\AssetsTransformer; use App\Http\Transformers\SelectlistTransformer; @@ -119,7 +120,7 @@ public function index(Request $request) * @param \App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ - public function store(ImageUploadRequest $request) + public function store(StoreAssetModelRequest $request) { $this->authorize('create', AssetModel::class); $assetmodel = new AssetModel; @@ -176,7 +177,7 @@ public function assets($id) * @param int $id * @return \Illuminate\Http\Response */ - public function update(ImageUploadRequest $request, $id) + public function update(StoreAssetModelRequest $request, $id) { $this->authorize('update', AssetModel::class); $assetmodel = AssetModel::findOrFail($id); diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index 9468e70290d5..42802fc708a6 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -64,7 +64,7 @@ public function create() : View * @since [v1.0] * @param ImageUploadRequest $request */ - public function store(ImageUploadRequest $request) : RedirectResponse + public function store(StoreAssetModelRequest $request) : RedirectResponse { $this->authorize('create', AssetModel::class); $model = new AssetModel; From d834ef51049d0765d3051402432c785961deb8a8 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 4 Jul 2024 21:52:37 +0100 Subject: [PATCH 09/42] Updated string Signed-off-by: snipe --- app/Http/Requests/StoreAssetModelRequest.php | 4 +++- resources/lang/en-US/admin/models/message.php | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/app/Http/Requests/StoreAssetModelRequest.php b/app/Http/Requests/StoreAssetModelRequest.php index 1457ae71fdd1..9872be4d264f 100644 --- a/app/Http/Requests/StoreAssetModelRequest.php +++ b/app/Http/Requests/StoreAssetModelRequest.php @@ -7,6 +7,7 @@ class StoreAssetModelRequest extends ImageUploadRequest { + /** * Determine if the user is authorized to make this request. */ @@ -19,6 +20,7 @@ public function prepareForValidation(): void { $this->category_type = Category::find($this->category_id)->category_type; + $this->merge([ 'category_type' => $this->category_type, ]); @@ -39,7 +41,7 @@ public function rules(): array public function messages(): array { - $messages = ['category_type.in' => 'The category must be an asset category, dummy.']; + $messages = ['category_type.in' => trans('admin/models/message.invalid_category_type')]; return $messages; } } diff --git a/resources/lang/en-US/admin/models/message.php b/resources/lang/en-US/admin/models/message.php index cc38c5453056..f61a2c535f8d 100644 --- a/resources/lang/en-US/admin/models/message.php +++ b/resources/lang/en-US/admin/models/message.php @@ -7,7 +7,7 @@ 'no_association' => 'WARNING! The asset model for this item is invalid or missing!', 'no_association_fix' => 'This will break things in weird and horrible ways. Edit this asset now to assign it a model.', 'assoc_users' => 'This model is currently associated with one or more assets and cannot be deleted. Please delete the assets, and then try deleting again. ', - + 'invalid_category_type' => 'The category must be an asset category.', 'create' => array( 'error' => 'Model was not created, please try again.', From 7330bf5832ad085085179c70d1fbbcfd26465829 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 4 Jul 2024 21:52:47 +0100 Subject: [PATCH 10/42] Check for invalid Signed-off-by: snipe --- tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php b/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php index 9d4d64cab2d0..ff2e5ef59fb0 100644 --- a/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php +++ b/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php @@ -69,6 +69,7 @@ public function testUserCannotChangeAssetModelCategoryType() 'category_id' => Category::factory()->forAccessories()->create()->id, ]) ->assertSessionHasErrors(['category_type']) + ->assertInvalid(['category_type']) ->assertStatus(302) ->assertRedirect(route('models.edit', ['model' => $model->id])); @@ -76,4 +77,5 @@ public function testUserCannotChangeAssetModelCategoryType() $this->assertFalse(AssetModel::where('name', 'Test Model Edited')->exists()); } + } From a1eafc108d2b122e488146eca3f67ddb885b2441 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 4 Jul 2024 22:37:58 +0100 Subject: [PATCH 11/42] Moar typehinting Signed-off-by: snipe --- .../AssetModelsFilesController.php | 14 ++- .../Controllers/BulkAssetModelsController.php | 12 ++- app/Http/Controllers/CategoriesController.php | 32 +++---- app/Http/Controllers/CheckInOutRequest.php | 5 +- app/Http/Controllers/CompaniesController.php | 28 ++---- .../Controllers/CustomFieldsController.php | 32 +++---- .../Controllers/CustomFieldsetsController.php | 36 +++----- app/Http/Controllers/DashboardController.php | 6 +- .../Controllers/DepartmentsController.php | 31 ++----- .../Controllers/DepreciationsController.php | 33 ++----- app/Http/Controllers/GoogleAuthController.php | 4 +- app/Http/Controllers/GroupsController.php | 26 ++---- app/Http/Controllers/LocationsController.php | 48 ++++------ .../Controllers/ManufacturersController.php | 39 +++----- app/Http/Controllers/ProfileController.php | 28 +++--- app/Http/Controllers/ReportsController.php | 90 ++++++++----------- app/Http/Controllers/SettingsController.php | 58 ++++-------- app/Http/Requests/StoreAssetModelRequest.php | 5 +- 18 files changed, 180 insertions(+), 347 deletions(-) diff --git a/app/Http/Controllers/AssetModelsFilesController.php b/app/Http/Controllers/AssetModelsFilesController.php index 8e0512801b83..5c21954d6d78 100644 --- a/app/Http/Controllers/AssetModelsFilesController.php +++ b/app/Http/Controllers/AssetModelsFilesController.php @@ -6,8 +6,10 @@ use App\Http\Requests\UploadFileRequest; use App\Models\Actionlog; use App\Models\AssetModel; -use Illuminate\Support\Facades\Response; +use Illuminate\Http\RedirectResponse; +use Illuminate\Http\Response; use Illuminate\Support\Facades\Storage; +use \Symfony\Component\HttpFoundation\StreamedResponse; class AssetModelsFilesController extends Controller { @@ -21,7 +23,7 @@ class AssetModelsFilesController extends Controller *@since [v1.0] * @author [A. Gianotto] [] */ - public function store(UploadFileRequest $request, $modelId = null) + public function store(UploadFileRequest $request, $modelId = null) : RedirectResponse { if (! $model = AssetModel::find($modelId)) { return redirect()->route('models.index')->with('error', trans('admin/hardware/message.does_not_exist')); @@ -54,10 +56,8 @@ public function store(UploadFileRequest $request, $modelId = null) * @param int $modelId * @param int $fileId * @since [v1.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show($modelId = null, $fileId = null) + public function show($modelId = null, $fileId = null) : StreamedResponse | Response | RedirectResponse { $model = AssetModel::find($modelId); // the asset is valid @@ -101,10 +101,8 @@ public function show($modelId = null, $fileId = null) * @param int $modelId * @param int $fileId * @since [v1.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($modelId = null, $fileId = null) + public function destroy($modelId = null, $fileId = null) : RedirectResponse { $model = AssetModel::find($modelId); $this->authorize('update', $model); diff --git a/app/Http/Controllers/BulkAssetModelsController.php b/app/Http/Controllers/BulkAssetModelsController.php index 672fc30079cf..36b21178b03f 100644 --- a/app/Http/Controllers/BulkAssetModelsController.php +++ b/app/Http/Controllers/BulkAssetModelsController.php @@ -5,7 +5,8 @@ use App\Helpers\Helper; use App\Models\AssetModel; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Redirect; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; class BulkAssetModelsController extends Controller { @@ -15,9 +16,8 @@ class BulkAssetModelsController extends Controller * @author [A. Gianotto] [] * @since [v1.7] * @param Request $request - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function edit(Request $request) + public function edit(Request $request) : View | RedirectResponse { $models_raw_array = $request->input('ids'); @@ -60,9 +60,8 @@ public function edit(Request $request) * @author [A. Gianotto] [] * @since [v1.7] * @param Request $request - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function update(Request $request) + public function update(Request $request): View | RedirectResponse { $this->authorize('update', AssetModel::class); @@ -104,9 +103,8 @@ public function update(Request $request) * * @author [A. Gianotto] [] * @since [v1.0] - * @return \Illuminate\Http\RedirectResponse */ - public function destroy(Request $request) + public function destroy(Request $request) : RedirectResponse { $this->authorize('delete', AssetModel::class); diff --git a/app/Http/Controllers/CategoriesController.php b/app/Http/Controllers/CategoriesController.php index c6a51f1b03df..df3bc5dce5f1 100755 --- a/app/Http/Controllers/CategoriesController.php +++ b/app/Http/Controllers/CategoriesController.php @@ -4,9 +4,11 @@ use App\Helpers\Helper; use App\Http\Requests\ImageUploadRequest; -use App\Models\Category as Category; +use App\Models\Category; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** * This class controls all actions related to Categories for @@ -24,10 +26,8 @@ class CategoriesController extends Controller * @author [A. Gianotto] [] * @see CategoriesController::getDatatable() method that generates the JSON response * @since [v1.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index() + public function index() : View { // Show the page $this->authorize('view', Category::class); @@ -41,10 +41,8 @@ public function index() * @author [A. Gianotto] [] * @see CategoriesController::store() method that stores the data * @since [v1.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create() + public function create() : View { // Show the page $this->authorize('create', Category::class); @@ -60,10 +58,8 @@ public function create() * @see CategoriesController::create() method that makes the form. * @since [v1.0] * @param ImageUploadRequest $request - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : RedirectResponse { $this->authorize('create', Category::class); $category = new Category(); @@ -90,10 +86,8 @@ public function store(ImageUploadRequest $request) * @see CategoriesController::postEdit() method saves the data * @param int $categoryId * @since [v1.0] - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit($categoryId = null) + public function edit($categoryId = null) : RedirectResponse | View { $this->authorize('update', Category::class); if (is_null($item = Category::find($categoryId))) { @@ -111,11 +105,9 @@ public function edit($categoryId = null) * @see CategoriesController::getEdit() method that makes the form. * @param ImageUploadRequest $request * @param int $categoryId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException * @since [v1.0] */ - public function update(ImageUploadRequest $request, $categoryId = null) + public function update(ImageUploadRequest $request, $categoryId = null) : RedirectResponse { $this->authorize('update', Category::class); if (is_null($category = Category::find($categoryId))) { @@ -149,10 +141,8 @@ public function update(ImageUploadRequest $request, $categoryId = null) * @author [A. Gianotto] [] * @since [v1.0] * @param int $categoryId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($categoryId) + public function destroy($categoryId) : RedirectResponse { $this->authorize('delete', Category::class); // Check if the category exists @@ -177,11 +167,9 @@ public function destroy($categoryId) * @author [A. Gianotto] [] * @see CategoriesController::getDataView() method that generates the JSON response * @param $id - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException * @since [v1.8] */ - public function show($id) + public function show($id) : View | RedirectResponse { $this->authorize('view', Category::class); if ($category = Category::find($id)) { diff --git a/app/Http/Controllers/CheckInOutRequest.php b/app/Http/Controllers/CheckInOutRequest.php index 6dd7e4aba3a2..328553eb4966 100644 --- a/app/Http/Controllers/CheckInOutRequest.php +++ b/app/Http/Controllers/CheckInOutRequest.php @@ -11,9 +11,8 @@ trait CheckInOutRequest { /** * Find target for checkout - * @return SnipeModel Target asset is being checked out to. */ - protected function determineCheckoutTarget() + protected function determineCheckoutTarget() : ?SnipeModel { // This item is checked out to a location switch (request('checkout_to_type')) { @@ -34,7 +33,7 @@ protected function determineCheckoutTarget() * @param SnipeModel $target Target with location * @return Asset Asset being updated */ - protected function updateAssetLocation($asset, $target) + protected function updateAssetLocation($asset, $target) : Asset { switch (request('checkout_to_type')) { case 'location': diff --git a/app/Http/Controllers/CompaniesController.php b/app/Http/Controllers/CompaniesController.php index 646f2c438423..589832af722b 100644 --- a/app/Http/Controllers/CompaniesController.php +++ b/app/Http/Controllers/CompaniesController.php @@ -7,6 +7,8 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Log; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** * This controller handles all actions related to Companies for @@ -21,10 +23,8 @@ final class CompaniesController extends Controller * * @author [Abdullah Alansari] [] * @since [v1.8] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index() + public function index() : View { $this->authorize('view', Company::class); @@ -36,10 +36,8 @@ public function index() * * @author [Abdullah Alansari] [] * @since [v1.8] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create() + public function create() : View { $this->authorize('create', Company::class); @@ -52,10 +50,8 @@ public function create() * @author [Abdullah Alansari] [] * @since [v1.8] * @param Request $request - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : RedirectResponse { $this->authorize('create', Company::class); @@ -81,10 +77,8 @@ public function store(ImageUploadRequest $request) * @author [Abdullah Alansari] [] * @since [v1.8] * @param int $companyId - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit($companyId) + public function edit($companyId) : View | RedirectResponse { if (is_null($item = Company::find($companyId))) { return redirect()->route('companies.index') @@ -103,10 +97,8 @@ public function edit($companyId) * @since [v1.8] * @param ImageUploadRequest $request * @param int $companyId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function update(ImageUploadRequest $request, $companyId) + public function update(ImageUploadRequest $request, $companyId) : RedirectResponse { if (is_null($company = Company::find($companyId))) { return redirect()->route('companies.index')->with('error', trans('admin/companies/message.does_not_exist')); @@ -135,10 +127,8 @@ public function update(ImageUploadRequest $request, $companyId) * @author [Abdullah Alansari] [] * @since [v1.8] * @param int $companyId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($companyId) + public function destroy($companyId) : RedirectResponse { if (is_null($company = Company::find($companyId))) { return redirect()->route('companies.index') @@ -165,7 +155,7 @@ public function destroy($companyId) ->with('success', trans('admin/companies/message.delete.success')); } - public function show($id) + public function show($id) : View | RedirectResponse { $this->authorize('view', Company::class); diff --git a/app/Http/Controllers/CustomFieldsController.php b/app/Http/Controllers/CustomFieldsController.php index 43d2fbfab161..42f6c212dbe8 100644 --- a/app/Http/Controllers/CustomFieldsController.php +++ b/app/Http/Controllers/CustomFieldsController.php @@ -8,6 +8,8 @@ use App\Models\CustomFieldset; use Illuminate\Support\Facades\Auth; use Illuminate\Http\Request; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** * This controller handles all actions related to Custom Asset Fields for @@ -25,10 +27,8 @@ class CustomFieldsController extends Controller * * @author [Brady Wetherington] [] * @since [v1.8] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index() + public function index() : View { $this->authorize('view', CustomField::class); @@ -45,10 +45,8 @@ public function index() * @see CustomFieldsController::storeField() * @author [A. Gianotto] [] * @since [v5.1.5] - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show() + public function show() : RedirectResponse { return redirect()->route('fields.index'); } @@ -60,10 +58,8 @@ public function show() * @see CustomFieldsController::storeField() * @author [Brady Wetherington] [] * @since [v1.8] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create(Request $request) + public function create(Request $request) : View { $this->authorize('create', CustomField::class); $fieldsets = CustomFieldset::get(); @@ -82,10 +78,8 @@ public function create(Request $request) * @see CustomFieldsController::createField() * @author [Brady Wetherington] [] * @since [v1.8] - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(CustomFieldRequest $request) + public function store(CustomFieldRequest $request) : RedirectResponse { $this->authorize('create', CustomField::class); @@ -144,10 +138,8 @@ public function store(CustomFieldRequest $request) * * @author [A. Gianotto] [] * @since [v3.0] - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function deleteFieldFromFieldset($field_id, $fieldset_id) + public function deleteFieldFromFieldset($field_id, $fieldset_id) : RedirectResponse { $field = CustomField::find($field_id); @@ -176,10 +168,8 @@ public function deleteFieldFromFieldset($field_id, $fieldset_id) * * @author [Brady Wetherington] [] * @since [v1.8] - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($field_id) + public function destroy($field_id) : RedirectResponse { if ($field = CustomField::find($field_id)) { $this->authorize('delete', $field); @@ -202,10 +192,8 @@ public function destroy($field_id) * @author [A. Gianotto] [] * @param int $id * @since [v4.0] - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit(Request $request, $id) + public function edit(Request $request, $id) : View | RedirectResponse { if ($field = CustomField::find($id)) { @@ -241,7 +229,7 @@ public function edit(Request $request, $id) * @return \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function update(CustomFieldRequest $request, $id) + public function update(CustomFieldRequest $request, $id) : RedirectResponse { $field = CustomField::find($id); diff --git a/app/Http/Controllers/CustomFieldsetsController.php b/app/Http/Controllers/CustomFieldsetsController.php index 64ba9dfb1c2b..b68fa57767ac 100644 --- a/app/Http/Controllers/CustomFieldsetsController.php +++ b/app/Http/Controllers/CustomFieldsetsController.php @@ -6,10 +6,9 @@ use App\Models\CustomField; use App\Models\CustomFieldset; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; -use Illuminate\Support\Facades\Input; use Illuminate\Support\Facades\Validator; -use Redirect; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** * This controller handles all actions related to Custom Asset Fields for @@ -23,7 +22,7 @@ class CustomFieldsetsController extends Controller { - public function index() + public function index() : RedirectResponse { return redirect()->route("fields.index") ->with("error", trans('admin/custom_fields/message.fieldset.does_not_exist')); @@ -34,11 +33,9 @@ public function index() * * @author [Brady Wetherington] [] * @param int $id - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException * @since [v1.8] */ - public function show($id) + public function show($id) : View | RedirectResponse { $cfset = CustomFieldset::with('fields') ->where('id', '=', $id)->orderBy('id', 'ASC')->first(); @@ -70,10 +67,8 @@ public function show($id) * * @author [Brady Wetherington] [] * @since [v1.8] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create() + public function create() : View { $this->authorize('create', CustomField::class); @@ -89,7 +84,7 @@ public function create() * @return \Illuminate\Http\RedirectResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(Request $request) + public function store(Request $request) : RedirectResponse { $this->authorize('create', CustomField::class); @@ -126,10 +121,8 @@ public function store(Request $request) * @author [A. Gianotto] [] * @param int $id * @since [v6.0.14] - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit($id) + public function edit($id) : View | RedirectResponse { $this->authorize('create', CustomField::class); @@ -147,10 +140,8 @@ public function edit($id) * @author [A. Gianotto] [] * @param int $id * @since [v6.0.14] - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function update(Request $request, $id) + public function update(Request $request, $id) : RedirectResponse { $this->authorize('create', CustomField::class); @@ -175,10 +166,8 @@ public function update(Request $request, $id) * @author [Brady Wetherington] [] * @param int $id * @since [v1.8] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($id) + public function destroy($id) : RedirectResponse { $fieldset = CustomFieldset::find($id); @@ -203,9 +192,8 @@ public function destroy($id) * * @author [Brady Wetherington] [] * @since [v1.8] - * @return \Illuminate\Contracts\View\View */ - public function associate(Request $request, $id) + public function associate(Request $request, $id) : RedirectResponse { $set = CustomFieldset::find($id); @@ -232,7 +220,7 @@ public function associate(Request $request, $id) * @author [A. Gianotto] [] * @since [v5.0] */ - public function makeFieldRequired($fieldset_id, $field_id) + public function makeFieldRequired($fieldset_id, $field_id) : RedirectResponse { $this->authorize('update', CustomField::class); $field = CustomField::findOrFail($field_id); @@ -250,7 +238,7 @@ public function makeFieldRequired($fieldset_id, $field_id) * @author [A. Gianotto] [] * @since [v5.0] */ - public function makeFieldOptional($fieldset_id, $field_id) + public function makeFieldOptional($fieldset_id, $field_id) : RedirectResponse { $this->authorize('update', CustomField::class); $field = CustomField::findOrFail($field_id); diff --git a/app/Http/Controllers/DashboardController.php b/app/Http/Controllers/DashboardController.php index 2366503c64e2..fc01c496c2b4 100755 --- a/app/Http/Controllers/DashboardController.php +++ b/app/Http/Controllers/DashboardController.php @@ -2,8 +2,9 @@ namespace App\Http\Controllers; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Artisan; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** @@ -21,9 +22,8 @@ class DashboardController extends Controller * * @author [A. Gianotto] [] * @since [v1.0] - * @return \Illuminate\Contracts\View\View */ - public function index() + public function index() : View | RedirectResponse { // Show the page if (auth()->user()->hasAccess('admin')) { diff --git a/app/Http/Controllers/DepartmentsController.php b/app/Http/Controllers/DepartmentsController.php index 51de30c88ee6..5818435deb70 100644 --- a/app/Http/Controllers/DepartmentsController.php +++ b/app/Http/Controllers/DepartmentsController.php @@ -6,7 +6,8 @@ use App\Models\Department; use App\Models\Company; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Log; @@ -26,10 +27,8 @@ public function __construct() * @see AssetController::getDatatable() method that generates the JSON response * @since [v4.0] * @param Request $request - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index(Request $request) + public function index(Request $request) : View { $this->authorize('index', Department::class); $company = null; @@ -46,10 +45,8 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param ImageUploadRequest $request - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : RedirectResponse { $this->authorize('create', Department::class); $department = new Department; @@ -74,10 +71,8 @@ public function store(ImageUploadRequest $request) * @author [A. Gianotto] [] * @param int $id * @since [v4.0] - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show($id) + public function show($id) : View | RedirectResponse { $department = Department::find($id); @@ -96,10 +91,8 @@ public function show($id) * @author [A. Gianotto] [] * @see DepartmentsController::postCreate() method that validates and stores the data * @since [v4.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create() + public function create() : View { $this->authorize('create', Department::class); @@ -112,10 +105,8 @@ public function create() * @author [A. Gianotto] [] * @param int $locationId * @since [v4.0] - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($id) + public function destroy($id) : RedirectResponse { if (is_null($department = Department::find($id))) { return redirect()->to(route('departments.index'))->with('error', trans('admin/departments/message.not_found')); @@ -146,10 +137,8 @@ public function destroy($id) * @see LocationsController::postCreate() method that validates and stores * @param int $departmentId * @since [v1.0] - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit($departmentId = null) + public function edit($departmentId = null) : View | RedirectResponse { if (is_null($item = Department::find($departmentId))) { return redirect()->back()->with('error', trans('admin/locations/message.does_not_exist')); @@ -167,10 +156,8 @@ public function edit($departmentId = null) * @see LocationsController::postCreate() method that validates and stores * @param int $departmentId * @since [v1.0] - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function update(ImageUploadRequest $request, $id) + public function update(ImageUploadRequest $request, $id) : RedirectResponse { if (is_null($department = Department::find($id))) { return redirect()->route('departments.index')->with('error', trans('admin/departments/message.does_not_exist')); diff --git a/app/Http/Controllers/DepreciationsController.php b/app/Http/Controllers/DepreciationsController.php index f453e4998e7b..57ad35fea509 100755 --- a/app/Http/Controllers/DepreciationsController.php +++ b/app/Http/Controllers/DepreciationsController.php @@ -5,7 +5,8 @@ use App\Models\Depreciation; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; - +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** * This controller handles all actions related to Depreciations for * the Snipe-IT Asset Management application. @@ -21,14 +22,10 @@ class DepreciationsController extends Controller * @author [A. Gianotto] [authorize('view', Depreciation::class); - - // Show the page return view('depreciations/index'); } @@ -38,10 +35,8 @@ public function index() * @author [A. Gianotto] [authorize('create', Depreciation::class); @@ -56,10 +51,8 @@ public function create() * @see DepreciationsController::postCreate() * @since [v1.0] * @param Request $request - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(Request $request) + public function store(Request $request) : RedirectResponse { $this->authorize('create', Depreciation::class); @@ -87,10 +80,8 @@ public function store(Request $request) * @see DepreciationsController::postEdit() * @param int $depreciationId * @since [v1.0] - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit($depreciationId = null) + public function edit($depreciationId = null) : RedirectResponse | View { // Check if the depreciation exists if (is_null($item = Depreciation::find($depreciationId))) { @@ -110,11 +101,9 @@ public function edit($depreciationId = null) * @see DepreciationsController::getEdit() * @param Request $request * @param int $depreciationId - * @return \Illuminate\Http\RedirectResponse * @since [v1.0] - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function update(Request $request, $depreciationId = null) + public function update(Request $request, $depreciationId = null) : RedirectResponse { // Check if the depreciation exists if (is_null($depreciation = Depreciation::find($depreciationId))) { @@ -146,10 +135,8 @@ public function update(Request $request, $depreciationId = null) * @author [A. Gianotto] [find($depreciationId))) { @@ -175,10 +162,8 @@ public function destroy($depreciationId) * @see DepreciationsController::postEdit() * @param int $depreciationId * @since [v1.0] - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show($id) + public function show($id) : View | RedirectResponse { if (is_null($depreciation = Depreciation::find($id))) { // Redirect to the blogs management page diff --git a/app/Http/Controllers/GoogleAuthController.php b/app/Http/Controllers/GoogleAuthController.php index 6f5d02fde9da..d873491c5b40 100644 --- a/app/Http/Controllers/GoogleAuthController.php +++ b/app/Http/Controllers/GoogleAuthController.php @@ -2,7 +2,7 @@ namespace App\Http\Controllers; -use Illuminate\Http\Request; +use Illuminate\Http\RedirectResponse; use App\Models\User; use Illuminate\Support\Facades\Auth; use Laravel\Socialite\Facades\Socialite; @@ -30,7 +30,7 @@ public function redirectToGoogle() return Socialite::driver('google')->redirect(); } - public function handleGoogleCallback() + public function handleGoogleCallback() : RedirectResponse { try { $socialUser = Socialite::driver('google')->user(); diff --git a/app/Http/Controllers/GroupsController.php b/app/Http/Controllers/GroupsController.php index 9cb68e3a7a9a..a85cabf2465c 100755 --- a/app/Http/Controllers/GroupsController.php +++ b/app/Http/Controllers/GroupsController.php @@ -5,7 +5,8 @@ use App\Helpers\Helper; use App\Models\Group; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** * This controller handles all actions related to User Groups for @@ -22,11 +23,9 @@ class GroupsController extends Controller * @author [A. Gianotto] [route('groups.index')->with('error', trans('admin/groups/message.group_not_found', ['id' => $id])); @@ -132,10 +127,8 @@ public function update(Request $request, $id = null) * @see GroupsController::getEdit() * @param int $id * @since [v1.0] - * @return \Illuminate\Http\RedirectResponse - * @throws \Exception */ - public function destroy($id) + public function destroy($id) : RedirectResponse { if (! config('app.lock_passwords')) { if (! $group = Group::find($id)) { @@ -154,10 +147,9 @@ public function destroy($id) * * @author [A. Gianotto] [] * @param $id - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @since [v4.0.11] */ - public function show($id) + public function show($id) : View | RedirectResponse { $group = Group::find($id); diff --git a/app/Http/Controllers/LocationsController.php b/app/Http/Controllers/LocationsController.php index dcdd937429f7..c498f0992602 100755 --- a/app/Http/Controllers/LocationsController.php +++ b/app/Http/Controllers/LocationsController.php @@ -6,11 +6,11 @@ use App\Models\Asset; use App\Models\Location; use App\Models\User; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; use Illuminate\Http\Request; use Illuminate\Support\Facades\Log; - +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** * This controller handles all actions related to Locations for * the Snipe-IT Asset Management application. @@ -26,10 +26,8 @@ class LocationsController extends Controller * @author [A. Gianotto] [] * @see LocationsController::getDatatable() method that generates the JSON response * @since [v1.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index() + public function index() : View { // Grab all the locations $this->authorize('view', Location::class); @@ -43,10 +41,8 @@ public function index() * @author [A. Gianotto] [] * @see LocationsController::postCreate() method that validates and stores the data * @since [v1.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create() + public function create() : View { $this->authorize('create', Location::class); @@ -62,10 +58,8 @@ public function create() * @see LocationsController::getCreate() method that makes the form * @since [v1.0] * @param ImageUploadRequest $request - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : RedirectResponse { $this->authorize('create', Location::class); $location = new Location(); @@ -80,7 +74,7 @@ public function store(ImageUploadRequest $request) $location->zip = $request->input('zip'); $location->ldap_ou = $request->input('ldap_ou'); $location->manager_id = $request->input('manager_id'); - $location->user_id = Auth::id(); + $location->user_id = auth()->id(); $location->phone = request('phone'); $location->fax = request('fax'); @@ -100,10 +94,8 @@ public function store(ImageUploadRequest $request) * @see LocationsController::postCreate() method that validates and stores * @param int $locationId * @since [v1.0] - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit($locationId = null) + public function edit($locationId = null) : View | RedirectResponse { $this->authorize('update', Location::class); // Check if the location exists @@ -121,11 +113,9 @@ public function edit($locationId = null) * @see LocationsController::getEdit() method that makes the form view * @param ImageUploadRequest $request * @param int $locationId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException * @since [v1.0] */ - public function update(ImageUploadRequest $request, $locationId = null) + public function update(ImageUploadRequest $request, $locationId = null) : RedirectResponse { $this->authorize('update', Location::class); // Check if the location exists @@ -163,10 +153,8 @@ public function update(ImageUploadRequest $request, $locationId = null) * @author [A. Gianotto] [] * @param int $locationId * @since [v1.0] - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($locationId) + public function destroy($locationId) : RedirectResponse { $this->authorize('delete', Location::class); if (is_null($location = Location::find($locationId))) { @@ -202,9 +190,8 @@ public function destroy($locationId) * @author [A. Gianotto] [] * @param int $id * @since [v1.0] - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function show($id = null) + public function show($id = null) : View | RedirectResponse { $location = Location::find($id); @@ -215,7 +202,7 @@ public function show($id = null) return redirect()->route('locations.index')->with('error', trans('admin/locations/message.does_not_exist')); } - public function print_assigned($id) + public function print_assigned($id) : View | RedirectResponse { if ($location = Location::where('id', $id)->first()) { @@ -240,9 +227,8 @@ public function print_assigned($id) * @author [A. Gianotto] [] * @param int $locationId * @since [v6.0.14] - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function getClone($locationId = null) + public function getClone($locationId = null) : View | RedirectResponse { $this->authorize('create', Location::class); @@ -263,7 +249,7 @@ public function getClone($locationId = null) } - public function print_all_assigned($id) + public function print_all_assigned($id) : View | RedirectResponse { if ($location = Location::where('id', $id)->first()) { $parent = Location::where('id', $location->parent_id)->first(); @@ -282,9 +268,8 @@ public function print_all_assigned($id) * * @author [A. Gianotto] [] * @since [v6.3.1] - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function postBulkDelete(Request $request) + public function postBulkDelete(Request $request) : View | RedirectResponse { $locations_raw_array = $request->input('ids'); @@ -315,9 +300,10 @@ public function postBulkDelete(Request $request) * * @author [A. Gianotto] [] * @since [v6.3.1] - * @return \Illuminate\Http\RedirectResponse + */ - public function postBulkDeleteStore(Request $request) { + public function postBulkDeleteStore(Request $request) : RedirectResponse + { $locations_raw_array = $request->input('ids'); if ((is_array($locations_raw_array)) && (count($locations_raw_array) > 0)) { diff --git a/app/Http/Controllers/ManufacturersController.php b/app/Http/Controllers/ManufacturersController.php index 78b7a04407d3..8e979e389624 100755 --- a/app/Http/Controllers/ManufacturersController.php +++ b/app/Http/Controllers/ManufacturersController.php @@ -2,17 +2,15 @@ namespace App\Http\Controllers; -use App\Helpers\Helper; use App\Http\Requests\ImageUploadRequest; use App\Models\Actionlog; -use App\Models\Asset; use App\Models\Manufacturer; -use App\Models\User; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; -use Redirect; use Illuminate\Support\Facades\Log; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** * This controller handles all actions related to Manufacturers for @@ -29,13 +27,10 @@ class ManufacturersController extends Controller * @author [A. Gianotto] [] * @see Api\ManufacturersController::index() method that generates the JSON response * @since [v1.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index() + public function index() : View { $this->authorize('index', Manufacturer::class); - return view('manufacturers/index'); } @@ -45,10 +40,8 @@ public function index() * @author [A. Gianotto] [] * @see ManufacturersController::store() * @since [v1.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create() + public function create() : View { $this->authorize('create', Manufacturer::class); @@ -62,10 +55,8 @@ public function create() * @see ManufacturersController::create() * @since [v1.0] * @param ImageUploadRequest $request - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : RedirectResponse { $this->authorize('create', Manufacturer::class); $manufacturer = new Manufacturer; @@ -92,10 +83,8 @@ public function store(ImageUploadRequest $request) * @see ManufacturersController::update() * @param int $manufacturerId * @since [v1.0] - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit($manufacturerId = null) + public function edit($manufacturerId = null) : View | RedirectResponse { // Handles manufacturer checks and permissions. $this->authorize('update', Manufacturer::class); @@ -116,11 +105,9 @@ public function edit($manufacturerId = null) * @see ManufacturersController::getEdit() * @param Request $request * @param int $manufacturerId - * @return \Illuminate\Http\RedirectResponse * @since [v1.0] - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function update(ImageUploadRequest $request, $manufacturerId = null) + public function update(ImageUploadRequest $request, $manufacturerId = null) : RedirectResponse { $this->authorize('update', Manufacturer::class); // Check if the manufacturer exists @@ -157,10 +144,8 @@ public function update(ImageUploadRequest $request, $manufacturerId = null) * @author [A. Gianotto] [] * @param int $manufacturerId * @since [v1.0] - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($manufacturerId) + public function destroy($manufacturerId) : RedirectResponse { $this->authorize('delete', Manufacturer::class); if (is_null($manufacturer = Manufacturer::withTrashed()->withCount('models as models_count')->find($manufacturerId))) { @@ -197,10 +182,8 @@ public function destroy($manufacturerId) * @author [A. Gianotto] [] * @param int $manufacturerId * @since [v1.0] - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show($manufacturerId = null) + public function show($manufacturerId = null) : View | RedirectResponse { $this->authorize('view', Manufacturer::class); $manufacturer = Manufacturer::find($manufacturerId); @@ -220,10 +203,8 @@ public function show($manufacturerId = null) * @author [A. Gianotto] [] * @since [v4.1.15] * @param int $manufacturers_id - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function restore($id) + public function restore($id) : RedirectResponse { $this->authorize('delete', Manufacturer::class); diff --git a/app/Http/Controllers/ProfileController.php b/app/Http/Controllers/ProfileController.php index 6791cab30341..f1e3c257c594 100755 --- a/app/Http/Controllers/ProfileController.php +++ b/app/Http/Controllers/ProfileController.php @@ -10,7 +10,8 @@ use Illuminate\Support\Facades\Gate; use Illuminate\Http\Request; use Illuminate\Support\Facades\Hash; - +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** * This controller handles all actions related to User Profiles for * the Snipe-IT Asset Management application. @@ -24,9 +25,8 @@ class ProfileController extends Controller * * @author [A. Gianotto] [] * @since [v1.0] - * @return \Illuminate\Contracts\View\View */ - public function getIndex() + public function getIndex() : View { $this->authorize('self.profile'); $user = auth()->user(); @@ -38,9 +38,8 @@ public function getIndex() * * @author [A. Gianotto] [] * @since [v1.0] - * @return \Illuminate\Http\RedirectResponse */ - public function postIndex(ImageUploadRequest $request) + public function postIndex(ImageUploadRequest $request) : RedirectResponse { $this->authorize('self.profile'); $user = auth()->user(); @@ -84,7 +83,7 @@ public function postIndex(ImageUploadRequest $request) * @author [A. Gianotto] [] * @since [v4.0] */ - public function api(): \Illuminate\Contracts\View\View + public function api(): View { // Make sure the self.api permission has been granted if (!Gate::allows('self.api')) { @@ -97,21 +96,17 @@ public function api(): \Illuminate\Contracts\View\View /** * User change email page. * - * @return \Illuminate\Contracts\View\View */ - public function password() + public function password() : View { $user = auth()->user(); - return view('account/change-password', compact('user')); } /** * Users change password form processing page. - * - * @return \Illuminate\Http\RedirectResponse */ - public function passwordSave(Request $request) + public function passwordSave(Request $request) : RedirectResponse { if (config('app.lock_passwords')) { return redirect()->route('account.password.index')->with('error', trans('admin/users/table.lock_passwords')); @@ -178,9 +173,8 @@ public function passwordSave(Request $request) * * @author [A. Gianotto] [] * @since [v4.0] - * @return \Illuminate\Contracts\View\View */ - public function getMenuState(Request $request) + public function getMenuState(Request $request) : void { if ($request->input('state') == 'open') { $request->session()->put('menu_state', 'open'); @@ -195,9 +189,8 @@ public function getMenuState(Request $request) * * @author A. Gianotto * @since [v6.0.12] - * @return Illuminate\View\View */ - public function printInventory() + public function printInventory() : View { $show_user = auth()->user(); @@ -215,9 +208,8 @@ public function printInventory() * * @author A. Gianotto * @since [v6.0.12] - * @return \Illuminate\Http\RedirectResponse */ - public function emailAssetList() + public function emailAssetList() : RedirectResponse { if (!$user = User::find(auth()->id())) { diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index 16c2c338937e..3752c36bb462 100644 --- a/app/Http/Controllers/ReportsController.php +++ b/app/Http/Controllers/ReportsController.php @@ -6,6 +6,8 @@ use App\Models\Accessory; use App\Models\Actionlog; use App\Models\Asset; +use App\Models\AssetModel; +use App\Models\Category; use App\Models\AssetMaintenance; use App\Models\CheckoutAcceptance; use App\Models\CustomField; @@ -14,17 +16,16 @@ use App\Models\Setting; use App\Notifications\CheckoutAssetNotification; use Carbon\Carbon; -use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Http\Request; +use Illuminate\Http\Response; use Illuminate\Support\Facades\Notification; -use Illuminate\Support\Facades\Response; -use Illuminate\Support\Facades\View; +use \Illuminate\Contracts\View\View; use League\Csv\Reader; use Symfony\Component\HttpFoundation\StreamedResponse; use League\Csv\EscapeFormula; use App\Http\Requests\CustomAssetReportRequest; use Illuminate\Support\Facades\Log; - +use Illuminate\Http\RedirectResponse; /** * This controller handles all actions related to Reports for @@ -49,7 +50,7 @@ public function __construct() * @since [v1.0] * @return \Illuminate\Contracts\View\View */ - public function getAccessoryReport() + public function getAccessoryReport() : View { $this->authorize('reports.view'); @@ -65,7 +66,7 @@ public function getAccessoryReport() * @since [v1.0] * @return \Illuminate\Http\Response */ - public function exportAccessoryReport() + public function exportAccessoryReport() : Response { $this->authorize('reports.view'); $accessories = Accessory::orderBy('created_at', 'DESC')->get(); @@ -104,9 +105,8 @@ public function exportAccessoryReport() * * @author [A. Gianotto] [] * @since [v1.0] - * @return \Illuminate\Contracts\View\View */ - public function getDeprecationReport() + public function getDeprecationReport() : View { $this->authorize('reports.view'); $depreciations = Depreciation::get(); @@ -119,9 +119,8 @@ public function getDeprecationReport() * @deprecated Server-side exports have been replaced by datatables export since v2. * @author [A. Gianotto] [] * @since [v1.0] - * @return \Illuminate\Http\Response */ - public function exportDeprecationReport() + public function exportDeprecationReport() : Response { $this->authorize('reports.view'); // Grab all the assets @@ -197,12 +196,10 @@ public function exportDeprecationReport() * * @author [A. Gianotto] [] * @since [v4.0] - * @return \Illuminate\Contracts\View\View */ - public function audit() + public function audit() : View { $this->authorize('reports.view'); - return view('reports/audit'); } @@ -212,9 +209,8 @@ public function audit() * * @author [A. Gianotto] [] * @since [v1.0] - * @return \Illuminate\Contracts\View\View */ - public function getActivityReport() + public function getActivityReport() : View { $this->authorize('reports.view'); @@ -226,9 +222,8 @@ public function getActivityReport() * * @author [A. Gianotto] [] * @since [v5.0.7] - * @return \Illuminate\Http\Response */ - public function postActivityReport(Request $request) + public function postActivityReport(Request $request) : StreamedResponse { ini_set('max_execution_time', 12000); $this->authorize('reports.view'); @@ -328,9 +323,8 @@ public function postActivityReport(Request $request) * * @author [A. Gianotto] [] * @since [v1.0] - * @return \Illuminate\Contracts\View\View */ - public function getLicenseReport() + public function getLicenseReport() : View { $this->authorize('reports.view'); $licenses = License::with('depreciation')->orderBy('created_at', 'DESC') @@ -346,9 +340,8 @@ public function getLicenseReport() * @deprecated Server-side exports have been replaced by datatables export since v2. * @author [A. Gianotto] [] * @since [v1.0] - * @return \Illuminate\Http\Response */ - public function exportLicenseReport() + public function exportLicenseReport() : Response { $this->authorize('reports.view'); $licenses = License::orderBy('created_at', 'DESC')->get(); @@ -398,9 +391,8 @@ public function exportLicenseReport() * @author [A. Gianotto] [] * @see ReportsController::postCustomReport() method that generates the CSV * @since [v1.0] - * @return \Illuminate\Http\Response */ - public function getCustomReport() + public function getCustomReport() : View { $this->authorize('reports.view'); $customfields = CustomField::get(); @@ -414,9 +406,8 @@ public function getCustomReport() * @author [A. Gianotto] [] * @see ReportsController::getCustomReport() method that generates form view * @since [v1.0] - * @return \Illuminate\Http\Response */ - public function postCustom(CustomAssetReportRequest $request) + public function postCustom(CustomAssetReportRequest $request) : StreamedResponse { ini_set('max_execution_time', env('REPORT_TIME_LIMIT', 12000)); //12000 seconds = 200 minutes $this->authorize('reports.view'); @@ -681,15 +672,15 @@ public function postCustom(CustomAssetReportRequest $request) } if (($request->filled('created_start')) && ($request->filled('created_end'))) { - $created_start = \Carbon::parse($request->input('created_start'))->startOfDay(); - $created_end = \Carbon::parse($request->input('created_end'))->endOfDay(); + $created_start = Carbon::parse($request->input('created_start'))->startOfDay(); + $created_end = Carbon::parse($request->input('created_end'))->endOfDay(); $assets->whereBetween('assets.created_at', [$created_start, $created_end]); } if (($request->filled('checkout_date_start')) && ($request->filled('checkout_date_end'))) { - $checkout_start = \Carbon::parse($request->input('checkout_date_start'))->startOfDay(); - $checkout_end = \Carbon::parse($request->input('checkout_date_end',now()))->endOfDay(); + $checkout_start = Carbon::parse($request->input('checkout_date_start'))->startOfDay(); + $checkout_end = Carbon::parse($request->input('checkout_date_end',now()))->endOfDay(); $actionlogassets = Actionlog::where('action_type','=', 'checkout') ->where('item_type', 'LIKE', '%Asset%',) @@ -700,9 +691,9 @@ public function postCustom(CustomAssetReportRequest $request) } if (($request->filled('checkin_date_start'))) { - $checkin_start = \Carbon::parse($request->input('checkin_date_start'))->startOfDay(); + $checkin_start = Carbon::parse($request->input('checkin_date_start'))->startOfDay(); // use today's date is `checkin_date_end` is not provided - $checkin_end = \Carbon::parse($request->input('checkin_date_end', now()))->endOfDay(); + $checkin_end = Carbon::parse($request->input('checkin_date_end', now()))->endOfDay(); $assets->whereBetween('assets.last_checkin', [$checkin_start, $checkin_end ]); } @@ -713,8 +704,8 @@ public function postCustom(CustomAssetReportRequest $request) } if (($request->filled('last_audit_start')) && ($request->filled('last_audit_end'))) { - $last_audit_start = \Carbon::parse($request->input('last_audit_start'))->startOfDay(); - $last_audit_end = \Carbon::parse($request->input('last_audit_end'))->endOfDay(); + $last_audit_start = Carbon::parse($request->input('last_audit_start'))->startOfDay(); + $last_audit_end = Carbon::parse($request->input('last_audit_end'))->endOfDay(); $assets->whereBetween('assets.last_audit_date', [$last_audit_start, $last_audit_end]); } @@ -1015,11 +1006,10 @@ public function postCustom(CustomAssetReportRequest $request) /** * getImprovementsReport * - * @return \Illuminate\Contracts\View\View * @author Vincent Sposato * @version v1.0 */ - public function getAssetMaintenancesReport() + public function getAssetMaintenancesReport() : View { $this->authorize('reports.view'); @@ -1029,11 +1019,10 @@ public function getAssetMaintenancesReport() /** * exportImprovementsReport * - * @return \Illuminate\Http\Response * @author Vincent Sposato * @version v1.0 */ - public function exportAssetMaintenancesReport() + public function exportAssetMaintenancesReport() : Response { $this->authorize('reports.view'); // Grab all the improvements @@ -1090,13 +1079,10 @@ public function exportAssetMaintenancesReport() /** * getAssetAcceptanceReport * - * @return mixed - * @throws \Illuminate\Auth\Access\AuthorizationException - * @author Vincent Sposato * @version v1.0 */ - public function getAssetAcceptanceReport($deleted = false) + public function getAssetAcceptanceReport($deleted = false) : View { $this->authorize('reports.view'); $showDeleted = $deleted == 'deleted'; @@ -1132,11 +1118,9 @@ public function getAssetAcceptanceReport($deleted = false) * sentAssetAcceptanceReminder * * @param integer|null $acceptanceId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException * @version v1.0 */ - public function sentAssetAcceptanceReminder(Request $request) + public function sentAssetAcceptanceReminder(Request $request) : RedirectResponse { $this->authorize('reports.view'); @@ -1190,11 +1174,9 @@ public function sentAssetAcceptanceReminder(Request $request) * sentAssetAcceptanceReminder * * @param integer|null $acceptanceId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException * @version v1.0 */ - public function deleteAssetAcceptance($acceptanceId = null) + public function deleteAssetAcceptance($acceptanceId = null) : RedirectResponse { $this->authorize('reports.view'); @@ -1213,11 +1195,10 @@ public function deleteAssetAcceptance($acceptanceId = null) /** * Exports the AssetAcceptance report to CSV * - * @return \Illuminate\Http\Response * @author Vincent Sposato * @version v1.0 */ - public function postAssetAcceptanceReport($deleted = false) + public function postAssetAcceptanceReport($deleted = false) : Response { $this->authorize('reports.view'); $showDeleted = $deleted == 'deleted'; @@ -1284,7 +1265,7 @@ public function postAssetAcceptanceReport($deleted = false) * @author Vincent Sposato * @version v1.0 */ - protected function getCheckedOutAssetsRequiringAcceptance($modelsInCategoriesThatRequireAcceptance) + protected function getCheckedOutAssetsRequiringAcceptance($modelsInCategoriesThatRequireAcceptance) : View { $this->authorize('reports.view'); $assets = Asset::deployed() @@ -1304,11 +1285,11 @@ protected function getCheckedOutAssetsRequiringAcceptance($modelsInCategoriesTha * @author Vincent Sposato * @version v1.0 */ - protected function getModelsInCategoriesThatRequireAcceptance($assetCategoriesRequiringAcceptance) + protected function getModelsInCategoriesThatRequireAcceptance($assetCategoriesRequiringAcceptance) : array { $this->authorize('reports.view'); - return array_pluck(Model::inCategory($assetCategoriesRequiringAcceptance) + return array_pluck(AssetModel::inCategory($assetCategoriesRequiringAcceptance) ->select('id') ->get() ->toArray(), 'id'); @@ -1321,7 +1302,7 @@ protected function getModelsInCategoriesThatRequireAcceptance($assetCategoriesRe * @author Vincent Sposato * @version v1.0 */ - protected function getCategoriesThatRequireAcceptance() + protected function getCategoriesThatRequireAcceptance() : array { $this->authorize('reports.view'); @@ -1334,11 +1315,10 @@ protected function getCategoriesThatRequireAcceptance() /** * getAssetsCheckedOutRequiringAcceptance * - * @return array * @author Vincent Sposato * @version v1.0 */ - protected function getAssetsCheckedOutRequiringAcceptance() + protected function getAssetsCheckedOutRequiringAcceptance() : array { $this->authorize('reports.view'); diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 0fa308410934..8f1324e93879 100755 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -19,7 +19,8 @@ use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Storage; use Illuminate\Validation\Rule; -use Illuminate\View\View; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; use Illuminate\Support\Str; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Auth; @@ -49,7 +50,7 @@ class SettingsController extends Controller * * @return \Illuminate\Contracts\View\View | \Illuminate\Http\Response */ - public function getSetupIndex() + public function getSetupIndex() : View { $start_settings['php_version_min'] = false; @@ -123,7 +124,7 @@ public function getSetupIndex() * @return bool This method will return true when exceptions (such as curl exception) is thrown. * Check the log files to see more details about the exception. */ - protected function dotEnvFileIsExposed() + protected function dotEnvFileIsExposed() : bool { try { return Http::timeout(10) @@ -153,12 +154,11 @@ protected function storagePathIsWritable(): bool * Save the first admin user from Setup. * * @author [A. Gianotto] [] - * * @since [v3.0] * - * @return \Illuminate\Http\RedirectResponse */ - public function postSaveFirstAdmin(SetupUserRequest $request) + public function postSaveFirstAdmin(SetupUserRequest $request) : RedirectResponse + { { $user = new User(); $user->first_name = $data['first_name'] = $request->input('first_name'); @@ -214,10 +214,8 @@ public function postSaveFirstAdmin(SetupUserRequest $request) * @author [A. Gianotto] [] * * @since [v3.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getSetupUser() + public function getSetupUser() : View { return view('setup/user') ->with('step', 3) @@ -230,10 +228,8 @@ public function getSetupUser() * @author [A. Gianotto] [] * * @since [v3.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getSetupDone() + public function getSetupDone() : View { return view('setup/done') ->with('step', 4) @@ -247,10 +243,8 @@ public function getSetupDone() * @author [A. Gianotto] [] * * @since [v3.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getSetupMigrate() + public function getSetupMigrate() : View { Artisan::call('migrate', ['--force' => true]); if ((! file_exists(storage_path().'/oauth-private.key')) || (! file_exists(storage_path().'/oauth-public.key'))) { @@ -270,10 +264,8 @@ public function getSetupMigrate() * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View */ - public function index() + public function index() : View { $settings = Setting::getSettings(); @@ -286,10 +278,9 @@ public function index() * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getEdit() + public function getEdit() : View + { $setting = Setting::getSettings(); @@ -302,10 +293,8 @@ public function getEdit() * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getSettings() + public function getSettings() : View { $setting = Setting::getSettings(); @@ -318,10 +307,9 @@ public function getSettings() * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function postSettings(Request $request) + public function postSettings(Request $request) : RedirectResponse + { if (is_null($setting = Setting::getSettings())) { return redirect()->to('admin')->with('error', trans('admin/settings/message.update.error')); @@ -375,10 +363,8 @@ public function postSettings(Request $request) * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getBranding() + public function getBranding() : View { $setting = Setting::getSettings(); @@ -391,10 +377,8 @@ public function getBranding() * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function postBranding(ImageUploadRequest $request) + public function postBranding(ImageUploadRequest $request) : RedirectResponse { // Something has gone horribly wrong - no settings record exists! if (is_null($setting = Setting::getSettings())) { @@ -493,10 +477,8 @@ public function postBranding(ImageUploadRequest $request) * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getSecurity() + public function getSecurity() : View { $setting = Setting::getSettings(); @@ -509,10 +491,8 @@ public function getSecurity() * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Http\RedirectResponse */ - public function postSecurity(Request $request) + public function postSecurity(Request $request) : RedirectResponse { $this->validate($request, [ 'pwd_secure_complexity' => 'array', diff --git a/app/Http/Requests/StoreAssetModelRequest.php b/app/Http/Requests/StoreAssetModelRequest.php index 9872be4d264f..eee32933e2ef 100644 --- a/app/Http/Requests/StoreAssetModelRequest.php +++ b/app/Http/Requests/StoreAssetModelRequest.php @@ -2,8 +2,9 @@ namespace App\Http\Requests; +use App\Models\AssetModel; use App\Models\Category; -use Illuminate\Validation\Rule; +use Illuminate\Support\Facades\Gate; class StoreAssetModelRequest extends ImageUploadRequest { @@ -13,7 +14,7 @@ class StoreAssetModelRequest extends ImageUploadRequest */ public function authorize(): bool { - return true; + return Gate::allows('create', new AssetModel); } public function prepareForValidation(): void From d3b62aa918fc0db34a6b5ae58b85d15697138959 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 4 Jul 2024 22:52:49 +0100 Subject: [PATCH 12/42] Few more Signed-off-by: snipe --- app/Http/Controllers/SettingsController.php | 144 +++++------------- .../Controllers/StatuslabelsController.php | 30 ++-- app/Http/Controllers/SuppliersController.php | 34 ++--- app/Http/Controllers/ViewAssetsController.php | 17 +-- 4 files changed, 65 insertions(+), 160 deletions(-) diff --git a/app/Http/Controllers/SettingsController.php b/app/Http/Controllers/SettingsController.php index 8f1324e93879..1f03a709c8e0 100755 --- a/app/Http/Controllers/SettingsController.php +++ b/app/Http/Controllers/SettingsController.php @@ -20,6 +20,7 @@ use Illuminate\Support\Facades\Storage; use Illuminate\Validation\Rule; use Illuminate\Http\RedirectResponse; +use Illuminate\Http\JsonResponse; use \Illuminate\Contracts\View\View; use Illuminate\Support\Str; use Illuminate\Support\Facades\Artisan; @@ -159,7 +160,7 @@ protected function storagePathIsWritable(): bool */ public function postSaveFirstAdmin(SetupUserRequest $request) : RedirectResponse { - { + $user = new User(); $user->first_name = $data['first_name'] = $request->input('first_name'); $user->last_name = $request->input('last_name'); @@ -547,10 +548,8 @@ public function postSecurity(Request $request) : RedirectResponse * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getLocalization() + public function getLocalization() : View { $setting = Setting::getSettings(); @@ -563,10 +562,8 @@ public function getLocalization() * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Http\RedirectResponse */ - public function postLocalization(Request $request) + public function postLocalization(Request $request) : RedirectResponse { if (is_null($setting = Setting::getSettings())) { return redirect()->to('admin')->with('error', trans('admin/settings/message.update.error')); @@ -595,10 +592,8 @@ public function postLocalization(Request $request) * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getAlerts() + public function getAlerts() : View { $setting = Setting::getSettings(); @@ -609,12 +604,9 @@ public function getAlerts() * Return a form to allow a super admin to update settings. * * @author [A. Gianotto] [] - * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function postAlerts(Request $request) + public function postAlerts(Request $request) : RedirectResponse { if (is_null($setting = Setting::getSettings())) { return redirect()->to('admin')->with('error', trans('admin/settings/message.update.error')); @@ -668,10 +660,8 @@ public function postAlerts(Request $request) * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getSlack() + public function getSlack() : View { $setting = Setting::getSettings(); @@ -684,10 +674,8 @@ public function getSlack() * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getAssetTags() + public function getAssetTags() : View { $setting = Setting::getSettings(); @@ -700,10 +688,8 @@ public function getAssetTags() * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function postAssetTags(Request $request) + public function postAssetTags(Request $request) : RedirectResponse { if (is_null($setting = Setting::getSettings())) { return redirect()->to('admin')->with('error', trans('admin/settings/message.update.error')); @@ -728,10 +714,8 @@ public function postAssetTags(Request $request) * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getBarcodes() + public function getBarcodes() : View { $setting = Setting::getSettings(); $is_gd_installed = extension_loaded('gd'); @@ -745,10 +729,8 @@ public function getBarcodes() * @author [A. Gianotto] [] * * @since [v1.0] - * - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function postBarcodes(Request $request) + public function postBarcodes(Request $request) : RedirectResponse { if (is_null($setting = Setting::getSettings())) { return redirect()->to('admin')->with('error', trans('admin/settings/message.update.error')); @@ -774,10 +756,8 @@ public function postBarcodes(Request $request) * @author [A. Gianotto] [] * * @since [v4.0] - * - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function getPhpInfo() + public function getPhpInfo() : View | RedirectResponse { if (config('app.debug') === true) { return view('settings.phpinfo'); @@ -791,12 +771,9 @@ public function getPhpInfo() * Return a form to allow a super admin to update settings. * * @author [A. Gianotto] [] - * * @since [v4.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getLabels() + public function getLabels() : View { return view('settings.labels') ->with('setting', Setting::getSettings()) @@ -807,12 +784,9 @@ public function getLabels() * Saves settings from form. * * @author [A. Gianotto] [] - * * @since [v4.0] - * - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function postLabels(Request $request) + public function postLabels(Request $request) : RedirectResponse { if (is_null($setting = Setting::getSettings())) { return redirect()->to('admin')->with('error', trans('admin/settings/message.update.error')); @@ -886,10 +860,8 @@ public function postLabels(Request $request) * @author [A. Gianotto] [] * * @since [v4.0] - * - * @return \Illuminate\Contracts\View\View */ - public function getLdapSettings() + public function getLdapSettings() : View { $setting = Setting::getSettings(); $groups = Group::pluck('name', 'id'); @@ -919,12 +891,9 @@ public function getLdapSettings() * Saves settings from form. * * @author [A. Gianotto] [] - * * @since [v4.0] - * - * @return \Illuminate\Http\RedirectResponse */ - public function postLdapSettings(Request $request) + public function postLdapSettings(Request $request) : RedirectResponse { if (is_null($setting = Setting::getSettings())) { return redirect()->to('admin')->with('error', trans('admin/settings/message.update.error')); @@ -978,15 +947,11 @@ public function postLdapSettings(Request $request) * Return a form to allow a super admin to update settings. * * @author Johnson Yi - * * @since v5.0.0 - * - * @return \Illuminate\Contracts\View\View */ - public function getSamlSettings() + public function getSamlSettings() : View { $setting = Setting::getSettings(); - return view('settings.saml', compact('setting')); } @@ -994,12 +959,9 @@ public function getSamlSettings() * Saves settings from form. * * @author Johnson Yi - * * @since v5.0.0 - * - * @return \Illuminate\Http\RedirectResponse */ - public function postSamlSettings(SettingsSamlRequest $request) + public function postSamlSettings(SettingsSamlRequest $request) : RedirectResponse { if (is_null($setting = Setting::getSettings())) { return redirect()->to('admin')->with('error', trans('admin/settings/message.update.error')); @@ -1028,10 +990,13 @@ public function postSamlSettings(SettingsSamlRequest $request) return redirect()->back()->withInput()->withErrors($setting->getErrors()); } - public static function getPDFBranding() - { - $pdf_branding= Setting::getSettings(); + /** + * Do we need this? Can we not just call getSettings() directly? + */ + public static function getPDFBranding() : Setting + { + $pdf_branding = Setting::getSettings(); return $pdf_branding; } @@ -1041,9 +1006,8 @@ public static function getPDFBranding() * * @author [A. Gianotto] [] * @since [v6.1.1] - * @return \Illuminate\Contracts\View\View */ - public function getGoogleLoginSettings() + public function getGoogleLoginSettings() : View { $setting = Setting::getSettings(); return view('settings.google', compact('setting')); @@ -1054,9 +1018,8 @@ public function getGoogleLoginSettings() * * @author [A. Gianotto] [] * @since [v6.1.1] - * @return \Illuminate\Http\RedirectResponse */ - public function postGoogleLoginSettings(Request $request) + public function postGoogleLoginSettings(Request $request) : RedirectResponse { if (!config('app.lock_passwords')) { $setting = Setting::getSettings(); @@ -1083,10 +1046,8 @@ public function postGoogleLoginSettings(Request $request) * @author [A. Gianotto] [] * * @since [v1.8] - * - * @return \Illuminate\Contracts\View\View */ - public function getBackups() + public function getBackups() : View { $settings = Setting::getSettings(); $path = 'app/backups'; @@ -1122,12 +1083,9 @@ public function getBackups() * Process the backup. * * @author [A. Gianotto] [] - * * @since [v1.8] - * - * @return \Illuminate\Http\RedirectResponse */ - public function postBackups() + public function postBackups() : RedirectResponse { if (! config('app.lock_passwords')) { Artisan::call('snipeit:backup', ['--filename' => 'manual-backup-'.date('Y-m-d-H-i-s')]); @@ -1156,12 +1114,9 @@ public function postBackups() * Download the backup file. * * @author [A. Gianotto] [] - * * @since [v1.8] - * - * @return \Illuminate\Http\RedirectResponse */ - public function downloadFile($filename = null) + public function downloadFile($filename = null) : RedirectResponse { $path = 'app/backups'; @@ -1182,12 +1137,9 @@ public function downloadFile($filename = null) * Delete the backup file. * * @author [A. Gianotto] [] - * * @since [v1.8] - * - * @return \Illuminate\Http\RedirectResponse */ - public function deleteFile($filename = null) + public function deleteFile($filename = null) : RedirectResponse { if (config('app.allow_backup_delete')=='true') { @@ -1220,13 +1172,11 @@ public function deleteFile($filename = null) * Uploads a backup file * * @author [A. Gianotto] [] - * * @since [v6.0] - * - * @return \Illuminate\Http\RedirectResponse */ - public function postUploadBackup(Request $request) { + public function postUploadBackup(Request $request) : RedirectResponse + { if (! config('app.lock_passwords')) { if (!$request->hasFile('file')) { @@ -1258,12 +1208,9 @@ public function postUploadBackup(Request $request) { * Restore the backup file. * * @author [A. Gianotto] [] - * * @since [v6.0] - * - * @return \Illuminate\Http\RedirectResponse */ - public function postRestore($filename = null) + public function postRestore($filename = null) : RedirectResponse { if (! config('app.lock_passwords')) { @@ -1331,13 +1278,11 @@ public function postRestore($filename = null) * @author [A. Gianotto] [] * * @since [v4.0] - * - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse */ - public function getPurge() + public function getPurge() : View | RedirectResponse { - Log::warning('User '.auth()->user()->username.' (ID'.auth()->id().') is attempting a PURGE'); + Log::warning('User '.auth()->user()->username.' (ID: '.auth()->id().') is attempting a PURGE'); if (config('app.allow_purge')=='true') { return view('settings.purge-form'); @@ -1350,12 +1295,9 @@ public function getPurge() * Purges soft-deletes. * * @author [A. Gianotto] [] - * * @since [v3.0] - * - * @return \Illuminate\Http\RedirectResponse */ - public function postPurge(Request $request) + public function postPurge(Request $request) : RedirectResponse { Log::warning('User '.auth()->user()->username.' (ID'.auth()->id().') is attempting a PURGE'); @@ -1399,12 +1341,9 @@ public function postPurge(Request $request) * in the routes file if you want to be able to cache the routes. * * @author [A. Gianotto] [] - * * @since [v4.0] - * - * @return \Illuminate\Contracts\View\View */ - public function api() + public function api() : View { return view('settings.api'); } @@ -1413,12 +1352,9 @@ public function api() * Test the email configuration. * * @author [A. Gianotto] [] - * * @since [v3.0] - * - * @return \Illuminate\Http\JsonResponse */ - public function ajaxTestEmail() + public function ajaxTestEmail() : JsonResponse { try { (new User())->forceFill([ @@ -1438,10 +1374,8 @@ public function ajaxTestEmail() * Get login attempts view * * @author [A. Gianotto] [] - * - * @return \Illuminate\Contracts\View\View */ - public function getLoginAttempts() + public function getLoginAttempts() : View { return view('settings.logins'); } diff --git a/app/Http/Controllers/StatuslabelsController.php b/app/Http/Controllers/StatuslabelsController.php index aba678d47c9b..535117e97fd3 100755 --- a/app/Http/Controllers/StatuslabelsController.php +++ b/app/Http/Controllers/StatuslabelsController.php @@ -6,6 +6,8 @@ use App\Models\Statuslabel; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** * This controller handles all actions related to Status Labels for @@ -17,18 +19,14 @@ class StatuslabelsController extends Controller { /** * Show a list of all the statuslabels. - * - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index() + public function index() : View { $this->authorize('view', Statuslabel::class); - return view('statuslabels.index'); } - public function show($id) + public function show($id) : View | RedirectResponse { $this->authorize('view', Statuslabel::class); if ($statuslabel = Statuslabel::find($id)) { @@ -41,10 +39,8 @@ public function show($id) /** * Statuslabel create. * - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create() + public function create() : View { // Show the page $this->authorize('create', Statuslabel::class); @@ -58,10 +54,8 @@ public function create() * Statuslabel create form processing. * * @param Request $request - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(Request $request) + public function store(Request $request) : RedirectResponse { $this->authorize('create', Statuslabel::class); // create a new model instance @@ -96,10 +90,8 @@ public function store(Request $request) * Statuslabel update. * * @param int $statuslabelId - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit($statuslabelId = null) + public function edit($statuslabelId = null) : View | RedirectResponse { $this->authorize('update', Statuslabel::class); // Check if the Statuslabel exists @@ -119,10 +111,8 @@ public function edit($statuslabelId = null) * Statuslabel update form processing page. * * @param int $statuslabelId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function update(Request $request, $statuslabelId = null) + public function update(Request $request, $statuslabelId = null) : RedirectResponse { $this->authorize('update', Statuslabel::class); // Check if the Statuslabel exists @@ -159,10 +149,8 @@ public function update(Request $request, $statuslabelId = null) * Delete the given Statuslabel. * * @param int $statuslabelId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($statuslabelId) + public function destroy($statuslabelId) : RedirectResponse { $this->authorize('delete', Statuslabel::class); // Check if the Statuslabel exists diff --git a/app/Http/Controllers/SuppliersController.php b/app/Http/Controllers/SuppliersController.php index 86b6147bd45d..e96e32b84fda 100755 --- a/app/Http/Controllers/SuppliersController.php +++ b/app/Http/Controllers/SuppliersController.php @@ -5,6 +5,8 @@ use App\Http\Requests\ImageUploadRequest; use App\Models\Supplier; use Illuminate\Support\Facades\Auth; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** * This controller handles all actions related to Suppliers for @@ -20,25 +22,19 @@ class SuppliersController extends Controller * @return \Illuminate\Contracts\View\View * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index() + public function index() : View { - // Grab all the suppliers $this->authorize('view', Supplier::class); - - // Show the page return view('suppliers/index'); } /** * Supplier create. * - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create() + public function create() : View { $this->authorize('create', Supplier::class); - return view('suppliers/edit')->with('item', new Supplier); } @@ -46,10 +42,8 @@ public function create() * Supplier create form processing. * * @param ImageUploadRequest $request - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : RedirectResponse { $this->authorize('create', Supplier::class); // Create a new supplier @@ -82,10 +76,8 @@ public function store(ImageUploadRequest $request) * Supplier update. * * @param int $supplierId - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit($supplierId = null) + public function edit($supplierId = null) : View | RedirectResponse { $this->authorize('update', Supplier::class); // Check if the supplier exists @@ -102,15 +94,12 @@ public function edit($supplierId = null) * Supplier update form processing page. * * @param int $supplierId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function update($supplierId, ImageUploadRequest $request) + public function update($supplierId, ImageUploadRequest $request) : RedirectResponse { $this->authorize('update', Supplier::class); - // Check if the supplier exists + if (is_null($supplier = Supplier::find($supplierId))) { - // Redirect to the supplier page return redirect()->route('suppliers.index')->with('error', trans('admin/suppliers/message.does_not_exist')); } @@ -141,10 +130,8 @@ public function update($supplierId, ImageUploadRequest $request) * Delete the given supplier. * * @param int $supplierId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($supplierId) + public function destroy($supplierId) : RedirectResponse { $this->authorize('delete', Supplier::class); if (is_null($supplier = Supplier::with('asset_maintenances', 'assets', 'licenses')->withCount('asset_maintenances as asset_maintenances_count', 'assets as assets_count', 'licenses as licenses_count')->find($supplierId))) { @@ -174,10 +161,9 @@ public function destroy($supplierId) * Get the asset information to present to the supplier view page * * @param null $supplierId - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @internal param int $assetId */ - public function show($supplierId = null) + public function show($supplierId = null) : View | RedirectResponse { $this->authorize('view', Supplier::class); $supplier = Supplier::find($supplierId); diff --git a/app/Http/Controllers/ViewAssetsController.php b/app/Http/Controllers/ViewAssetsController.php index 87a993fa1f72..db4afc83223a 100755 --- a/app/Http/Controllers/ViewAssetsController.php +++ b/app/Http/Controllers/ViewAssetsController.php @@ -8,11 +8,11 @@ use App\Models\Company; use App\Models\Setting; use App\Models\User; -use App\Models\CustomField; use App\Notifications\RequestAssetCancelation; use App\Notifications\RequestAssetNotification; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; +use Illuminate\Http\RedirectResponse; +use \Illuminate\Contracts\View\View; /** * This controller handles all actions related to the ability for users @@ -25,9 +25,8 @@ class ViewAssetsController extends Controller /** * Redirect to the profile page. * - * @return \Illuminate\Http\RedirectResponse */ - public function getIndex() + public function getIndex() : View | RedirectResponse { $user = User::with( 'assets', @@ -72,9 +71,8 @@ public function getIndex() /** * Returns view of requestable items for a user. - * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ - public function getRequestableIndex() + public function getRequestableIndex() : View { $assets = Asset::with('model', 'defaultLoc', 'location', 'assignedTo', 'requests')->Hardware()->RequestableAssets(); $models = AssetModel::with('category', 'requests', 'assets')->RequestableModels()->get(); @@ -82,7 +80,7 @@ public function getRequestableIndex() return view('account/requestable-assets', compact('assets', 'models')); } - public function getRequestItem(Request $request, $itemType, $itemId = null, $cancel_by_admin = false, $requestingUser = null) + public function getRequestItem(Request $request, $itemType, $itemId = null, $cancel_by_admin = false, $requestingUser = null) : RedirectResponse { $item = null; $fullItemType = 'App\\Models\\'.studly_case($itemType); @@ -144,9 +142,8 @@ public function getRequestItem(Request $request, $itemType, $itemId = null, $can /** * Process a specific requested asset * @param null $assetId - * @return \Illuminate\Http\RedirectResponse */ - public function getRequestAsset($assetId = null) + public function getRequestAsset($assetId = null) : RedirectResponse { $user = auth()->user(); @@ -196,7 +193,7 @@ public function getRequestAsset($assetId = null) return redirect()->route('requestable-assets')->with('success')->with('success', trans('admin/hardware/message.requests.success')); } - public function getRequestedAssets() + public function getRequestedAssets() : View { return view('account/requested'); } From 1f28bf14c193e95cfc5e44f7803ecc6ee2f23b90 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 00:21:58 +0100 Subject: [PATCH 13/42] Added category tests Signed-off-by: snipe --- .../Categories/Api/IndexCategoriesTest.php | 71 ++++++++++++++ .../Categories/Ui/CreateCategoriesTest.php | 51 ++++++++++ .../Categories/Ui/IndexCategoriesTest.php | 23 +++++ .../Categories/Ui/UpdateCategoriesTest.php | 96 +++++++++++++++++++ 4 files changed, 241 insertions(+) create mode 100644 tests/Feature/Categories/Api/IndexCategoriesTest.php create mode 100644 tests/Feature/Categories/Ui/CreateCategoriesTest.php create mode 100644 tests/Feature/Categories/Ui/IndexCategoriesTest.php create mode 100644 tests/Feature/Categories/Ui/UpdateCategoriesTest.php diff --git a/tests/Feature/Categories/Api/IndexCategoriesTest.php b/tests/Feature/Categories/Api/IndexCategoriesTest.php new file mode 100644 index 000000000000..f3717e62929e --- /dev/null +++ b/tests/Feature/Categories/Api/IndexCategoriesTest.php @@ -0,0 +1,71 @@ +actingAsForApi(User::factory()->create()) + ->getJson(route('api.departments.index')) + ->assertForbidden(); + } + + public function testCategoryIndexReturnsExpectedSearchResults() + { + Category::factory()->count(10)->create(); + Category::factory()->count(1)->forAssets()->create(['name' => 'My Test Category']); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->getJson( + route('api.categories.index', [ + 'search' => 'My Test Category', + 'sort' => 'name', + 'order' => 'asc', + 'offset' => '0', + 'limit' => '20', + ])) + ->assertOk() + ->assertOk() + ->assertJsonStructure([ + 'total', + 'rows', + ]) + ->assertJson([ + 'total' => 1, + ]); + + } + + + public function testCategoryIndexReturnsExpectedCategories() + { + $this->markTestIncomplete('Not sure why the category factory is generating one more than expected here.'); + Category::factory()->count(3)->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->getJson( + route('api.categories.index', [ + 'sort' => 'id', + 'order' => 'asc', + 'offset' => '0', + 'limit' => '20', + ])) + ->assertOk() + ->assertJsonStructure([ + 'total', + 'rows', + ]) + ->assertJson([ + 'total' => 3, + ]); + + } + +} diff --git a/tests/Feature/Categories/Ui/CreateCategoriesTest.php b/tests/Feature/Categories/Ui/CreateCategoriesTest.php new file mode 100644 index 000000000000..87312ad257f8 --- /dev/null +++ b/tests/Feature/Categories/Ui/CreateCategoriesTest.php @@ -0,0 +1,51 @@ +actingAs(User::factory()->create()) + ->post(route('categories.store'), [ + 'name' => 'Test Category', + 'category_type' => 'asset', + ]) + ->assertForbidden(); + } + + public function testUserCanCreateCategories() + { + $this->assertFalse(Category::where('name', 'Test Category')->exists()); + + $this->actingAs(User::factory()->superuser()->create()) + ->post(route('categories.store'), [ + 'name' => 'Test Category', + 'category_type' => 'asset' + ]) + ->assertRedirect(route('categories.index')); + + $this->assertTrue(Category::where('name', 'Test Category')->exists()); + } + + public function testUserCannotCreateCategoriesWithInvalidType() + { + $this->assertFalse(Category::where('name', 'Test Category')->exists()); + + $this->actingAs(User::factory()->superuser()->create()) + ->from(route('categories.create')) + ->post(route('categories.store'), [ + 'name' => 'Test Category', + 'category_type' => 'invalid' + ]) + ->assertRedirect(route('categories.create')); + + $this->assertFalse(Category::where('name', 'Test Category')->exists()); + } + +} diff --git a/tests/Feature/Categories/Ui/IndexCategoriesTest.php b/tests/Feature/Categories/Ui/IndexCategoriesTest.php new file mode 100644 index 000000000000..caacba99a94f --- /dev/null +++ b/tests/Feature/Categories/Ui/IndexCategoriesTest.php @@ -0,0 +1,23 @@ +actingAs(User::factory()->create()) + ->get(route('categories.index')) + ->assertForbidden(); + } + + public function testUserCanListCategories() + { + $this->actingAs(User::factory()->superuser()->create()) + ->get(route('categories.index')) + ->assertOk(); + } +} diff --git a/tests/Feature/Categories/Ui/UpdateCategoriesTest.php b/tests/Feature/Categories/Ui/UpdateCategoriesTest.php new file mode 100644 index 000000000000..98b67c7d32ab --- /dev/null +++ b/tests/Feature/Categories/Ui/UpdateCategoriesTest.php @@ -0,0 +1,96 @@ +actingAs(User::factory()->create()) + ->post(route('categories.store'), [ + 'name' => 'Test Category', + 'category_type' => 'asset' + ]) + ->assertStatus(403) + ->assertForbidden(); + } + + public function testUserCanCreateCategories() + { + $this->actingAs(User::factory()->superuser()->create()) + ->post(route('categories.store'), [ + 'name' => 'Test Category', + 'category_type' => 'asset' + ]) + ->assertStatus(302) + ->assertSessionHasNoErrors() + ->assertRedirect(route('categories.index')); + + $this->assertTrue(Category::where('name', 'Test Category')->exists()); + } + + public function testUserCanEditAssetCategory() + { + $category = Category::factory()->forAssets()->create(['name' => 'Test Category']); + $this->assertTrue(Category::where('name', 'Test Category')->exists()); + + $response = $this->actingAs(User::factory()->superuser()->create()) + ->put(route('categories.update', ['category' => $category]), [ + 'name' => 'Test Category Edited', + ]) + ->assertStatus(302) + ->assertSessionHasNoErrors() + ->assertRedirect(route('categories.index')); + + $this->followRedirects($response)->assertSee('Success'); + $this->assertTrue(Category::where('name', 'Test Category Edited')->exists()); + + } + + public function testUserCanChangeCategoryTypeIfNoAssetsAssociated() + { + $category = Category::factory()->forAssets()->create(['name' => 'Test Category']); + $this->assertTrue(Category::where('name', 'Test Category')->exists()); + + $response = $this->actingAs(User::factory()->superuser()->create()) + ->from(route('categories.edit', ['category' => $category->id])) + ->put(route('categories.update', ['category' => $category]), [ + 'name' => 'Test Category Edited', + 'category_type' => 'accessory', + ]) + ->assertSessionHasNoErrors() + ->assertStatus(302) + ->assertRedirect(route('categories.index')); + + $this->followRedirects($response)->assertSee('Success'); + $this->assertTrue(Category::where('name', 'Test Category Edited')->exists()); + + } + + public function testUserCannotChangeCategoryTypeIfAssetsAreAssociated() + { + Asset::factory()->count(5)->laptopMbp()->create(); + $category = Category::where('name', 'Laptops')->first(); + + $response = $this->actingAs(User::factory()->superuser()->create()) + ->from(route('categories.edit', ['category' => $category->id])) + ->put(route('categories.update', ['category' => $category]), [ + 'name' => 'Test Category Edited', + 'category_type' => 'accessory', + ]) + ->assertSessionHasErrors(['category_type']) + ->assertInvalid(['category_type']) + ->assertStatus(302) + ->assertRedirect(route('categories.edit', ['category' => $category->id])); + + $this->followRedirects($response)->assertSee(trans('general.error')); + $this->assertFalse(Category::where('name', 'Test Category Edited')->exists()); + + } + +} From 6761ff44cc28bc66d6e4bb8af67a3f86707d9752 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 00:22:46 +0100 Subject: [PATCH 14/42] Removed parens Signed-off-by: snipe --- app/Models/Category.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/Models/Category.php b/app/Models/Category.php index 17e41da1fcc3..f21038bab0e4 100755 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -171,15 +171,15 @@ public function itemCount() switch ($this->category_type) { case 'asset': - return $this->assets()->count(); + return $this->assets->count(); case 'accessory': - return $this->accessories()->count(); + return $this->accessories->count(); case 'component': - return $this->components()->count(); + return $this->components->count(); case 'consumable': - return $this->consumables()->count(); + return $this->consumables->count(); case 'license': - return $this->licenses()->count(); + return $this->licenses->count(); default: return 0; } From 7215746d7a8e01947199d2b80d0edd2e61edcafd Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 00:23:02 +0100 Subject: [PATCH 15/42] Prevent users from editing category types if there are still items Signed-off-by: snipe --- app/Http/Controllers/CategoriesController.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/CategoriesController.php b/app/Http/Controllers/CategoriesController.php index df3bc5dce5f1..ac57ad6a6dd8 100755 --- a/app/Http/Controllers/CategoriesController.php +++ b/app/Http/Controllers/CategoriesController.php @@ -112,14 +112,24 @@ public function update(ImageUploadRequest $request, $categoryId = null) : Redire $this->authorize('update', Category::class); if (is_null($category = Category::find($categoryId))) { // Redirect to the categories management page - return redirect()->to('admin/categories')->with('error', trans('admin/categories/message.does_not_exist')); + return redirect()->route('categories.index')->with('error', trans('admin/categories/message.does_not_exist')); } // Update the category data $category->name = $request->input('name'); // If the item count is > 0, we disable the category type in the edit. Disabled items // don't POST, so if the category_type is blank we just set it to the default. + + + // Don't allow the user to change the category_type once it's been created + if (($request->filled('category_type') && ($category->itemCount() > 0))) { + $request->validate(['category_type' => 'in:'.$category->category_type]); + } + $category->category_type = $request->input('category_type', $category->category_type); + + $category->fill($request->all()); + $category->eula_text = $request->input('eula_text'); $category->use_default_eula = $request->input('use_default_eula', '0'); $category->require_acceptance = $request->input('require_acceptance', '0'); From 8baef8d7c51feeb22457893e2e18fe889709277b Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 00:26:28 +0100 Subject: [PATCH 16/42] Derp. Fixed typo Signed-off-by: snipe --- tests/Feature/Categories/Api/IndexCategoriesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Categories/Api/IndexCategoriesTest.php b/tests/Feature/Categories/Api/IndexCategoriesTest.php index f3717e62929e..fc48fdc547bc 100644 --- a/tests/Feature/Categories/Api/IndexCategoriesTest.php +++ b/tests/Feature/Categories/Api/IndexCategoriesTest.php @@ -7,7 +7,7 @@ use Illuminate\Testing\Fluent\AssertableJson; use Tests\TestCase; -class IndexCategoryTest extends TestCase +class IndexCategoriesTest extends TestCase { public function testViewingCategoryIndexRequiresPermission() From 96d1aea54ab8e1beb951d1e22915b23b2d4bff72 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 00:54:50 +0100 Subject: [PATCH 17/42] More cleanup Signed-off-by: snipe --- .../Accessories/AccessoriesController.php | 37 +++++----------- .../AccessoriesFilesController.php | 20 +++------ .../AccessoryCheckinController.php | 17 ++------ .../AccessoryCheckoutController.php | 11 ++--- .../Account/AcceptanceController.php | 14 +++--- app/Http/Controllers/ActionlogController.php | 15 ++++--- .../Assets/AssetCheckinController.php | 12 ++---- .../Assets/AssetCheckoutController.php | 9 ++-- .../Assets/AssetFilesController.php | 16 +++---- .../Controllers/Assets/AssetsController.php | 41 ++++++------------ .../Assets/BulkAssetsController.php | 43 ++++++++----------- app/Http/Controllers/ReportsController.php | 2 +- 12 files changed, 86 insertions(+), 151 deletions(-) diff --git a/app/Http/Controllers/Accessories/AccessoriesController.php b/app/Http/Controllers/Accessories/AccessoriesController.php index f335ccf37a39..bb2e74899b7b 100755 --- a/app/Http/Controllers/Accessories/AccessoriesController.php +++ b/app/Http/Controllers/Accessories/AccessoriesController.php @@ -7,10 +7,10 @@ use App\Http\Requests\ImageUploadRequest; use App\Models\Accessory; use App\Models\Company; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Validator; -use Redirect; +use \Illuminate\Contracts\View\View; +use \Illuminate\Http\RedirectResponse; use Illuminate\Support\Facades\Log; /** This controller handles all actions related to Accessories for @@ -27,13 +27,10 @@ class AccessoriesController extends Controller * @author [A. Gianotto] [] * @see AccessoriesController::getDatatable() method that generates the JSON response * @since [v1.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index() + public function index() : View { $this->authorize('index', Accessory::class); - return view('accessories/index'); } @@ -41,10 +38,8 @@ public function index() * Returns a view with a form to create a new Accessory. * * @author [A. Gianotto] [] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create() + public function create() : View { $this->authorize('create', Accessory::class); $category_type = 'accessory'; @@ -58,10 +53,8 @@ public function create() * * @author [A. Gianotto] [] * @param ImageUploadRequest $request - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : RedirectResponse { $this->authorize(Accessory::class); @@ -100,15 +93,12 @@ public function store(ImageUploadRequest $request) * * @author [A. Gianotto] [] * @param int $accessoryId - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit($accessoryId = null) + public function edit($accessoryId = null) : View | RedirectResponse { if ($item = Accessory::find($accessoryId)) { $this->authorize($item); - return view('accessories/edit', compact('item'))->with('category_type', 'accessory'); } @@ -122,9 +112,8 @@ public function edit($accessoryId = null) * @author [J. Vinsmoke] * @param int $accessoryId * @since [v6.0] - * @return \Illuminate\Contracts\View\View */ - public function getClone($accessoryId = null) + public function getClone($accessoryId = null) : View | RedirectResponse { $this->authorize('create', Accessory::class); @@ -151,10 +140,8 @@ public function getClone($accessoryId = null) * @author [A. Gianotto] [] * @param ImageUploadRequest $request * @param int $accessoryId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function update(ImageUploadRequest $request, $accessoryId = null) + public function update(ImageUploadRequest $request, $accessoryId = null) : RedirectResponse { if ($accessory = Accessory::withCount('users as users_count')->find($accessoryId)) { @@ -205,10 +192,8 @@ public function update(ImageUploadRequest $request, $accessoryId = null) * * @author [A. Gianotto] [] * @param int $accessoryId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($accessoryId) + public function destroy($accessoryId) : RedirectResponse { if (is_null($accessory = Accessory::find($accessoryId))) { return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.not_found')); @@ -243,10 +228,8 @@ public function destroy($accessoryId) * @param int $accessoryID * @see AccessoriesController::getDataView() method that generates the JSON response * @since [v1.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show($accessoryID = null) + public function show($accessoryID = null) : View | RedirectResponse { $accessory = Accessory::withCount('users as users_count')->find($accessoryID); $this->authorize('view', $accessory); diff --git a/app/Http/Controllers/Accessories/AccessoriesFilesController.php b/app/Http/Controllers/Accessories/AccessoriesFilesController.php index 75fa0b2f1220..baf09c66b0e9 100644 --- a/app/Http/Controllers/Accessories/AccessoriesFilesController.php +++ b/app/Http/Controllers/Accessories/AccessoriesFilesController.php @@ -7,10 +7,11 @@ use App\Http\Requests\UploadFileRequest; use App\Models\Actionlog; use App\Models\Accessory; -use Illuminate\Support\Facades\Response; use Illuminate\Support\Facades\Storage; -use Symfony\Accessory\HttpFoundation\JsonResponse; use Illuminate\Support\Facades\Log; +use \Illuminate\Contracts\View\View; +use \Illuminate\Http\RedirectResponse; +use Illuminate\Support\Facades\Response; class AccessoriesFilesController extends Controller { @@ -19,20 +20,17 @@ class AccessoriesFilesController extends Controller * * @param UploadFileRequest $request * @param int $accessoryId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException - *@author [A. Gianotto] [] + * @author [A. Gianotto] [] * @since [v1.0] * @todo Switch to using the AssetFileRequest form request validator. */ - public function store(UploadFileRequest $request, $accessoryId = null) + public function store(UploadFileRequest $request, $accessoryId = null) : RedirectResponse { if (config('app.lock_passwords')) { return redirect()->route('accessories.show', ['accessory'=>$accessoryId])->with('error', trans('general.feature_disabled')); } - $accessory = Accessory::find($accessoryId); if (isset($accessory->id)) { @@ -69,10 +67,8 @@ public function store(UploadFileRequest $request, $accessoryId = null) * @since [v1.0] * @param int $accessoryId * @param int $fileId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($accessoryId = null, $fileId = null) + public function destroy($accessoryId = null, $fileId = null) : RedirectResponse { $accessory = Accessory::find($accessoryId); @@ -107,10 +103,8 @@ public function destroy($accessoryId = null, $fileId = null) * @since [v1.4] * @param int $accessoryId * @param int $fileId - * @return \Symfony\Accessory\HttpFoundation\Response - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show($accessoryId = null, $fileId = null, $download = true) + public function show($accessoryId = null, $fileId = null, $download = true) : View | RedirectResponse | Response { Log::debug('Private filesystem is: '.config('filesystems.default')); diff --git a/app/Http/Controllers/Accessories/AccessoryCheckinController.php b/app/Http/Controllers/Accessories/AccessoryCheckinController.php index a1af20594560..eff635d24a2a 100644 --- a/app/Http/Controllers/Accessories/AccessoryCheckinController.php +++ b/app/Http/Controllers/Accessories/AccessoryCheckinController.php @@ -7,8 +7,9 @@ use App\Models\Accessory; use App\Models\User; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; +use \Illuminate\Contracts\View\View; +use \Illuminate\Http\RedirectResponse; class AccessoryCheckinController extends Controller { @@ -19,15 +20,10 @@ class AccessoryCheckinController extends Controller * @param Request $request * @param int $accessoryUserId * @param string $backto - * @return \Illuminate\Contracts\View\View - * @internal param int $accessoryId - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create($accessoryUserId = null, $backto = null) + public function create($accessoryUserId = null, $backto = null) : View | RedirectResponse { - // Check if the accessory exists if (is_null($accessory_user = DB::table('accessories_users')->find($accessoryUserId))) { - // Redirect to the accessory management page with error return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.not_found')); } @@ -44,15 +40,10 @@ public function create($accessoryUserId = null, $backto = null) * @author [A. Gianotto] [] * @param null $accessoryUserId * @param string $backto - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException - * @internal param int $accessoryId */ - public function store(Request $request, $accessoryUserId = null, $backto = null) + public function store(Request $request, $accessoryUserId = null, $backto = null) : RedirectResponse { - // Check if the accessory exists if (is_null($accessory_user = DB::table('accessories_users')->find($accessoryUserId))) { - // Redirect to the accessory management page with error return redirect()->route('accessories.index')->with('error', trans('admin/accessories/message.does_not_exist')); } diff --git a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php index c980e29efd27..103cbd09b051 100644 --- a/app/Http/Controllers/Accessories/AccessoryCheckoutController.php +++ b/app/Http/Controllers/Accessories/AccessoryCheckoutController.php @@ -10,7 +10,8 @@ use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Input; +use \Illuminate\Contracts\View\View; +use \Illuminate\Http\RedirectResponse; class AccessoryCheckoutController extends Controller { @@ -19,10 +20,8 @@ class AccessoryCheckoutController extends Controller * * @author [A. Gianotto] [] * @param int $id - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function create($id) + public function create($id) : View | RedirectResponse { if ($accessory = Accessory::withCount('users as users_count')->find($id)) { @@ -59,10 +58,8 @@ public function create($id) * @author [A. Gianotto] [] * @param Request $request * @param int $accessoryId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function store(Request $request, $accessoryId) + public function store(Request $request, $accessoryId) : RedirectResponse { // Check if the accessory exists if (is_null($accessory = Accessory::withCount('users as users_count')->find($accessoryId))) { diff --git a/app/Http/Controllers/Account/AcceptanceController.php b/app/Http/Controllers/Account/AcceptanceController.php index 6ea00fd9d0aa..2c9d29fc160d 100644 --- a/app/Http/Controllers/Account/AcceptanceController.php +++ b/app/Http/Controllers/Account/AcceptanceController.php @@ -28,20 +28,18 @@ use App\Http\Controllers\SettingsController; use Barryvdh\DomPDF\Facade\Pdf; use Carbon\Carbon; -use phpDocumentor\Reflection\Types\Compound; +use \Illuminate\Contracts\View\View; +use \Illuminate\Http\RedirectResponse; use Illuminate\Support\Facades\Log; class AcceptanceController extends Controller { /** * Show a listing of pending checkout acceptances for the current user - * - * @return \Illuminate\Contracts\View\View */ - public function index() + public function index() : View { $acceptances = CheckoutAcceptance::forUser(auth()->user())->pending()->get(); - return view('account/accept.index', compact('acceptances')); } @@ -49,9 +47,8 @@ public function index() * Shows a form to either accept or decline the checkout acceptance * * @param int $id - * @return mixed */ - public function create($id) + public function create($id) : View | RedirectResponse { $acceptance = CheckoutAcceptance::find($id); @@ -80,9 +77,8 @@ public function create($id) * * @param Request $request * @param int $id - * @return \Illuminate\Http\RedirectResponse */ - public function store(Request $request, $id) + public function store(Request $request, $id) : RedirectResponse { $acceptance = CheckoutAcceptance::find($id); diff --git a/app/Http/Controllers/ActionlogController.php b/app/Http/Controllers/ActionlogController.php index 2d194b7ca6ca..a38233f0e39c 100644 --- a/app/Http/Controllers/ActionlogController.php +++ b/app/Http/Controllers/ActionlogController.php @@ -3,13 +3,13 @@ namespace App\Http\Controllers; use App\Helpers\Helper; -use App\Models\Actionlog; -use Illuminate\Support\Facades\Response; +use Illuminate\Http\RedirectResponse; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Storage; +use \Illuminate\Http\Response; class ActionlogController extends Controller { - public function displaySig($filename) + public function displaySig($filename) : RedirectResponse | Response | bool { // PHP doesn't let you handle file not found errors well with // file_get_contents, so we set the error reporting for just this class @@ -17,6 +17,7 @@ public function displaySig($filename) $disk = config('filesystems.default'); switch (config("filesystems.disks.$disk.driver")) { + case 's3': $file = 'private_uploads/signatures/'.$filename; return redirect()->away(Storage::disk($disk)->temporaryUrl($file, now()->addMinutes(5))); @@ -30,15 +31,15 @@ public function displaySig($filename) Log::warning('File '.$file.' not found'); return false; } else { - return Response::make($contents)->header('Content-Type', $filetype); + return response()->make($contents)->header('Content-Type', $filetype); } } } - public function getStoredEula($filename){ + public function getStoredEula($filename) : Response + { $this->authorize('view', \App\Models\Asset::class); $file = config('app.private_uploads').'/eula-pdfs/'.$filename; - - return Response::download($file); + return response()->download($file); } } diff --git a/app/Http/Controllers/Assets/AssetCheckinController.php b/app/Http/Controllers/Assets/AssetCheckinController.php index 10aa95794214..4794fa0411d7 100644 --- a/app/Http/Controllers/Assets/AssetCheckinController.php +++ b/app/Http/Controllers/Assets/AssetCheckinController.php @@ -11,10 +11,10 @@ use App\Models\CheckoutAcceptance; use App\Models\LicenseSeat; use Illuminate\Database\Eloquent\Builder; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; -use Illuminate\Support\Facades\View; use Illuminate\Support\Facades\Log; +use \Illuminate\Contracts\View\View; +use \Illuminate\Http\RedirectResponse; class AssetCheckinController extends Controller { @@ -26,11 +26,9 @@ class AssetCheckinController extends Controller * @author [A. Gianotto] [] * @param int $assetId * @param string $backto - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException * @since [v1.0] */ - public function create($assetId, $backto = null) + public function create($assetId, $backto = null) : View | RedirectResponse { // Check if the asset exists if (is_null($asset = Asset::find($assetId))) { @@ -60,11 +58,9 @@ public function create($assetId, $backto = null) * @param AssetCheckinRequest $request * @param int $assetId * @param null $backto - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException * @since [v1.0] */ - public function store(AssetCheckinRequest $request, $assetId = null, $backto = null) + public function store(AssetCheckinRequest $request, $assetId = null, $backto = null) : RedirectResponse { // Check if the asset exists if (is_null($asset = Asset::find($assetId))) { diff --git a/app/Http/Controllers/Assets/AssetCheckoutController.php b/app/Http/Controllers/Assets/AssetCheckoutController.php index dfc11f1fc76e..355f9387bba3 100644 --- a/app/Http/Controllers/Assets/AssetCheckoutController.php +++ b/app/Http/Controllers/Assets/AssetCheckoutController.php @@ -9,8 +9,9 @@ use App\Http\Requests\AssetCheckoutRequest; use App\Models\Asset; use Illuminate\Database\Eloquent\ModelNotFoundException; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Session; +use \Illuminate\Contracts\View\View; +use \Illuminate\Http\RedirectResponse; class AssetCheckoutController extends Controller { @@ -25,7 +26,7 @@ class AssetCheckoutController extends Controller * @since [v1.0] * @return \Illuminate\Contracts\View\View */ - public function create($assetId) + public function create($assetId) : View | RedirectResponse { // Check if the asset exists if (is_null($asset = Asset::with('company')->find(e($assetId)))) { @@ -53,11 +54,9 @@ public function create($assetId) * * @author [A. Gianotto] [] * @param AssetCheckoutRequest $request - * @param int $assetId - * @return \Illuminate\Http\RedirectResponse * @since [v1.0] */ - public function store(AssetCheckoutRequest $request, $assetId) + public function store(AssetCheckoutRequest $request, $assetId) : RedirectResponse { try { // Check if the asset exists diff --git a/app/Http/Controllers/Assets/AssetFilesController.php b/app/Http/Controllers/Assets/AssetFilesController.php index fcd3375f8dc0..d79afbff6670 100644 --- a/app/Http/Controllers/Assets/AssetFilesController.php +++ b/app/Http/Controllers/Assets/AssetFilesController.php @@ -7,8 +7,11 @@ use App\Http\Requests\UploadFileRequest; use App\Models\Actionlog; use App\Models\Asset; -use Illuminate\Support\Facades\Response; +use \Illuminate\Http\Response; use Illuminate\Support\Facades\Storage; +use \Illuminate\Contracts\View\View; +use \Illuminate\Http\RedirectResponse; +use Symfony\Component\HttpFoundation\StreamedResponse; class AssetFilesController extends Controller { @@ -22,7 +25,7 @@ class AssetFilesController extends Controller *@since [v1.0] * @author [A. Gianotto] [] */ - public function store(UploadFileRequest $request, $assetId = null) + public function store(UploadFileRequest $request, $assetId = null) : RedirectResponse { if (! $asset = Asset::find($assetId)) { return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist')); @@ -54,10 +57,8 @@ public function store(UploadFileRequest $request, $assetId = null) * @param int $assetId * @param int $fileId * @since [v1.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function show($assetId = null, $fileId = null) + public function show($assetId = null, $fileId = null) : View | RedirectResponse | Response | StreamedResponse { $asset = Asset::find($assetId); // the asset is valid @@ -105,10 +106,8 @@ public function show($assetId = null, $fileId = null) * @param int $assetId * @param int $fileId * @since [v1.0] - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function destroy($assetId = null, $fileId = null) + public function destroy($assetId = null, $fileId = null) : RedirectResponse { $asset = Asset::find($assetId); $this->authorize('update', $asset); @@ -131,7 +130,6 @@ public function destroy($assetId = null, $fileId = null) ->with('success', trans('admin/hardware/message.deletefile.success')); } - // Redirect to the hardware management page return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist')); } } diff --git a/app/Http/Controllers/Assets/AssetsController.php b/app/Http/Controllers/Assets/AssetsController.php index ec258b88ec9f..e3099c4d5531 100755 --- a/app/Http/Controllers/Assets/AssetsController.php +++ b/app/Http/Controllers/Assets/AssetsController.php @@ -20,14 +20,15 @@ use App\View\Label; use Carbon\Carbon; use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\View; use Illuminate\Support\Facades\Gate; use Illuminate\Http\Request; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Validator; use League\Csv\Reader; -use Illuminate\Support\Facades\Redirect; +use Illuminate\Http\Response; +use Illuminate\Contracts\View\View; +use Illuminate\Http\RedirectResponse; /** * This class controls all actions related to assets for @@ -55,10 +56,8 @@ public function __construct() * @see AssetController::getDatatable() method that generates the JSON response * @since [v1.0] * @param Request $request - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function index(Request $request) + public function index(Request $request) : View { $this->authorize('index', Asset::class); $company = Company::find($request->input('company_id')); @@ -72,13 +71,12 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v1.0] * @param Request $request - * @return \Illuminate\Contracts\View\View * @internal param int $model_id */ - public function create(Request $request) + public function create(Request $request) : View { $this->authorize('create', Asset::class); - $view = View::make('hardware/edit') + $view = view('hardware/edit') ->with('statuslabel_list', Helper::statusLabelList()) ->with('item', new Asset) ->with('statuslabel_types', Helper::statusTypeList()); @@ -96,9 +94,8 @@ public function create(Request $request) * * @author [A. Gianotto] [] * @since [v1.0] - * @return \Illuminate\Http\RedirectResponse */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : RedirectResponse { $this->authorize(Asset::class); @@ -207,7 +204,6 @@ public function store(ImageUploadRequest $request) } if ($success) { - Log::debug(e($asset->asset_tag)); return redirect()->route('hardware.index') ->with('success-unescaped', trans('admin/hardware/message.create.success_linked', ['link' => route('hardware.show', $asset->id), 'id', 'tag' => e($asset->asset_tag)])); @@ -217,11 +213,6 @@ public function store(ImageUploadRequest $request) return redirect()->back()->withInput()->withErrors($asset->getErrors()); } - public function getOptionCookie(Request $request){ - $value = $request->cookie('optional_info'); - echo $value; - return $value; - } /** * Returns a view that presents a form to edit an existing asset. @@ -231,7 +222,7 @@ public function getOptionCookie(Request $request){ * @since [v1.0] * @return \Illuminate\Contracts\View\View */ - public function edit($assetId = null) + public function edit($assetId = null) : View | RedirectResponse { if (! $item = Asset::find($assetId)) { // Redirect to the asset management page with error @@ -254,7 +245,7 @@ public function edit($assetId = null) * @since [v1.0] * @return \Illuminate\Contracts\View\View */ - public function show($assetId = null) + public function show($assetId = null) : View | RedirectResponse { $asset = Asset::withTrashed()->find($assetId); $this->authorize('view', $asset); @@ -292,11 +283,10 @@ public function show($assetId = null) * Validate and process asset edit form. * * @param int $assetId - * @return \Illuminate\Http\RedirectResponse|Redirect * @since [v1.0] * @author [A. Gianotto] [] */ - public function update(ImageUploadRequest $request, $assetId = null) + public function update(ImageUploadRequest $request, $assetId = null) : RedirectResponse { // Check if the asset exists if (! $asset = Asset::find($assetId)) { @@ -411,9 +401,8 @@ public function update(ImageUploadRequest $request, $assetId = null) * @author [A. Gianotto] [] * @param int $assetId * @since [v1.0] - * @return \Illuminate\Http\RedirectResponse */ - public function destroy($assetId) + public function destroy($assetId) : RedirectResponse { // Check if the asset exists if (is_null($asset = Asset::find($assetId))) { @@ -445,9 +434,8 @@ public function destroy($assetId) * * @author [A. Gianotto] [] * @since [v3.0] - * @return \Illuminate\Http\RedirectResponse */ - public function getAssetBySerial(Request $request) + public function getAssetBySerial(Request $request) : RedirectResponse { $topsearch = ($request->get('topsearch')=="true"); @@ -465,7 +453,7 @@ public function getAssetBySerial(Request $request) * @since [v3.0] * @return \Illuminate\Http\RedirectResponse */ - public function getAssetByTag(Request $request, $tag=null) + public function getAssetByTag(Request $request, $tag=null) : RedirectResponse { $tag = $tag ? $tag : $request->get('assetTag'); $topsearch = ($request->get('topsearch') == 'true'); @@ -485,9 +473,8 @@ public function getAssetByTag(Request $request, $tag=null) * @author [A. Gianotto] [] * @param int $assetId * @since [v1.0] - * @return Response */ - public function getQrCode($assetId = null) + public function getQrCode($assetId = null) : Response { $settings = Setting::getSettings(); diff --git a/app/Http/Controllers/Assets/BulkAssetsController.php b/app/Http/Controllers/Assets/BulkAssetsController.php index c2b5c7369640..13f9c4fc9228 100644 --- a/app/Http/Controllers/Assets/BulkAssetsController.php +++ b/app/Http/Controllers/Assets/BulkAssetsController.php @@ -2,7 +2,6 @@ namespace App\Http\Controllers\Assets; -use App\Models\Actionlog; use App\Helpers\Helper; use App\Http\Controllers\CheckInOutRequest; use App\Http\Controllers\Controller; @@ -12,14 +11,15 @@ use App\Models\Setting; use App\View\Label; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Crypt; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Log; -use Illuminate\Support\Facades\Session; use App\Http\Requests\AssetCheckoutRequest; use App\Models\CustomField; +use Illuminate\Contracts\View\View; +use Illuminate\Http\RedirectResponse; +use Illuminate\Database\Eloquent\ModelNotFoundException; class BulkAssetsController extends Controller { @@ -36,12 +36,10 @@ class BulkAssetsController extends Controller * action would make a lot more sense here and make things a lot more clear. * * @author [A. Gianotto] [] - * @return \Illuminate\Contracts\View\View * @internal param int $assetId * @since [v2.0] - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function edit(Request $request) + public function edit(Request $request) : View | RedirectResponse { $this->authorize('view', Asset::class); @@ -194,7 +192,7 @@ public function edit(Request $request) * @internal param array $assets * @since [v2.0] */ - public function update(Request $request) + public function update(Request $request) : RedirectResponse { $this->authorize('update', Asset::class); $has_errors = 0; @@ -455,9 +453,8 @@ public function update(Request $request) /** * Adds parameter to update array for an item if it exists in request * @param string $field field name - * @return BulkAssetsController Model for Chaining */ - protected function conditionallyAddItem($field) + protected function conditionallyAddItem($field) : BulkAssetsController { if (request()->filled($field)) { $this->update_array[$field] = request()->input($field); @@ -471,12 +468,10 @@ protected function conditionallyAddItem($field) * * @author [A. Gianotto] [] * @param Request $request - * @return \Illuminate\Contracts\View\View - * @throws \Illuminate\Auth\Access\AuthorizationException * @internal param array $assets * @since [v2.0] */ - public function destroy(Request $request) + public function destroy(Request $request) : RedirectResponse { $this->authorize('delete', Asset::class); @@ -505,21 +500,17 @@ public function destroy(Request $request) /** * Show Bulk Checkout Page - * @return \Illuminate\Contracts\View\View View to checkout multiple assets */ - public function showCheckout() + public function showCheckout() : View { $this->authorize('checkout', Asset::class); - // Filter out assets that are not deployable. - return view('hardware/bulk-checkout'); } /** * Process Multiple Checkout Request - * @return \Illuminate\Contracts\View\View */ - public function storeCheckout(AssetCheckoutRequest $request) + public function storeCheckout(AssetCheckoutRequest $request) : RedirectResponse | ModelNotFoundException { $this->authorize('checkout', Asset::class); @@ -584,17 +575,19 @@ public function storeCheckout(AssetCheckoutRequest $request) } } - public function restore(Request $request) { + public function restore(Request $request) : RedirectResponse + { $this->authorize('update', Asset::class); - $assetIds = $request->get('ids'); - if (empty($assetIds)) { - return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.restore.nothing_updated')); + $assetIds = $request->get('ids'); + + if (empty($assetIds)) { + return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.restore.nothing_updated')); } else { foreach ($assetIds as $key => $assetId) { - $asset = Asset::withTrashed()->find($assetId); - $asset->restore(); + $asset = Asset::withTrashed()->find($assetId); + $asset->restore(); } - return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.restore.success')); + return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.restore.success')); } } } diff --git a/app/Http/Controllers/ReportsController.php b/app/Http/Controllers/ReportsController.php index 3752c36bb462..6030a721fe4a 100644 --- a/app/Http/Controllers/ReportsController.php +++ b/app/Http/Controllers/ReportsController.php @@ -93,7 +93,7 @@ public function exportAccessoryReport() : Response } $csv = implode("\n", $rows); - $response = Response::make($csv, 200); + $response = response()->make($csv, 200); $response->header('Content-Type', 'text/csv'); $response->header('Content-disposition', 'attachment;filename=report.csv'); From 4089b4cd68ec515b1ef6e0145d9010881a45947a Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 01:46:09 +0100 Subject: [PATCH 18/42] Added category tests Signed-off-by: snipe --- .../Categories/Api/CreateCategoriesTest.php | 60 +++++++++++++++++++ .../Categories/Api/UpdateCategoryTest.php | 56 +++++++++++++++++ 2 files changed, 116 insertions(+) create mode 100644 tests/Feature/Categories/Api/CreateCategoriesTest.php create mode 100644 tests/Feature/Categories/Api/UpdateCategoryTest.php diff --git a/tests/Feature/Categories/Api/CreateCategoriesTest.php b/tests/Feature/Categories/Api/CreateCategoriesTest.php new file mode 100644 index 000000000000..db5810cf1459 --- /dev/null +++ b/tests/Feature/Categories/Api/CreateCategoriesTest.php @@ -0,0 +1,60 @@ +actingAsForApi(User::factory()->create()) + ->postJson(route('api.categories.store')) + ->assertForbidden(); + } + + public function testCanCreateCategoryWithCategoryType() + { + $response = $this->actingAsForApi(User::factory()->superuser()->create()) + ->postJson(route('api.categories.store'), [ + 'name' => 'Test Category', + 'eula_text' => 'Test EULA', + 'category_type' => 'accessory', + ]) + ->assertOk() + ->assertStatusMessageIs('success') + ->assertStatus(200) + ->json(); + + $this->assertTrue(Category::where('name', 'Test Category')->exists()); + + $category = Category::find($response['payload']['id']); + $this->assertEquals('Test Category', $category->name); + $this->assertEquals('Test EULA', $category->eula_text); + $this->assertEquals('accessory', $category->category_type); + } + + public function testCannotCreateCategoryWithoutCategoryType() + { + $this->actingAsForApi(User::factory()->superuser()->create()) + ->postJson(route('api.categories.store'), [ + 'name' => 'Test Category', + 'eula_text' => 'Test EULA', + ]) + ->assertOk() + ->assertStatusMessageIs('error') + ->assertStatus(200) + ->json(); + + $this->assertFalse(Category::where('name', 'Test Category')->exists()); + + } + +} diff --git a/tests/Feature/Categories/Api/UpdateCategoryTest.php b/tests/Feature/Categories/Api/UpdateCategoryTest.php new file mode 100644 index 000000000000..5c67ea76e56d --- /dev/null +++ b/tests/Feature/Categories/Api/UpdateCategoryTest.php @@ -0,0 +1,56 @@ +create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->patchJson(route('api.categories.update', $category), [ + 'name' => 'Test Category', + 'eula_text' => 'Test EULA', + ]) + ->assertOk() + ->assertStatusMessageIs('success') + ->assertStatus(200) + ->json(); + + //dd($response); + $category->refresh(); + $this->assertEquals('Test Category', $category->name, 'Name was not updated'); + $this->assertEquals('Test EULA', $category->eula_text, 'EULA was not updated'); + + } + + public function testCannotUpdateCategoryViaPatchWithCategoryType() + { + $category = Category::factory()->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->patchJson(route('api.categories.update', $category), [ + 'name' => 'Test Category', + 'eula_text' => 'Test EULA', + 'category_type' => 'accessory', + ]) + ->assertOk() + ->assertStatusMessageIs('error') + ->assertStatus(200) + ->json(); + + //dd($response); + $category->refresh(); + $this->assertNotEquals('Test Category', $category->name, 'Name was not updated'); + $this->assertNotEquals('Test EULA', $category->eula_text, 'EULA was not updated'); + $this->assertNotEquals('accessory', $category->category_type, 'EULA was not updated'); + + } + +} From 6da6411f3baa1b44c040f7481347882c064467e3 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 01:46:36 +0100 Subject: [PATCH 19/42] Added location API index test Signed-off-by: snipe --- .../Locations/Api/IndexLocationsTest.php | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tests/Feature/Locations/Api/IndexLocationsTest.php diff --git a/tests/Feature/Locations/Api/IndexLocationsTest.php b/tests/Feature/Locations/Api/IndexLocationsTest.php new file mode 100644 index 000000000000..38857a760713 --- /dev/null +++ b/tests/Feature/Locations/Api/IndexLocationsTest.php @@ -0,0 +1,45 @@ +getJson(route('api.locations.index'))->assertRedirect(); + } + + public function testViewingLocationIndexRequiresPermission() + { + $this->actingAsForApi(User::factory()->create()) + ->getJson(route('api.locations.index')) + ->assertForbidden(); + } + + public function testLocationIndexReturnsExpectedLocations() + { + Location::factory()->count(3)->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->getJson( + route('api.locations.index', [ + 'sort' => 'name', + 'order' => 'asc', + 'offset' => '0', + 'limit' => '20', + ])) + ->assertOk() + ->assertJsonStructure([ + 'total', + 'rows', + ]) + ->assertJson(fn(AssertableJson $json) => $json->has('rows', 3)->etc()); + } + +} From 5ebcc3b1a086d7233233e5d295ac23d97f5b0439 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 02:36:55 +0100 Subject: [PATCH 20/42] More type hinting Signed-off-by: snipe --- app/Http/Controllers/Api/CategoriesController.php | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/app/Http/Controllers/Api/CategoriesController.php b/app/Http/Controllers/Api/CategoriesController.php index 2aa4b3741c62..a4eb192bc2ab 100644 --- a/app/Http/Controllers/Api/CategoriesController.php +++ b/app/Http/Controllers/Api/CategoriesController.php @@ -8,9 +8,9 @@ use App\Http\Transformers\SelectlistTransformer; use App\Models\Category; use Illuminate\Http\Request; +use Illuminate\Http\JsonResponse; use App\Http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; -use Illuminate\Support\Facades\Validator; class CategoriesController extends Controller { @@ -21,7 +21,7 @@ class CategoriesController extends Controller * @since [v4.0] * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : array { $this->authorize('view', Category::class); $allowed_columns = [ @@ -115,7 +115,7 @@ public function index(Request $request) * @param \App\Http\Requests\ImageUploadRequest $request * @return \Illuminate\Http\Response */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : JsonResponse { $this->authorize('create', Category::class); $category = new Category; @@ -136,9 +136,8 @@ public function store(ImageUploadRequest $request) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : array { $this->authorize('view', Category::class); $category = Category::withCount('assets as assets_count', 'accessories as accessories_count', 'consumables as consumables_count', 'components as components_count', 'licenses as licenses_count')->findOrFail($id); @@ -156,7 +155,7 @@ public function show($id) * @param int $id * @return \Illuminate\Http\Response */ - public function update(ImageUploadRequest $request, $id) + public function update(ImageUploadRequest $request, $id) : JsonResponse { $this->authorize('update', Category::class); $category = Category::findOrFail($id); From 1d45e0df7952b1ec9c4ca36c9a968ef96b814c70 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 03:20:54 +0100 Subject: [PATCH 21/42] Added model create test Signed-off-by: snipe --- .../AssetModels/Api/CreateAssetModelsTest.php | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 tests/Feature/AssetModels/Api/CreateAssetModelsTest.php diff --git a/tests/Feature/AssetModels/Api/CreateAssetModelsTest.php b/tests/Feature/AssetModels/Api/CreateAssetModelsTest.php new file mode 100644 index 000000000000..a0b1c27b7381 --- /dev/null +++ b/tests/Feature/AssetModels/Api/CreateAssetModelsTest.php @@ -0,0 +1,61 @@ +actingAsForApi(User::factory()->create()) + ->postJson(route('api.models.store')) + ->assertForbidden(); + } + + public function testCanCreateAssetModelWithAssetModelType() + { + $response = $this->actingAsForApi(User::factory()->superuser()->create()) + ->postJson(route('api.models.store'), [ + 'name' => 'Test AssetModel', + 'category_id' => Category::factory()->create()->id + ]) + ->assertOk() + ->assertStatusMessageIs('success') + ->assertStatus(200) + ->json(); + + $this->assertTrue(AssetModel::where('name', 'Test AssetModel')->exists()); + + $model = AssetModel::find($response['payload']['id']); + $this->assertEquals('Test AssetModel', $model->name); + } + + public function testCannotCreateAssetModelWithoutCategory() + { + $response = $this->actingAsForApi(User::factory()->superuser()->create()) + ->postJson(route('api.models.store'), [ + 'name' => 'Test AssetModel', + ]) + ->assertStatus(200) + ->assertOk() + ->assertStatusMessageIs('error') + ->assertJson([ + 'messages' => [ + 'category_id' => ['The category id field is required.'], + ], + ]) + ->json(); + + // dd($response); + $this->assertFalse(AssetModel::where('name', 'Test AssetModel')->exists()); + + } + +} From 75da626c85191905f0f709e9531daeb0ae4789c4 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 03:21:24 +0100 Subject: [PATCH 22/42] Check for category ID Signed-off-by: snipe --- app/Http/Requests/StoreAssetModelRequest.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/app/Http/Requests/StoreAssetModelRequest.php b/app/Http/Requests/StoreAssetModelRequest.php index eee32933e2ef..052f23f5a541 100644 --- a/app/Http/Requests/StoreAssetModelRequest.php +++ b/app/Http/Requests/StoreAssetModelRequest.php @@ -20,11 +20,14 @@ public function authorize(): bool public function prepareForValidation(): void { - $this->category_type = Category::find($this->category_id)->category_type; + if ($this->category_id) { + $this->category_type = Category::find($this->category_id)->category_type; + + $this->merge([ + 'category_type' => $this->category_type, + ]); + } - $this->merge([ - 'category_type' => $this->category_type, - ]); } /** @@ -35,7 +38,7 @@ public function prepareForValidation(): void public function rules(): array { return array_merge( - ['category_type' => 'required|in:asset'], + ['category_type' => 'in:asset'], parent::rules(), ); } From 34bde52f74bcc360554dcd4fbe5231b2cf590159 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 03:21:34 +0100 Subject: [PATCH 23/42] Fixed typos Signed-off-by: snipe --- app/Models/AssetModel.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/app/Models/AssetModel.php b/app/Models/AssetModel.php index c5fb9284aada..07e7a5e24477 100755 --- a/app/Models/AssetModel.php +++ b/app/Models/AssetModel.php @@ -9,6 +9,7 @@ use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Storage; use Watson\Validating\ValidatingTrait; +use \App\Presenters\AssetModelPresenter; /** * Model for Asset Models. Asset Models contain higher level @@ -20,7 +21,7 @@ class AssetModel extends SnipeModel { use HasFactory; use SoftDeletes; - protected $presenter = \App\Presenters\AssetModelPresenter::class; + protected $presenter = AssetModelPresenter::class; use Loggable, Requestable, Presentable; protected $table = 'models'; @@ -37,7 +38,7 @@ class AssetModel extends SnipeModel ]; /** - * Whether the model should inject it's identifier to the unique + * Whether the model should inject its identifier to the unique * validation rules before attempting validation. If this property * is not set in the model it will default to true. * From efb9d107cf461b3c86904aa7e783f054d532af68 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 03:22:03 +0100 Subject: [PATCH 24/42] Removed debugging Signed-off-by: snipe --- tests/Feature/Categories/Api/UpdateCategoryTest.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/Feature/Categories/Api/UpdateCategoryTest.php b/tests/Feature/Categories/Api/UpdateCategoryTest.php index 5c67ea76e56d..1a784c11795f 100644 --- a/tests/Feature/Categories/Api/UpdateCategoryTest.php +++ b/tests/Feature/Categories/Api/UpdateCategoryTest.php @@ -44,8 +44,7 @@ public function testCannotUpdateCategoryViaPatchWithCategoryType() ->assertStatusMessageIs('error') ->assertStatus(200) ->json(); - - //dd($response); + $category->refresh(); $this->assertNotEquals('Test Category', $category->name, 'Name was not updated'); $this->assertNotEquals('Test EULA', $category->eula_text, 'EULA was not updated'); From a0914ccb288313b7606bab78ea09c0ab5101d261 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 03:22:56 +0100 Subject: [PATCH 25/42] Fleshed out tests Signed-off-by: snipe --- .../Categories/Api/CreateCategoriesTest.php | 31 ++++++++++++++++--- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/tests/Feature/Categories/Api/CreateCategoriesTest.php b/tests/Feature/Categories/Api/CreateCategoriesTest.php index db5810cf1459..fc464242af29 100644 --- a/tests/Feature/Categories/Api/CreateCategoriesTest.php +++ b/tests/Feature/Categories/Api/CreateCategoriesTest.php @@ -20,7 +20,7 @@ public function testRequiresPermissionToCreateCategory() ->assertForbidden(); } - public function testCanCreateCategoryWithCategoryType() + public function testCanCreateCategoryWithValidCategoryType() { $response = $this->actingAsForApi(User::factory()->superuser()->create()) ->postJson(route('api.categories.store'), [ @@ -42,17 +42,40 @@ public function testCanCreateCategoryWithCategoryType() } public function testCannotCreateCategoryWithoutCategoryType() + { + $response = $this->actingAsForApi(User::factory()->superuser()->create()) + ->postJson(route('api.categories.store'), [ + 'name' => 'Test Category', + ]) + ->assertOk() + ->assertStatus(200) + ->assertStatusMessageIs('error') + ->assertJson([ + 'messages' => [ + 'category_type' => ['The category type field is required.'], + ], + ]); + $this->assertFalse(Category::where('name', 'Test Category')->exists()); + + } + + public function testCannotCreateCategoryWithInvalidCategoryType() { $this->actingAsForApi(User::factory()->superuser()->create()) ->postJson(route('api.categories.store'), [ 'name' => 'Test Category', 'eula_text' => 'Test EULA', + 'category_type' => 'invalid', ]) ->assertOk() - ->assertStatusMessageIs('error') ->assertStatus(200) - ->json(); - + ->assertStatusMessageIs('error') + ->assertJson([ + 'messages' => [ + 'category_type' => ['The selected category type is invalid.'], + ], + ]); + $this->assertFalse(Category::where('name', 'Test Category')->exists()); } From 2a796fdc09b322829c860fc46bcaff91234a1c96 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 03:23:48 +0100 Subject: [PATCH 26/42] Format error Signed-off-by: snipe --- app/Http/Controllers/Api/CategoriesController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/Api/CategoriesController.php b/app/Http/Controllers/Api/CategoriesController.php index a4eb192bc2ab..29b9c969d1a3 100644 --- a/app/Http/Controllers/Api/CategoriesController.php +++ b/app/Http/Controllers/Api/CategoriesController.php @@ -163,7 +163,7 @@ public function update(ImageUploadRequest $request, $id) : JsonResponse // Don't allow the user to change the category_type once it's been created if (($request->filled('category_type')) && ($category->category_type != $request->input('category_type'))) { return response()->json( - Helper::formatStandardApiResponse('error', null, trans('admin/categories/message.update.cannot_change_category_type')) + Helper::formatStandardApiResponse('error', null, ['category_type' => trans('admin/categories/message.update.cannot_change_category_type')], 422) ); } $category->fill($request->all()); From b3cc7b1578cdf7236930f460f0ec2f417d79126e Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 05:59:28 +0100 Subject: [PATCH 27/42] More tests Signed-off-by: snipe --- .../AssetModels/Api/IndexAssetModelsTest.php | 67 ++++++++++ .../AssetModels/Api/UpdateAssetModelsTest.php | 120 ++++++++++++++++++ 2 files changed, 187 insertions(+) create mode 100644 tests/Feature/AssetModels/Api/IndexAssetModelsTest.php create mode 100644 tests/Feature/AssetModels/Api/UpdateAssetModelsTest.php diff --git a/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php b/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php new file mode 100644 index 000000000000..8d7e39c48991 --- /dev/null +++ b/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php @@ -0,0 +1,67 @@ +getJson(route('api.models.index'))->assertRedirect(); + } + + public function testViewingAssetModelIndexRequiresPermission() + { + $this->actingAsForApi(User::factory()->create()) + ->getJson(route('api.models.index')) + ->assertForbidden(); + } + + public function testAssetModelIndexReturnsExpectedAssetModels() + { + AssetModel::factory()->count(3)->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->getJson( + route('api.models.index', [ + 'sort' => 'name', + 'order' => 'asc', + 'offset' => '0', + 'limit' => '20', + ])) + ->assertOk() + ->assertJsonStructure([ + 'total', + 'rows', + ]) + ->assertJson(fn(AssertableJson $json) => $json->has('rows', 3)->etc()); + } + + public function testAssetModelIndexSearchReturnsExpectedAssetModels() + { + AssetModel::factory()->count(3)->create(); + AssetModel::factory()->count(1)->create(['name' => 'Test Model']); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->getJson( + route('api.models.index', [ + 'search' => 'Test Model', + 'sort' => 'id', + 'order' => 'asc', + 'offset' => '0', + 'limit' => '20', + ])) + ->assertOk() + ->assertJsonStructure([ + 'total', + 'rows', + ]) + ->assertJson(fn(AssertableJson $json) => $json->has('rows', 1)->etc()); + } + +} diff --git a/tests/Feature/AssetModels/Api/UpdateAssetModelsTest.php b/tests/Feature/AssetModels/Api/UpdateAssetModelsTest.php new file mode 100644 index 000000000000..c5ec2126508f --- /dev/null +++ b/tests/Feature/AssetModels/Api/UpdateAssetModelsTest.php @@ -0,0 +1,120 @@ +create(); + $this->actingAsForApi(User::factory()->create()) + ->patchJson(route('api.models.update', $model)) + ->assertForbidden(); + } + + public function testCanUpdateAssetModelViaPatch() + { + $model = AssetModel::factory()->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->patchJson(route('api.models.update', $model), [ + 'name' => 'Test Model', + 'category_id' => Category::factory()->forAssets()->create()->id, + ]) + ->assertOk() + ->assertStatusMessageIs('success') + ->assertStatus(200) + ->json(); + + $model->refresh(); + $this->assertEquals('Test Model', $model->name, 'Name was not updated'); + + } + + public function testCannotUpdateAssetModelViaPatchWithAccessoryCategory() + { + $category = Category::factory()->forAccessories()->create(); + $model = AssetModel::factory()->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->patchJson(route('api.models.update', $model), [ + 'name' => 'Test Model', + 'category_id' => $category->id, + ]) + ->assertOk() + ->assertStatusMessageIs('error') + ->assertStatus(200) + ->json(); + + $category->refresh(); + $this->assertNotEquals('Test Model', $model->name, 'Name was not updated'); + $this->assertNotEquals('category_id', $category->id, 'Category ID was not updated'); + } + + public function testCannotUpdateAssetModelViaPatchWithLicenseCategory() + { + $category = Category::factory()->forLicenses()->create(); + $model = AssetModel::factory()->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->patchJson(route('api.models.update', $model), [ + 'name' => 'Test Model', + 'category_id' => $category->id, + ]) + ->assertOk() + ->assertStatusMessageIs('error') + ->assertStatus(200) + ->json(); + + $category->refresh(); + $this->assertNotEquals('Test Model', $model->name, 'Name was not updated'); + $this->assertNotEquals('category_id', $category->id, 'Category ID was not updated'); + } + + public function testCannotUpdateAssetModelViaPatchWithConsumableCategory() + { + $category = Category::factory()->forConsumables()->create(); + $model = AssetModel::factory()->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->patchJson(route('api.models.update', $model), [ + 'name' => 'Test Model', + 'category_id' => $category->id, + ]) + ->assertOk() + ->assertStatusMessageIs('error') + ->assertStatus(200) + ->json(); + + $category->refresh(); + $this->assertNotEquals('Test Model', $model->name, 'Name was not updated'); + $this->assertNotEquals('category_id', $category->id, 'Category ID was not updated'); + } + + public function testCannotUpdateAssetModelViaPatchWithComponentCategory() + { + $category = Category::factory()->forComponents()->create(); + $model = AssetModel::factory()->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->patchJson(route('api.models.update', $model), [ + 'name' => 'Test Model', + 'category_id' => $category->id, + ]) + ->assertOk() + ->assertStatusMessageIs('error') + ->assertStatus(200) + ->json(); + + $category->refresh(); + $this->assertNotEquals('Test Model', $model->name, 'Name was not updated'); + $this->assertNotEquals('category_id', $category->id, 'Category ID was not updated'); + } + +} From 0d6fd2643dcfe5fd56c141e800a0b7c43248ca83 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 05:59:39 +0100 Subject: [PATCH 28/42] More tweaks to the form request Signed-off-by: snipe --- app/Http/Requests/StoreAssetModelRequest.php | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/app/Http/Requests/StoreAssetModelRequest.php b/app/Http/Requests/StoreAssetModelRequest.php index 052f23f5a541..635d45cf890d 100644 --- a/app/Http/Requests/StoreAssetModelRequest.php +++ b/app/Http/Requests/StoreAssetModelRequest.php @@ -21,11 +21,11 @@ public function prepareForValidation(): void { if ($this->category_id) { - $this->category_type = Category::find($this->category_id)->category_type; - - $this->merge([ - 'category_type' => $this->category_type, - ]); + if ($category = Category::find($this->category_id)) { + $this->merge([ + 'category_type' => $category->category_type ?? null, + ]); + } } } @@ -48,4 +48,9 @@ public function messages(): array $messages = ['category_type.in' => trans('admin/models/message.invalid_category_type')]; return $messages; } + + public function response(array $errors) + { + return $this->redirector->back()->withInput()->withErrors($errors, $this->errorBag); + } } From 1d65781d8dcb832d2d9bf4bc62c3b709a85de06a Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 07:07:20 +0100 Subject: [PATCH 29/42] More type-hinting Signed-off-by: snipe --- app/Helpers/StorageHelper.php | 7 +- .../Controllers/Api/AccessoriesController.php | 1 + .../Controllers/Api/AssetFilesController.php | 37 +++-------- .../Api/AssetMaintenancesController.php | 19 ++---- .../Controllers/Api/AssetModelsController.php | 23 +++---- app/Http/Controllers/Api/AssetsController.php | 56 ++++++---------- .../Controllers/Api/CategoriesController.php | 4 +- .../Controllers/Api/CompaniesController.php | 18 ++---- .../Controllers/Api/ComponentsController.php | 43 +++++-------- .../Controllers/Api/ConsumablesController.php | 27 +++----- .../Api/CustomFieldsController.php | 22 +++---- .../Api/CustomFieldsetsController.php | 25 +++----- .../Controllers/Api/DepartmentsController.php | 21 ++---- .../Api/DepreciationsController.php | 16 ++--- app/Http/Controllers/Api/GroupsController.php | 17 ++--- app/Http/Controllers/Api/ImportController.php | 15 ++--- app/Http/Controllers/Api/LabelsController.php | 8 +-- .../Api/LicenseSeatsController.php | 23 +++---- .../Controllers/Api/LicensesController.php | 22 ++----- .../Controllers/Api/LocationsController.php | 18 ++---- .../Api/ManufacturersController.php | 21 +++--- .../Api/PredefinedKitsController.php | 64 +++++++------------ .../Controllers/Api/ProfileController.php | 21 +++--- .../Controllers/Api/ReportsController.php | 4 +- .../Controllers/Api/SettingsController.php | 33 ++++------ .../Api/StatuslabelsController.php | 31 ++++----- .../Controllers/Api/SuppliersController.php | 17 ++--- app/Http/Controllers/Api/UsersController.php | 42 +++++------- .../AssetModels/Api/IndexAssetModelsTest.php | 2 +- 29 files changed, 245 insertions(+), 412 deletions(-) diff --git a/app/Helpers/StorageHelper.php b/app/Helpers/StorageHelper.php index 94dfe62c76fc..2cdab1d66c40 100644 --- a/app/Helpers/StorageHelper.php +++ b/app/Helpers/StorageHelper.php @@ -3,10 +3,13 @@ namespace App\Helpers; use Illuminate\Support\Facades\Storage; - +use Illuminate\Http\Response; +use Illuminate\Http\RedirectResponse; +use Symfony\Component\HttpFoundation\BinaryFileResponse; +use Symfony\Component\HttpFoundation\StreamedResponse; class StorageHelper { - public static function downloader($filename, $disk = 'default') + public static function downloader($filename, $disk = 'default') : BinaryFileResponse | RedirectResponse | StreamedResponse { if ($disk == 'default') { $disk = config('filesystems.default'); diff --git a/app/Http/Controllers/Api/AccessoriesController.php b/app/Http/Controllers/Api/AccessoriesController.php index ad8995e4dd4a..383b95e6ea43 100644 --- a/app/Http/Controllers/Api/AccessoriesController.php +++ b/app/Http/Controllers/Api/AccessoriesController.php @@ -16,6 +16,7 @@ use Illuminate\Http\Request; use App\Http\Requests\ImageUploadRequest; + class AccessoriesController extends Controller { /** diff --git a/app/Http/Controllers/Api/AssetFilesController.php b/app/Http/Controllers/Api/AssetFilesController.php index 500c241f9f64..4369d287d5c2 100644 --- a/app/Http/Controllers/Api/AssetFilesController.php +++ b/app/Http/Controllers/Api/AssetFilesController.php @@ -4,28 +4,15 @@ use App\Helpers\StorageHelper; use Illuminate\Support\Facades\Storage; -use Illuminate\Database\Eloquent\Builder; -use Illuminate\Http\JsonResponse; -use Illuminate\Support\Facades\Crypt; -use Illuminate\Support\Facades\Gate; use App\Helpers\Helper; use App\Http\Controllers\Controller; use App\Models\Asset; -use App\Models\AssetModel; use App\Models\Actionlog; -use \Illuminate\Support\Facades\Auth; -use Carbon\Carbon; -use Illuminate\Support\Facades\DB; -use Illuminate\Http\Request; use App\Http\Requests\UploadFileRequest; +use Illuminate\Http\JsonResponse; use Illuminate\Support\Facades\Log; -use Input; -use Paginator; -use Slack; -use Str; -use TCPDF; -use Validator; -use Route; +use Symfony\Component\HttpFoundation\StreamedResponse; +use Symfony\Component\HttpFoundation\BinaryFileResponse; /** @@ -44,12 +31,10 @@ class AssetFilesController extends Controller * * @param \App\Http\Requests\UploadFileRequest $request * @param int $assetId - * @return \Illuminate\Http\JsonResponse - * @throws \Illuminate\Auth\Access\AuthorizationException * @since [v6.0] * @author [T. Scarsbrook] [] */ - public function store(UploadFileRequest $request, $assetId = null) + public function store(UploadFileRequest $request, $assetId = null) : JsonResponse { // Start by checking if the asset being acted upon exists if (! $asset = Asset::find($assetId)) { @@ -59,7 +44,7 @@ public function store(UploadFileRequest $request, $assetId = null) // Make sure we are allowed to update this asset $this->authorize('update', $asset); - if ($request->hasFile('file')) { + if ($request->hasFile('file')) { // If the file storage directory doesn't exist; create it if (! Storage::exists('private_uploads/assets')) { Storage::makeDirectory('private_uploads/assets', 775); @@ -84,12 +69,10 @@ public function store(UploadFileRequest $request, $assetId = null) * List the files for an asset. * * @param int $assetId - * @return \Illuminate\Http\JsonResponse - * @throws \Illuminate\Auth\Access\AuthorizationException * @since [v6.0] * @author [T. Scarsbrook] [] */ - public function list($assetId = null) + public function list($assetId = null) : JsonResponse { // Start by checking if the asset being acted upon exists if (! $asset = Asset::find($assetId)) { @@ -128,7 +111,7 @@ public function list($assetId = null) * @since [v6.0] * @author [T. Scarsbrook] [] */ - public function show($assetId = null, $fileId = null) + public function show($assetId = null, $fileId = null) : JsonResponse | StreamedResponse | Storage | StorageHelper | BinaryFileResponse { // Start by checking if the asset being acted upon exists if (! $asset = Asset::find($assetId)) { @@ -146,7 +129,7 @@ public function show($assetId = null, $fileId = null) // Form the full filename with path $file = 'private_uploads/assets/'.$log->filename; - \Log::debug('Checking for '.$file); + Log::debug('Checking for '.$file); if ($log->action_type == 'audit') { $file = 'private_uploads/audits/'.$log->filename; @@ -178,12 +161,10 @@ public function show($assetId = null, $fileId = null) * * @param int $assetId * @param int $fileId - * @return \Illuminate\Http\JsonResponse - * @throws \Illuminate\Auth\Access\AuthorizationException * @since [v6.0] * @author [T. Scarsbrook] [] */ - public function destroy($assetId = null, $fileId = null) + public function destroy($assetId = null, $fileId = null) : JsonResponse { // Start by checking if the asset being acted upon exists if (! $asset = Asset::find($assetId)) { diff --git a/app/Http/Controllers/Api/AssetMaintenancesController.php b/app/Http/Controllers/Api/AssetMaintenancesController.php index 1ecb3be2579c..ac247a887387 100644 --- a/app/Http/Controllers/Api/AssetMaintenancesController.php +++ b/app/Http/Controllers/Api/AssetMaintenancesController.php @@ -9,9 +9,8 @@ use App\Models\AssetMaintenance; use App\Models\Company; use Illuminate\Support\Facades\Auth; -use Carbon\Carbon; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Input; +use Illuminate\Http\JsonResponse; /** * This controller handles all actions related to Asset Maintenance for @@ -22,7 +21,6 @@ class AssetMaintenancesController extends Controller { - /** * Generates the JSON response for asset maintenances listing view. * @@ -30,9 +28,8 @@ class AssetMaintenancesController extends Controller * @author Vincent Sposato * @version v1.0 * @since [v1.8] - * @return string JSON */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('view', Asset::class); @@ -120,9 +117,8 @@ public function index(Request $request) * @author Vincent Sposato * @version v1.0 * @since [v1.8] - * @return string JSON */ - public function store(Request $request) + public function store(Request $request) : JsonResponse { $this->authorize('update', Asset::class); // create a new model instance @@ -148,9 +144,8 @@ public function store(Request $request) * @param int $request * @version v1.0 * @since [v4.0] - * @return string JSON */ - public function update(Request $request, $id) + public function update(Request $request, $id) : JsonResponse { $this->authorize('update', Asset::class); @@ -186,9 +181,8 @@ public function update(Request $request, $id) * @param int $assetMaintenanceId * @version v1.0 * @since [v4.0] - * @return string JSON */ - public function destroy($assetMaintenanceId) + public function destroy($assetMaintenanceId) : JsonResponse { $this->authorize('update', Asset::class); // Check if the asset maintenance exists @@ -212,9 +206,8 @@ public function destroy($assetMaintenanceId) * @param int $assetMaintenanceId * @version v1.0 * @since [v4.0] - * @return string JSON */ - public function show($assetMaintenanceId) + public function show($assetMaintenanceId) : JsonResponse { $this->authorize('view', Asset::class); $assetMaintenance = AssetMaintenance::findOrFail($assetMaintenanceId); diff --git a/app/Http/Controllers/Api/AssetModelsController.php b/app/Http/Controllers/Api/AssetModelsController.php index e777208d360d..835f4d22e00e 100644 --- a/app/Http/Controllers/Api/AssetModelsController.php +++ b/app/Http/Controllers/Api/AssetModelsController.php @@ -11,9 +11,9 @@ use App\Models\Asset; use App\Models\AssetModel; use Illuminate\Http\Request; -use App\Http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; use Illuminate\Support\Facades\Log; +use Illuminate\Http\JsonResponse; /** * This class controls all actions related to asset models for @@ -29,9 +29,8 @@ class AssetModelsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('view', AssetModel::class); $allowed_columns = @@ -117,10 +116,9 @@ public function index(Request $request) * * @author [A. Gianotto] [] * @since [v4.0] - * @param \App\Http\Requests\ImageUploadRequest $request - * @return \Illuminate\Http\Response + * @param \App\Http\Requests\StoreAssetModelRequest $request */ - public function store(StoreAssetModelRequest $request) + public function store(StoreAssetModelRequest $request) : JsonResponse { $this->authorize('create', AssetModel::class); $assetmodel = new AssetModel; @@ -141,9 +139,8 @@ public function store(StoreAssetModelRequest $request) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : array { $this->authorize('view', AssetModel::class); $assetmodel = AssetModel::withCount('assets as assets_count')->findOrFail($id); @@ -157,9 +154,8 @@ public function show($id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function assets($id) + public function assets($id) : array { $this->authorize('view', AssetModel::class); $assets = Asset::where('model_id', '=', $id)->get(); @@ -177,7 +173,7 @@ public function assets($id) * @param int $id * @return \Illuminate\Http\Response */ - public function update(StoreAssetModelRequest $request, $id) + public function update(StoreAssetModelRequest $request, $id) : JsonResponse { $this->authorize('update', AssetModel::class); $assetmodel = AssetModel::findOrFail($id); @@ -210,9 +206,8 @@ public function update(StoreAssetModelRequest $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', AssetModel::class); $assetmodel = AssetModel::findOrFail($id); @@ -242,7 +237,7 @@ public function destroy($id) * @since [v4.0.16] * @see \App\Http\Transformers\SelectlistTransformer */ - public function selectlist(Request $request) + public function selectlist(Request $request) : array { $this->authorize('view.selectlists'); diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 41f2e9bf2b2d..f0ac97b7f94a 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -25,17 +25,12 @@ use App\Models\Location; use App\Models\Setting; use App\Models\User; -use \Illuminate\Support\Facades\Auth; +use Illuminate\Support\Facades\Auth; use Carbon\Carbon; use Illuminate\Support\Facades\DB; use Illuminate\Http\Request; use App\Http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Log; -use Paginator; -use Slack; -use Str; -use TCPDF; -use Validator; use Illuminate\Support\Facades\Route; @@ -56,9 +51,8 @@ class AssetsController extends Controller * @author [A. Gianotto] [] * @param int $assetId * @since [v4.0] - * @return \Illuminate\Http\JsonResponse */ - public function index(Request $request, $action = null, $upcoming_status = null) + public function index(Request $request, $action = null, $upcoming_status = null) : JsonResponse | array { $filter_non_deprecable_assets = false; @@ -415,9 +409,8 @@ public function index(Request $request, $action = null, $upcoming_status = null) * @param string $tag * @since [v4.2.1] * @author [A. Gianotto] [] - * @return \Illuminate\Http\JsonResponse */ - public function showByTag(Request $request, $tag) + public function showByTag(Request $request, $tag) : JsonResponse | array { $this->authorize('index', Asset::class); $assets = Asset::where('asset_tag', $tag)->with('assetstatus')->with('assignedTo'); @@ -455,7 +448,7 @@ public function showByTag(Request $request, $tag) * @since [v4.2.1] * @return \Illuminate\Http\JsonResponse */ - public function showBySerial(Request $request, $serial) + public function showBySerial(Request $request, $serial) : JsonResponse | array { $this->authorize('index', Asset::class); $assets = Asset::where('serial', $serial)->with('assetstatus')->with('assignedTo'); @@ -482,19 +475,20 @@ public function showBySerial(Request $request, $serial) * @since [v4.0] * @return \Illuminate\Http\JsonResponse */ - public function show(Request $request, $id) + public function show(Request $request, $id) : JsonResponse | array { - if ($asset = Asset::with('assetstatus')->with('assignedTo')->withTrashed() - ->withCount('checkins as checkins_count', 'checkouts as checkouts_count', 'userRequests as user_requests_count')->findOrFail($id)) { + if ($asset = Asset::with('assetstatus') + ->with('assignedTo')->withTrashed() + ->withCount('checkins as checkins_count', 'checkouts as checkouts_count', 'userRequests as user_requests_count')->find($id)) { $this->authorize('view', $asset); return (new AssetsTransformer)->transformAsset($asset, $request->input('components') ); } - + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200); } - public function licenses(Request $request, $id) + public function licenses(Request $request, $id) : array { $this->authorize('view', Asset::class); $this->authorize('view', License::class); @@ -511,9 +505,8 @@ public function licenses(Request $request, $id) * @author [A. Gianotto] [] * @since [v4.0.16] * @see \App\Http\Transformers\SelectlistTransformer - * @return \Illuminate\Http\JsonResponse */ - public function selectlist(Request $request) + public function selectlist(Request $request) : array { $assets = Asset::select([ @@ -660,9 +653,8 @@ public function store(StoreAssetRequest $request): JsonResponse * @author [A. Gianotto] [] * @param \App\Http\Requests\ImageUploadRequest $request * @since [v4.0] - * @return \Illuminate\Http\JsonResponse */ - public function update(ImageUploadRequest $request, $id) + public function update(ImageUploadRequest $request, $id) : JsonResponse { $this->authorize('update', Asset::class); @@ -756,9 +748,8 @@ public function update(ImageUploadRequest $request, $id) * @author [A. Gianotto] [] * @param int $assetId * @since [v4.0] - * @return \Illuminate\Http\JsonResponse */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', Asset::class); @@ -785,9 +776,8 @@ public function destroy($id) * @author [A. Gianotto] [] * @param int $assetId * @since [v5.1.18] - * @return \Illuminate\Http\JsonResponse */ - public function restore(Request $request, $assetId = null) + public function restore(Request $request, $assetId = null) : JsonResponse { if ($asset = Asset::withTrashed()->find($assetId)) { @@ -815,9 +805,8 @@ public function restore(Request $request, $assetId = null) * @author [N. Butler] * @param string $tag * @since [v6.0.5] - * @return \Illuminate\Http\JsonResponse */ - public function checkoutByTag(AssetCheckoutRequest $request, $tag) + public function checkoutByTag(AssetCheckoutRequest $request, $tag) : JsonResponse { if ($asset = Asset::where('asset_tag', $tag)->first()) { return $this->checkout($request, $asset->id); @@ -831,9 +820,8 @@ public function checkoutByTag(AssetCheckoutRequest $request, $tag) * @author [A. Gianotto] [] * @param int $assetId * @since [v4.0] - * @return \Illuminate\Http\JsonResponse */ - public function checkout(AssetCheckoutRequest $request, $asset_id) + public function checkout(AssetCheckoutRequest $request, $asset_id) : JsonResponse { $this->authorize('checkout', Asset::class); $asset = Asset::findOrFail($asset_id); @@ -909,9 +897,8 @@ public function checkout(AssetCheckoutRequest $request, $asset_id) * @author [A. Gianotto] [] * @param int $assetId * @since [v4.0] - * @return \Illuminate\Http\JsonResponse */ - public function checkin(Request $request, $asset_id) + public function checkin(Request $request, $asset_id) : JsonResponse { $asset = Asset::with('model')->findOrFail($asset_id); $this->authorize('checkin', $asset); @@ -993,9 +980,8 @@ function (Builder $query) use ($asset) { * * @author [A. Janes] [] * @since [v6.0] - * @return \Illuminate\Http\JsonResponse */ - public function checkinByTag(Request $request, $tag = null) + public function checkinByTag(Request $request, $tag = null) : JsonResponse { $this->authorize('checkin', Asset::class); if(null == $tag && null !== ($request->input('asset_tag'))) { @@ -1019,9 +1005,8 @@ public function checkinByTag(Request $request, $tag = null) * @author [A. Gianotto] [] * @param int $id * @since [v4.0] - * @return \Illuminate\Http\JsonResponse */ - public function audit(Request $request) + public function audit(Request $request) : JsonResponse { $this->authorize('audit', Asset::class); @@ -1112,9 +1097,8 @@ public function audit(Request $request) * * @author [A. Gianotto] [] * @since [v4.0] - * @return \Illuminate\Http\JsonResponse */ - public function requestable(Request $request) + public function requestable(Request $request) : JsonResponse | array { $this->authorize('viewRequestable', Asset::class); diff --git a/app/Http/Controllers/Api/CategoriesController.php b/app/Http/Controllers/Api/CategoriesController.php index 29b9c969d1a3..6e9866f90bec 100644 --- a/app/Http/Controllers/Api/CategoriesController.php +++ b/app/Http/Controllers/Api/CategoriesController.php @@ -184,7 +184,7 @@ public function update(ImageUploadRequest $request, $id) : JsonResponse * @param int $id * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', Category::class); $category = Category::withCount('assets as assets_count', 'accessories as accessories_count', 'consumables as consumables_count', 'components as components_count', 'licenses as licenses_count')->findOrFail($id); @@ -207,7 +207,7 @@ public function destroy($id) * @since [v4.0.16] * @see \App\Http\Transformers\SelectlistTransformer */ - public function selectlist(Request $request, $category_type = 'asset') + public function selectlist(Request $request, $category_type = 'asset') : array { $this->authorize('view.selectlists'); $categories = Category::select([ diff --git a/app/Http/Controllers/Api/CompaniesController.php b/app/Http/Controllers/Api/CompaniesController.php index 9c667973a3b7..0d78df9accc5 100644 --- a/app/Http/Controllers/Api/CompaniesController.php +++ b/app/Http/Controllers/Api/CompaniesController.php @@ -10,6 +10,7 @@ use Illuminate\Http\Request; use App\Http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; +use Illuminate\Http\JsonResponse; class CompaniesController extends Controller { @@ -18,9 +19,8 @@ class CompaniesController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('view', Company::class); @@ -79,9 +79,8 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request - * @return \Illuminate\Http\Response */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : JsonResponse { $this->authorize('create', Company::class); $company = new Company; @@ -102,9 +101,8 @@ public function store(ImageUploadRequest $request) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : array { $this->authorize('view', Company::class); $company = Company::findOrFail($id); @@ -120,9 +118,8 @@ public function show($id) * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id - * @return \Illuminate\Http\Response */ - public function update(ImageUploadRequest $request, $id) + public function update(ImageUploadRequest $request, $id) : JsonResponse { $this->authorize('update', Company::class); $company = Company::findOrFail($id); @@ -144,9 +141,8 @@ public function update(ImageUploadRequest $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', Company::class); $company = Company::findOrFail($id); @@ -169,7 +165,7 @@ public function destroy($id) * @since [v4.0.16] * @see \App\Http\Transformers\SelectlistTransformer */ - public function selectlist(Request $request) + public function selectlist(Request $request) : array { $this->authorize('view.selectlists'); $companies = Company::select([ diff --git a/app/Http/Controllers/Api/ComponentsController.php b/app/Http/Controllers/Api/ComponentsController.php index fc2ad9cfcac5..69bd828487d8 100644 --- a/app/Http/Controllers/Api/ComponentsController.php +++ b/app/Http/Controllers/Api/ComponentsController.php @@ -5,16 +5,17 @@ use App\Helpers\Helper; use App\Http\Controllers\Controller; use App\Http\Transformers\ComponentsTransformer; -use App\Models\Company; use App\Models\Component; use Illuminate\Http\Request; use App\Http\Requests\ImageUploadRequest; use App\Events\CheckoutableCheckedIn; -use App\Events\ComponentCheckedIn; use App\Models\Asset; use Illuminate\Support\Facades\Validator; use Illuminate\Database\Query\Builder; use Illuminate\Support\Facades\Log; +use Illuminate\Http\JsonResponse; +use Illuminate\Support\Facades\DB; +use Carbon\Carbon; class ComponentsController extends Controller { @@ -24,9 +25,8 @@ class ComponentsController extends Controller * @author [A. Gianotto] [] * @since [v4.0] * - * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('view', Component::class); @@ -116,9 +116,8 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request - * @return \Illuminate\Http\Response */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : JsonResponse { $this->authorize('create', Component::class); $component = new Component; @@ -137,9 +136,8 @@ public function store(ImageUploadRequest $request) * * @author [A. Gianotto] [] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : array { $this->authorize('view', Component::class); $component = Component::findOrFail($id); @@ -156,9 +154,8 @@ public function show($id) * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id - * @return \Illuminate\Http\Response */ - public function update(ImageUploadRequest $request, $id) + public function update(ImageUploadRequest $request, $id) : JsonResponse { $this->authorize('update', Component::class); $component = Component::findOrFail($id); @@ -179,9 +176,8 @@ public function update(ImageUploadRequest $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', Component::class); $component = Component::findOrFail($id); @@ -198,9 +194,8 @@ public function destroy($id) * @since [v4.0] * @param Request $request * @param int $id - * @return \Illuminate\Http\Response */ - public function getAssets(Request $request, $id) + public function getAssets(Request $request, $id) : array { $this->authorize('view', \App\Models\Asset::class); @@ -241,10 +236,8 @@ public function getAssets(Request $request, $id) * @since [v5.1.8] * @param Request $request * @param int $componentId - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function checkout(Request $request, $componentId) + public function checkout(Request $request, $componentId) : JsonResponse { // Check if the component exists if (!$component = Component::find($componentId)) { @@ -275,7 +268,7 @@ public function checkout(Request $request, $componentId) $component->assets()->attach($component->id, [ 'component_id' => $component->id, - 'created_at' => \Carbon::now(), + 'created_at' => Carbon::now(), 'assigned_qty' => $request->get('assigned_qty', 1), 'user_id' => auth()->id(), 'asset_id' => $request->get('assigned_to'), @@ -297,12 +290,10 @@ public function checkout(Request $request, $componentId) * @since [v5.1.8] * @param Request $request * @param $component_asset_id - * @return \Illuminate\Http\RedirectResponse - * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function checkin(Request $request, $component_asset_id) + public function checkin(Request $request, $component_asset_id) : JsonResponse { - if ($component_assets = \DB::table('components_assets')->find($component_asset_id)) { + if ($component_assets = DB::table('components_assets')->find($component_asset_id)) { if (is_null($component = Component::find($component_assets->component_id))) { @@ -315,7 +306,7 @@ public function checkin(Request $request, $component_asset_id) if ($max_to_checkin > 1) { - $validator = \Validator::make($request->all(), [ + $validator = Validator::make($request->all(), [ "checkin_qty" => "required|numeric|between:1,$max_to_checkin" ]); @@ -334,19 +325,19 @@ public function checkin(Request $request, $component_asset_id) Log::debug($component_asset_id.' - '.$qty_remaining_in_checkout.' remaining in record '.$component_assets->id); - \DB::table('components_assets')->where('id', + DB::table('components_assets')->where('id', $component_asset_id)->update(['assigned_qty' => $qty_remaining_in_checkout]); // If the checked-in qty is exactly the same as the assigned_qty, // we can simply delete the associated components_assets record if ($qty_remaining_in_checkout == 0) { - \DB::table('components_assets')->where('id', '=', $component_asset_id)->delete(); + DB::table('components_assets')->where('id', '=', $component_asset_id)->delete(); } $asset = Asset::find($component_assets->asset_id); - event(new CheckoutableCheckedIn($component, $asset, auth()->user(), $request->input('note'), \Carbon::now())); + event(new CheckoutableCheckedIn($component, $asset, auth()->user(), $request->input('note'), Carbon::now())); return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/components/message.checkin.success'))); diff --git a/app/Http/Controllers/Api/ConsumablesController.php b/app/Http/Controllers/Api/ConsumablesController.php index 65033e000241..7c65e49bcd64 100644 --- a/app/Http/Controllers/Api/ConsumablesController.php +++ b/app/Http/Controllers/Api/ConsumablesController.php @@ -12,8 +12,8 @@ use App\Models\User; use Illuminate\Http\Request; use App\Http\Requests\ImageUploadRequest; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Log; +use Illuminate\Http\JsonResponse; class ConsumablesController extends Controller { @@ -22,10 +22,8 @@ class ConsumablesController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * - * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : array { $this->authorize('index', Consumable::class); @@ -132,9 +130,8 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request - * @return \Illuminate\Http\Response */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : JsonResponse { $this->authorize('create', Consumable::class); $consumable = new Consumable; @@ -153,9 +150,8 @@ public function store(ImageUploadRequest $request) * * @author [A. Gianotto] [] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : array { $this->authorize('view', Consumable::class); $consumable = Consumable::with('users')->findOrFail($id); @@ -170,9 +166,8 @@ public function show($id) * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id - * @return \Illuminate\Http\Response */ - public function update(ImageUploadRequest $request, $id) + public function update(ImageUploadRequest $request, $id) : JsonResponse { $this->authorize('update', Consumable::class); $consumable = Consumable::findOrFail($id); @@ -192,9 +187,8 @@ public function update(ImageUploadRequest $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', Consumable::class); $consumable = Consumable::findOrFail($id); @@ -211,9 +205,8 @@ public function destroy($id) * @see \App\Http\Controllers\Consumables\ConsumablesController::getView() method that returns the form. * @since [v1.0] * @param int $consumableId - * @return array */ - public function getDataView($consumableId) + public function getDataView($consumableId) : array { $consumable = Consumable::with(['consumableAssignments'=> function ($query) { $query->orderBy($query->getModel()->getTable().'.created_at', 'DESC'); @@ -252,9 +245,8 @@ public function getDataView($consumableId) * @author [A. Gutierrez] [] * @param int $id * @since [v4.9.5] - * @return \Illuminate\Http\JsonResponse */ - public function checkout(Request $request, $id) + public function checkout(Request $request, $id) : JsonResponse { // Check if the consumable exists if (!$consumable = Consumable::with('users')->find($id)) { @@ -278,7 +270,6 @@ public function checkout(Request $request, $id) if (!$user = User::find($request->input('assigned_to'))) { // Return error message return response()->json(Helper::formatStandardApiResponse('error', null, 'No user found')); - Log::debug('No valid user'); } // Update the consumable data @@ -304,7 +295,7 @@ public function checkout(Request $request, $id) * * @see \App\Http\Transformers\SelectlistTransformer */ - public function selectlist(Request $request) + public function selectlist(Request $request) : array { $consumables = Consumable::select([ 'consumables.id', diff --git a/app/Http/Controllers/Api/CustomFieldsController.php b/app/Http/Controllers/Api/CustomFieldsController.php index ca00d00077cc..207a92e274f5 100644 --- a/app/Http/Controllers/Api/CustomFieldsController.php +++ b/app/Http/Controllers/Api/CustomFieldsController.php @@ -8,7 +8,8 @@ use App\Models\CustomField; use App\Models\CustomFieldset; use Illuminate\Http\Request; -use Validator; +use Illuminate\Support\Facades\Validator; +use Illuminate\Http\JsonResponse; class CustomFieldsController extends Controller { @@ -20,7 +21,7 @@ class CustomFieldsController extends Controller * @since [v3.0] * @return array */ - public function index() + public function index() : array { $this->authorize('index', CustomField::class); $fields = CustomField::get(); @@ -33,9 +34,8 @@ public function index() * @author [V. Cordes] [] * @param int $id * @since [v4.1.10] - * @return \Illuminate\Contracts\View\View */ - public function show($id) + public function show($id) : JsonResponse | array { $this->authorize('view', CustomField::class); if ($field = CustomField::find($id)) { @@ -52,9 +52,8 @@ public function show($id) * @since [v4.1.10] * @param \Illuminate\Http\Request $request * @param int $id - * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(Request $request, $id) : JsonResponse { $this->authorize('update', CustomField::class); $field = CustomField::findOrFail($id); @@ -86,9 +85,8 @@ public function update(Request $request, $id) * @author [V. Cordes] [] * @since [v4.1.10] * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(Request $request) : JsonResponse { $this->authorize('create', CustomField::class); $field = new CustomField; @@ -136,7 +134,7 @@ public function postReorder(Request $request, $id) return $fieldset->fields()->sync($fields); } - public function associate(Request $request, $field_id) + public function associate(Request $request, $field_id) : JsonResponse { $this->authorize('update', CustomFieldset::class); @@ -155,10 +153,9 @@ public function associate(Request $request, $field_id) return response()->json(Helper::formatStandardApiResponse('success', $fieldset, trans('admin/custom_fields/message.fieldset.update.success'))); } - public function disassociate(Request $request, $field_id) + public function disassociate(Request $request, $field_id) : JsonResponse { $this->authorize('update', CustomFieldset::class); - $field = CustomField::findOrFail($field_id); $fieldset_id = $request->input('fieldset_id'); @@ -179,9 +176,8 @@ public function disassociate(Request $request, $field_id) * * @author [Brady Wetherington] [] * @since [v1.8] - * @return \Illuminate\Http\RedirectResponse */ - public function destroy($field_id) + public function destroy($field_id) : JsonResponse { $field = CustomField::findOrFail($field_id); diff --git a/app/Http/Controllers/Api/CustomFieldsetsController.php b/app/Http/Controllers/Api/CustomFieldsetsController.php index 0034ec038624..5dbd507917a5 100644 --- a/app/Http/Controllers/Api/CustomFieldsetsController.php +++ b/app/Http/Controllers/Api/CustomFieldsetsController.php @@ -9,8 +9,7 @@ use App\Models\CustomFieldset; use App\Models\CustomField; use Illuminate\Http\Request; -use Redirect; -use View; +use Illuminate\Http\JsonResponse; /** * This controller handles all actions related to Custom Asset Fieldsets for @@ -30,9 +29,8 @@ class CustomFieldsetsController extends Controller * @author [Josh Gibson] * @param int $id * @since [v1.8] - * @return \Illuminate\Contracts\View\View */ - public function index() + public function index() : array { $this->authorize('index', CustomField::class); $fieldsets = CustomFieldset::withCount('fields as fields_count', 'models as models_count')->get(); @@ -46,9 +44,8 @@ public function index() * @author [Josh Gibson] * @param int $id * @since [v1.8] - * @return \Illuminate\Contracts\View\View */ - public function show($id) + public function show($id) : JsonResponse | array { $this->authorize('view', CustomField::class); if ($fieldset = CustomFieldset::find($id)) { @@ -65,9 +62,8 @@ public function show($id) * @since [v4.0] * @param \Illuminate\Http\Request $request * @param int $id - * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(Request $request, $id) : JsonResponse { $this->authorize('update', CustomField::class); $fieldset = CustomFieldset::findOrFail($id); @@ -86,9 +82,8 @@ public function update(Request $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(Request $request) : JsonResponse { $this->authorize('create', CustomField::class); $fieldset = new CustomFieldset; @@ -118,9 +113,8 @@ public function store(Request $request) * * @author [A. Gianotto] [] * @since [v4.0] - * @return \Illuminate\Http\RedirectResponse */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', CustomField::class); $fieldset = CustomFieldset::findOrFail($id); @@ -147,7 +141,7 @@ public function destroy($id) * @param $fieldsetId * @return string JSON */ - public function fields($id) + public function fields($id) : array { $this->authorize('view', CustomField::class); $set = CustomFieldset::findOrFail($id); @@ -164,14 +158,11 @@ public function fields($id) * @param $fieldsetId * @return string JSON */ - public function fieldsWithDefaultValues($fieldsetId, $modelId) + public function fieldsWithDefaultValues($fieldsetId, $modelId) : array { $this->authorize('view', CustomField::class); - $set = CustomFieldset::findOrFail($fieldsetId); - $fields = $set->fields; - return (new CustomFieldsTransformer)->transformCustomFieldsWithDefaultValues($fields, $modelId, $fields->count()); } } diff --git a/app/Http/Controllers/Api/DepartmentsController.php b/app/Http/Controllers/Api/DepartmentsController.php index 662051439aba..eabc79ec2b9c 100644 --- a/app/Http/Controllers/Api/DepartmentsController.php +++ b/app/Http/Controllers/Api/DepartmentsController.php @@ -6,12 +6,11 @@ use App\Http\Controllers\Controller; use App\Http\Transformers\DepartmentsTransformer; use App\Http\Transformers\SelectlistTransformer; -use App\Models\Company; use App\Models\Department; -use Illuminate\Support\Facades\Auth; use Illuminate\Http\Request; use App\Http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; +use Illuminate\Http\JsonResponse; class DepartmentsController extends Controller { @@ -20,9 +19,8 @@ class DepartmentsController extends Controller * * @author [Godfrey Martinez] [] * @since [v4.0] - * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('view', Department::class); $allowed_columns = ['id', 'name', 'image', 'users_count']; @@ -91,9 +89,8 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request - * @return \Illuminate\Http\Response */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : JsonResponse { $this->authorize('create', Department::class); $department = new Department; @@ -116,13 +113,11 @@ public function store(ImageUploadRequest $request) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : array { $this->authorize('view', Department::class); $department = Department::findOrFail($id); - return (new DepartmentsTransformer)->transformDepartment($department); } @@ -133,9 +128,8 @@ public function show($id) * @since [v5.0] * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id - * @return \Illuminate\Http\Response */ - public function update(ImageUploadRequest $request, $id) + public function update(ImageUploadRequest $request, $id) : JsonResponse { $this->authorize('update', Department::class); $department = Department::findOrFail($id); @@ -156,9 +150,8 @@ public function update(ImageUploadRequest $request, $id) * @author [A. Gianotto] [] * @param int $locationId * @since [v4.0] - * @return \Illuminate\Http\RedirectResponse */ - public function destroy($id) + public function destroy($id) : JsonResponse { $department = Department::findOrFail($id); @@ -180,7 +173,7 @@ public function destroy($id) * @since [v4.0.16] * @see \App\Http\Transformers\SelectlistTransformer */ - public function selectlist(Request $request) + public function selectlist(Request $request) : array { $this->authorize('view.selectlists'); diff --git a/app/Http/Controllers/Api/DepreciationsController.php b/app/Http/Controllers/Api/DepreciationsController.php index 502a0741b7a4..93088bb04adb 100644 --- a/app/Http/Controllers/Api/DepreciationsController.php +++ b/app/Http/Controllers/Api/DepreciationsController.php @@ -7,6 +7,7 @@ use App\Http\Transformers\DepreciationsTransformer; use App\Models\Depreciation; use Illuminate\Http\Request; +use Illuminate\Http\JsonResponse; class DepreciationsController extends Controller { @@ -15,9 +16,8 @@ class DepreciationsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('view', Depreciation::class); $allowed_columns = ['id','name','months','depreciation_min','created_at']; @@ -48,9 +48,8 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(Request $request) : JsonResponse { $this->authorize('create', Depreciation::class); $depreciation = new Depreciation; @@ -69,9 +68,8 @@ public function store(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : JsonResponse | array { $this->authorize('view', Depreciation::class); $depreciation = Depreciation::findOrFail($id); @@ -86,9 +84,8 @@ public function show($id) * @since [v4.0] * @param \Illuminate\Http\Request $request * @param int $id - * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(Request $request, $id) : JsonResponse { $this->authorize('update', Depreciation::class); $depreciation = Depreciation::findOrFail($id); @@ -107,9 +104,8 @@ public function update(Request $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', Depreciation::class); $depreciation = Depreciation::withCount('models as models_count')->findOrFail($id); diff --git a/app/Http/Controllers/Api/GroupsController.php b/app/Http/Controllers/Api/GroupsController.php index 9b5e822b3f2a..878650c7189a 100644 --- a/app/Http/Controllers/Api/GroupsController.php +++ b/app/Http/Controllers/Api/GroupsController.php @@ -7,7 +7,7 @@ use App\Http\Transformers\GroupsTransformer; use App\Models\Group; use Illuminate\Http\Request; -use Illuminate\Support\Facades\Auth; +use Illuminate\Http\JsonResponse; class GroupsController extends Controller @@ -17,9 +17,8 @@ class GroupsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('superadmin'); @@ -56,9 +55,8 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(Request $request) : JsonResponse { $this->authorize('superadmin'); $group = new Group; @@ -83,9 +81,8 @@ public function store(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : array { $this->authorize('superadmin'); $group = Group::findOrFail($id); @@ -99,9 +96,8 @@ public function show($id) * @since [v4.0] * @param \Illuminate\Http\Request $request * @param int $id - * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(Request $request, $id) : JsonResponse { $this->authorize('superadmin'); $group = Group::findOrFail($id); @@ -122,9 +118,8 @@ public function update(Request $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('superadmin'); $group = Group::findOrFail($id); diff --git a/app/Http/Controllers/Api/ImportController.php b/app/Http/Controllers/Api/ImportController.php index d5323bfdd4c5..13f43012174a 100644 --- a/app/Http/Controllers/Api/ImportController.php +++ b/app/Http/Controllers/Api/ImportController.php @@ -17,15 +17,15 @@ use League\Csv\Reader; use Symfony\Component\HttpFoundation\File\Exception\FileException; use Illuminate\Support\Facades\Log; +use Illuminate\Http\JsonResponse; class ImportController extends Controller { /** * Display a listing of the resource. * - * @return \Illuminate\Http\Response */ - public function index() + public function index() : JsonResponse | array { $this->authorize('import'); $imports = Import::latest()->get(); @@ -37,9 +37,8 @@ public function index() * Process and store a CSV upload file. * * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\JsonResponse */ - public function store() + public function store() : JsonResponse { $this->authorize('import'); if (! config('app.lock_passwords')) { @@ -152,9 +151,8 @@ public function store() * Processes the specified Import. * * @param int $import_id - * @return \Illuminate\Http\Response */ - public function process(ItemImportRequest $request, $import_id) + public function process(ItemImportRequest $request, $import_id) : JsonResponse { $this->authorize('import'); @@ -212,9 +210,8 @@ public function process(ItemImportRequest $request, $import_id) * Remove the specified resource from storage. * * @param int $import_id - * @return \Illuminate\Http\Response */ - public function destroy($import_id) + public function destroy($import_id) : JsonResponse { $this->authorize('create', Asset::class); @@ -231,6 +228,8 @@ public function destroy($import_id) return response()->json(Helper::formatStandardApiResponse('warning', null, trans('admin/hardware/message.import.file_not_deleted_warning'))); } + } + return response()->json(Helper::formatStandardApiResponse('warning', null, trans('admin/hardware/message.import.file_not_deleted_warning'))); } } diff --git a/app/Http/Controllers/Api/LabelsController.php b/app/Http/Controllers/Api/LabelsController.php index 9366161c8ec8..0b542354224d 100644 --- a/app/Http/Controllers/Api/LabelsController.php +++ b/app/Http/Controllers/Api/LabelsController.php @@ -8,7 +8,7 @@ use App\Models\Labels\Label; use Illuminate\Http\Request; use Illuminate\Support\ItemNotFoundException; -use Illuminate\Support\Facades\Auth; +use Illuminate\Http\JsonResponse; class LabelsController extends Controller { @@ -16,9 +16,8 @@ class LabelsController extends Controller * Returns JSON listing of all labels. * * @author Grant Le Roux - * @return \Illuminate\Http\JsonResponse */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('view', Label::class); @@ -50,9 +49,8 @@ public function index(Request $request) * * @author Grant Le Roux * @param string $labelName - * @return \Illuminate\Http\JsonResponse */ - public function show(string $labelName) + public function show(string $labelName) : JsonResponse | array { $labelName = str_replace('/', '\\', $labelName); try { diff --git a/app/Http/Controllers/Api/LicenseSeatsController.php b/app/Http/Controllers/Api/LicenseSeatsController.php index f15792cdcd1d..a9630aa296bb 100644 --- a/app/Http/Controllers/Api/LicenseSeatsController.php +++ b/app/Http/Controllers/Api/LicenseSeatsController.php @@ -9,7 +9,7 @@ use App\Models\License; use App\Models\LicenseSeat; use App\Models\User; -use Illuminate\Support\Facades\Auth; +use Illuminate\Http\JsonResponse; use Illuminate\Http\Request; class LicenseSeatsController extends Controller @@ -19,11 +19,10 @@ class LicenseSeatsController extends Controller * * @param \Illuminate\Http\Request $request * @param int $licenseId - * @return \Illuminate\Http\Response */ - public function index(Request $request, $licenseId) + public function index(Request $request, $licenseId) : JsonResponse | array { - // + if ($license = License::find($licenseId)) { $this->authorize('view', $license); @@ -64,11 +63,10 @@ public function index(Request $request, $licenseId) * * @param int $licenseId * @param int $seatId - * @return \Illuminate\Http\Response */ - public function show($licenseId, $seatId) + public function show($licenseId, $seatId) : JsonResponse | array { - // + $this->authorize('view', License::class); // sanity checks: // 1. does the license seat exist? @@ -89,19 +87,18 @@ public function show($licenseId, $seatId) * @param \Illuminate\Http\Request $request * @param int $licenseId * @param int $seatId - * @return \Illuminate\Http\Response */ - public function update(Request $request, $licenseId, $seatId) + public function update(Request $request, $licenseId, $seatId) : JsonResponse | array { $this->authorize('checkout', License::class); - // sanity checks: - // 1. does the license seat exist? + if (! $licenseSeat = LicenseSeat::find($seatId)) { return response()->json(Helper::formatStandardApiResponse('error', null, 'Seat not found')); } - // 2. does the seat belong to the specified license? - if (! $license = $licenseSeat->license()->first() || $license->id != intval($licenseId)) { + + $license = $licenseSeat->license()->first(); + if (!$license || $license->id != intval($licenseId)) { return response()->json(Helper::formatStandardApiResponse('error', null, 'Seat does not belong to the specified license')); } diff --git a/app/Http/Controllers/Api/LicensesController.php b/app/Http/Controllers/Api/LicensesController.php index c35b669d70be..579d80eeb5f4 100644 --- a/app/Http/Controllers/Api/LicensesController.php +++ b/app/Http/Controllers/Api/LicensesController.php @@ -4,14 +4,12 @@ use App\Helpers\Helper; use App\Http\Controllers\Controller; -use App\Http\Transformers\LicenseSeatsTransformer; use App\Http\Transformers\LicensesTransformer; use App\Http\Transformers\SelectlistTransformer; -use App\Models\Company; use App\Models\License; -use App\Models\LicenseSeat; use Illuminate\Http\Request; use Illuminate\Support\Facades\DB; +use Illuminate\Http\JsonResponse; class LicensesController extends Controller { @@ -21,9 +19,8 @@ class LicensesController extends Controller * @author [A. Gianotto] [] * @since [v4.0] * - * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('view', License::class); @@ -156,11 +153,9 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(Request $request) : JsonResponse { - // $this->authorize('create', License::class); $license = new License; $license->fill($request->all()); @@ -177,9 +172,8 @@ public function store(Request $request) * * @author [A. Gianotto] [] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : JsonResponse | array { $this->authorize('view', License::class); $license = License::withCount('freeSeats')->findOrFail($id); @@ -195,9 +189,8 @@ public function show($id) * @since [v4.0] * @param \Illuminate\Http\Request $request * @param int $id - * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(Request $request, $id) : JsonResponse | array { // $this->authorize('update', License::class); @@ -218,9 +211,8 @@ public function update(Request $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { // $license = License::findOrFail($id); @@ -248,7 +240,7 @@ public function destroy($id) * * @see \App\Http\Transformers\SelectlistTransformer */ - public function selectlist(Request $request) + public function selectlist(Request $request) : array { $licenses = License::select([ 'licenses.id', diff --git a/app/Http/Controllers/Api/LocationsController.php b/app/Http/Controllers/Api/LocationsController.php index 78cf43c671e1..2ceeb8374e13 100644 --- a/app/Http/Controllers/Api/LocationsController.php +++ b/app/Http/Controllers/Api/LocationsController.php @@ -11,6 +11,7 @@ use Illuminate\Http\Request; use Illuminate\Pagination\LengthAwarePaginator; use Illuminate\Support\Collection; +use Illuminate\Http\JsonResponse; class LocationsController extends Controller { @@ -21,7 +22,7 @@ class LocationsController extends Controller * @since [v4.0] * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('view', Location::class); $allowed_columns = [ @@ -138,9 +139,8 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request - * @return \Illuminate\Http\Response */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : JsonResponse { $this->authorize('create', Location::class); $location = new Location; @@ -160,9 +160,8 @@ public function store(ImageUploadRequest $request) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : JsonResponse | array { $this->authorize('view', Location::class); $location = Location::with('parent', 'manager', 'children') @@ -199,9 +198,8 @@ public function show($id) * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id - * @return \Illuminate\Http\JsonResponse */ - public function update(ImageUploadRequest $request, $id) + public function update(ImageUploadRequest $request, $id) : JsonResponse { $this->authorize('update', Location::class); $location = Location::findOrFail($id); @@ -230,9 +228,8 @@ public function update(ImageUploadRequest $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', Location::class); $location = Location::withCount('assignedAssets as assigned_assets_count') @@ -280,7 +277,7 @@ public function destroy($id) * @since [v4.0.16] * @see \App\Http\Transformers\SelectlistTransformer */ - public function selectlist(Request $request) + public function selectlist(Request $request) : array { // If a user is in the process of editing their profile, as determined by the referrer, // then we check that they have permission to edit their own location. @@ -325,7 +322,6 @@ public function selectlist(Request $request) $paginated_results = new LengthAwarePaginator($locations_formatted->forPage($page, 500), $locations_formatted->count(), 500, $page, []); - //return []; return (new SelectlistTransformer)->transformSelectlist($paginated_results); } } diff --git a/app/Http/Controllers/Api/ManufacturersController.php b/app/Http/Controllers/Api/ManufacturersController.php index 36fbe5354cfc..eb89693e5c06 100644 --- a/app/Http/Controllers/Api/ManufacturersController.php +++ b/app/Http/Controllers/Api/ManufacturersController.php @@ -10,8 +10,8 @@ use App\Models\Manufacturer; use Illuminate\Http\Request; use App\Http\Requests\ImageUploadRequest; -use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; +use Illuminate\Http\JsonResponse; class ManufacturersController extends Controller { @@ -22,7 +22,7 @@ class ManufacturersController extends Controller * @since [v4.0] * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('view', Manufacturer::class); $allowed_columns = ['id', 'name', 'url', 'support_url', 'support_email', 'warranty_lookup_url', 'support_phone', 'created_at', 'updated_at', 'image', 'assets_count', 'consumables_count', 'components_count', 'licenses_count']; @@ -83,9 +83,8 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request - * @return \Illuminate\Http\Response */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : JsonResponse { $this->authorize('create', Manufacturer::class); $manufacturer = new Manufacturer; @@ -105,9 +104,8 @@ public function store(ImageUploadRequest $request) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : JsonResponse | array { $this->authorize('view', Manufacturer::class); $manufacturer = Manufacturer::withCount('assets as assets_count')->withCount('licenses as licenses_count')->withCount('consumables as consumables_count')->withCount('accessories as accessories_count')->findOrFail($id); @@ -122,9 +120,8 @@ public function show($id) * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id - * @return \Illuminate\Http\Response */ - public function update(ImageUploadRequest $request, $id) + public function update(ImageUploadRequest $request, $id) : JsonResponse { $this->authorize('update', Manufacturer::class); $manufacturer = Manufacturer::findOrFail($id); @@ -144,9 +141,8 @@ public function update(ImageUploadRequest $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', Manufacturer::class); $manufacturer = Manufacturer::findOrFail($id); @@ -167,10 +163,9 @@ public function destroy($id) * @author [A. Gianotto] [] * @since [v6.3.4] * @param int $id - * @return \Illuminate\Http\JsonResponse * @throws \Illuminate\Auth\Access\AuthorizationException */ - public function restore($id) + public function restore($id) : JsonResponse { $this->authorize('delete', Manufacturer::class); @@ -206,7 +201,7 @@ public function restore($id) * @since [v4.0.16] * @see \App\Http\Transformers\SelectlistTransformer */ - public function selectlist(Request $request) + public function selectlist(Request $request) : array { $this->authorize('view.selectlists'); diff --git a/app/Http/Controllers/Api/PredefinedKitsController.php b/app/Http/Controllers/Api/PredefinedKitsController.php index b398dbfae235..2bc118db38fb 100644 --- a/app/Http/Controllers/Api/PredefinedKitsController.php +++ b/app/Http/Controllers/Api/PredefinedKitsController.php @@ -7,6 +7,8 @@ use App\Http\Transformers\PredefinedKitsTransformer; use App\Models\PredefinedKit; use Illuminate\Http\Request; +use Illuminate\Http\JsonResponse; +use App\Http\Transformers\SelectlistTransformer; /** * @author [D. Minaev.] [] @@ -18,7 +20,7 @@ class PredefinedKitsController extends Controller * * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('view', PredefinedKit::class); $allowed_columns = ['id', 'name']; @@ -47,9 +49,8 @@ public function index(Request $request) * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(Request $request) : JsonResponse { $this->authorize('create', PredefinedKit::class); $kit = new PredefinedKit; @@ -66,9 +67,8 @@ public function store(Request $request) * Display the specified resource. * * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : array { $this->authorize('view', PredefinedKit::class); $kit = PredefinedKit::findOrFail($id); @@ -81,9 +81,8 @@ public function show($id) * * @param \Illuminate\Http\Request $request * @param int $id kit id - * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(Request $request, $id) : JsonResponse { $this->authorize('update', PredefinedKit::class); $kit = PredefinedKit::findOrFail($id); @@ -100,9 +99,8 @@ public function update(Request $request, $id) * Remove the specified resource from storage. * * @param int $id - * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', PredefinedKit::class); $kit = PredefinedKit::findOrFail($id); @@ -123,7 +121,7 @@ public function destroy($id) * * @see \App\Http\Transformers\SelectlistTransformer */ - public function selectlist(Request $request) + public function selectlist(Request $request) : array { $kits = PredefinedKit::select([ 'id', @@ -145,7 +143,7 @@ public function selectlist(Request $request) * @param int $id * @return \Illuminate\Http\Response */ - public function indexLicenses($kit_id) + public function indexLicenses($kit_id) : array { $this->authorize('view', PredefinedKit::class); $kit = PredefinedKit::findOrFail($kit_id); @@ -160,7 +158,7 @@ public function indexLicenses($kit_id) * @param int $id * @return \Illuminate\Http\Response */ - public function storeLicense(Request $request, $kit_id) + public function storeLicense(Request $request, $kit_id) : JsonResponse { $this->authorize('update', PredefinedKit::class); @@ -186,9 +184,8 @@ public function storeLicense(Request $request, $kit_id) * * @param \Illuminate\Http\Request $request * @param int $kit_id - * @return \Illuminate\Http\Response */ - public function updateLicense(Request $request, $kit_id, $license_id) + public function updateLicense(Request $request, $kit_id, $license_id) : JsonResponse { $this->authorize('update', PredefinedKit::class); $kit = PredefinedKit::findOrFail($kit_id); @@ -205,9 +202,8 @@ public function updateLicense(Request $request, $kit_id, $license_id) * Remove the specified resource from storage. * * @param int $kit_id - * @return \Illuminate\Http\Response */ - public function detachLicense($kit_id, $license_id) + public function detachLicense($kit_id, $license_id) : JsonResponse { $this->authorize('update', PredefinedKit::class); $kit = PredefinedKit::findOrFail($kit_id); @@ -221,9 +217,8 @@ public function detachLicense($kit_id, $license_id) * Display the specified resource. * * @param int $kit_id - * @return \Illuminate\Http\Response */ - public function indexModels($kit_id) + public function indexModels($kit_id) : array { $this->authorize('view', PredefinedKit::class); $kit = PredefinedKit::findOrFail($kit_id); @@ -236,9 +231,8 @@ public function indexModels($kit_id) * Store the specified resource. * * @param int $id - * @return \Illuminate\Http\Response */ - public function storeModel(Request $request, $kit_id) + public function storeModel(Request $request, $kit_id) : JsonResponse { $this->authorize('update', PredefinedKit::class); @@ -264,9 +258,8 @@ public function storeModel(Request $request, $kit_id) * * @param \Illuminate\Http\Request $request * @param int $kit_id - * @return \Illuminate\Http\Response */ - public function updateModel(Request $request, $kit_id, $model_id) + public function updateModel(Request $request, $kit_id, $model_id) : JsonResponse { $this->authorize('update', PredefinedKit::class); $kit = PredefinedKit::findOrFail($kit_id); @@ -283,9 +276,8 @@ public function updateModel(Request $request, $kit_id, $model_id) * Remove the specified resource from storage. * * @param int $kit_id - * @return \Illuminate\Http\Response */ - public function detachModel($kit_id, $model_id) + public function detachModel($kit_id, $model_id) : JsonResponse { $this->authorize('update', PredefinedKit::class); $kit = PredefinedKit::findOrFail($kit_id); @@ -299,9 +291,8 @@ public function detachModel($kit_id, $model_id) * Display the specified resource. * * @param int $kit_id - * @return \Illuminate\Http\Response */ - public function indexConsumables($kit_id) + public function indexConsumables($kit_id) : array { $this->authorize('view', PredefinedKit::class); $kit = PredefinedKit::findOrFail($kit_id); @@ -314,9 +305,8 @@ public function indexConsumables($kit_id) * Store the specified resource. * * @param int $id - * @return \Illuminate\Http\Response */ - public function storeConsumable(Request $request, $kit_id) + public function storeConsumable(Request $request, $kit_id) : JsonResponse { $this->authorize('update', PredefinedKit::class); @@ -342,9 +332,8 @@ public function storeConsumable(Request $request, $kit_id) * * @param \Illuminate\Http\Request $request * @param int $kit_id - * @return \Illuminate\Http\Response */ - public function updateConsumable(Request $request, $kit_id, $consumable_id) + public function updateConsumable(Request $request, $kit_id, $consumable_id) : JsonResponse { $this->authorize('update', PredefinedKit::class); $kit = PredefinedKit::findOrFail($kit_id); @@ -361,9 +350,8 @@ public function updateConsumable(Request $request, $kit_id, $consumable_id) * Remove the specified resource from storage. * * @param int $kit_id - * @return \Illuminate\Http\Response */ - public function detachConsumable($kit_id, $consumable_id) + public function detachConsumable($kit_id, $consumable_id) : JsonResponse { $this->authorize('update', PredefinedKit::class); $kit = PredefinedKit::findOrFail($kit_id); @@ -377,9 +365,8 @@ public function detachConsumable($kit_id, $consumable_id) * Display the specified resource. * * @param int $kit_id - * @return \Illuminate\Http\Response */ - public function indexAccessories($kit_id) + public function indexAccessories($kit_id) : array { $this->authorize('view', PredefinedKit::class); $kit = PredefinedKit::findOrFail($kit_id); @@ -392,9 +379,8 @@ public function indexAccessories($kit_id) * Store the specified resource. * * @param int $kit_id - * @return \Illuminate\Http\Response */ - public function storeAccessory(Request $request, $kit_id) + public function storeAccessory(Request $request, $kit_id) : JsonResponse { $this->authorize('update', PredefinedKit::class); @@ -420,9 +406,8 @@ public function storeAccessory(Request $request, $kit_id) * * @param \Illuminate\Http\Request $request * @param int $kit_id - * @return \Illuminate\Http\Response */ - public function updateAccessory(Request $request, $kit_id, $accessory_id) + public function updateAccessory(Request $request, $kit_id, $accessory_id) : JsonResponse { $this->authorize('update', PredefinedKit::class); $kit = PredefinedKit::findOrFail($kit_id); @@ -439,9 +424,8 @@ public function updateAccessory(Request $request, $kit_id, $accessory_id) * Remove the specified resource from storage. * * @param int $kit_id - * @return \Illuminate\Http\Response */ - public function detachAccessory($kit_id, $accessory_id) + public function detachAccessory($kit_id, $accessory_id) : JsonResponse { $this->authorize('update', PredefinedKit::class); $kit = PredefinedKit::findOrFail($kit_id); diff --git a/app/Http/Controllers/Api/ProfileController.php b/app/Http/Controllers/Api/ProfileController.php index 41adbd58458b..fb7335145f9b 100644 --- a/app/Http/Controllers/Api/ProfileController.php +++ b/app/Http/Controllers/Api/ProfileController.php @@ -6,13 +6,13 @@ use App\Http\Controllers\Controller; use App\Models\CheckoutRequest; use Illuminate\Http\Response; -use Illuminate\Support\Facades\Auth; use Illuminate\Http\Request; use Laravel\Passport\TokenRepository; use Illuminate\Contracts\Validation\Factory as ValidationFactory; use Illuminate\Support\Facades\Gate; use App\Models\CustomField; use Illuminate\Support\Facades\DB; +use Illuminate\Http\JsonResponse; class ProfileController extends Controller { @@ -42,10 +42,8 @@ public function __construct(TokenRepository $tokenRepository, ValidationFactory * * @author [A. Gianotto] [] * @since [v4.3.0] - * - * @return array */ - public function requestedAssets() + public function requestedAssets() : array { $checkoutRequests = CheckoutRequest::where('user_id', '=', auth()->id())->get(); @@ -95,10 +93,9 @@ public function requestedAssets() * * @author [A. Gianotto] [] * @since [v6.0.5] - * - * @return \Illuminate\Http\Response */ - public function createApiToken(Request $request) { + public function createApiToken(Request $request) : JsonResponse + { if (!Gate::allows('self.api')) { abort(403); @@ -125,10 +122,9 @@ public function createApiToken(Request $request) { * * @author [A. Gianotto] [] * @since [v6.0.5] - * - * @return \Illuminate\Http\Response */ - public function deleteApiToken($tokenId) { + public function deleteApiToken($tokenId) : Response + { if (!Gate::allows('self.api')) { abort(403); @@ -154,10 +150,9 @@ public function deleteApiToken($tokenId) { * * @author [A. Gianotto] [] * @since [v6.0.5] - * - * @return \Illuminate\Http\Response */ - public function showApiTokens(Request $request) { + public function showApiTokens() : JsonResponse + { if (!Gate::allows('self.api')) { abort(403); diff --git a/app/Http/Controllers/Api/ReportsController.php b/app/Http/Controllers/Api/ReportsController.php index 61e777febc07..b0f64189d581 100644 --- a/app/Http/Controllers/Api/ReportsController.php +++ b/app/Http/Controllers/Api/ReportsController.php @@ -6,6 +6,7 @@ use App\Http\Transformers\ActionlogsTransformer; use App\Models\Actionlog; use Illuminate\Http\Request; +use Illuminate\Http\JsonResponse; class ReportsController extends Controller { @@ -14,9 +15,8 @@ class ReportsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @return \Illuminate\Contracts\View\View */ - public function index(Request $request) + public function index(Request $request) : JsonResponse | array { $this->authorize('reports.view'); diff --git a/app/Http/Controllers/Api/SettingsController.php b/app/Http/Controllers/Api/SettingsController.php index 5be5a72afba2..7eb28a4815a8 100644 --- a/app/Http/Controllers/Api/SettingsController.php +++ b/app/Http/Controllers/Api/SettingsController.php @@ -9,26 +9,22 @@ use App\Http\Controllers\Controller; use App\Models\Ldap; use App\Models\Setting; -use Mail; -use App\Notifications\SlackTest; use App\Notifications\MailTest; -use GuzzleHttp\Client; use Illuminate\Http\JsonResponse; -use Illuminate\Http\Response; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Notification; use Illuminate\Support\Facades\Storage; -use Illuminate\Support\Facades\Validator; -use App\Http\Requests\SlackSettingsRequest; +use Illuminate\Support\Facades\Validator; use App\Http\Transformers\LoginAttemptsTransformer; +use Symfony\Component\HttpFoundation\BinaryFileResponse; class SettingsController extends Controller { - public function ldaptest() + public function ldaptest() : JsonResponse { $settings = Setting::getSettings(); @@ -88,7 +84,7 @@ public function ldaptest() } - public function ldaptestlogin(Request $request) + public function ldaptestlogin(Request $request) : JsonResponse { if (Setting::getSettings()->ldap_enabled != '1') { @@ -148,9 +144,8 @@ public function ldaptestlogin(Request $request) * * @author [A. Gianotto] [] * @since [v3.0] - * @return \Illuminate\Http\JsonResponse */ - public function ajaxTestEmail() + public function ajaxTestEmail() : JsonResponse { if (!config('app.lock_passwords')) { try { @@ -170,9 +165,8 @@ public function ajaxTestEmail() * * @author [A. Gianotto] [] * @since [v5.0.0] - * @return \Illuminate\Http\JsonResponse */ - public function purgeBarcodes() + public function purgeBarcodes() : JsonResponse { $file_count = 0; $files = Storage::disk('public')->files('barcodes'); @@ -211,9 +205,8 @@ public function purgeBarcodes() * @author [A. Gianotto] [] * @since [v5.0.0] * @param \Illuminate\Http\Request $request - * @return array | JsonResponse */ - public function showLoginAttempts(Request $request) + public function showLoginAttempts(Request $request) : array { $allowed_columns = ['id', 'username', 'remote_ip', 'user_agent', 'successful', 'created_at']; @@ -233,9 +226,9 @@ public function showLoginAttempts(Request $request) * Lists backup files * * @author [A. Gianotto] - * @return array | JsonResponse */ - public function listBackups() { + public function listBackups() : array + { $settings = Setting::getSettings(); $path = 'app/backups'; $backup_files = Storage::files($path); @@ -276,9 +269,9 @@ public function listBackups() { * exhausts memory on larger files. * * @author [A. Gianotto] - * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse */ - public function downloadBackup($file) { + public function downloadBackup($file) : JsonResponse | BinaryFileResponse + { $path = storage_path('app/backups'); @@ -296,9 +289,9 @@ public function downloadBackup($file) { * * @author [A. Gianotto] * @since [v6.3.1] - * @return \Illuminate\Http\JsonResponse|\Symfony\Component\HttpFoundation\BinaryFileResponse */ - public function downloadLatestBackup() { + public function downloadLatestBackup() : JsonResponse | BinaryFileResponse + { $fileData = collect(); foreach (Storage::files('app/backups') as $file) { diff --git a/app/Http/Controllers/Api/StatuslabelsController.php b/app/Http/Controllers/Api/StatuslabelsController.php index 7c8e260c2f0f..95ac5956fa62 100644 --- a/app/Http/Controllers/Api/StatuslabelsController.php +++ b/app/Http/Controllers/Api/StatuslabelsController.php @@ -11,7 +11,7 @@ use App\Models\Statuslabel; use Illuminate\Http\Request; use App\Http\Transformers\PieChartTransformer; -use Illuminate\Support\Arr; +use Illuminate\Http\JsonResponse; class StatuslabelsController extends Controller { @@ -20,9 +20,8 @@ class StatuslabelsController extends Controller * * @author [A. Gianotto] [] * @since [v4.0] - * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request) : array { $this->authorize('view', Statuslabel::class); $allowed_columns = ['id', 'name', 'created_at', 'assets_count', 'color', 'notes', 'default_label']; @@ -72,9 +71,8 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param \Illuminate\Http\Request $request - * @return \Illuminate\Http\Response */ - public function store(Request $request) + public function store(Request $request) : JsonResponse { $this->authorize('create', Statuslabel::class); $request->except('deployable', 'pending', 'archived'); @@ -108,9 +106,8 @@ public function store(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : array { $this->authorize('view', Statuslabel::class); $statuslabel = Statuslabel::findOrFail($id); @@ -126,9 +123,8 @@ public function show($id) * @since [v4.0] * @param \Illuminate\Http\Request $request * @param int $id - * @return \Illuminate\Http\Response */ - public function update(Request $request, $id) + public function update(Request $request, $id) : JsonResponse { $this->authorize('update', Statuslabel::class); $statuslabel = Statuslabel::findOrFail($id); @@ -163,9 +159,8 @@ public function update(Request $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', Statuslabel::class); $statuslabel = Statuslabel::findOrFail($id); @@ -188,9 +183,8 @@ public function destroy($id) * * @author [A. Gianotto] [] * @since [v3.0] - * @return array */ - public function getAssetCountByStatuslabel() + public function getAssetCountByStatuslabel() : array { $this->authorize('view', Statuslabel::class); $statuslabels = Statuslabel::withCount('assets')->get(); @@ -215,9 +209,8 @@ public function getAssetCountByStatuslabel() * * @author [A. Gianotto] [] * @since [v6.0.11] - * @return array */ - public function getAssetCountByMetaStatus() + public function getAssetCountByMetaStatus() : array { $this->authorize('view', Statuslabel::class); @@ -245,9 +238,8 @@ public function getAssetCountByMetaStatus() * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function assets(Request $request, $id) + public function assets(Request $request, $id) : array { $this->authorize('view', Statuslabel::class); $this->authorize('index', Asset::class); @@ -281,9 +273,8 @@ public function assets(Request $request, $id) * * @author [A. Gianotto] [] * @since [v4.0] - * @return bool */ - public function checkIfDeployable($id) + public function checkIfDeployable($id) : string { $statuslabel = Statuslabel::findOrFail($id); if ($statuslabel->getStatuslabelType() == 'deployable') { @@ -300,7 +291,7 @@ public function checkIfDeployable($id) * @since [v6.1.1] * @see \App\Http\Transformers\SelectlistTransformer */ - public function selectlist(Request $request) + public function selectlist(Request $request) : array { $this->authorize('view.selectlists'); diff --git a/app/Http/Controllers/Api/SuppliersController.php b/app/Http/Controllers/Api/SuppliersController.php index 3e3d637be066..f752f222411a 100644 --- a/app/Http/Controllers/Api/SuppliersController.php +++ b/app/Http/Controllers/Api/SuppliersController.php @@ -10,6 +10,7 @@ use Illuminate\Http\Request; use App\Http\Requests\ImageUploadRequest; use Illuminate\Support\Facades\Storage; +use Illuminate\Http\JsonResponse; class SuppliersController extends Controller { @@ -20,7 +21,7 @@ class SuppliersController extends Controller * @since [v4.0] * @return \Illuminate\Http\Response */ - public function index(Request $request) + public function index(Request $request): array { $this->authorize('view', Supplier::class); $allowed_columns = [' @@ -114,9 +115,8 @@ public function index(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request - * @return \Illuminate\Http\Response */ - public function store(ImageUploadRequest $request) + public function store(ImageUploadRequest $request) : JsonResponse { $this->authorize('create', Supplier::class); $supplier = new Supplier; @@ -136,9 +136,8 @@ public function store(ImageUploadRequest $request) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function show($id) + public function show($id) : array { $this->authorize('view', Supplier::class); $supplier = Supplier::findOrFail($id); @@ -154,9 +153,8 @@ public function show($id) * @since [v4.0] * @param \App\Http\Requests\ImageUploadRequest $request * @param int $id - * @return \Illuminate\Http\Response */ - public function update(ImageUploadRequest $request, $id) + public function update(ImageUploadRequest $request, $id) : JsonResponse { $this->authorize('update', Supplier::class); $supplier = Supplier::findOrFail($id); @@ -176,9 +174,8 @@ public function update(ImageUploadRequest $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\Response */ - public function destroy($id) + public function destroy($id) : JsonResponse { $this->authorize('delete', Supplier::class); $supplier = Supplier::with('asset_maintenances', 'assets', 'licenses')->withCount('asset_maintenances as asset_maintenances_count', 'assets as assets_count', 'licenses as licenses_count')->findOrFail($id); @@ -209,7 +206,7 @@ public function destroy($id) * @since [v4.0.16] * @see \App\Http\Transformers\SelectlistTransformer */ - public function selectlist(Request $request) + public function selectlist(Request $request) : array { $this->authorize('view.selectlists'); diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 2f60f84826d3..9200f80b1dde 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -24,6 +24,7 @@ use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Log; use App\Http\Requests\DeleteUserRequest; +use Illuminate\Http\JsonResponse; class UsersController extends Controller { @@ -35,7 +36,7 @@ class UsersController extends Controller * * @return array */ - public function index(Request $request) + public function index(Request $request) : array { $this->authorize('view', User::class); @@ -301,7 +302,7 @@ public function index(Request $request) * @since [v4.0.16] * @see \App\Http\Transformers\SelectlistTransformer */ - public function selectlist(Request $request) + public function selectlist(Request $request) : array { $users = User::select( [ @@ -357,9 +358,8 @@ public function selectlist(Request $request) * @author [A. Gianotto] [] * @since [v4.0] * @param \Illuminate\Http\Request $request - * @return array | \Illuminate\Http\JsonResponse */ - public function store(SaveUserRequest $request) + public function store(SaveUserRequest $request) : JsonResponse { $this->authorize('create', User::class); @@ -404,9 +404,8 @@ public function store(SaveUserRequest $request) * * @author [A. Gianotto] [] * @param int $id - * @return array | \Illuminate\Http\JsonResponse */ - public function show($id) + public function show($id) : JsonResponse | array { $this->authorize('view', User::class); @@ -427,9 +426,8 @@ public function show($id) * @since [v4.0] * @param \Illuminate\Http\Request $request * @param int $id - * @return \Illuminate\Http\JsonResponse */ - public function update(SaveUserRequest $request, $id) + public function update(SaveUserRequest $request, $id) : JsonResponse { $this->authorize('update', User::class); @@ -518,9 +516,8 @@ public function update(SaveUserRequest $request, $id) * @author [A. Gianotto] [] * @since [v4.0] * @param int $id - * @return \Illuminate\Http\JsonResponse */ - public function destroy(DeleteUserRequest $request, $id) + public function destroy(DeleteUserRequest $request, $id) : JsonResponse { $this->authorize('delete', User::class); @@ -556,9 +553,8 @@ public function destroy(DeleteUserRequest $request, $id) * @author [A. Gianotto] [] * @since [v3.0] * @param $userId - * @return array | \Illuminate\Http\JsonResponse */ - public function assets(Request $request, $id) + public function assets(Request $request, $id) : JsonResponse | array { $this->authorize('view', User::class); $this->authorize('view', Asset::class); @@ -601,9 +597,9 @@ public function assets(Request $request, $id) * @since [v6.0.13] * @param Request $request * @param $id - * @return string JSON */ - public function emailAssetList(Request $request, $id) + public function emailAssetList(Request $request, $id) : JsonResponse + { $this->authorize('update', User::class); @@ -629,9 +625,8 @@ public function emailAssetList(Request $request, $id) * @author [A. Gianotto] [] * @since [v3.0] * @param $userId - * @return array | \Illuminate\Http\JsonResponse */ - public function consumables(Request $request, $id) + public function consumables(Request $request, $id) : array { $this->authorize('view', User::class); $this->authorize('view', Consumable::class); @@ -647,9 +642,8 @@ public function consumables(Request $request, $id) * @author [A. Gianotto] [] * @since [v4.6.14] * @param $userId - * @return array */ - public function accessories($id) + public function accessories($id) : array { $this->authorize('view', User::class); $user = User::findOrFail($id); @@ -666,9 +660,8 @@ public function accessories($id) * @author [N. Mathar] [] * @since [v5.0] * @param $userId - * @return array | \Illuminate\Http\JsonResponse */ - public function licenses($id) + public function licenses($id) : JsonResponse | array { $this->authorize('view', User::class); $this->authorize('view', License::class); @@ -689,9 +682,8 @@ public function licenses($id) * @author [A. Gianotto] [] * @since [v3.0] * @param $userId - * @return string JSON */ - public function postTwoFactorReset(Request $request) + public function postTwoFactorReset(Request $request) : JsonResponse { $this->authorize('update', User::class); @@ -729,9 +721,8 @@ public function postTwoFactorReset(Request $request) * @author [Juan Font] [] * @since [v4.4.2] * @param \Illuminate\Http\Request $request - * @return array */ - public function getCurrentUserInfo(Request $request) + public function getCurrentUserInfo(Request $request) : array { return (new UsersTransformer)->transformUser($request->user()); } @@ -742,9 +733,8 @@ public function getCurrentUserInfo(Request $request) * @author [E. Taylor] [] * @param int $userId * @since [v6.0.0] - * @return \Illuminate\Http\JsonResponse */ - public function restore($userId) + public function restore($userId) : JsonResponse { $this->authorize('delete', User::class); diff --git a/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php b/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php index 8d7e39c48991..26e4dd75b1c0 100644 --- a/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php +++ b/tests/Feature/AssetModels/Api/IndexAssetModelsTest.php @@ -63,5 +63,5 @@ public function testAssetModelIndexSearchReturnsExpectedAssetModels() ]) ->assertJson(fn(AssertableJson $json) => $json->has('rows', 1)->etc()); } - + } From 0a7e0539850a376a2fcf510a698509329a22860f Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 07:53:02 +0100 Subject: [PATCH 30/42] Added basic Department UI tests Signed-off-by: snipe --- .../Departments/Api/CreateDepartmentsTest.php | 23 +++++++ ...IndexTest.php => DepartmentsIndexTest.php} | 2 +- .../Departments/Api/UpdateDepartmentTest.php | 41 +++++++++++++ .../Departments/Ui/CreateDepartmentsTest.php | 37 +++++++++++ .../Departments/Ui/IndexDepartmentsTest.php | 23 +++++++ .../Departments/Ui/UpdateDepartmentsTest.php | 61 +++++++++++++++++++ 6 files changed, 186 insertions(+), 1 deletion(-) create mode 100644 tests/Feature/Departments/Api/CreateDepartmentsTest.php rename tests/Feature/Departments/Api/{DepartmentIndexTest.php => DepartmentsIndexTest.php} (98%) create mode 100644 tests/Feature/Departments/Api/UpdateDepartmentTest.php create mode 100644 tests/Feature/Departments/Ui/CreateDepartmentsTest.php create mode 100644 tests/Feature/Departments/Ui/IndexDepartmentsTest.php create mode 100644 tests/Feature/Departments/Ui/UpdateDepartmentsTest.php diff --git a/tests/Feature/Departments/Api/CreateDepartmentsTest.php b/tests/Feature/Departments/Api/CreateDepartmentsTest.php new file mode 100644 index 000000000000..be2bbbc18dd4 --- /dev/null +++ b/tests/Feature/Departments/Api/CreateDepartmentsTest.php @@ -0,0 +1,23 @@ +actingAsForApi(User::factory()->create()) + ->postJson(route('api.departments.store')) + ->assertForbidden(); + } + +} diff --git a/tests/Feature/Departments/Api/DepartmentIndexTest.php b/tests/Feature/Departments/Api/DepartmentsIndexTest.php similarity index 98% rename from tests/Feature/Departments/Api/DepartmentIndexTest.php rename to tests/Feature/Departments/Api/DepartmentsIndexTest.php index 462c0dc8e857..e3616e249166 100644 --- a/tests/Feature/Departments/Api/DepartmentIndexTest.php +++ b/tests/Feature/Departments/Api/DepartmentsIndexTest.php @@ -8,7 +8,7 @@ use Illuminate\Testing\Fluent\AssertableJson; use Tests\TestCase; -class DepartmentIndexTest extends TestCase +class DepartmentsIndexTest extends TestCase { public function testViewingDepartmentIndexRequiresAuthentication() { diff --git a/tests/Feature/Departments/Api/UpdateDepartmentTest.php b/tests/Feature/Departments/Api/UpdateDepartmentTest.php new file mode 100644 index 000000000000..2a6401e7f121 --- /dev/null +++ b/tests/Feature/Departments/Api/UpdateDepartmentTest.php @@ -0,0 +1,41 @@ +create(); + $this->actingAsForApi(User::factory()->create()) + ->patchJson(route('api.departments.update', $department)) + ->assertForbidden(); + } + + public function testCanUpdateDepartmentViaPatch() + { + $department = Department::factory()->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->patchJson(route('api.departments.update', $department), [ + 'name' => 'Test Department', + ]) + ->assertOk() + ->assertStatusMessageIs('success') + ->assertStatus(200) + ->json(); + + $department->refresh(); + $this->assertEquals('Test Department', $department->name, 'Name was not updated'); + + } + + + +} diff --git a/tests/Feature/Departments/Ui/CreateDepartmentsTest.php b/tests/Feature/Departments/Ui/CreateDepartmentsTest.php new file mode 100644 index 000000000000..17045cbc0bdb --- /dev/null +++ b/tests/Feature/Departments/Ui/CreateDepartmentsTest.php @@ -0,0 +1,37 @@ +actingAs(User::factory()->create()) + ->post(route('departments.store'), [ + 'name' => 'Test Department', + 'company_id' => Company::factory()->create()->id + ]) + ->assertForbidden(); + } + + public function testUserCanCreateDepartments() + { + $this->assertFalse(Department::where('name', 'Test Department')->exists()); + + $this->actingAs(User::factory()->superuser()->create()) + ->post(route('departments.store'), [ + 'name' => 'Test Department', + 'company_id' => Company::factory()->create()->id + ]) + ->assertRedirect(route('departments.index')); + + $this->assertTrue(Department::where('name', 'Test Department')->exists()); + } + + +} diff --git a/tests/Feature/Departments/Ui/IndexDepartmentsTest.php b/tests/Feature/Departments/Ui/IndexDepartmentsTest.php new file mode 100644 index 000000000000..461ec8fffb68 --- /dev/null +++ b/tests/Feature/Departments/Ui/IndexDepartmentsTest.php @@ -0,0 +1,23 @@ +actingAs(User::factory()->create()) + ->get(route('departments.index')) + ->assertForbidden(); + } + + public function testUserCanListDepartments() + { + $this->actingAs(User::factory()->superuser()->create()) + ->get(route('departments.index')) + ->assertOk(); + } +} diff --git a/tests/Feature/Departments/Ui/UpdateDepartmentsTest.php b/tests/Feature/Departments/Ui/UpdateDepartmentsTest.php new file mode 100644 index 000000000000..e4ae7c6240fd --- /dev/null +++ b/tests/Feature/Departments/Ui/UpdateDepartmentsTest.php @@ -0,0 +1,61 @@ +actingAs(User::factory()->create()) + ->post(route('departments.store'), [ + 'name' => 'Test Department', + 'category_id' => Category::factory()->create()->id + ]) + ->assertStatus(403) + ->assertForbidden(); + } + + public function testUserCanCreateDepartments() + { + $this->assertFalse(Department::where('name', 'Test Department')->exists()); + + $this->actingAs(User::factory()->superuser()->create()) + ->post(route('departments.store'), [ + 'name' => 'Test Department', + 'category_id' => Category::factory()->create()->id + ]) + ->assertStatus(302) + ->assertSessionHasNoErrors() + ->assertRedirect(route('departments.index')); + + $this->assertTrue(Department::where('name', 'Test Department')->exists()); + } + + public function testUserCanEditDepartments() + { + $category = Category::factory()->forAssets()->create(); + $department = Department::factory()->create(['name' => 'Test Department']); + $this->assertTrue(Department::where('name', 'Test Department')->exists()); + + $response = $this->actingAs(User::factory()->superuser()->create()) + ->put(route('departments.update', ['department' => $department]), [ + 'name' => 'Test Department Edited', + 'category_id' => $department->category_id, + ]) + ->assertStatus(302) + ->assertSessionHasNoErrors() + ->assertRedirect(route('departments.index')); + + $this->followRedirects($response)->assertSee('Success'); + $this->assertTrue(Department::where('name', 'Test Department Edited')->exists()); + + } + + + +} From 16d65631aa24daa56ff7f9b9e96bcea74f8a6474 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 07:54:22 +0100 Subject: [PATCH 31/42] Removed duplicate tests Signed-off-by: snipe --- .../AssetModels/Ui/UpdateAssetModelsTest.php | 18 +----------------- .../Departments/Ui/UpdateDepartmentsTest.php | 18 ------------------ 2 files changed, 1 insertion(+), 35 deletions(-) diff --git a/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php b/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php index ff2e5ef59fb0..423eaad57416 100644 --- a/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php +++ b/tests/Feature/AssetModels/Ui/UpdateAssetModelsTest.php @@ -19,23 +19,7 @@ public function testPermissionRequiredToStoreAssetModel() ->assertStatus(403) ->assertForbidden(); } - - public function testUserCanCreateAssetModels() - { - $this->assertFalse(AssetModel::where('name', 'Test Model')->exists()); - - $this->actingAs(User::factory()->superuser()->create()) - ->post(route('models.store'), [ - 'name' => 'Test Model', - 'category_id' => Category::factory()->create()->id - ]) - ->assertStatus(302) - ->assertSessionHasNoErrors() - ->assertRedirect(route('models.index')); - - $this->assertTrue(AssetModel::where('name', 'Test Model')->exists()); - } - + public function testUserCanEditAssetModels() { $category = Category::factory()->forAssets()->create(); diff --git a/tests/Feature/Departments/Ui/UpdateDepartmentsTest.php b/tests/Feature/Departments/Ui/UpdateDepartmentsTest.php index e4ae7c6240fd..e6e614d32c57 100644 --- a/tests/Feature/Departments/Ui/UpdateDepartmentsTest.php +++ b/tests/Feature/Departments/Ui/UpdateDepartmentsTest.php @@ -14,38 +14,20 @@ public function testPermissionRequiredToStoreDepartment() $this->actingAs(User::factory()->create()) ->post(route('departments.store'), [ 'name' => 'Test Department', - 'category_id' => Category::factory()->create()->id ]) ->assertStatus(403) ->assertForbidden(); } - public function testUserCanCreateDepartments() - { - $this->assertFalse(Department::where('name', 'Test Department')->exists()); - - $this->actingAs(User::factory()->superuser()->create()) - ->post(route('departments.store'), [ - 'name' => 'Test Department', - 'category_id' => Category::factory()->create()->id - ]) - ->assertStatus(302) - ->assertSessionHasNoErrors() - ->assertRedirect(route('departments.index')); - - $this->assertTrue(Department::where('name', 'Test Department')->exists()); - } public function testUserCanEditDepartments() { - $category = Category::factory()->forAssets()->create(); $department = Department::factory()->create(['name' => 'Test Department']); $this->assertTrue(Department::where('name', 'Test Department')->exists()); $response = $this->actingAs(User::factory()->superuser()->create()) ->put(route('departments.update', ['department' => $department]), [ 'name' => 'Test Department Edited', - 'category_id' => $department->category_id, ]) ->assertStatus(302) ->assertSessionHasNoErrors() From 24ad74f136392531b3402650603f8f96f770c471 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 08:21:18 +0100 Subject: [PATCH 32/42] Updated factories Signed-off-by: snipe --- database/factories/AssetFactory.php | 6 +++--- database/factories/LocationFactory.php | 9 +++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/database/factories/AssetFactory.php b/database/factories/AssetFactory.php index 43845c307595..8f910b903d1d 100644 --- a/database/factories/AssetFactory.php +++ b/database/factories/AssetFactory.php @@ -300,11 +300,11 @@ public function assignedToUser(User $user = null) }); } - public function assignedToLocation() + public function assignedToLocation(Location $location = null) { - return $this->state(function () { + return $this->state(function () use ($location) { return [ - 'assigned_to' => Location::factory(), + 'assigned_to' => $location->id ?? Location::factory(), 'assigned_type' => Location::class, ]; }); diff --git a/database/factories/LocationFactory.php b/database/factories/LocationFactory.php index 6db268e8c1c8..abd773010e36 100644 --- a/database/factories/LocationFactory.php +++ b/database/factories/LocationFactory.php @@ -25,4 +25,13 @@ public function definition() 'image' => rand(1, 9).'.jpg', ]; } + + public function deletedLocation() + { + return $this->state(function () { + return [ + 'deleted_at' => $this->faker->dateTime(), + ]; + }); + } } From 7455bf329d914c85dbd5e499a05ea461512e465b Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 08:21:31 +0100 Subject: [PATCH 33/42] Added more locations tests Signed-off-by: snipe --- .../Locations/Api/CreateLocationsTest.php | 30 ++++++ .../Locations/Api/DeleteLocationsTest.php | 93 +++++++++++++++++++ .../Locations/Ui/IndexLocationsTest.php | 23 +++++ tests/Feature/Users/Api/DeleteUserTest.php | 5 - 4 files changed, 146 insertions(+), 5 deletions(-) create mode 100644 tests/Feature/Locations/Api/CreateLocationsTest.php create mode 100644 tests/Feature/Locations/Api/DeleteLocationsTest.php create mode 100644 tests/Feature/Locations/Ui/IndexLocationsTest.php diff --git a/tests/Feature/Locations/Api/CreateLocationsTest.php b/tests/Feature/Locations/Api/CreateLocationsTest.php new file mode 100644 index 000000000000..f4ac09890cc3 --- /dev/null +++ b/tests/Feature/Locations/Api/CreateLocationsTest.php @@ -0,0 +1,30 @@ +create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->patchJson(route('api.locations.update', $location), [ + 'name' => 'Test Location', + ]) + ->assertOk() + ->assertStatusMessageIs('success') + ->assertStatus(200) + ->json(); + + $location->refresh(); + $this->assertEquals('Test Location', $location->name, 'Name was not updated'); + + } + +} diff --git a/tests/Feature/Locations/Api/DeleteLocationsTest.php b/tests/Feature/Locations/Api/DeleteLocationsTest.php new file mode 100644 index 000000000000..270582c901e2 --- /dev/null +++ b/tests/Feature/Locations/Api/DeleteLocationsTest.php @@ -0,0 +1,93 @@ +actingAsForApi(User::factory()->superuser()->create()) + ->deleteJson(route('api.users.destroy', 'invalid-id')) + ->assertOk() + ->assertStatus(200) + ->assertStatusMessageIs('error') + ->json(); + } + + public function testErrorReturnedViaApiIfLocationIsAlreadyDeleted() + { + $location = Location::factory()->deletedLocation()->create(); + $this->actingAsForApi(User::factory()->superuser()->create()) + ->deleteJson(route('api.locations.destroy', $location->id)) + ->assertOk() + ->assertStatus(200) + ->assertStatusMessageIs('error') + ->json(); + } + + public function testDisallowLocationDeletionViaApiIfStillHasPeople() + { + $location = Location::factory()->create(); + User::factory()->count(5)->create(['location_id' => $location->id]); + + $this->assertFalse($location->isDeletable()); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->deleteJson(route('api.locations.destroy', $location->id)) + ->assertOk() + ->assertStatus(200) + ->assertStatusMessageIs('error') + ->json(); + } + + public function testDisallowUserDeletionViaApiIfStillHasChildLocations() + { + $parent = Location::factory()->create(); + Location::factory()->count(5)->create(['parent_id' => $parent->id]); + $this->assertFalse($parent->isDeletable()); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->deleteJson(route('api.locations.destroy', $parent->id)) + ->assertOk() + ->assertStatus(200) + ->assertStatusMessageIs('error') + ->json(); + } + + public function testDisallowUserDeletionViaApiIfStillHasAssetsAssigned() + { + $location = Location::factory()->create(); + Asset::factory()->count(5)->assignedToLocation($location)->create(); + + $this->assertFalse($location->isDeletable()); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->deleteJson(route('api.locations.destroy', $location->id)) + ->assertOk() + ->assertStatus(200) + ->assertStatusMessageIs('error') + ->json(); + } + + public function testDisallowUserDeletionViaApiIfStillHasAssetsAsLocation() + { + $location = Location::factory()->create(); + Asset::factory()->count(5)->create(['location_id' => $location->id]); + + $this->assertFalse($location->isDeletable()); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->deleteJson(route('api.locations.destroy', $location->id)) + ->assertOk() + ->assertStatus(200) + ->assertStatusMessageIs('error') + ->json(); + } + +} diff --git a/tests/Feature/Locations/Ui/IndexLocationsTest.php b/tests/Feature/Locations/Ui/IndexLocationsTest.php new file mode 100644 index 000000000000..6ccac12f3482 --- /dev/null +++ b/tests/Feature/Locations/Ui/IndexLocationsTest.php @@ -0,0 +1,23 @@ +actingAs(User::factory()->create()) + ->get(route('locations.index')) + ->assertForbidden(); + } + + public function testUserCanListLocations() + { + $this->actingAs(User::factory()->superuser()->create()) + ->get(route('locations.index')) + ->assertOk(); + } +} diff --git a/tests/Feature/Users/Api/DeleteUserTest.php b/tests/Feature/Users/Api/DeleteUserTest.php index e8da98fe3141..49625daac35c 100644 --- a/tests/Feature/Users/Api/DeleteUserTest.php +++ b/tests/Feature/Users/Api/DeleteUserTest.php @@ -152,9 +152,4 @@ public function testUsersCannotDeleteThemselves() } - - - - - } From 97775fb79036de3ac7e18c4e6aa78c546288096f Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 08:29:00 +0100 Subject: [PATCH 34/42] Location test additions Signed-off-by: snipe --- .../Locations/Api/CreateLocationsTest.php | 22 +++++++---- .../Locations/Api/UpdateLocationsTest.php | 38 +++++++++++++++++++ 2 files changed, 52 insertions(+), 8 deletions(-) create mode 100644 tests/Feature/Locations/Api/UpdateLocationsTest.php diff --git a/tests/Feature/Locations/Api/CreateLocationsTest.php b/tests/Feature/Locations/Api/CreateLocationsTest.php index f4ac09890cc3..600ae7c61f32 100644 --- a/tests/Feature/Locations/Api/CreateLocationsTest.php +++ b/tests/Feature/Locations/Api/CreateLocationsTest.php @@ -6,25 +6,31 @@ use App\Models\User; use Tests\TestCase; -class UpdateLocationsTest extends TestCase +class CreateLocationsTest extends TestCase { - public function testCanUpdateLocationViaPatchWithoutLocationType() + public function testRequiresPermissionToCreateLocation() + { + $this->actingAsForApi(User::factory()->create()) + ->postJson(route('api.departments.store')) + ->assertForbidden(); + } + + public function testCannotCreateNewLocationsWithTheSameName() { $location = Location::factory()->create(); + $location2 = Location::factory()->create(); $this->actingAsForApi(User::factory()->superuser()->create()) - ->patchJson(route('api.locations.update', $location), [ - 'name' => 'Test Location', + ->patchJson(route('api.locations.update', $location2), [ + 'name' => $location->name, ]) ->assertOk() - ->assertStatusMessageIs('success') + ->assertStatusMessageIs('error') ->assertStatus(200) ->json(); - $location->refresh(); - $this->assertEquals('Test Location', $location->name, 'Name was not updated'); } - + } diff --git a/tests/Feature/Locations/Api/UpdateLocationsTest.php b/tests/Feature/Locations/Api/UpdateLocationsTest.php new file mode 100644 index 000000000000..a3dd8c228c38 --- /dev/null +++ b/tests/Feature/Locations/Api/UpdateLocationsTest.php @@ -0,0 +1,38 @@ +actingAsForApi(User::factory()->create()) + ->postJson(route('api.locations.store', Location::factory()->create())) + ->assertForbidden(); + } + + public function testCanUpdateLocationViaPatch() + { + $location = Location::factory()->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->patchJson(route('api.locations.update', $location), [ + 'name' => 'Test Location', + ]) + ->assertOk() + ->assertStatusMessageIs('success') + ->assertStatus(200) + ->json(); + + $location->refresh(); + $this->assertEquals('Test Location', $location->name, 'Name was not updated'); + + } + + +} From 283f24b21346800f73dabc176cdf6635442aef89 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 08:34:17 +0100 Subject: [PATCH 35/42] Derp. Renamed file Signed-off-by: snipe --- .../Api/{UpdateCategoryTest.php => UpdateCategoriesTest.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/Feature/Categories/Api/{UpdateCategoryTest.php => UpdateCategoriesTest.php} (100%) diff --git a/tests/Feature/Categories/Api/UpdateCategoryTest.php b/tests/Feature/Categories/Api/UpdateCategoriesTest.php similarity index 100% rename from tests/Feature/Categories/Api/UpdateCategoryTest.php rename to tests/Feature/Categories/Api/UpdateCategoriesTest.php From 978a0272a5fbbd9b30610b4a0e38990795bb236b Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 08:37:31 +0100 Subject: [PATCH 36/42] Sigh. Renamed again Signed-off-by: snipe --- .../Api/{UpdateDepartmentTest.php => UpdateDepartmentsTest.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename tests/Feature/Departments/Api/{UpdateDepartmentTest.php => UpdateDepartmentsTest.php} (100%) diff --git a/tests/Feature/Departments/Api/UpdateDepartmentTest.php b/tests/Feature/Departments/Api/UpdateDepartmentsTest.php similarity index 100% rename from tests/Feature/Departments/Api/UpdateDepartmentTest.php rename to tests/Feature/Departments/Api/UpdateDepartmentsTest.php From f32bf35c2213eb7054db958e33283f14207ddcbe Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 10:50:03 +0100 Subject: [PATCH 37/42] Fixed another rename :-/ Signed-off-by: snipe --- tests/Feature/Locations/Api/IndexLocationsTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Feature/Locations/Api/IndexLocationsTest.php b/tests/Feature/Locations/Api/IndexLocationsTest.php index 38857a760713..7b3b104638e6 100644 --- a/tests/Feature/Locations/Api/IndexLocationsTest.php +++ b/tests/Feature/Locations/Api/IndexLocationsTest.php @@ -8,7 +8,7 @@ use Illuminate\Testing\Fluent\AssertableJson; use Tests\TestCase; -class LocationIndexTest extends TestCase +class IndexLocationsTest extends TestCase { public function testViewingLocationIndexRequiresAuthentication() { From a8ddb2f44a07b42d28e1db72ba1156d6efc9d1c9 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 11:29:30 +0100 Subject: [PATCH 38/42] Added non-circular tests Signed-off-by: snipe --- .../Locations/Api/CreateLocationsTest.php | 20 +++++++ .../Locations/Ui/CreateLocationsTest.php | 37 ++++++++++++ .../Locations/Ui/UpdateLocationsTest.php | 57 +++++++++++++++++++ 3 files changed, 114 insertions(+) create mode 100644 tests/Feature/Locations/Ui/CreateLocationsTest.php create mode 100644 tests/Feature/Locations/Ui/UpdateLocationsTest.php diff --git a/tests/Feature/Locations/Api/CreateLocationsTest.php b/tests/Feature/Locations/Api/CreateLocationsTest.php index 600ae7c61f32..171508b72519 100644 --- a/tests/Feature/Locations/Api/CreateLocationsTest.php +++ b/tests/Feature/Locations/Api/CreateLocationsTest.php @@ -33,4 +33,24 @@ public function testCannotCreateNewLocationsWithTheSameName() } + public function testUserCannotCreateLocationsThatAreTheirOwnParent() + { + $location = Location::factory()->create(); + + $this->actingAsForApi(User::factory()->superuser()->create()) + ->patchJson(route('api.locations.update', $location), [ + 'parent_id' => $location->id, + ]) + ->assertOk() + ->assertStatusMessageIs('error') + ->assertJson([ + 'messages' => [ + 'parent_id' => ['The parent id must not create a circular reference.'], + ], + ]) + ->json(); + + + } + } diff --git a/tests/Feature/Locations/Ui/CreateLocationsTest.php b/tests/Feature/Locations/Ui/CreateLocationsTest.php new file mode 100644 index 000000000000..5e229f104349 --- /dev/null +++ b/tests/Feature/Locations/Ui/CreateLocationsTest.php @@ -0,0 +1,37 @@ +actingAs(User::factory()->create()) + ->post(route('locations.store'), [ + 'name' => 'Test Location', + 'company_id' => Company::factory()->create()->id + ]) + ->assertForbidden(); + } + + public function testUserCanCreateLocations() + { + $this->assertFalse(Location::where('name', 'Test Location')->exists()); + + $this->actingAs(User::factory()->superuser()->create()) + ->post(route('locations.store'), [ + 'name' => 'Test Location', + 'company_id' => Company::factory()->create()->id + ]) + ->assertRedirect(route('locations.index')); + + $this->assertTrue(Location::where('name', 'Test Location')->exists()); + } + + +} diff --git a/tests/Feature/Locations/Ui/UpdateLocationsTest.php b/tests/Feature/Locations/Ui/UpdateLocationsTest.php new file mode 100644 index 000000000000..5359cd1b7195 --- /dev/null +++ b/tests/Feature/Locations/Ui/UpdateLocationsTest.php @@ -0,0 +1,57 @@ +actingAs(User::factory()->create()) + ->post(route('locations.store'), [ + 'name' => 'Test Location', + ]) + ->assertStatus(403) + ->assertForbidden(); + } + + + public function testUserCanEditLocations() + { + $location = Location::factory()->create(['name' => 'Test Location']); + $this->assertTrue(Location::where('name', 'Test Location')->exists()); + + $response = $this->actingAs(User::factory()->superuser()->create()) + ->put(route('locations.update', ['location' => $location]), [ + 'name' => 'Test Location Edited', + ]) + ->assertStatus(302) + ->assertSessionHasNoErrors() + ->assertRedirect(route('locations.index')); + + $this->followRedirects($response)->assertSee('Success'); + $this->assertTrue(Location::where('name', 'Test Location Edited')->exists()); + } + + public function testUserCannotEditLocationsToMakeThemTheirOwnParent() + { + $location = Location::factory()->create(); + + $response = $this->actingAs(User::factory()->superuser()->create()) + ->from(route('locations.edit', ['location' => $location->id])) + ->put(route('locations.update', ['location' => $location]), [ + 'name' => 'Test Location', + 'parent_id' => $location->id, + ]) + ->assertRedirect(route('locations.edit', ['location' => $location])); + + $this->followRedirects($response)->assertSee(trans('general.error')); + $this->assertFalse(Location::where('name', 'Test Location')->exists()); + } + + + +} From e98823f7fa549574efc21879cda6a04aad14ff40 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 11:54:49 +0100 Subject: [PATCH 39/42] Added 403 to sad panda page Signed-off-by: snipe --- app/Http/Middleware/CheckPermissions.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Middleware/CheckPermissions.php b/app/Http/Middleware/CheckPermissions.php index a51484f283cd..3eda3e314465 100644 --- a/app/Http/Middleware/CheckPermissions.php +++ b/app/Http/Middleware/CheckPermissions.php @@ -26,6 +26,6 @@ public function handle($request, Closure $next, $section = null) return response()->view('layouts/basic', [ 'content' => view('errors/403'), - ]); + ], 403); } } From 8c6ecd35ec4e48ab298557ca1d9ba7b3489e2081 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 5 Jul 2024 11:56:24 +0100 Subject: [PATCH 40/42] Added more basic tests Signed-off-by: snipe --- .../Components/Ui/ComponentIndexTest.php | 23 +++++++++++++++++ .../Consumables/Ui/ConsumableIndexTest.php | 23 +++++++++++++++++ tests/Feature/Groups/Ui/IndexGroupTest.php | 25 +++++++++++++++++++ .../Feature/Licenses/Ui/LicenseIndexTest.php | 23 +++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100644 tests/Feature/Components/Ui/ComponentIndexTest.php create mode 100644 tests/Feature/Consumables/Ui/ConsumableIndexTest.php create mode 100644 tests/Feature/Groups/Ui/IndexGroupTest.php create mode 100644 tests/Feature/Licenses/Ui/LicenseIndexTest.php diff --git a/tests/Feature/Components/Ui/ComponentIndexTest.php b/tests/Feature/Components/Ui/ComponentIndexTest.php new file mode 100644 index 000000000000..41b733e046ad --- /dev/null +++ b/tests/Feature/Components/Ui/ComponentIndexTest.php @@ -0,0 +1,23 @@ +actingAs(User::factory()->create()) + ->get(route('components.index')) + ->assertForbidden(); + } + + public function testUserCanListComponents() + { + $this->actingAs(User::factory()->superuser()->create()) + ->get(route('components.index')) + ->assertOk(); + } +} diff --git a/tests/Feature/Consumables/Ui/ConsumableIndexTest.php b/tests/Feature/Consumables/Ui/ConsumableIndexTest.php new file mode 100644 index 000000000000..3c438187cab6 --- /dev/null +++ b/tests/Feature/Consumables/Ui/ConsumableIndexTest.php @@ -0,0 +1,23 @@ +actingAs(User::factory()->create()) + ->get(route('consumables.index')) + ->assertForbidden(); + } + + public function testUserCanListConsumables() + { + $this->actingAs(User::factory()->superuser()->create()) + ->get(route('consumables.index')) + ->assertOk(); + } +} diff --git a/tests/Feature/Groups/Ui/IndexGroupTest.php b/tests/Feature/Groups/Ui/IndexGroupTest.php new file mode 100644 index 000000000000..a2d67f903c8e --- /dev/null +++ b/tests/Feature/Groups/Ui/IndexGroupTest.php @@ -0,0 +1,25 @@ +actingAs(User::factory()->create()) + ->get(route('groups.index')) + ->assertForbidden(); + + //$this->followRedirects($response)->assertSee('sad-panda.png'); + } + + public function testUserCanListGroups() + { + $this->actingAs(User::factory()->superuser()->create()) + ->get(route('groups.index')) + ->assertOk(); + } +} diff --git a/tests/Feature/Licenses/Ui/LicenseIndexTest.php b/tests/Feature/Licenses/Ui/LicenseIndexTest.php new file mode 100644 index 000000000000..3eb33e5ee44d --- /dev/null +++ b/tests/Feature/Licenses/Ui/LicenseIndexTest.php @@ -0,0 +1,23 @@ +actingAs(User::factory()->create()) + ->get(route('licenses.index')) + ->assertForbidden(); + } + + public function testUserCanListLicenses() + { + $this->actingAs(User::factory()->superuser()->create()) + ->get(route('licenses.index')) + ->assertOk(); + } +} From 4c19b4d831961aa9dbff1bedee561ac58abccfcb Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 8 Jul 2024 17:39:38 +0100 Subject: [PATCH 41/42] Added aria-hidden="true" to error icon for accessibility Signed-off-by: snipe --- resources/views/partials/forms/edit/company-select.blade.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/views/partials/forms/edit/company-select.blade.php b/resources/views/partials/forms/edit/company-select.blade.php index 59a4748da1e1..9aaa5cafdf1a 100644 --- a/resources/views/partials/forms/edit/company-select.blade.php +++ b/resources/views/partials/forms/edit/company-select.blade.php @@ -31,7 +31,7 @@ @endif - {!! $errors->first($fieldname, '
:message
') !!} + {!! $errors->first($fieldname, '
:message
') !!} From 202f5077dea2a096a1d62d7af442ce640c9feafb Mon Sep 17 00:00:00 2001 From: snipe Date: Mon, 8 Jul 2024 17:41:20 +0100 Subject: [PATCH 42/42] Removed return Signed-off-by: snipe --- app/Http/Controllers/LabelsController.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/Http/Controllers/LabelsController.php b/app/Http/Controllers/LabelsController.php index 98b030d85ca5..8e6ba5e2cd7b 100755 --- a/app/Http/Controllers/LabelsController.php +++ b/app/Http/Controllers/LabelsController.php @@ -21,7 +21,6 @@ class LabelsController extends Controller * Returns the Label view with test data * * @param string $labelName - * @return \Illuminate\Contracts\View\View | \Illuminate\Http\RedirectResponse * @author Grant Le Roux */ public function show(string $labelName)