Skip to content

A free open source IT asset/license management system

some good progress, lots of testing needs to be done on the new inclusion of SubstituteBindings

spencerrlonggb239b3a4dbcc
diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php
index ec8e65e5c6..0f1879ba50 100644
--- a/app/Http/Controllers/Api/AssetsController.php
+++ b/app/Http/Controllers/Api/AssetsController.php
@@ -618,78 +618,77 @@ class AssetsController extends Controller
* Accepts a POST request to update an asset
*
* @author [A. Gianotto] [<[email protected]>]
- * @param \App\Http\Requests\ImageUploadRequest $request
* @since [v4.0]
* @return \Illuminate\Http\JsonResponse
*/
public function update(UpdateAssetRequest $request, Asset $asset)
{
- $asset->update($request->validated());
-
- // TODO: how much of this should go to validator?
- ($request->filled('model_id')) ?
- $asset->model()->associate(AssetModel::find($request->get('model_id'))) : null;
- ($request->filled('rtd_location_id')) ?
- $asset->location_id = $request->get('rtd_location_id') : '';
- ($request->filled('company_id')) ?
- $asset->company_id = Company::getIdForCurrentUser($request->get('company_id')) : '';
-
- ($request->filled('rtd_location_id')) ?
- $asset->location_id = $request->get('rtd_location_id') : null;
-
- /**
- * this is here just legacy reasons. Api\AssetController
- * used image_source once to allow encoded image uploads.
- */
- if ($request->has('image_source')) {
- $request->offsetSet('image', $request->offsetGet('image_source'));
- }
-
- $asset = $request->handleImages($asset);
- $model = AssetModel::find($asset->model_id);
-
- // Update custom fields
- if (($model) && (isset($model->fieldset))) {
- foreach ($model->fieldset->fields as $field) {
- if ($request->has($field->db_column)) {
- if ($field->field_encrypted == '1') {
- if (Gate::allows('admin')) {
- $asset->{$field->db_column} = \Crypt::encrypt($request->input($field->db_column));
- }
- } else {
- $asset->{$field->db_column} = $request->input($field->db_column);
+ $asset->fill($request->validated());
+
+ // TODO: how much of this can go in the validator?
+ ($request->filled('model_id')) ?
+ $asset->model()->associate(AssetModel::find($request->get('model_id'))) : null;
+ ($request->filled('rtd_location_id')) ?
+ $asset->location_id = $request->get('rtd_location_id') : '';
+ ($request->filled('company_id')) ?
+ $asset->company_id = Company::getIdForCurrentUser($request->get('company_id')) : '';
+
+ ($request->filled('rtd_location_id')) ?
+ $asset->location_id = $request->get('rtd_location_id') : null;
+
+ /**
+ * this is here just legacy reasons. Api\AssetController
+ * used image_source once to allow encoded image uploads.
+ */
+ if ($request->has('image_source')) {
+ $request->offsetSet('image', $request->offsetGet('image_source'));
+ }
+
+ $asset = $request->handleImages($asset);
+ $model = AssetModel::find($asset->model_id);
+
+ // Update custom fields
+ if (($model) && (isset($model->fieldset))) {
+ foreach ($model->fieldset->fields as $field) {
+ if ($request->has($field->db_column)) {
+ if ($field->field_encrypted == '1') {
+ if (Gate::allows('admin')) {
+ $asset->{$field->db_column} = \Crypt::encrypt($request->input($field->db_column));
}
+ } else {
+ $asset->{$field->db_column} = $request->input($field->db_column);
}
}
}
+ }
- if ($asset->save()) {
- if (($request->filled('assigned_user')) && ($target = User::find($request->get('assigned_user')))) {
- $location = $target->location_id;
- } elseif (($request->filled('assigned_asset')) && ($target = Asset::find($request->get('assigned_asset')))) {
- $location = $target->location_id;
-
- Asset::where('assigned_type', \App\Models\Asset::class)->where('assigned_to', $id)
- ->update(['location_id' => $target->location_id]);
- } elseif (($request->filled('assigned_location')) && ($target = Location::find($request->get('assigned_location')))) {
- $location = $target->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);
- }
+ if ($asset->save()) {
+ if (($request->filled('assigned_user')) && ($target = User::find($request->get('assigned_user')))) {
+ $location = $target->location_id;
+ } elseif (($request->filled('assigned_asset')) && ($target = Asset::find($request->get('assigned_asset')))) {
+ $location = $target->location_id;
+
+ Asset::where('assigned_type', \App\Models\Asset::class)->where('assigned_to', $id)
+ ->update(['location_id' => $target->location_id]);
+ } elseif (($request->filled('assigned_location')) && ($target = Location::find($request->get('assigned_location')))) {
+ $location = $target->id;
+ }
- if ($asset->image) {
- $asset->image = $asset->getImageUrl();
- }
+ 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);
+ }
- return response()->json(Helper::formatStandardApiResponse('success', $asset, trans('admin/hardware/message.update.success')));
+ if ($asset->image) {
+ $asset->image = $asset->getImageUrl();
}
- return response()->json(Helper::formatStandardApiResponse('error', null, $asset->getErrors()), 200);
+ return response()->json(Helper::formatStandardApiResponse('success', $asset, trans('admin/hardware/message.update.success')));
+ }
+
+ return response()->json(Helper::formatStandardApiResponse('error', null, $asset->getErrors()), 200);
- // TODO: confirm that everything expects a _200_ model not found exception
+ // TODO: confirm that everything expects a _200_ model not found exception
}
diff --git a/app/Http/Kernel.php b/app/Http/Kernel.php
index 36014dc7de..55340dc41a 100644
--- a/app/Http/Kernel.php
+++ b/app/Http/Kernel.php
@@ -48,6 +48,7 @@ class Kernel extends HttpKernel
'api' => [
'auth:api',
+ \Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];
diff --git a/app/Http/Requests/UpdateAssetRequest.php b/app/Http/Requests/UpdateAssetRequest.php
index f33f2d075a..7fd2826f1d 100644
--- a/app/Http/Requests/UpdateAssetRequest.php
+++ b/app/Http/Requests/UpdateAssetRequest.php
@@ -19,27 +19,12 @@ class UpdateAssetRequest extends ImageUploadRequest
public function prepareForValidation()
{
- dump($this->asset);
- $asset = $this->route('asset');
- dump($asset);
- dump($this->route()->getName());
// the following are 'required' attributes that may or may not be present on an patch request
// so supplying them here instead of doing funky array modification to the rules
- if (!$this->has('asset_tag')) {
- // TODO: not sure if i'll be able to use the route model binding here because of not-found return stuff, need to test
- $asset_tag = $this->asset->asset_tag;
- }
- if (!$this->has('model_id')) {
- $model_id = $this->asset->model_id;
- }
- if (!$this->has('status_id')) {
- $status_id = $this->asset->status_id;
- }
-
- $this->merge([
- 'asset_tag' => $asset_tag,
- 'model_id' => $model_id,
- 'status_id' => $status_id,
+ return $this->merge([
+ 'asset_tag' => $this->asset_tag ?? $this->asset->asset_tag,
+ 'model_id' => $this->model_id ?? $this->asset->model_id,
+ 'status_id' => $this->status_id ?? $this->asset->status_id,
]);
}
@@ -53,6 +38,7 @@ class UpdateAssetRequest extends ImageUploadRequest
$rules = array_merge(
(new Asset())->getRules(),
parent::rules(),
+ //['model_id' => 'required|integer|exists:models,id,deleted_at,NULL|not_array']
);
return $rules;
diff --git a/routes/api.php b/routes/api.php
index 842e6210df..9ad3ae2c13 100644
--- a/routes/api.php
+++ b/routes/api.php
@@ -1,7 +1,6 @@
<?php
use App\Http\Controllers\Api;
-// use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
@@ -533,20 +532,18 @@ Route::group(['prefix' => 'v1', 'middleware' => ['api', 'throttle:api']], functi
});
-
-
-
+ // pulling this out of resource route group to begin normalizing
+ Route::patch('/hardware/{asset}', [Api\AssetsController::class, 'update'])->name('api.assets.update');
Route::resource('hardware',
Api\AssetsController::class,
['names' => [
'index' => 'api.assets.index',
'show' => 'api.assets.show',
- 'update' => 'api.assets.update',
'store' => 'api.assets.store',
'destroy' => 'api.assets.destroy',
],
- 'except' => ['create', 'edit'],
+ 'except' => ['create', 'edit', 'update'],
'parameters' => ['asset' => 'asset_id'],
]
); // end assets API routes