diff --git a/app/Http/Controllers/Api/AssetsController.php b/app/Http/Controllers/Api/AssetsController.php index 21a4779b46..d2e4ec1e95 100644 --- a/app/Http/Controllers/Api/AssetsController.php +++ b/app/Http/Controllers/Api/AssetsController.php @@ -754,34 +754,32 @@ class AssetsController extends Controller */ public function restore(Request $request, $assetId = null) { - // Get asset information - $asset = Asset::withTrashed()->find($assetId); - $this->authorize('delete', $asset); - if (isset($asset->id)) { + if ($asset = Asset::withTrashed()->find($assetId)) { + $this->authorize('delete', $asset); - if ($asset->deleted_at=='') { - $message = 'Asset was not deleted. No data was changed.'; + if ($asset->deleted_at == '') { + return response()->json(Helper::formatStandardApiResponse('error', trans('general.not_deleted', ['item_type' => trans('general.asset')])), 200); + } - } else { - - $message = trans('admin/hardware/message.restore.success'); - // Restore the asset - Asset::withTrashed()->where('id', $assetId)->restore(); + if ($asset->restore()) { $logaction = new Actionlog(); $logaction->item_type = Asset::class; $logaction->item_id = $asset->id; - $logaction->created_at = date("Y-m-d H:i:s"); + $logaction->created_at = date('Y-m-d H:i:s'); $logaction->user_id = Auth::user()->id; $logaction->logaction('restored'); + + return response()->json(Helper::formatStandardApiResponse('success', trans('admin/hardware/message.restore.success')), 200); } - return response()->json(Helper::formatStandardApiResponse('success', (new AssetsTransformer)->transformAsset($asset, $request), $message)); - - + // Check validation to make sure we're not restoring an asset with the same asset tag (or unique attribute) as an existing asset + return response()->json(Helper::formatStandardApiResponse('error', trans('general.could_not_restore', ['item_type' => trans('general.asset'), 'error' => $asset->getErrors()->first()])), 200); } + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/hardware/message.does_not_exist')), 200); + } /** diff --git a/app/Http/Controllers/Api/ManufacturersController.php b/app/Http/Controllers/Api/ManufacturersController.php index dadef87e26..73d585817c 100644 --- a/app/Http/Controllers/Api/ManufacturersController.php +++ b/app/Http/Controllers/Api/ManufacturersController.php @@ -6,9 +6,11 @@ use App\Helpers\Helper; use App\Http\Controllers\Controller; use App\Http\Transformers\ManufacturersTransformer; use App\Http\Transformers\SelectlistTransformer; +use App\Models\Actionlog; use App\Models\Manufacturer; use Illuminate\Http\Request; use App\Http\Requests\ImageUploadRequest; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Storage; class ManufacturersController extends Controller @@ -159,6 +161,44 @@ class ManufacturersController extends Controller } + /** + * Restore a given Manufacturer (mark as un-deleted) + * + * @author [A. Gianotto] [] + * @since [v6.3.4] + * @param int $id + * @return \Illuminate\Http\JsonResponse + * @throws \Illuminate\Auth\Access\AuthorizationException + */ + public function restore($id) + { + $this->authorize('delete', Manufacturer::class); + + if ($manufacturer = Manufacturer::withTrashed()->find($id)) { + + if ($manufacturer->deleted_at == '') { + return response()->json(Helper::formatStandardApiResponse('error', trans('general.not_deleted', ['item_type' => trans('general.manufacturer')])), 200); + } + + if ($manufacturer->restore()) { + + $logaction = new Actionlog(); + $logaction->item_type = Manufacturer::class; + $logaction->item_id = $manufacturer->id; + $logaction->created_at = date('Y-m-d H:i:s'); + $logaction->user_id = Auth::user()->id; + $logaction->logaction('restored'); + + return response()->json(Helper::formatStandardApiResponse('success', trans('admin/manufacturers/message.restore.success')), 200); + } + + // Check validation to make sure we're not restoring an item with the same unique attributes as a non-deleted one + return response()->json(Helper::formatStandardApiResponse('error', trans('general.could_not_restore', ['item_type' => trans('general.manufacturer'), 'error' => $manufacturer->getErrors()->first()])), 200); + } + + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/manufacturers/message.does_not_exist'))); + } + /** * Gets a paginated collection for the select2 menus * diff --git a/app/Http/Controllers/Api/UsersController.php b/app/Http/Controllers/Api/UsersController.php index 5a2cd7dcf1..3fd609ffd5 100644 --- a/app/Http/Controllers/Api/UsersController.php +++ b/app/Http/Controllers/Api/UsersController.php @@ -11,6 +11,7 @@ use App\Http\Transformers\ConsumablesTransformer; use App\Http\Transformers\LicensesTransformer; use App\Http\Transformers\SelectlistTransformer; use App\Http\Transformers\UsersTransformer; +use App\Models\Actionlog; use App\Models\Asset; use App\Models\Company; use App\Models\License; @@ -688,17 +689,31 @@ class UsersController extends Controller */ public function restore($userId = null) { - // Get asset information - $user = User::withTrashed()->find($userId); - $this->authorize('delete', $user); - if (isset($user->id)) { - // Restore the user - User::withTrashed()->where('id', $userId)->restore(); - return response()->json(Helper::formatStandardApiResponse('success', null, trans('admin/users/message.success.restored'))); + if ($user = User::withTrashed()->find($userId)) { + $this->authorize('delete', $user); + + if ($user->deleted_at == '') { + return response()->json(Helper::formatStandardApiResponse('error', trans('general.not_deleted', ['item_type' => trans('general.user')])), 200); + } + + if ($user->restore()) { + + $logaction = new Actionlog(); + $logaction->item_type = User::class; + $logaction->item_id = $user->id; + $logaction->created_at = date('Y-m-d H:i:s'); + $logaction->user_id = Auth::user()->id; + $logaction->logaction('restored'); + + return response()->json(Helper::formatStandardApiResponse('success', trans('admin/users/message.restore.success')), 200); + } + + // Check validation to make sure we're not restoring a user with the same username as an existing user + return response()->json(Helper::formatStandardApiResponse('error', trans('general.could_not_restore', ['item_type' => trans('general.user'), 'error' => $user->getErrors()->first()])), 200); } - - $id = $userId; - return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/users/message.user_not_found', compact('id'))), 200); + + return response()->json(Helper::formatStandardApiResponse('error', null, trans('admin/users/message.user_not_found')), 200); + } } diff --git a/app/Http/Transformers/AssetModelsTransformer.php b/app/Http/Transformers/AssetModelsTransformer.php index ff0a13048b..a7b1c6a49f 100644 --- a/app/Http/Transformers/AssetModelsTransformer.php +++ b/app/Http/Transformers/AssetModelsTransformer.php @@ -73,7 +73,7 @@ class AssetModelsTransformer $permissions_array['available_actions'] = [ 'update' => (Gate::allows('update', AssetModel::class) && ($assetmodel->deleted_at == '')), - 'delete' => (Gate::allows('delete', AssetModel::class) && ($assetmodel->assets_count == 0)), + 'delete' => $assetmodel->isDeletable(), 'clone' => (Gate::allows('create', AssetModel::class) && ($assetmodel->deleted_at == '')), 'restore' => (Gate::allows('create', AssetModel::class) && ($assetmodel->deleted_at != '')), ]; diff --git a/app/Http/Transformers/AssetsTransformer.php b/app/Http/Transformers/AssetsTransformer.php index d38eac2419..f5d5ae12b5 100644 --- a/app/Http/Transformers/AssetsTransformer.php +++ b/app/Http/Transformers/AssetsTransformer.php @@ -147,7 +147,7 @@ class AssetsTransformer 'clone' => Gate::allows('create', Asset::class) ? true : false, 'restore' => ($asset->deleted_at!='' && Gate::allows('create', Asset::class)) ? true : false, 'update' => ($asset->deleted_at=='' && Gate::allows('update', Asset::class)) ? true : false, - 'delete' => ($asset->deleted_at=='' && $asset->assigned_to =='' && Gate::allows('delete', Asset::class)) ? true : false, + 'delete' => ($asset->deleted_at=='' && $asset->assigned_to =='' && Gate::allows('delete', Asset::class) && ($asset->deleted_at == '')) ? true : false, ]; diff --git a/app/Http/Transformers/UsersTransformer.php b/app/Http/Transformers/UsersTransformer.php index 45de50fa7d..0ebaca2692 100644 --- a/app/Http/Transformers/UsersTransformer.php +++ b/app/Http/Transformers/UsersTransformer.php @@ -79,7 +79,7 @@ class UsersTransformer $permissions_array['available_actions'] = [ 'update' => (Gate::allows('update', User::class) && ($user->deleted_at == '')), - 'delete' => (Gate::allows('delete', User::class) && ($user->assets_count == 0) && ($user->licenses_count == 0) && ($user->accessories_count == 0)), + 'delete' => $user->isDeletable(), 'clone' => (Gate::allows('create', User::class) && ($user->deleted_at == '')), 'restore' => (Gate::allows('create', User::class) && ($user->deleted_at != '')), ];