mirror of
https://github.com/snipe/snipe-it.git
synced 2024-12-25 21:54:14 -08:00
Refactorer controller restore methods
Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
parent
0e51a0935d
commit
f7ccef16e7
|
@ -4,7 +4,10 @@ namespace App\Http\Controllers;
|
||||||
|
|
||||||
use App\Helpers\Helper;
|
use App\Helpers\Helper;
|
||||||
use App\Http\Requests\ImageUploadRequest;
|
use App\Http\Requests\ImageUploadRequest;
|
||||||
|
use App\Models\Actionlog;
|
||||||
|
use App\Models\Asset;
|
||||||
use App\Models\AssetModel;
|
use App\Models\AssetModel;
|
||||||
|
use App\Models\User;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Input;
|
use Illuminate\Support\Facades\Input;
|
||||||
|
@ -209,7 +212,7 @@ class AssetModelsController extends Controller
|
||||||
$this->authorize('delete', AssetModel::class);
|
$this->authorize('delete', AssetModel::class);
|
||||||
// Check if the model exists
|
// Check if the model exists
|
||||||
if (is_null($model = AssetModel::find($modelId))) {
|
if (is_null($model = AssetModel::find($modelId))) {
|
||||||
return redirect()->route('models.index')->with('error', trans('admin/models/message.not_found'));
|
return redirect()->route('models.index')->with('error', trans('admin/models/message.does_not_exist'));
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($model->assets()->count() > 0) {
|
if ($model->assets()->count() > 0) {
|
||||||
|
@ -237,22 +240,42 @@ class AssetModelsController extends Controller
|
||||||
*
|
*
|
||||||
* @author [A. Gianotto] [<snipe@snipe.net>]
|
* @author [A. Gianotto] [<snipe@snipe.net>]
|
||||||
* @since [v1.0]
|
* @since [v1.0]
|
||||||
* @param int $modelId
|
* @param int $id
|
||||||
* @return Redirect
|
* @return Redirect
|
||||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||||
*/
|
*/
|
||||||
public function getRestore($modelId = null)
|
public function getRestore($id)
|
||||||
{
|
{
|
||||||
$this->authorize('create', AssetModel::class);
|
$this->authorize('create', AssetModel::class);
|
||||||
// Get user information
|
|
||||||
$model = AssetModel::withTrashed()->find($modelId);
|
|
||||||
|
|
||||||
if (isset($model->id)) {
|
if ($model = AssetModel::withTrashed()->find($id)) {
|
||||||
$model->restore();
|
|
||||||
|
|
||||||
return redirect()->route('models.index')->with('success', trans('admin/models/message.restore.success'));
|
if ($model->deleted_at == '') {
|
||||||
|
return redirect()->back()->with('error', trans('general.not_deleted', ['item_type' => trans('general.asset_model')]));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($model->restore()) {
|
||||||
|
$logaction = new Actionlog();
|
||||||
|
$logaction->item_type = User::class;
|
||||||
|
$logaction->item_id = $model->id;
|
||||||
|
$logaction->created_at = date('Y-m-d H:i:s');
|
||||||
|
$logaction->user_id = Auth::user()->id;
|
||||||
|
$logaction->logaction('restored');
|
||||||
|
|
||||||
|
|
||||||
|
// Redirect them to the deleted page if there are more, otherwise the section index
|
||||||
|
$deleted_models = AssetModel::onlyTrashed()->count();
|
||||||
|
if ($deleted_models > 0) {
|
||||||
|
return redirect()->back()->with('success', trans('admin/models/message.restore.success'));
|
||||||
|
}
|
||||||
|
return redirect()->route('models.index')->with('success', trans('admin/models/message.restore.success'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check validation
|
||||||
|
return redirect()->back()->with('error', trans('general.could_not_restore', ['item_type' => trans('general.asset_model'), 'error' => $model->getErrors()->first()]));
|
||||||
}
|
}
|
||||||
return redirect()->back()->with('error', trans('admin/models/message.not_found'));
|
|
||||||
|
return redirect()->back()->with('error', trans('admin/models/message.does_not_exist'));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,6 +6,7 @@ use App\Helpers\Helper;
|
||||||
use App\Http\Controllers\Controller;
|
use App\Http\Controllers\Controller;
|
||||||
use App\Http\Requests\ImageUploadRequest;
|
use App\Http\Requests\ImageUploadRequest;
|
||||||
use App\Models\Actionlog;
|
use App\Models\Actionlog;
|
||||||
|
use App\Models\Manufacturer;
|
||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use App\Models\Asset;
|
use App\Models\Asset;
|
||||||
use App\Models\AssetModel;
|
use App\Models\AssetModel;
|
||||||
|
@ -794,21 +795,31 @@ class AssetsController extends Controller
|
||||||
*/
|
*/
|
||||||
public function getRestore($assetId = null)
|
public function getRestore($assetId = null)
|
||||||
{
|
{
|
||||||
// Get asset information
|
if ($asset = Asset::withTrashed()->find($assetId)) {
|
||||||
$asset = Asset::withTrashed()->find($assetId);
|
$this->authorize('delete', $asset);
|
||||||
$this->authorize('delete', $asset);
|
|
||||||
if (isset($asset->id)) {
|
|
||||||
// Restore the asset
|
|
||||||
Asset::withTrashed()->where('id', $assetId)->restore();
|
|
||||||
|
|
||||||
$logaction = new Actionlog();
|
if ($asset->deleted_at == '') {
|
||||||
$logaction->item_type = Asset::class;
|
return redirect()->back()->with('error', trans('general.not_deleted', ['item_type' => trans('general.asset')]));
|
||||||
$logaction->item_id = $asset->id;
|
}
|
||||||
$logaction->created_at = date('Y-m-d H:i:s');
|
|
||||||
$logaction->user_id = Auth::user()->id;
|
|
||||||
$logaction->logaction('restored');
|
|
||||||
|
|
||||||
return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.restore.success'));
|
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->user_id = Auth::user()->id;
|
||||||
|
$logaction->logaction('restored');
|
||||||
|
|
||||||
|
// Redirect them to the deleted page if there are more, otherwise the section index
|
||||||
|
$deleted_assets = Asset::onlyTrashed()->count();
|
||||||
|
if ($deleted_assets > 0) {
|
||||||
|
return redirect()->back()->with('success', trans('admin/hardware/message.success.restored'));
|
||||||
|
}
|
||||||
|
return redirect()->route('hardware.index')->with('success', trans('admin/hardware/message.restore.success'));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check validation to make sure we're not restoring an asset with the same asset tag (or unique attribute) as an existing asset
|
||||||
|
return redirect()->back()->with('error', trans('general.could_not_restore', ['item_type' => trans('general.asset'), 'error' => $asset->getErrors()->first()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
|
return redirect()->route('hardware.index')->with('error', trans('admin/hardware/message.does_not_exist'));
|
||||||
|
|
|
@ -2,8 +2,12 @@
|
||||||
|
|
||||||
namespace App\Http\Controllers;
|
namespace App\Http\Controllers;
|
||||||
|
|
||||||
|
use App\Helpers\Helper;
|
||||||
use App\Http\Requests\ImageUploadRequest;
|
use App\Http\Requests\ImageUploadRequest;
|
||||||
|
use App\Models\Actionlog;
|
||||||
|
use App\Models\Asset;
|
||||||
use App\Models\Manufacturer;
|
use App\Models\Manufacturer;
|
||||||
|
use App\Models\User;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\Facades\Auth;
|
use Illuminate\Support\Facades\Auth;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
|
@ -218,22 +222,37 @@ class ManufacturersController extends Controller
|
||||||
* @return Redirect
|
* @return Redirect
|
||||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||||
*/
|
*/
|
||||||
public function restore($manufacturers_id)
|
public function restore($id)
|
||||||
{
|
{
|
||||||
$this->authorize('create', Manufacturer::class);
|
$this->authorize('delete', Manufacturer::class);
|
||||||
$manufacturer = Manufacturer::onlyTrashed()->where('id', $manufacturers_id)->first();
|
|
||||||
|
|
||||||
if ($manufacturer) {
|
if ($manufacturer = Manufacturer::withTrashed()->find($id)) {
|
||||||
|
|
||||||
|
if ($manufacturer->deleted_at == '') {
|
||||||
|
return redirect()->back()->with('error', trans('general.not_deleted', ['item_type' => trans('general.manufacturer')]));
|
||||||
|
}
|
||||||
|
|
||||||
// Not sure why this is necessary - it shouldn't fail validation here, but it fails without this, so....
|
|
||||||
$manufacturer->setValidating(false);
|
|
||||||
if ($manufacturer->restore()) {
|
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');
|
||||||
|
|
||||||
|
// Redirect them to the deleted page if there are more, otherwise the section index
|
||||||
|
$deleted_manufacturers = Manufacturer::onlyTrashed()->count();
|
||||||
|
if ($deleted_manufacturers > 0) {
|
||||||
|
return redirect()->back()->with('success', trans('admin/manufacturers/message.success.restored'));
|
||||||
|
}
|
||||||
return redirect()->route('manufacturers.index')->with('success', trans('admin/manufacturers/message.restore.success'));
|
return redirect()->route('manufacturers.index')->with('success', trans('admin/manufacturers/message.restore.success'));
|
||||||
}
|
}
|
||||||
|
|
||||||
return redirect()->back()->with('error', 'Could not restore.');
|
// Check validation to make sure we're not restoring an asset with the same asset tag (or unique attribute) as an existing asset
|
||||||
|
return redirect()->back()->with('error', trans('general.could_not_restore', ['item_type' => trans('general.manufacturer'), 'error' => $manufacturer->getErrors()->first()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
return redirect()->back()->with('error', trans('admin/manufacturers/message.does_not_exist'));
|
return redirect()->route('manufacturers.index')->with('error', trans('admin/manufacturers/message.does_not_exist'));
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,10 +7,10 @@ use App\Http\Controllers\Controller;
|
||||||
use App\Http\Controllers\UserNotFoundException;
|
use App\Http\Controllers\UserNotFoundException;
|
||||||
use App\Http\Requests\ImageUploadRequest;
|
use App\Http\Requests\ImageUploadRequest;
|
||||||
use App\Http\Requests\SaveUserRequest;
|
use App\Http\Requests\SaveUserRequest;
|
||||||
|
use App\Models\Actionlog;
|
||||||
use App\Models\Asset;
|
use App\Models\Asset;
|
||||||
use App\Models\Company;
|
use App\Models\Company;
|
||||||
use App\Models\Group;
|
use App\Models\Group;
|
||||||
use App\Models\Ldap;
|
|
||||||
use App\Models\Setting;
|
use App\Models\Setting;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Notifications\WelcomeNotification;
|
use App\Notifications\WelcomeNotification;
|
||||||
|
@ -385,18 +385,35 @@ class UsersController extends Controller
|
||||||
*/
|
*/
|
||||||
public function getRestore($id = null)
|
public function getRestore($id = null)
|
||||||
{
|
{
|
||||||
$this->authorize('update', User::class);
|
if ($user = User::withTrashed()->find($id)) {
|
||||||
// Get user information
|
$this->authorize('delete', $user);
|
||||||
if (! User::onlyTrashed()->find($id)) {
|
|
||||||
return redirect()->route('users.index')->with('error', trans('admin/users/messages.user_not_found'));
|
if ($user->deleted_at == '') {
|
||||||
|
return redirect()->back()->with('error', trans('general.not_deleted', ['item_type' => trans('general.user')]));
|
||||||
|
}
|
||||||
|
|
||||||
|
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');
|
||||||
|
|
||||||
|
// Redirect them to the deleted page if there are more, otherwise the section index
|
||||||
|
$deleted_users = User::onlyTrashed()->count();
|
||||||
|
if ($deleted_users > 0) {
|
||||||
|
return redirect()->back()->with('success', trans('admin/users/message.success.restored'));
|
||||||
|
}
|
||||||
|
return redirect()->route('users.index')->with('success', trans('admin/users/message.success.restored'));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check validation to make sure we're not restoring a user with the same username as an existing user
|
||||||
|
return redirect()->back()->with('error', trans('general.could_not_restore', ['item_type' => trans('general.user'), 'error' => $user->getErrors()->first()]));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore the user
|
return redirect()->route('users.index')->with('error', trans('admin/users/message.does_not_exist'));
|
||||||
if (User::withTrashed()->where('id', $id)->restore()) {
|
|
||||||
return redirect()->route('users.index')->with('success', trans('admin/users/message.success.restored'));
|
|
||||||
}
|
|
||||||
|
|
||||||
return redirect()->route('users.index')->with('error', 'User could not be restored.');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue