Fixed bulk delete+checkin for users

This commit is contained in:
snipe 2016-05-12 21:01:31 -07:00
parent ae0e7b9078
commit a726934cde
8 changed files with 106 additions and 35 deletions

View file

@ -7,6 +7,7 @@ use App\Models\Actionlog;
use App\Models\Company; use App\Models\Company;
use App\Models\Setting; use App\Models\Setting;
use App\Models\User; use App\Models\User;
use Carbon\Carbon;
use Config; use Config;
use DB; use DB;
use Input; use Input;
@ -306,16 +307,16 @@ class AccessoriesController extends Controller
$accessory->users()->attach($accessory->id, array( $accessory->users()->attach($accessory->id, array(
'accessory_id' => $accessory->id, 'accessory_id' => $accessory->id,
'created_at' => Carbon::now(),
'user_id' => Auth::user()->id,
'assigned_to' => e(Input::get('assigned_to')))); 'assigned_to' => e(Input::get('assigned_to'))));
$admin_user = Auth::user();
$logaction = new Actionlog(); $logaction = new Actionlog();
$logaction->accessory_id = $accessory->id; $logaction->accessory_id = $accessory->id;
$logaction->checkedout_to = $accessory->assigned_to; $logaction->checkedout_to = $accessory->assigned_to;
$logaction->asset_type = 'accessory'; $logaction->asset_type = 'accessory';
$logaction->location_id = Auth::user()->location_id; $logaction->location_id = $user->location_id;
$logaction->user_id = $admin_user->id; $logaction->user_id = Auth::user()->id;
$logaction->note = e(Input::get('note')); $logaction->note = e(Input::get('note'));

View file

@ -5,6 +5,7 @@ use App\Http\Requests\SetupUserRequest;
use App\Http\Requests\AssetFileRequest; use App\Http\Requests\AssetFileRequest;
use App\Helpers\Helper; use App\Helpers\Helper;
use App\Models\Accessory; use App\Models\Accessory;
use App\Models\LicenseSeat;
use App\Models\Actionlog; use App\Models\Actionlog;
use App\Models\Asset; use App\Models\Asset;
use App\Models\Group; use App\Models\Group;
@ -29,6 +30,7 @@ use Str;
use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\JsonResponse;
use URL; use URL;
use View; use View;
use Request;
/** /**
* This controller handles all actions related to Users for * This controller handles all actions related to Users for
@ -388,9 +390,12 @@ class UsersController extends Controller
} else { } else {
$statuslabel_list = Helper::statusLabelList(); $statuslabel_list = Helper::statusLabelList();
$user_raw_array = array_keys(Input::get('edit_user')); $user_raw_array = array_keys(Input::get('edit_user'));
$licenses = DB::table('license_seats')->whereIn('assigned_to', $user_raw_array)->get();
$users = User::whereIn('id', $user_raw_array)->with('groups'); //print_r($licenses);
$users = Company::scopeCompanyables($users)->get();
$users = User::whereIn('id', $user_raw_array)->with('groups', 'assets', 'licenses','accessories')->get();
// $users = Company::scopeCompanyables($users)->get();
return View::make('users/confirm-bulk-delete', compact('users', 'statuslabel_list')); return View::make('users/confirm-bulk-delete', compact('users', 'statuslabel_list'));
} }
@ -425,11 +430,14 @@ class UsersController extends Controller
if (!config('app.lock_passwords')) { if (!config('app.lock_passwords')) {
$users = User::whereIn('id', $user_raw_array)->get();
$assets = Asset::whereIn('assigned_to', $user_raw_array)->get(); $assets = Asset::whereIn('assigned_to', $user_raw_array)->get();
$accessories = DB::table('accessories_users')->whereIn('assigned_to', $user_raw_array)->get(); $accessories = DB::table('accessories_users')->whereIn('assigned_to', $user_raw_array)->get();
$licenses = DB::table('license_seats')->whereIn('assigned_to', $user_raw_array)->get();
$license_array = array();
$accessory_array = array();
$users = User::whereIn('id', $user_raw_array);
$users = Company::scopeCompanyables($users)->delete();
foreach ($assets as $asset) { foreach ($assets as $asset) {
@ -441,10 +449,10 @@ class UsersController extends Controller
$logaction->checkedout_to = $asset->assigned_to; $logaction->checkedout_to = $asset->assigned_to;
$logaction->asset_type = 'hardware'; $logaction->asset_type = 'hardware';
$logaction->user_id = Auth::user()->id; $logaction->user_id = Auth::user()->id;
$logaction->note = 'Bulk checkin'; $logaction->note = 'Bulk checkin asset and delete user';
$log = $logaction->logaction('checkin from'); $logaction->logaction('checkin from');
$update_assets = Asset::whereIn('id', $asset_array)->update( Asset::whereIn('id', $asset_array)->update(
array( array(
'status_id' => e(Input::get('status_id')), 'status_id' => e(Input::get('status_id')),
'assigned_to' => null, 'assigned_to' => null,
@ -453,31 +461,46 @@ class UsersController extends Controller
} }
foreach ($accessories as $accessory) { foreach ($accessories as $accessory) {
$accessory_array[] = $accessory->id; $accessory_array[] = $accessory->accessory_id;
// Update the asset log // Update the asset log
$logaction = new Actionlog(); $logaction = new Actionlog();
$logaction->accessory_id = $accessory->id; $logaction->accessory_id = $accessory->id;
$logaction->checkedout_to = $accessory->assigned_to; $logaction->checkedout_to = $accessory->assigned_to;
$logaction->asset_type = 'accessory'; $logaction->asset_type = 'accessory';
$logaction->user_id = Auth::user()->id; $logaction->user_id = Auth::user()->id;
$logaction->note = 'Bulk checkin'; $logaction->note = 'Bulk checkin accessory and delete user';
$log = $logaction->logaction('checkin from'); $logaction->logaction('checkin from');
$update_accessories = DB::table('accessories_users')->whereIn('id', $accessory_array)->update(
array(
'assigned_to' => null,
)
);
} }
foreach ($licenses as $license) {
$license_array[] = $license->id;
// Update the asset log
$logaction = new Actionlog();
$logaction->accessory_id = $license->id;
$logaction->checkedout_to = $license->assigned_to;
$logaction->asset_type = 'software';
$logaction->user_id = Auth::user()->id;
$logaction->note = 'Bulk checkin license and delete user';
$logaction->logaction('checkin from');
}
LicenseSeat::whereIn('id', $license_array)->update(['assigned_to' => NULL]);
foreach ($users as $user) {
$user->accessories()->sync(array());
$user->delete();
}
return redirect()->route('users')->with('success', 'Your selected users have been deleted and their assets have been updated.'); return redirect()->route('users')->with('success', 'Your selected users have been deleted and their assets have been updated.');
} else { } else {
return redirect()->route('users')->with('error', 'Bulk delete is not enabled in this installation'); return redirect()->route('users')->with('error', 'Bulk delete is not enabled in this installation');
} }
/** @noinspection PhpUnreachableStatementInspection Known to be unreachable but kept following discussion: https://github.com/snipe/snipe-it/pull/1423 */
return redirect()->route('users')->with('error', 'An error has occurred');
} }
} }
@ -725,7 +748,7 @@ class UsersController extends Controller
try { try {
// Check if this email already exists in the system // Check if this email already exists in the system
$user = DB::table('users')->where('username', $row[2])->first(); $user = User::where('username', $row[2])->first();
if ($user) { if ($user) {
$duplicates .= $row[2] . ', '; $duplicates .= $row[2] . ', ';
} else { } else {
@ -884,7 +907,7 @@ class UsersController extends Controller
$rows[] = array( $rows[] = array(
'id' => $user->id, 'id' => $user->id,
'checkbox' =>'<div class="text-center hidden-xs hidden-sm"><input type="checkbox" name="edit_user['.e($user->id).']" class="one_required"></div>', 'checkbox' => ($status!='deleted') ? '<div class="text-center hidden-xs hidden-sm"><input type="checkbox" name="edit_user['.e($user->id).']" class="one_required"></div>' : '',
'name' => '<a title="'.e($user->fullName()).'" href="../admin/users/'.e($user->id).'/view">'.e($user->fullName()).'</a>', 'name' => '<a title="'.e($user->fullName()).'" href="../admin/users/'.e($user->id).'/view">'.e($user->fullName()).'</a>',
'email' => ($user->email!='') ? 'email' => ($user->email!='') ?
'<a href="mailto:'.e($user->email).'" class="hidden-md hidden-lg">'.e($user->email).'</a>' '<a href="mailto:'.e($user->email).'" class="hidden-md hidden-lg">'.e($user->email).'</a>'

View file

@ -62,12 +62,21 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon
} }
public function isSuperUser() { public function isSuperUser() {
$user_permissions = json_decode($this->permissions, true); if (!$user_permissions = json_decode($this->permissions, true)) {
$user_groups = $this->groups(); return false;
}
$group_array = array();
foreach ($this->groups() as $user_group) {
$group_permissions = json_decode($user_group->permissions, true);
$group_array[] = $group_permissions;
}
if ((array_key_exists('superuser', $user_permissions)) && ($user_permissions['superuser']=='1')) { if ((array_key_exists('superuser', $user_permissions)) && ($user_permissions['superuser']=='1')) {
return true; return true;
} else { } else {
if ((array_key_exists('superuser', $group_array)) && ($group_array['superuser']=='1')) {
return true;
}
return false; return false;
} }

View file

@ -18,6 +18,7 @@ $factory->defineAs(App\Models\Asset::class, 'asset', function (Faker\Generator $
'rtd_location_id' => $faker->numberBetween(1,5), 'rtd_location_id' => $faker->numberBetween(1,5),
'serial' => $faker->uuid, 'serial' => $faker->uuid,
'status_id' => 1, 'status_id' => 1,
'user_id' => 1,
'asset_tag' => $faker->unixTime('now'), 'asset_tag' => $faker->unixTime('now'),
'notes' => $faker->sentence, 'notes' => $faker->sentence,
'purchase_date' => $faker->dateTime(), 'purchase_date' => $faker->dateTime(),

View file

@ -438,10 +438,12 @@
<tr> <tr>
<td>{{ $asset->created_at }}</td> <td>{{ $asset->created_at }}</td>
<td> <td>
@if (isset($asset->adminuser->id)) {{ $asset->adminuser->fullName() }} @if ($asset->adminuser)
{{ $asset->adminuser->fullName() }}
@else @else
{{ trans('general.unknown_admin') }} @trans('general.unknown_admin')
@endif @endif
</td> </td>
<td>{{ trans('general.created_asset') }}</td> <td>{{ trans('general.created_asset') }}</td>
<td></td> <td></td>

View file

@ -2,7 +2,7 @@
{{-- Page title --}} {{-- Page title --}}
@section('title') @section('title')
Bulk Edit/Delete Bulk Checkin &amp; Delete
@parent @parent
@stop @stop
@ -45,15 +45,23 @@ Bulk Edit/Delete
<th class="col-md-1"></th> <th class="col-md-1"></th>
<th class="col-md-6">Name</th> <th class="col-md-6">Name</th>
<th class="col-md-5">Groups</th> <th class="col-md-5">Groups</th>
<th class="col-md-5">Assets</th>
<th class="col-md-5">Accessories</th>
<th class="col-md-5">Licenses</th>
</tr> </tr>
</thead> </thead>
<tfoot> <tfoot>
<tr> <tr>
<td colspan="3" class="warning"> <td colspan="6" class="warning">
{{ Form::select('status_id', $statuslabel_list , Input::old('status_id'), array('class'=>'select2', 'style'=>'width:250px')) }} {{ Form::select('status_id', $statuslabel_list , Input::old('status_id'), array('class'=>'select2', 'style'=>'width:250px')) }}
<label>Update all assets for these users to this status</label> <label>Update all assets for these users to this status</label>
</td> </td>
</tr> </tr>
<tr>
<td colspan="6" class="warning">
<label><input type="checkbox" name="edit_user['.e($user->id).']" checked> Check in all properties associated with these users</label>
</td>
</tr>
</tfoot> </tfoot>
<tbody> <tbody>
@foreach ($users as $user) @foreach ($users as $user)
@ -65,6 +73,7 @@ Bulk Edit/Delete
<input type="checkbox" name="edit_user[]" value="{{ $user->id }}" disabled> <input type="checkbox" name="edit_user[]" value="{{ $user->id }}" disabled>
@endif @endif
</td> </td>
<td> <td>
<span{{ (Auth::user()->id==$user->id ? ' style="text-decoration: line-through"' : '') }}>{{ $user->fullName() }} ({{ $user->username }})</span> <span{{ (Auth::user()->id==$user->id ? ' style="text-decoration: line-through"' : '') }}>{{ $user->fullName() }} ({{ $user->username }})</span>
@ -72,8 +81,23 @@ Bulk Edit/Delete
</td> </td>
<td> <td>
@foreach ($user->groups as $group)
<a href=" {{ config('app.url') }}/admin/groups/{{ $group->id }}'/edit" class="label label-default">
{{ $group->name }}
</a>
@endforeach
</td> </td>
<td>
{{ number_format($user->assets->count()) }}
</td>
<td>
{{ number_format($user->accessories->count()) }}
</td>
<td>
{{ number_format($user->licenses->count()) }}
</td>
</tr> </tr>
@endforeach @endforeach
</tbody> </tbody>

View file

@ -42,12 +42,14 @@
'route' => ['users/bulkedit'], 'route' => ['users/bulkedit'],
'class' => 'form-inline' ]) }} 'class' => 'form-inline' ]) }}
@if (Input::get('status')!='deleted')
<div id="toolbar"> <div id="toolbar">
<select name="bulk_actions" class="form-control select2" style="width: 200px;"> <select name="bulk_actions" class="form-control select2" style="width: 200px;">
<option value="delete">Bulk Delete</option> <option value="delete">Bulk Checkin &amp; Delete</option>
</select> </select>
<button class="btn btn-default" id="bulkEdit" disabled>Go</button> <button class="btn btn-default" id="bulkEdit" disabled>Go</button>
</div> </div>
@endif
<table <table
@ -62,7 +64,11 @@
data-cookie-id-table="userTableDisplay-{{ config('version.hash_version') }}"> data-cookie-id-table="userTableDisplay-{{ config('version.hash_version') }}">
<thead> <thead>
<tr> <tr>
<th data-class="hidden-xs hidden-sm" data-switchable="false" data-searchable="false" data-sortable="false" data-field="checkbox"><div class="text-center"><input type="checkbox" id="checkAll" style="padding-left: 0px;" style="hidden-xs hidden-sm"></div></th> <th data-class="hidden-xs hidden-sm" data-switchable="false" data-searchable="false" data-sortable="false" data-field="checkbox">
@if (Input::get('status')!='deleted')
<div class="text-center"><input type="checkbox" id="checkAll" style="padding-left: 0px;" style="hidden-xs hidden-sm"></div>
@endif
</th>
<th data-switchable="true" data-sortable="true" data-field="id" data-visible="false">{{ trans('general.id') }}</th> <th data-switchable="true" data-sortable="true" data-field="id" data-visible="false">{{ trans('general.id') }}</th>
<th data-switchable="true" data-sortable="false" data-field="companyName" data-visible="false">{{ trans('admin/companies/table.title') }}</th> <th data-switchable="true" data-sortable="false" data-field="companyName" data-visible="false">{{ trans('admin/companies/table.title') }}</th>
<th data-switchable="true" data-sortable="true" data-field="employee_num" data-visible="false">{{ trans('admin/users/table.employee_num') }}</th> <th data-switchable="true" data-sortable="true" data-field="employee_num" data-visible="false">{{ trans('admin/users/table.employee_num') }}</th>

View file

@ -132,7 +132,12 @@
<a href="{{ route('delete/user', $user->id) }}" style="width: 100%;" class="btn btn-sm btn-warning">{{ trans('button.delete') }}</a> <a href="{{ route('delete/user', $user->id) }}" style="width: 100%;" class="btn btn-sm btn-warning">{{ trans('button.delete') }}</a>
</div> </div>
<div class="col-md-12" style="padding-top: 5px;"> <div class="col-md-12" style="padding-top: 5px;">
<a href="{{ route('delete/user', $user->id) }}" style="width: 100%;" class="btn btn-sm btn-danger">{{ trans('button.checkin_and_delete') }}</a> <form action="{{ route('users/bulkedit') }}" method="POST">
<!-- CSRF Token -->
<input type="hidden" name="_token" value="{{ csrf_token() }}" />
<input type="hidden" name="edit_user[{{ $user->id }}]" value="{{ $user->id }}" />
<button style="width: 100%;" class="btn btn-sm btn-danger">{{ trans('button.checkin_and_delete') }}</button>
</form>
</div> </div>
@else @else
<div class="col-md-12" style="padding-top: 5px;"> <div class="col-md-12" style="padding-top: 5px;">