forked from snipe/snipe-it
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assetcontroller cleanup (snipe#5858)
* Extract method/cleanup * Remove apiStore method that is unusued since api controllers. * Use proper model exception * Remove old user importer. This is now supported by the general importer framework. * Refactor AssetsController methods. This is a giant diff without many functional changes, mostly cosmetic. I've pulled a number of methods out of assetscontroller, preferring instead to create some more targetted controllers for related actions. I think this cleans up the file some, and suggests some places for future targetted improvement. Fix weird missing things. * Fix Unit test failing after date changes. * Pass valid string to be translated. * Some method cleanup for codacy. * Extract trait for common checkout uses and codacy fixes.
- Loading branch information
Showing
20 changed files
with
761 additions
and
1,064 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Helpers\Helper; | ||
use App\Http\Requests\AssetCheckinRequest; | ||
use App\Models\Asset; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class AssetCheckinController extends Controller | ||
{ | ||
|
||
/** | ||
* Returns a view that presents a form to check an asset back into inventory. | ||
* | ||
* @author [A. Gianotto] [<snipe@snipe.net>] | ||
* @param int $assetId | ||
* @param string $backto | ||
* @since [v1.0] | ||
* @return View | ||
*/ | ||
public function create($assetId, $backto = null) | ||
{ | ||
// Check if the asset exists | ||
if (is_null($asset = Asset::find($assetId))) { | ||
// Redirect to the asset management page with error | ||
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist')); | ||
} | ||
|
||
$this->authorize('checkin', $asset); | ||
return view('hardware/checkin', compact('asset'))->with('statusLabel_list', Helper::statusLabelList())->with('backto', $backto); | ||
} | ||
|
||
/** | ||
* Validate and process the form data to check an asset back into inventory. | ||
* | ||
* @author [A. Gianotto] [<snipe@snipe.net>] | ||
* @param AssetCheckinRequest $request | ||
* @param int $assetId | ||
* @param null $backto | ||
* @return Redirect | ||
* @since [v1.0] | ||
*/ | ||
public function store(AssetCheckinRequest $request, $assetId = null, $backto = null) | ||
{ | ||
// Check if the asset exists | ||
if (is_null($asset = Asset::find($assetId))) { | ||
// Redirect to the asset management page with error | ||
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist')); | ||
} | ||
|
||
$this->authorize('checkin', $asset); | ||
|
||
if ($asset->assignedType() == Asset::USER) { | ||
$user = $asset->assignedTo; | ||
} | ||
if (is_null($target = $asset->assignedTo)) { | ||
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkin.already_checked_in')); | ||
} | ||
|
||
$asset->expected_checkin = null; | ||
$asset->last_checkout = null; | ||
$asset->assigned_to = null; | ||
$asset->assignedTo()->disassociate($asset); | ||
$asset->assigned_type = null; | ||
$asset->accepted = null; | ||
$asset->name = e($request->get('name')); | ||
|
||
if ($request->has('status_id')) { | ||
$asset->status_id = e($request->get('status_id')); | ||
} | ||
|
||
$asset->location_id = $asset->rtd_location_id; | ||
|
||
if ($request->has('location_id')) { | ||
$asset->location_id = e($request->get('location_id')); | ||
} | ||
|
||
// Was the asset updated? | ||
if ($asset->save()) { | ||
$logaction = $asset->logCheckin($target, e(request('note'))); | ||
|
||
$data['log_id'] = $logaction->id; | ||
$data['first_name'] = get_class($target) == User::class ? $target->first_name : ''; | ||
$data['last_name'] = get_class($target) == User::class ? $target->last_name : ''; | ||
$data['item_name'] = $asset->present()->name(); | ||
$data['checkin_date'] = $logaction->created_at; | ||
$data['item_tag'] = $asset->asset_tag; | ||
$data['item_serial'] = $asset->serial; | ||
$data['note'] = $logaction->note; | ||
$data['manufacturer_name'] = $asset->model->manufacturer->name; | ||
$data['model_name'] = $asset->model->name; | ||
$data['model_number'] = $asset->model->model_number; | ||
|
||
if ($backto=='user') { | ||
return redirect()->route("users.show", $user->id)->with('success', trans('admin/hardware/message.checkin.success')); | ||
} | ||
return redirect()->route("hardware.index")->with('success', trans('admin/hardware/message.checkin.success')); | ||
} | ||
// Redirect to the asset management page with error | ||
return redirect()->route("hardware.index")->with('error', trans('admin/hardware/message.checkin.error')); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Exceptions\CheckoutNotAllowed; | ||
use App\Http\Controllers\CheckInOutRequest; | ||
use App\Http\Requests\AssetCheckoutRequest; | ||
use App\Models\Asset; | ||
use App\Models\Location; | ||
use App\Models\User; | ||
use Illuminate\Database\Eloquent\ModelNotFoundException; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Auth; | ||
|
||
class AssetCheckoutController extends Controller | ||
{ | ||
use CheckInOutRequest; | ||
/** | ||
* Returns a view that presents a form to check an asset out to a | ||
* user. | ||
* | ||
* @author [A. Gianotto] [<snipe@snipe.net>] | ||
* @param int $assetId | ||
* @since [v1.0] | ||
* @return View | ||
*/ | ||
public function create($assetId) | ||
{ | ||
// Check if the asset exists | ||
if (is_null($asset = Asset::find(e($assetId)))) { | ||
// Redirect to the asset management page with error | ||
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist')); | ||
} | ||
|
||
$this->authorize('checkout', $asset); | ||
|
||
if ($asset->availableForCheckout()) { | ||
return view('hardware/checkout', compact('asset')); | ||
} | ||
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkout.not_available')); | ||
|
||
// Get the dropdown of users and then pass it to the checkout view | ||
|
||
} | ||
|
||
/** | ||
* Validate and process the form data to check out an asset to a user. | ||
* | ||
* @author [A. Gianotto] [<snipe@snipe.net>] | ||
* @param AssetCheckoutRequest $request | ||
* @param int $assetId | ||
* @return Redirect | ||
* @since [v1.0] | ||
*/ | ||
public function store(AssetCheckoutRequest $request, $assetId) | ||
{ | ||
try { | ||
// Check if the asset exists | ||
if (!$asset = Asset::find($assetId)) { | ||
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist')); | ||
} elseif (!$asset->availableForCheckout()) { | ||
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.checkout.not_available')); | ||
} | ||
$this->authorize('checkout', $asset); | ||
$admin = Auth::user(); | ||
|
||
$target = $this->determineCheckoutTarget($asset); | ||
if ($asset->is($target)) { | ||
throw new CheckoutNotAllowed('You cannot check an asset out to itself.'); | ||
} | ||
$asset = $this->updateAssetLocation($asset, $target); | ||
|
||
$checkout_at = date("Y-m-d H:i:s"); | ||
if (($request->has('checkout_at')) && ($request->get('checkout_at')!= date("Y-m-d"))) { | ||
$checkout_at = $request->get('checkout_at'); | ||
} | ||
|
||
$expected_checkin = ''; | ||
if ($request->has('expected_checkin')) { | ||
$expected_checkin = $request->get('expected_checkin'); | ||
} | ||
|
||
if ($asset->checkOut($target, $admin, $checkout_at, $expected_checkin, e($request->get('note')), $request->get('name'))) { | ||
return redirect()->route("hardware.index")->with('success', trans('admin/hardware/message.checkout.success')); | ||
} | ||
|
||
// Redirect to the asset management page with error | ||
return redirect()->to("hardware/$assetId/checkout")->with('error', trans('admin/hardware/message.checkout.error'))->withErrors($asset->getErrors()); | ||
} catch (ModelNotFoundException $e) { | ||
return redirect()->back()->with('error', trans('admin/hardware/message.checkout.error'))->withErrors($asset->getErrors()); | ||
} catch (CheckoutNotAllowed $e) { | ||
return redirect()->back()->with('error', $e->getMessage()); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers; | ||
|
||
use App\Helpers\Helper; | ||
use App\Http\Requests\AssetFileRequest; | ||
use App\Models\Actionlog; | ||
use App\Models\Asset; | ||
use Illuminate\Http\Request; | ||
use Illuminate\Support\Facades\Response; | ||
|
||
class AssetFilesController extends Controller | ||
{ | ||
/** | ||
* Upload a file to the server. | ||
* | ||
* @author [A. Gianotto] [<snipe@snipe.net>] | ||
* @param AssetFileRequest $request | ||
* @param int $assetId | ||
* @return Redirect | ||
* @since [v1.0] | ||
*/ | ||
public function store(AssetFileRequest $request, $assetId = null) | ||
{ | ||
if (!$asset = Asset::find($assetId)) { | ||
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist')); | ||
} | ||
|
||
$this->authorize('update', $asset); | ||
|
||
$destinationPath = config('app.private_uploads').'/assets'; | ||
|
||
if ($request->hasFile('assetfile')) { | ||
foreach ($request->file('assetfile') as $file) { | ||
$extension = $file->getClientOriginalExtension(); | ||
$filename = 'hardware-'.$asset->id.'-'.str_random(8); | ||
$filename .= '-'.str_slug(basename($file->getClientOriginalName(), '.'.$extension)).'.'.$extension; | ||
$file->move($destinationPath, $filename); | ||
$asset->logUpload($filename, e($request->get('notes'))); | ||
} | ||
return redirect()->back()->with('success', trans('admin/hardware/message.upload.success')); | ||
} | ||
|
||
return redirect()->back()->with('error', trans('admin/hardware/message.upload.nofiles')); | ||
} | ||
|
||
/** | ||
* Check for permissions and display the file. | ||
* | ||
* @author [A. Gianotto] [<snipe@snipe.net>] | ||
* @param int $assetId | ||
* @param int $fileId | ||
* @since [v1.0] | ||
* @return View | ||
*/ | ||
public function show($assetId = null, $fileId = null, $download = true) | ||
{ | ||
$asset = Asset::find($assetId); | ||
// the asset is valid | ||
if (isset($asset->id)) { | ||
$this->authorize('view', $asset); | ||
|
||
if (!$log = Actionlog::find($fileId)) { | ||
return response('No matching record for that asset/file', 500) | ||
->header('Content-Type', 'text/plain'); | ||
} | ||
|
||
$file = $log->get_src('assets'); | ||
|
||
if ($log->action_type =='audit') { | ||
$file = $log->get_src('audits'); | ||
} | ||
|
||
if (!file_exists($file)) { | ||
return response('File '.$file.' not found on server', 404) | ||
->header('Content-Type', 'text/plain'); | ||
} | ||
|
||
if ($download != 'true') { | ||
if ($contents = file_get_contents($file)) { | ||
return Response::make($contents)->header('Content-Type', mime_content_type($file)); | ||
} | ||
return JsonResponse::create(["error" => "Failed validation: "], 500); | ||
} | ||
return Response::download($file); | ||
} | ||
// Prepare the error message | ||
$error = trans('admin/hardware/message.does_not_exist', ['id' => $fileId]); | ||
|
||
// Redirect to the hardware management page | ||
return redirect()->route('hardware.index')->with('error', $error); | ||
} | ||
|
||
/** | ||
* Delete the associated file | ||
* | ||
* @author [A. Gianotto] [<snipe@snipe.net>] | ||
* @param int $assetId | ||
* @param int $fileId | ||
* @since [v1.0] | ||
* @return View | ||
*/ | ||
public function destroy($assetId = null, $fileId = null) | ||
{ | ||
$asset = Asset::find($assetId); | ||
$this->authorize('update', $asset); | ||
$destinationPath = config('app.private_uploads').'/imports/assets'; | ||
|
||
// the asset is valid | ||
if (isset($asset->id)) { | ||
$this->authorize('update', $asset); | ||
|
||
$log = Actionlog::find($fileId); | ||
$full_filename = $destinationPath.'/'.$log->filename; | ||
if (file_exists($full_filename)) { | ||
unlink($destinationPath.'/'.$log->filename); | ||
} | ||
$log->delete(); | ||
return redirect()->back()->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')); | ||
} | ||
} |
Oops, something went wrong.