Skip to content

Commit

Permalink
Fixed snipe#3416 - bulk delete asset models
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed Dec 12, 2017
1 parent 1d82f80 commit 608bb1b
Show file tree
Hide file tree
Showing 9 changed files with 196 additions and 21 deletions.
7 changes: 5 additions & 2 deletions app/Console/Commands/RecryptFromMcrypt.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -81,7 +82,9 @@ public function handle()
$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');

Expand Down
94 changes: 81 additions & 13 deletions app/Http/Controllers/AssetModelsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,20 +393,39 @@ public function postBulkEdit(Request $request)

$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')
Expand All @@ -429,6 +448,7 @@ public function postBulkEditSave(Request $request)
$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');
}
Expand All @@ -455,4 +475,52 @@ public function postBulkEditSave(Request $request)

}

/**
* Validate and delete the given Asset Models. An Asset Model
* cannot be deleted if there are associated assets.
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @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'));

}

}
3 changes: 3 additions & 0 deletions resources/lang/en/admin/models/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
'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. <a href="/hardware/models/:model_id/restore">Click here to restore it</a>.',
'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',
Expand Down
6 changes: 6 additions & 0 deletions resources/lang/en/admin/models/message.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@
'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.'
),

);
92 changes: 92 additions & 0 deletions resources/views/models/bulk-delete.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
@extends('layouts/default')

{{-- Page title --}}
@section('title')
{{ trans('admin/models/general.bulk_delete') }}
@parent
@stop

@section('header_right')
<a href="{{ URL::previous() }}" class="btn btn-primary pull-right">
{{ trans('general.back') }}</a>
@stop

{{-- Page content --}}
@section('content')
<div class="row">
<!-- left column -->
<div class="col-md-8 col-md-offset-2">
<p>{{ trans('admin/models/general.bulk_delete_help') }}</p>
<form class="form-horizontal" method="post" action="{{ route('models.bulkdelete.store') }}" autocomplete="off" role="form">
{{csrf_field()}}
<div class="box box-default">
<div class="box-header with-border">
<h3 class="box-title" style="color: red">{{ trans('admin/models/general.bulk_delete_warn', ['model_count' => $valid_count]) }}</h3>
</div>

<div class="box-body">
<table class="table table-striped table-condensed">
<thead>
<tr>
<td class="col-md-1">
<label>
<input type="checkbox" class="all minimal" checked="checked">
</label>

</td>
<td class="col-md-1"><i class="fa fa-barcode"></i></td>
<td class="col-md-10">Name</td>
</tr>
</thead>
<tbody>
@foreach ($models as $model)
<tr{!! (($model->assets_count > 0 ) ? ' class="danger"' : '') !!}>
<td>
<input type="checkbox" name="ids[]" class="minimal{{ (($model->assets_count == 0) ? '' : ' disabled') }}" value="{{ $model->id }}" {!! (($model->assets_count == 0) ? ' checked="checked"' : ' disabled') !!}>
</td>
<td>{{ $model->assets_count }}</td>
<td>{{ $model->name }}</td>

</tr>
@endforeach
</tbody>
</table>
</div><!-- /.box-body -->

<div class="box-footer text-right">
<a class="btn btn-link pull-left" href="{{ URL::previous() }}" method="post" enctype="multipart/form-data">{{ trans('button.cancel') }}</a>
<button type="submit" class="btn btn-success" id="submit-button"><i class="fa fa-check icon-white"></i> {{ trans('general.delete') }}</button>
</div><!-- /.box-footer -->
</div><!-- /.box -->
</form>
</div> <!-- .col-md-12-->
</div><!--.row-->
@stop
@section('moar_scripts')
<script>
// Check-all / Uncheck all
$(function () {
var checkAll = $('input.all');
var checkboxes = $('input.minimal');
checkAll.on('ifChecked ifUnchecked', function(event) {
if (event.type == 'ifChecked') {
checkboxes.iCheck('check');
} else {
checkboxes.iCheck('uncheck');
}
});
checkboxes.on('ifChanged', function(event){
if(checkboxes.filter(':checked').length == checkboxes.length) {
checkAll.prop('checked', 'checked');
} else {
checkAll.removeProp('checked');
}
checkAll.iCheck('update');
});
});
</script>
@stop
1 change: 1 addition & 0 deletions resources/views/models/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
<div id="toolbar">
<select name="bulk_actions" class="form-control select2" style="width: 300px;">
<option value="edit">Bulk Edit</option>
<option value="delete">Bulk Delete</option>
</select>
<button class="btn btn-primary" id="bulkEdit" disabled>Go</button>
</div>
Expand Down
1 change: 1 addition & 0 deletions routes/web/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
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', [
Expand Down
Empty file modified snipeit.sh
100755 → 100644
Empty file.
13 changes: 7 additions & 6 deletions upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}


Expand Down Expand Up @@ -164,3 +164,4 @@
echo "--------------------------------------------------------\n\n";



0 comments on commit 608bb1b

Please sign in to comment.