diff --git a/app/Console/Commands/RecryptFromMcrypt.php b/app/Console/Commands/RecryptFromMcrypt.php index f4f7ab3832..a45a1c9bc4 100644 --- a/app/Console/Commands/RecryptFromMcrypt.php +++ b/app/Console/Commands/RecryptFromMcrypt.php @@ -16,7 +16,8 @@ class RecryptFromMcrypt extends Command * * @var string */ - protected $signature = 'snipeit:legacy-recrypt'; + protected $signature = 'snipeit:legacy-recrypt + {--force : Force a re-crypt of encrypted data from MCRYPT.}'; /** * The console command description. @@ -81,7 +82,9 @@ class RecryptFromMcrypt extends Command $this->error('================================!!!! WARNING !!!!================================'); $this->comment("This tool will attempt to decrypt your old Snipe-IT (mcrypt, now deprecated) encrypted data and re-encrypt it using OpenSSL. \n\nYou should only continue if you have backed up any and all old APP_KEYs and have backed up your data."); - if ($this->confirm("Are you SURE you wish to continue?")) { + $force = ($this->option('force')) ? true : false; + + if ($force || ($this->confirm("Are you SURE you wish to continue?"))) { $backup_file = 'backups/env-backups/'.'app_key-'.date('Y-m-d-gis'); diff --git a/app/Http/Controllers/AssetModelsController.php b/app/Http/Controllers/AssetModelsController.php index 3fe707c344..ca43b0c925 100755 --- a/app/Http/Controllers/AssetModelsController.php +++ b/app/Http/Controllers/AssetModelsController.php @@ -393,20 +393,39 @@ class AssetModelsController extends Controller $models_raw_array = Input::get('ids'); - if (is_array($models_raw_array)) { - $models = AssetModel::whereIn('id', $models_raw_array)->get(); - $nochange = ['NC' => 'No Change']; - $fieldset_list = $nochange + Helper::customFieldsetList(); - $depreciation_list = $nochange + Helper::depreciationList(); - $category_list = $nochange + Helper::categoryList('asset'); - $manufacturer_list = $nochange + Helper::manufacturerList(); + // Make sure some IDs have been selected + if ((is_array($models_raw_array)) && (count($models_raw_array) > 0)) { + + + $models = AssetModel::whereIn('id', $models_raw_array)->withCount('assets')->orderBy('assets_count', 'ASC')->get(); + + // If deleting.... + if ($request->input('bulk_actions')=='delete') { + $valid_count = 0; + foreach ($models as $model) { + if ($model->assets_count == 0) { + $valid_count++; + } + } + return view('models/bulk-delete', compact('models'))->with('valid_count', $valid_count); + + // Otherwise display the bulk edit screen + } else { + + $nochange = ['NC' => 'No Change']; + $fieldset_list = $nochange + Helper::customFieldsetList(); + $depreciation_list = $nochange + Helper::depreciationList(); + $category_list = $nochange + Helper::categoryList('asset'); + $manufacturer_list = $nochange + Helper::manufacturerList(); + + + return view('models/bulk-edit', compact('models')) + ->with('manufacturer_list', $manufacturer_list) + ->with('category_list', $category_list) + ->with('fieldset_list', $fieldset_list) + ->with('depreciation_list', $depreciation_list); + } - - return view('models/bulk-edit', compact('models')) - ->with('manufacturer_list', $manufacturer_list) - ->with('category_list', $category_list) - ->with('fieldset_list', $fieldset_list) - ->with('depreciation_list', $depreciation_list); } return redirect()->route('models.index') @@ -429,6 +448,7 @@ class AssetModelsController extends Controller $models_raw_array = Input::get('ids'); $update_array = array(); + if (($request->has('manufacturer_id') && ($request->input('manufacturer_id')!='NC'))) { $update_array['manufacturer_id'] = $request->input('manufacturer_id'); } @@ -455,4 +475,52 @@ class AssetModelsController extends Controller } + /** + * Validate and delete the given Asset Models. An Asset Model + * cannot be deleted if there are associated assets. + * + * @author [A. Gianotto] [] + * @since [v1.0] + * @param int $modelId + * @return Redirect + */ + public function postBulkDelete(Request $request) + { + $models_raw_array = Input::get('ids'); + + if ((is_array($models_raw_array)) && (count($models_raw_array) > 0)) { + + $models = AssetModel::whereIn('id', $models_raw_array)->withCount('assets')->get(); + + $del_error_count = 0; + $del_count = 0; + + foreach ($models as $model) { + \Log::debug($model->id); + + if ($model->assets_count > 0) { + $del_error_count++; + } else { + $model->delete(); + $del_count++; + } + } + + \Log::debug($del_count); + \Log::debug($del_error_count); + + if ($del_error_count == 0) { + return redirect()->route('models.index') + ->with('success', trans('admin/models/message.bulkdelete.success',['success_count'=> $del_count] )); + } + + return redirect()->route('models.index') + ->with('warning', trans('admin/models/message.bulkdelete.success_partial', ['fail_count'=>$del_error_count, 'success_count'=> $del_count])); + } + + return redirect()->route('models.index') + ->with('error', trans('admin/models/message.bulkdelete.error')); + + } + } diff --git a/resources/lang/en/admin/models/general.php b/resources/lang/en/admin/models/general.php index e0da09f780..7fc92ca6ae 100644 --- a/resources/lang/en/admin/models/general.php +++ b/resources/lang/en/admin/models/general.php @@ -4,6 +4,9 @@ return array( 'about_models_title' => 'About Asset Models', 'about_models_text' => 'Asset Models are a way to group identical assets. "MBP 2013", "IPhone 6s", etc.', 'deleted' => 'This model has been deleted. Click here to restore it.', + 'bulk_delete' => 'Bulk Delete Asset Models', + 'bulk_delete_help' => 'Use the checkboxes below to confirm the deletion of the selected asset models. Asset models that have assets associated with them cannot be deleted until the assets are associated with a different model.', + 'bulk_delete_warn' => 'You are about to delete :model_count asset models.', 'restore' => 'Restore Model', 'requestable' => 'Users may request this model', 'show_mac_address' => 'Show MAC address field in assets in this model', diff --git a/resources/lang/en/admin/models/message.php b/resources/lang/en/admin/models/message.php index 5b1b9a1a1b..e3b29d5b4b 100644 --- a/resources/lang/en/admin/models/message.php +++ b/resources/lang/en/admin/models/message.php @@ -33,4 +33,10 @@ return array( 'success' => 'Models updated.' ), + 'bulkdelete' => array( + 'error' => 'No models were selected, so nothing was deleted.', + 'success' => ':success_count model(s) deleted!', + 'success_partial' => ':success_count model(s) were deleted, however :fail_count were unable to be deleted because they still have assets associated with them.' + ), + ); diff --git a/resources/views/models/bulk-delete.blade.php b/resources/views/models/bulk-delete.blade.php new file mode 100644 index 0000000000..5a21db83e3 --- /dev/null +++ b/resources/views/models/bulk-delete.blade.php @@ -0,0 +1,92 @@ +@extends('layouts/default') + +{{-- Page title --}} +@section('title') + {{ trans('admin/models/general.bulk_delete') }} + @parent +@stop + +@section('header_right') + + {{ trans('general.back') }} +@stop + +{{-- Page content --}} +@section('content') +
+ +
+

{{ trans('admin/models/general.bulk_delete_help') }}

+
+ {{csrf_field()}} +
+
+

{{ trans('admin/models/general.bulk_delete_warn', ['model_count' => $valid_count]) }}

+
+ +
+ + + + + + + + + + @foreach ($models as $model) + assets_count > 0 ) ? ' class="danger"' : '') !!}> + + + + + + @endforeach + +
+ + + Name
+ assets_count == 0) ? ' checked="checked"' : ' disabled') !!}> + {{ $model->assets_count }}{{ $model->name }}
+
+ + +
+
+
+
+@stop +@section('moar_scripts') + +@stop diff --git a/resources/views/models/index.blade.php b/resources/views/models/index.blade.php index b7ab4caf2b..c1092650ca 100755 --- a/resources/views/models/index.blade.php +++ b/resources/views/models/index.blade.php @@ -45,6 +45,7 @@
diff --git a/routes/web/models.php b/routes/web/models.php index e629307b3c..25437051fe 100644 --- a/routes/web/models.php +++ b/routes/web/models.php @@ -10,6 +10,7 @@ Route::group([ 'prefix' => 'models', 'middleware' => ['auth'] ], function () { Route::get('{modelId}/custom_fields', ['as' => 'custom_fields/model','uses' => 'AssetModelsController@getCustomFields']); Route::post('bulkedit', ['as' => 'models.bulkedit.index','uses' => 'AssetModelsController@postBulkEdit']); Route::post('bulksave', ['as' => 'models.bulkedit.store','uses' => 'AssetModelsController@postBulkEditSave']); + Route::post('bulkdelete', ['as' => 'models.bulkdelete.store','uses' => 'AssetModelsController@postBulkDelete']); }); Route::resource('models', 'AssetModelsController', [ diff --git a/snipeit.sh b/snipeit.sh old mode 100755 new mode 100644 diff --git a/upgrade.php b/upgrade.php index 1370ae4f99..c4be286c45 100644 --- a/upgrade.php +++ b/upgrade.php @@ -2,14 +2,14 @@ (PHP_SAPI !== 'cli' || isset($_SERVER['HTTP_USER_AGENT'])) && die('Access denied.'); if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { - echo "Skipping user check as it is not supported on Windows\n"; + echo "Skipping user check as it is not supported on Windows\n"; } else { - $pwu_data = posix_getpwuid(posix_geteuid()); - $username = $pwu_data['name']; + $pwu_data = posix_getpwuid(posix_geteuid()); + $username = $pwu_data['name']; - if (($username=='root') || ($username=='admin')) { - die("\nERROR: This script should not be run as root/admin. Exiting.\n\n"); - } + if (($username=='root') || ($username=='admin')) { + die("\nERROR: This script should not be run as root/admin. Exiting.\n\n"); + } } @@ -164,3 +164,4 @@ echo "your upgraded Snipe-IT.\n"; echo "--------------------------------------------------------\n\n"; +