Skip to content

Commit 3693241

Browse files
authored
Merge pull request #17959 from marcusmoore/fixes/17958-handle-force-deleted-model-in-bulk-edit
Fixes #17958 - handle accessing deleted model during bulk asset update
2 parents 291be64 + 3c3acff commit 3693241

File tree

4 files changed

+24
-5
lines changed

4 files changed

+24
-5
lines changed

app/Http/Controllers/Assets/BulkAssetsController.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ public function edit(Request $request) : View | RedirectResponse
163163
$modelNames = [];
164164

165165
foreach($models as $model) {
166-
$modelNames[] = $model->model->name;
166+
$modelNames[] = $model->model?->name;
167167
}
168168

169169
if ($request->filled('bulk_actions')) {
@@ -470,7 +470,7 @@ public function update(Request $request) : RedirectResponse
470470
*/
471471

472472
// Does the model have a fieldset?
473-
if ($asset->model->fieldset) {
473+
if ($asset->model?->fieldset) {
474474
foreach ($asset->model->fieldset->fields as $field) {
475475

476476
// null custom fields

app/Observers/AssetObserver.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public function saving(Asset $asset)
175175

176176
// determine if explicit and set eol_explicit to true
177177
if (!is_null($asset->asset_eol_date) && !is_null($asset->purchase_date)) {
178-
if($asset->model->eol > 0) {
178+
if ($asset->model?->eol > 0) {
179179
$months = (int) Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date, true);
180180
if($months != $asset->model->eol) {
181181
$asset->eol_explicit = true;
@@ -184,7 +184,7 @@ public function saving(Asset $asset)
184184
} elseif (!is_null($asset->asset_eol_date) && is_null($asset->purchase_date)) {
185185
$asset->eol_explicit = true;
186186
}
187-
if ((!is_null($asset->asset_eol_date)) && (!is_null($asset->purchase_date)) && (is_null($asset->model->eol) || ($asset->model->eol == 0))) {
187+
if ((!is_null($asset->asset_eol_date)) && (!is_null($asset->purchase_date)) && (is_null($asset->model?->eol) || ($asset->model?->eol == 0))) {
188188
$asset->eol_explicit = true;
189189
}
190190

resources/views/models/custom_fields_form_bulk_edit.blade.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
@endphp
66

77
@foreach($models as $model)
8-
@if ($model->fieldset ? $model->fieldset->count() > 0 : false)
8+
@if (($model) && ($model->fieldset ? $model->fieldset->count() > 0 : false))
99
@php
1010
$anyModelHasCustomFields++;
1111
@endphp

tests/Feature/Assets/Ui/BulkEditAssetsTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,25 @@ public function testUserWithPermissionsCanAccessPage()
2929
])->assertStatus(200);
3030
}
3131

32+
public function test_handles_model_being_deleted()
33+
{
34+
$this->withoutExceptionHandling();
35+
36+
$user = User::factory()->viewAssets()->editAssets()->create();
37+
$assets = Asset::factory()->count(2)->create();
38+
39+
$assets->first()->model->forceDelete();
40+
41+
$id_array = $assets->pluck('id')->toArray();
42+
43+
$this->actingAs($user)->post('/hardware/bulkedit', [
44+
'ids' => $id_array,
45+
'order' => 'asc',
46+
'bulk_actions' => 'edit',
47+
'sort' => 'id'
48+
])->assertStatus(200);
49+
}
50+
3251
public function test_standard_user_cannot_access_page()
3352
{
3453
$user = User::factory()->create();

0 commit comments

Comments
 (0)