Fixes #2422 - Adds bulk edit for select user attributes

This commit is contained in:
snipe 2017-03-10 20:07:44 -08:00
parent fee60ec8dc
commit ad510cecd5
9 changed files with 220 additions and 41 deletions

View file

@ -157,7 +157,7 @@ class Helper
*/
public static function companyList()
{
$company_list = array('0' => trans('general.select_company')) + DB::table('companies')
$company_list = array('' => trans('general.select_company')) + DB::table('companies')
->orderBy('name', 'asc')
->pluck('name', 'id')
->toArray();

View file

@ -405,23 +405,97 @@ class UsersController extends Controller
* @since [v1.7]
* @return \Illuminate\Contracts\View\View
*/
public function postBulkEdit()
public function postBulkEdit(Request $request)
{
$this->authorize('update', User::class);
if ((!Input::has('ids')) || (count(Input::has('ids')) == 0)) {
return redirect()->back()->with('error', 'No users selected');
} else {
$statuslabel_list = Helper::statusLabelList();
$user_raw_array = array_keys(Input::get('ids'));
$licenses = DB::table('license_seats')->whereIn('assigned_to', $user_raw_array)->get();
$users = User::whereIn('id', $user_raw_array)->with('groups', 'assignedAssets', 'licenses', 'accessories')->get();
// $users = Company::scopeCompanyables($users)->get();
$users = User::whereIn('id', $user_raw_array)->with('groups', 'assets', 'licenses', 'accessories')->get();
if ($request->input('bulk_actions')=='edit') {
return View::make('users/bulk-edit', compact('users'))
->with('location_list', Helper::locationsList())
->with('company_list', Helper::companyList())
->with('manager_list', Helper::managerList())
->with('manager_list', Helper::managerList())
->with('groups', Group::pluck('name', 'id'));
}
return View::make('users/confirm-bulk-delete', compact('users', 'statuslabel_list'));
}
}
/**
* Save bulk-edited users
*
* @author [A. Gianotto] [<snipe@snipe.net>]
* @since [v1.0]
* @return \Illuminate\Http\RedirectResponse
*/
public function postBulkEditSave(Request $request)
{
$this->authorize('update', User::class);
if ((!Input::has('ids')) || (count(Input::has('ids')) == 0)) {
return redirect()->back()->with('error', 'No users selected');
} else {
$user_raw_array = Input::get('ids');
$update_array = array();
$users = User::whereIn('id', $user_raw_array)->where('id','!=',Auth::user()->id)->get();
if ($request->has('location_id')) {
$update_array['location_id'] = $request->input('location_id');
}
if ($request->has('company_id')) {
$update_array['company_id'] = $request->input('company_id');
}
if ($request->has('manager_id')) {
// Do not allow a manager update if the selected manager is one of the users being
// edited.
if (!array_key_exists($request->input('manager_id'), $user_raw_array)) {
$update_array['manager_id'] = $request->input('manager_id');
$manager_conflict = false;
} else {
$manager_conflict = true;
}
}
if ($request->has('activated')) {
$update_array['activated'] = $request->input('activated');
}
if (count($update_array) > 0) {
User::whereIn('id', $user_raw_array)->where('id','!=',Auth::user()->id)->update($update_array);
}
// Only sync groups if groups were selected
if ($request->has('groups')) {
foreach ($users as $user) {
$user->groups()->sync($request->input('groups'));
}
}
}
if ($manager_conflict) {
return redirect()->route('users.index')
->with('warning', trans('admin/users/message.bulk_manager_warn'));
}
return redirect()->route('users.index')
->with('success', trans('admin/users/message.success.update_bulk'));
}
/**
* Soft-delete bulk users
*

View file

@ -87,6 +87,14 @@ class UserPresenter extends Presenter
"true" => false,
"formatter" => "locationsLinkObjFormatter"
],
[
"field" => "manager",
"searchable" => true,
"sortable" => true,
"title" => trans('admin/users/table.manager'),
"visible" => true,
"formatter" => "usersLinkObjFormatter"
],
[
"field" => "assets_count",
"searchable" => false,

View file

@ -4,6 +4,8 @@
return array(
'assets_user' => 'Assets assigned to :name',
'bulk_update_warn' => 'You are about to edit the properties of :user_count users. Please note that you cannot change your own user attributes using this form, and must make edits to your own user individually.',
'bulk_update_help' => 'This form allows you to update multiple users at once. Only fill in the fields you need to change. Any fields left blank will remain unchanged.',
'current_assets' => 'Assets currently checked out to this user',
'clone' => 'Clone User',
'contact_user' => 'Contact :name',

View file

@ -4,6 +4,7 @@ return array(
'accepted' => 'You have successfully accepted this asset.',
'declined' => 'You have successfully declined this asset.',
'bulk_manager_warn' => 'Your users have been successfully updated, however your manager entry was not saved because the manager you selected was also in the user list to be edited, and users may not be their own manager. Please select your users again, excluding the manager.',
'user_exists' => 'User already exists!',
'user_not_found' => 'User [:id] does not exist.',
'user_login_required' => 'The login field is required',
@ -16,6 +17,7 @@ return array(
'success' => array(
'create' => 'User was successfully created.',
'update' => 'User was successfully updated.',
'update_bulk' => 'Users were successfully updated!',
'delete' => 'User was successfully deleted.',
'ban' => 'User was successfully banned.',
'unban' => 'User was successfully unbanned.',

View file

@ -0,0 +1,121 @@
@extends('layouts/default')
{{-- Page title --}}
@section('title')
Bulk Edit
@parent
@stop
@section('header_right')
<a href="{{ URL::previous() }}" class="btn btn-sm btn-primary pull-right">
{{ trans('general.back') }}</a>
@stop
{{-- Page content --}}
@section('content')
<div class="row">
<div class="col-md-8 col-md-offset-2">
<p>{{ trans('admin/users/general.bulk_update_help') }}</p>
<div class="callout callout-warning">
<i class="fa fa-warning"></i> {{ trans('admin/users/general.bulk_update_warn', ['user_count' => count($users)]) }}
</div>
<form class="form-horizontal" method="post" action="{{ route('users/bulkeditsave') }}" autocomplete="off" role="form">
{{ csrf_field() }}
<div class="box box-default">
<div class="box-body">
<!-- Location -->
<div class="form-group {{ $errors->has('location_id') ? ' has-error' : '' }}">
<label for="status_id" class="col-md-3 control-label">
{{ trans('admin/users/table.location') }}
</label>
<div class="col-md-7">
{{ Form::select('location_id', $location_list , Input::old('rtd_location_id'), array('class'=>'select2', 'style'=>'width:350px')) }}
{!! $errors->first('status_id', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
</div>
</div>
<!-- Company -->
<div class="form-group {{ $errors->has('company_id') ? ' has-error' : '' }}">
<label for="company_id" class="col-md-3 control-label">
{{ trans('general.company') }}
</label>
<div class="col-md-7">
{{ Form::select('company_id', $company_list , Input::old('company_id'), array('class'=>'select2', 'style'=>'width:350px')) }}
{!! $errors->first('company_id', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
</div>
</div>
<!-- manager -->
<div class="form-group {{ $errors->has('manager_id') ? ' has-error' : '' }}">
<label for="manager_id" class="col-md-3 control-label">
{{ trans('admin/users/table.manager') }}
</label>
<div class="col-md-7">
{{ Form::select('manager_id', $manager_list , Input::old('manager_id'), array('class'=>'select2', 'style'=>'width:350px')) }}
{!! $errors->first('manager_id', '<span class="alert-msg"><i class="fa fa-times"></i> :message</span>') !!}
</div>
</div>
<!-- activated -->
<div class="form-group">
<div class="col-sm-3 control-label">
Activated
</div>
<div class="col-sm-9">
<div class="checkbox">
<label for="activated">
{{ Form::radio('activated', '', true) }} Do not change activation status <br>
{{ Form::radio('activated', '1', Input::old('activated')) }} User is activated<br>
{{ Form::radio('activated', '0', Input::old('activated')) }} User is de-activated
</label>
</div>
</div>
</div> <!--/form-group-->
<!-- Groups -->
<div class="form-group{{ $errors->has('groups') ? ' has-error' : '' }}">
<label class="col-md-3 control-label" for="groups"> {{ trans('general.groups') }}</label>
<div class="col-md-6">
@if ((Config::get('app.lock_passwords') || (!Auth::user()->isSuperUser())))
<span class="help-block">Only superadmins may edit group memberships.</p>
@else
<div class="controls">
<select name="groups[]" id="groups[]" multiple="multiple" class="form-control">
@foreach ($groups as $id => $group)
<option value="{{ $id }}">{{ $group }} </option>
@endforeach
</select>
<span class="help-block">
{{ trans('admin/users/table.groupnotes') }}
</span>
</div> <!--/controls-->
@endif
</div> <!--/col-md-5-->
</div>
@foreach ($users as $user)
<input type="hidden" name="ids[{{ $user->id }}]" value="{{ $user->id }}">
@endforeach
</div> <!--/.box-body-->
<div class="box-footer text-right">
<button type="submit" class="btn btn-success"><i class="fa fa-check icon-white"></i> {{ trans('general.save') }}</button>
</div>
</div> <!--/.box.box-default-->
</form>
</div> <!--/.col-md-8-->
</div>
@stop

View file

@ -357,41 +357,6 @@
</div>
</div>
<!-- Groups -->
<div class="form-group{{ $errors->has('groups') ? ' has-error' : '' }}">
<label class="col-md-3 control-label" for="groups"> {{ trans('general.groups') }}</label>
<div class="col-md-5">
@if ((Config::get('app.lock_passwords') || (!Auth::user()->isSuperUser())))
@if (count($userGroups->keys()) > 0)
<ul>
@foreach ($groups as $id => $group)
{!! ($userGroups->keys()->contains($id) ? '<li>'.e($group).'</li>' : '') !!}
@endforeach
</ul>
@endif
<span class="help-block">Only superadmins may edit group memberships.</p>
@else
<div class="controls">
<select
name="groups[]"
id="groups[]"
multiple="multiple"
class="form-control"
>
@foreach ($groups as $id => $group)
<option value="{{ $id }}" {{ ($userGroups->keys()->contains($id) ? ' selected="selected"' : '') }}>
{{ $group }}
</option>
@endforeach
</select>
<span class="help-block">
{{ trans('admin/users/table.groupnotes') }}
</span>
</div> <!--/controls-->
@endif
</div> <!--/col-md-5-->
</div>
<!-- Email user -->
@if (!$user->id)

View file

@ -50,6 +50,7 @@
<div id="toolbar">
<select name="bulk_actions" class="form-control select2" style="width: 200px;">
<option value="delete">Bulk Checkin &amp; Delete</option>
<option value="edit">Bulk Edit</option>
</select>
<button class="btn btn-default" id="bulkEdit" disabled>Go</button>
</div>
@ -59,10 +60,9 @@
<table
name="users"
data-toolbar="#toolbar"
data-toggle="table"
class="table table-striped snipe-table"
id="table"
data-maintain-selected="true"
data-toggle="table"
data-url="{{ route('api.users.index',
array(''=>e(Input::get('status')),'company_id'=>e(Input::get('company_id')))) }}"
data-cookie="true"

View file

@ -36,6 +36,13 @@ Route::group([ 'prefix' => 'users', 'middleware' => ['web','auth']], function ()
'uses' => 'UsersController@postBulkSave',
]
);
Route::post(
'bulkeditsave',
[
'as' => 'users/bulkeditsave',
'uses' => 'UsersController@postBulkEditSave',
]
);
});