Skip to content

Commit

Permalink
Fixes #851 - bulk user delete
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed Jul 25, 2015
1 parent cf3619d commit 6dea227
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 8 deletions.
68 changes: 61 additions & 7 deletions app/controllers/admin/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Sentry;
use Str;
use Validator;
use Statuslabel;
use View;
use Datatable;
use League\Csv\Reader;
Expand Down Expand Up @@ -431,15 +432,68 @@ public function getDelete($id = null)

public function postBulkEdit() {

if (!Input::has('edit_user')) {
if ((!Input::has('edit_user')) || (count(Input::has('edit_user')) == 0)) {
return Redirect::back()->with('error', 'No users selected');
} else {
$user_raw_array = Input::get('edit_user');
foreach ($user_raw_array as $user_id => $value) {
$user_ids[] = $user_id;
$statuslabel_list = array('' => Lang::get('general.select_statuslabel')) + Statuslabel::orderBy('name', 'asc')->lists('name', 'id');
$user_raw_array = Input::get('edit_user');
$users = User::whereIn('id', $user_raw_array)->with('groups')->get();
return View::make('backend/users/confirm-bulk-delete', compact('users','statuslabel_list'));

}
}

}

public function postBulkSave() {

if ((!Input::has('edit_user')) || (count(Input::has('edit_user')) == 0)) {
return Redirect::back()->with('error', 'No users selected');

} elseif ((!Input::has('status_id')) || (count(Input::has('status_id')) == 0)) {
return Redirect::route('users')->with('error', 'No status selected');
} else {

$user_raw_array = Input::get('edit_user');
$asset_array = array();

if(($key = array_search(Sentry::getId(), $user_raw_array)) !== false) {
unset($user_raw_array[$key]);
}

if (!Config::get('app.lock_passwords')) {

$assets = Asset::whereIn('assigned_to', $user_raw_array)->get();
$users = User::whereIn('id', $user_raw_array)->delete();

foreach ($assets as $asset) {

$asset_array[] = $asset->id;

// Update the asset log
$logaction = new Actionlog();
$logaction->asset_id = $asset->id;
$logaction->checkedout_to = $asset->assigned_to;
$logaction->asset_type = 'hardware';
$logaction->user_id = Sentry::getUser()->id;
$logaction->note = 'Bulk checkin';
$log = $logaction->logaction('checkin from');

$update_assets = Asset::whereIn('id', $asset_array)->update(
array(
'status_id' => e(Input::get('status_id')),
'assigned_to' => '',
));


}


return Redirect::route('users')->with('success', 'Your selected users have been deleted and their assets have been updated.');
} else {
return Redirect::back()->with('error', 'Bulk delete is not enabled in this installation');
}

return Redirect::back()->with('error', 'An error has occurred');
}

}
Expand Down Expand Up @@ -483,7 +537,7 @@ public function getRestore($id = null)
public function getView($userId = null)
{

$user = User::with('assets','assets.model','consumables','accessories','licenses','userloc')->find($userId);
$user = User::with('assets','assets.model','consumables','accessories','licenses','userloc')->withTrashed()->find($userId);

$userlog = $user->userlog->load('assetlog','consumablelog','assetlog.model','licenselog','accessorylog','userlog','adminlog');

Expand Down Expand Up @@ -764,7 +818,7 @@ public function getDatatable($status = null)
return Datatable::collection($users)
->addColumn('',function($users)
{
return '<div class="text-center"><input type="checkbox" name="edit_user['.$users->id.']" class="one_required"></div>';
return '<div class="text-center"><input type="checkbox" name="edit_user[]" value="'.$users->id.'" class="one_required"></div>';
})
->addColumn('name',function($users)
{
Expand Down
101 changes: 101 additions & 0 deletions app/views/backend/users/confirm-bulk-delete.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
@extends('backend/layouts/default')

{{-- Page title --}}
@section('title')
Bulk Edit/Delete ::
@parent
@stop

{{-- Page content --}}
@section('content')
<div class="page-header">
<h3>
Bulk Process Users

<div class="pull-right">
<a href="{{ route('users') }}" class="btn-flat gray pull-right"><i class="fa fa-arrow-circle-left icon-white"></i> @lang('general.back')</a>

</div>
</h3>
</div>


<form class="form-horizontal" role="form" method="post" action="{{ route('users/bulksave') }}">
<!-- CSRF Token -->
<input type="hidden" name="_token" value="{{ csrf_token() }}" />

<!-- Tabs Content -->
<div class="col-md-12 col-sm-12">
<!-- General tab -->

<div class="col-md-12">
<div class="alert alert-danger">
<i class="fa fa-exclamation-circle"></i>
<strong>WARNING: </strong>
You are about to delete the {{{ count($users) }}} user(s) listed below. Admin names are highlighted in red.
</div>
</div>

@if (Config::get('app.lock_passwords'))
<p>Note: This feature is disabled on the demo.</p>
@endif

<div class="table-responsive">
<table class="display table table-hover">
<thead>
<tr>
<th class="col-md-1"></th>
<th class="col-md-4">Name</th>
<th class="col-md-7">Groups</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="2">
Update all assets for these users to this status:
</td>
<td>
{{ Form::select('status_id', $statuslabel_list , Input::old('status_id'), array('class'=>'select2', 'style'=>'width:350px')) }}
</td>
</tr>
</tfoot>
<tbody>

@foreach ($users as $user)
<tr>
<td>
@if (Sentry::getId()!=$user->id)
<input type="checkbox" name="edit_user[]" value="{{{ $user->id }}}" checked="checked">
@else
<input type="checkbox" name="edit_user[]" value="{{{ $user->id }}}" disabled>
@endif
</td>
<td{{ ($user->hasAccess('admin') ? ' style="color: #b94a48; font-weight: bold"' : '') }}>
<span{{ (Sentry::getId()==$user->id ? ' style="text-decoration: line-through"' : '') }}>{{{ $user->fullName() }}}</span>

{{ (Sentry::getId()==$user->id ? ' (cannot delete yourself)' : '') }}

</td>
<td>
@foreach ($user->groups as $group)
<span class="label label-default">{{{ $group->name}}}</span>
@endforeach
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>

<!-- Form Actions -->
<div class="form-group">
<div class="col-sm-offset-2 col-sm-4">
<a class="btn btn-link" href="{{ route('users') }}">@lang('button.cancel')</a>
<button type="submit" class="btn btn-default">@lang('button.submit')</button>
</div>
</div>

</form>

@stop
1 change: 0 additions & 1 deletion app/views/backend/users/datatable.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
<tr>
<td colspan="11">
<select name="bulk_actions">
<option value="edit">Bulk Edit</option>
<option value="delete">Bulk Delete</option>
</select>
<button class="btn btn-default" id="bulkEdit" disabled>Go</button></td>
Expand Down

0 comments on commit 6dea227

Please sign in to comment.