Skip to content

Commit f68813a

Browse files
committed
Merge remote-tracking branch 'origin/develop'
2 parents 37a90d0 + 6dceefb commit f68813a

File tree

6 files changed

+109
-42
lines changed

6 files changed

+109
-42
lines changed

app/Exceptions/Handler.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ public function render($request, Throwable $e)
143143
->withInput();
144144
}
145145

146-
// This gets the MVC model name from the exception and formats in a way that's less fugly
147-
$model_name = strtolower(implode(" ", preg_split('/(?=[A-Z])/', last(explode('\\', $e->getModel())))));
146+
// This gets the MVC model name from the exception and formats in a way that's less fugly
147+
$model_name = trim(strtolower(implode(" ", preg_split('/(?=[A-Z])/', last(explode('\\', $e->getModel()))))));
148148
$route = str_plural(strtolower(last(explode('\\', $e->getModel())))).'.index';
149149

150150
// Sigh.
@@ -160,9 +160,7 @@ public function render($request, Throwable $e)
160160
$route = 'maintenances.index';
161161
} elseif ($route === 'licenseseats.index') {
162162
$route = 'licenses.index';
163-
} elseif ($route === 'customfields.index') {
164-
$route = 'fields.index';
165-
} elseif ($route === 'customfieldsets.index') {
163+
} elseif (($route === 'customfieldsets.index') || ($route === 'customfields.index')) {
166164
$route = 'fields.index';
167165
}
168166

app/Http/Controllers/CustomFieldsController.php

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,9 @@ public function store(CustomFieldRequest $request) : RedirectResponse
144144
*/
145145
public function deleteFieldFromFieldset($field_id, $fieldset_id) : RedirectResponse
146146
{
147+
$this->authorize('update', CustomField::class);
147148
$field = CustomField::find($field_id);
148149

149-
$this->authorize('update', $field);
150-
151150
// Check that the field exists - this is mostly related to the demo, where we
152151
// rewrite the data every x minutes, so it's possible someone might be disassociating
153152
// a field from a fieldset just as we're wiping the database
@@ -157,11 +156,12 @@ public function deleteFieldFromFieldset($field_id, $fieldset_id) : RedirectRespo
157156
return redirect()->route('fieldsets.show', ['fieldset' => $fieldset_id])
158157
->with('success', trans('admin/custom_fields/message.field.delete.success'));
159158
} else {
160-
return redirect()->back()->withErrors(['message' => "Field is in use and cannot be deleted."]);
159+
return redirect()->back()->with('error', trans('admin/custom_fields/message.field.delete.error'))
160+
->withInput();
161161
}
162162
}
163163

164-
return redirect()->back()->withErrors(['message' => "Error deleting field from fieldset"]);
164+
return redirect()->back()->with('error', trans('admin/custom_fields/message.field.delete.error'));
165165

166166

167167
}
@@ -172,20 +172,16 @@ public function deleteFieldFromFieldset($field_id, $fieldset_id) : RedirectRespo
172172
* @author [Brady Wetherington] [<[email protected]>]
173173
* @since [v1.8]
174174
*/
175-
public function destroy($field_id) : RedirectResponse
175+
public function destroy(CustomField $field) : RedirectResponse
176176
{
177-
if ($field = CustomField::find($field_id)) {
178-
$this->authorize('delete', $field);
177+
$this->authorize('delete', CustomField::class);
179178

180-
if (($field->fieldset) && ($field->fieldset->count() > 0)) {
181-
return redirect()->back()->withErrors(['message' => 'Field is in-use']);
182-
}
183-
$field->delete();
184-
return redirect()->route("fields.index")
185-
->with("success", trans('admin/custom_fields/message.field.delete.success'));
179+
if (($field->fieldset) && ($field->fieldset->count() > 0)) {
180+
return redirect()->back()->with('error', trans('admin/custom_fields/message.field.delete.in_use'));
186181
}
187-
188-
return redirect()->back()->withErrors(['message' => 'Field does not exist']);
182+
$field->delete();
183+
return redirect()->route("fields.index")
184+
->with("success", trans('admin/custom_fields/message.field.delete.success'));
189185
}
190186

191187

@@ -198,7 +194,7 @@ public function destroy($field_id) : RedirectResponse
198194
*/
199195
public function edit(Request $request, CustomField $field) : View | RedirectResponse
200196
{
201-
$this->authorize('update', $field);
197+
$this->authorize('update', CustomField::class);
202198
$fieldsets = CustomFieldset::get();
203199
$customFormat = '';
204200
if ((stripos($field->format, 'regex') === 0) && ($field->format !== CustomField::PREDEFINED_FORMATS['MAC'])) {
@@ -228,7 +224,7 @@ public function edit(Request $request, CustomField $field) : View | RedirectResp
228224
*/
229225
public function update(CustomFieldRequest $request, CustomField $field) : RedirectResponse
230226
{
231-
$this->authorize('update', $field);
227+
$this->authorize('update', CustomField::class);
232228
$show_in_email = $request->get("show_in_email", 0);
233229
$display_in_user_view = $request->get("display_in_user_view", 0);
234230

@@ -265,7 +261,6 @@ public function update(CustomFieldRequest $request, CustomField $field) : Redire
265261

266262
if ($field->save()) {
267263

268-
269264
// Sync fields with fieldsets
270265
$fieldset_array = $request->input('associate_fieldsets');
271266
if ($request->has('associate_fieldsets') && (is_array($fieldset_array))) {

app/Policies/SnipePermissionsPolicy.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ public function index(User $user)
8585
}
8686

8787
/**
88-
* Determine whether the user can view the accessory.
88+
* Determine whether the user can view the model.
8989
*
9090
* @param \App\Models\User $user
9191
* @return mixed
@@ -101,7 +101,7 @@ public function files(User $user, $item = null)
101101
}
102102

103103
/**
104-
* Determine whether the user can create accessories.
104+
* Determine whether the user can create model.
105105
*
106106
* @param \App\Models\User $user
107107
* @return mixed
@@ -112,7 +112,7 @@ public function create(User $user)
112112
}
113113

114114
/**
115-
* Determine whether the user can update the accessory.
115+
* Determine whether the user can update the model.
116116
*
117117
* @param \App\Models\User $user
118118
* @return mixed
@@ -124,7 +124,7 @@ public function update(User $user, $item = null)
124124

125125

126126
/**
127-
* Determine whether the user can update the accessory.
127+
* Determine whether the user can update the model.
128128
*
129129
* @param \App\Models\User $user
130130
* @return mixed
@@ -135,7 +135,7 @@ public function checkout(User $user, $item = null)
135135
}
136136

137137
/**
138-
* Determine whether the user can delete the accessory.
138+
* Determine whether the user can delete the model.
139139
*
140140
* @param \App\Models\User $user
141141
* @return mixed
@@ -151,7 +151,7 @@ public function delete(User $user, $item = null)
151151
}
152152

153153
/**
154-
* Determine whether the user can manage the accessory.
154+
* Determine whether the user can manage the model.
155155
*
156156
* @param \App\Models\User $user
157157
* @return mixed

database/factories/UserFactory.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,17 @@ public function canImport()
351351
return $this->appendPermission(['import' => '1']);
352352
}
353353

354+
public function createCustomFields()
355+
{
356+
return $this->appendPermission(['customfields.create' => '1']);
357+
}
358+
359+
public function viewCustomFields()
360+
{
361+
return $this->appendPermission(['customfields.view' => '1']);
362+
}
363+
364+
354365
public function deleteCustomFields()
355366
{
356367
return $this->appendPermission(['customfields.delete' => '1']);

resources/views/custom_fields/index.blade.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -84,15 +84,12 @@ class="table table-striped snipe-table"
8484
@endcan
8585

8686
@can('delete', $fieldset)
87-
<form method="POST" action="{{ route('fieldsets.destroy', $fieldset->id) }}" accept-charset="UTF-8" style="display:inline-block">
88-
{{ method_field('DELETE') }}
89-
@csrf
87+
9088
@if($fieldset->models->count() > 0)
9189
<button type="submit" class="btn btn-danger btn-sm disabled" data-tooltip="true" title="{{ trans('general.cannot_be_deleted') }}" disabled><i class="fas fa-trash"></i></button>
9290
@else
93-
<button type="submit" class="btn btn-danger btn-sm delete-asset" data-tooltip="true" title="{{ trans('general.delete') }}" data-toggle="modal" data-title="{{ trans('general.delete') }}" data-content="{{ trans('general.sure_to_delete_var', ['item' => $fieldset->name]) }}" data-icon="fa fa-trash" data-target="#dataConfirmModal" onClick="return false;"><i class="fas fa-trash"></i></button>
91+
<a type="submit" href="{{ route('fieldsets.destroy', $fieldset) }}" class="btn btn-danger btn-sm delete-asset" data-tooltip="true" title="{{ trans('general.delete') }}" data-toggle="modal" data-title="{{ trans('general.delete') }}" data-content="{{ trans('general.sure_to_delete_var', ['item' => $fieldset->name]) }}" data-icon="fa fa-trash" data-target="#dataConfirmModal" onClick="return false;"><i class="fas fa-trash"></i></a>
9492
@endif
95-
</form>
9693
@endcan
9794
</nobr>
9895
</td>
@@ -237,9 +234,6 @@ class="sr-only">{{ trans('admin/custom_fields/general.unique') }}</span></i></th
237234
</td>
238235
<td>
239236
<nobr>
240-
<form method="POST" action="{{ route('fields.destroy', $field->id) }}" accept-charset="UTF-8" style="display:inline-block">
241-
{{ method_field('DELETE') }}
242-
@csrf
243237
@can('update', $field)
244238
<a href="{{ route('fields.edit', $field->id) }}" class="btn btn-warning btn-sm" data-tooltip="true" title="{{ trans('general.update') }}">
245239
<i class="fas fa-pencil-alt" aria-hidden="true"></i>
@@ -249,19 +243,19 @@ class="sr-only">{{ trans('admin/custom_fields/general.unique') }}</span></i></th
249243

250244
@can('delete', $field)
251245

252-
@if($field->fieldset->count()>0)
246+
@if ($field->fieldset->count() > 0)
253247
<button type="submit" class="btn btn-danger btn-sm disabled" data-tooltip="true" title="{{ trans('general.cannot_be_deleted') }}" disabled>
254248
<i class="fas fa-trash" aria-hidden="true"></i>
255-
<span class="sr-only">{{ trans('button.delete') }}</span></button>
249+
<span class="sr-only">{{ trans('button.delete') }}</span>
250+
</button>
256251
@else
257-
<button type="submit" class="btn btn-danger btn-sm delete-asset" data-tooltip="true" title="{{ trans('general.delete') }}" data-toggle="modal" data-title="{{ trans('general.delete') }}" data-content="{{ trans('general.sure_to_delete_var', ['item' => $field->name]) }}" data-target="#dataConfirmModal" data-icon="fa fa-trash" onClick="return false;">
252+
<a href="{{ route('fields.destroy', $field) }}" class="btn btn-danger btn-sm delete-asset" data-tooltip="true" title="{{ trans('general.delete') }}" data-toggle="modal" data-title="{{ trans('general.delete') }}" data-content="{{ trans('general.sure_to_delete_var', ['item' => $field->name]) }}" data-target="#dataConfirmModal" data-icon="fa fa-trash" onClick="return false;">
258253
<i class="fas fa-trash" aria-hidden="true"></i>
259254
<span class="sr-only">{{ trans('button.delete') }}</span>
260-
</button>
255+
</a>
261256
@endif
262257

263258
@endcan
264-
</form>
265259
</nobr>
266260
</td>
267261
</tr>
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace Tests\Feature\CustomFields\Ui;
4+
5+
use App\Models\CustomField;
6+
use App\Models\CustomFieldset;
7+
use App\Models\User;
8+
use Tests\TestCase;
9+
10+
class DeleteCustomFieldsTest extends TestCase
11+
{
12+
public function testPermissionNeededToDeleteField()
13+
{
14+
$this->actingAs(User::factory()->create())
15+
->delete(route('fields.destroy', CustomField::factory()->create()))
16+
->assertForbidden();
17+
}
18+
19+
20+
public function testCanDeleteCustomField()
21+
{
22+
$field = CustomField::factory()->create();
23+
$this->assertDatabaseHas('custom_fields', ['id' => $field->id]);
24+
25+
$this->actingAs(User::factory()->deleteCustomFields()->create())
26+
->delete(route('fields.destroy', $field))
27+
->assertRedirectToRoute('fields.index')
28+
->assertStatus(302)
29+
->assertSessionHas('success');
30+
31+
$this->assertDatabaseMissing('custom_fields', ['id' => $field->id]);
32+
}
33+
34+
public function testCannotDeleteCustomFieldThatDoesNotExist()
35+
{
36+
37+
$response = $this->actingAs(User::factory()->viewCustomFields()->deleteCustomFields()->create())
38+
->delete(route('fields.destroy', '49857589'))
39+
->assertRedirect(route('fields.index'))
40+
->assertSessionHas('error');
41+
42+
$temp = $this->followRedirects($response);
43+
$temp->assertSee(trans('general.error'))->assertSee(trans('general.generic_model_not_found', ['model' => 'custom field']));
44+
45+
}
46+
47+
public function testCannotDeleteFieldThatIsAssociatedWithFieldsets()
48+
{
49+
$field = CustomField::factory()->create();
50+
$fieldset = CustomFieldset::factory()->create();
51+
52+
$this->actingAs(User::factory()->superuser()->create())
53+
->post(route('fieldsets.associate', $fieldset), [
54+
'field_id' => $field->id,
55+
]);
56+
57+
$response = $this->actingAs(User::factory()->viewCustomFields()->deleteCustomFields()->create())
58+
->from(route('fields.index'))
59+
->delete(route('fields.destroy', $field))
60+
->assertStatus(302)
61+
->assertRedirect(route('fields.index'))
62+
->assertSessionHas('error');
63+
64+
$this->followRedirects($response)->assertSee(trans('general.error'))->assertSee(trans('admin/custom_fields/message.field.delete.in_use'));
65+
66+
// Ensure the field is still in the database
67+
$this->assertDatabaseHas('custom_fields', ['id' => $field->id]);
68+
}
69+
}

0 commit comments

Comments
 (0)