mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-24 21:24:13 -08:00
Basic sanity checking on some of the API controllers
This commit is contained in:
parent
9027bd1d9d
commit
2a4bf65883
|
@ -148,6 +148,11 @@ class AssetModelsController extends Controller
|
||||||
$this->authorize('delete', AssetModel::class);
|
$this->authorize('delete', AssetModel::class);
|
||||||
$assetmodel = AssetModel::findOrFail($id);
|
$assetmodel = AssetModel::findOrFail($id);
|
||||||
$this->authorize('delete', $assetmodel);
|
$this->authorize('delete', $assetmodel);
|
||||||
|
|
||||||
|
if ($assetmodel->assets()->count() > 0) {
|
||||||
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/models/message.assoc_users')));
|
||||||
|
}
|
||||||
|
|
||||||
$assetmodel->delete();
|
$assetmodel->delete();
|
||||||
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/assetmodels/message.delete.success')));
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/assetmodels/message.delete.success')));
|
||||||
|
|
||||||
|
|
|
@ -113,7 +113,16 @@ class CategoriesController extends Controller
|
||||||
{
|
{
|
||||||
$this->authorize('delete', Category::class);
|
$this->authorize('delete', Category::class);
|
||||||
$category = Category::findOrFail($id);
|
$category = Category::findOrFail($id);
|
||||||
$this->authorize('delete', $category);
|
|
||||||
|
if ($category->has_models() > 0) {
|
||||||
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/categories/message.assoc_items', ['asset_type'=>'model'])));
|
||||||
|
} elseif ($category->accessories()->count() > 0) {
|
||||||
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/categories/message.assoc_items', ['asset_type'=>'accessory'])));
|
||||||
|
} elseif ($category->consumables()->count() > 0) {
|
||||||
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/categories/message.assoc_items', ['asset_type'=>'consumable'])));
|
||||||
|
} elseif ($category->components()->count() > 0) {
|
||||||
|
return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/categories/message.assoc_items', ['asset_type'=>'component'])));
|
||||||
|
}
|
||||||
$category->delete();
|
$category->delete();
|
||||||
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/categories/message.delete.success')));
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/categories/message.delete.success')));
|
||||||
|
|
||||||
|
|
|
@ -117,9 +117,24 @@ class CompaniesController extends Controller
|
||||||
$this->authorize('delete', Company::class);
|
$this->authorize('delete', Company::class);
|
||||||
$company = Company::findOrFail($id);
|
$company = Company::findOrFail($id);
|
||||||
$this->authorize('delete', $company);
|
$this->authorize('delete', $company);
|
||||||
|
|
||||||
|
try {
|
||||||
$company->delete();
|
$company->delete();
|
||||||
return response()
|
return response()
|
||||||
->json(Helper::formatStandardApiResponse('success', null, trans('admin/companies/message.delete.success')));
|
->json(Helper::formatStandardApiResponse('success', null, trans('admin/companies/message.delete.success')));
|
||||||
|
} catch (\Illuminate\Database\QueryException $exception) {
|
||||||
|
/*
|
||||||
|
* NOTE: This happens when there's a foreign key constraint violation
|
||||||
|
* For example when rows in other tables are referencing this company
|
||||||
|
*/
|
||||||
|
if ($exception->getCode() == 23000) {
|
||||||
|
return response()
|
||||||
|
->json(Helper::formatStandardApiResponse('error', null, trans('admin/companies/message.assoc_users')));
|
||||||
|
|
||||||
|
} else {
|
||||||
|
throw $exception;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,4 +35,26 @@ class CustomFieldsController extends Controller
|
||||||
return $fieldset->fields()->sync($fields);
|
return $fieldset->fields()->sync($fields);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete a custom field.
|
||||||
|
*
|
||||||
|
* @author [Brady Wetherington] [<uberbrady@gmail.com>]
|
||||||
|
* @since [v1.8]
|
||||||
|
* @return Redirect
|
||||||
|
*/
|
||||||
|
public function destroy($field_id)
|
||||||
|
{
|
||||||
|
$field = CustomField::find($field_id);
|
||||||
|
|
||||||
|
if ($field->fieldset->count() >0) {
|
||||||
|
return response()->json(Helper::formatStandardApiResponse('error', null, 'Field is in use.'));
|
||||||
|
} else {
|
||||||
|
$field->delete();
|
||||||
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/custom_fields/message.field.delete.success')));
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,7 @@ use Illuminate\Http\Request;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
use App\Models\Depreciation;
|
use App\Models\Depreciation;
|
||||||
use App\Models\Asset;
|
use App\Http\Transformers\DepreciationsTransformer;
|
||||||
use App\Http\Transformers\DatatablesTransformer;
|
|
||||||
|
|
||||||
class DepreciationsController extends Controller
|
class DepreciationsController extends Controller
|
||||||
{
|
{
|
||||||
|
@ -23,7 +22,7 @@ class DepreciationsController extends Controller
|
||||||
$this->authorize('view', Depreciation::class);
|
$this->authorize('view', Depreciation::class);
|
||||||
$allowed_columns = ['id','name','created_at'];
|
$allowed_columns = ['id','name','created_at'];
|
||||||
|
|
||||||
$depreciations = Depreciation::select('id','name','months');
|
$depreciations = Depreciation::select('id','name','months','user_id','created_at','updated_at');
|
||||||
|
|
||||||
if ($request->has('search')) {
|
if ($request->has('search')) {
|
||||||
$depreciations = $depreciations->TextSearch($request->input('search'));
|
$depreciations = $depreciations->TextSearch($request->input('search'));
|
||||||
|
@ -37,7 +36,7 @@ class DepreciationsController extends Controller
|
||||||
|
|
||||||
$total = $depreciations->count();
|
$total = $depreciations->count();
|
||||||
$depreciations = $depreciations->skip($offset)->take($limit)->get();
|
$depreciations = $depreciations->skip($offset)->take($limit)->get();
|
||||||
return (new DatatablesTransformer)->transformDatatables($depreciations, $total);
|
return (new DepreciationsTransformer)->transformDepreciations($depreciations, $total);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -74,7 +73,7 @@ class DepreciationsController extends Controller
|
||||||
{
|
{
|
||||||
$this->authorize('view', Depreciation::class);
|
$this->authorize('view', Depreciation::class);
|
||||||
$depreciation = Depreciation::findOrFail($id);
|
$depreciation = Depreciation::findOrFail($id);
|
||||||
return $depreciation;
|
return (new DepreciationsTransformer)->transformDepreciation($depreciation);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -113,6 +112,11 @@ class DepreciationsController extends Controller
|
||||||
$this->authorize('delete', Depreciation::class);
|
$this->authorize('delete', Depreciation::class);
|
||||||
$depreciation = Depreciation::findOrFail($id);
|
$depreciation = Depreciation::findOrFail($id);
|
||||||
$this->authorize('delete', $depreciation);
|
$this->authorize('delete', $depreciation);
|
||||||
|
|
||||||
|
if ($depreciation->has_models() > 0) {
|
||||||
|
return response()->json(Helper::formatStandardApiResponse('error', trans('admin/depreciations/message.assoc_users')));
|
||||||
|
}
|
||||||
|
|
||||||
$depreciation->delete();
|
$depreciation->delete();
|
||||||
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/depreciations/message.delete.success')));
|
return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/depreciations/message.delete.success')));
|
||||||
|
|
||||||
|
|
43
app/Http/Transformers/DepreciationsTransformer.php
Normal file
43
app/Http/Transformers/DepreciationsTransformer.php
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
<?php
|
||||||
|
namespace App\Http\Transformers;
|
||||||
|
|
||||||
|
use App\Models\Depreciation;
|
||||||
|
use Illuminate\Database\Eloquent\Collection;
|
||||||
|
use Gate;
|
||||||
|
use App\Helpers\Helper;
|
||||||
|
|
||||||
|
class DepreciationsTransformer
|
||||||
|
{
|
||||||
|
|
||||||
|
public function transformDepreciations (Collection $depreciations)
|
||||||
|
{
|
||||||
|
$array = array();
|
||||||
|
foreach ($depreciations as $depreciation) {
|
||||||
|
$array[] = self::transformDepreciation($depreciation);
|
||||||
|
}
|
||||||
|
return (new DatatablesTransformer)->transformDatatables($array);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function transformDepreciation (Depreciation $depreciation)
|
||||||
|
{
|
||||||
|
$array = [
|
||||||
|
'id' => e($depreciation->id),
|
||||||
|
'name' => e($depreciation->name),
|
||||||
|
'months' => $depreciation->months . ' '. trans('general.months'),
|
||||||
|
'created_at' => Helper::getFormattedDateObject($depreciation->created_at, 'datetime'),
|
||||||
|
'updated_at' => Helper::getFormattedDateObject($depreciation->updated_at, 'datetime'),
|
||||||
|
];
|
||||||
|
|
||||||
|
$permissions_array['available_actions'] = [
|
||||||
|
'update' => Gate::allows('update', Depreciation::class) ? true : false,
|
||||||
|
'delete' => Gate::allows('delete', Depreciation::class) ? true : false,
|
||||||
|
];
|
||||||
|
|
||||||
|
$array += $permissions_array;
|
||||||
|
|
||||||
|
return $array;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue