diff --git a/app/Http/Controllers/AccessoriesController.php b/app/Http/Controllers/AccessoriesController.php index bf5a601f29..5c29dc7642 100755 --- a/app/Http/Controllers/AccessoriesController.php +++ b/app/Http/Controllers/AccessoriesController.php @@ -7,6 +7,7 @@ use App\Models\Actionlog; use App\Models\Company; use App\Models\Setting; use App\Models\User; +use Carbon\Carbon; use Config; use DB; use Input; @@ -306,16 +307,16 @@ class AccessoriesController extends Controller $accessory->users()->attach($accessory->id, array( 'accessory_id' => $accessory->id, + 'created_at' => Carbon::now(), + 'user_id' => Auth::user()->id, 'assigned_to' => e(Input::get('assigned_to')))); - $admin_user = Auth::user(); - $logaction = new Actionlog(); $logaction->accessory_id = $accessory->id; $logaction->checkedout_to = $accessory->assigned_to; $logaction->asset_type = 'accessory'; - $logaction->location_id = Auth::user()->location_id; - $logaction->user_id = $admin_user->id; + $logaction->location_id = $user->location_id; + $logaction->user_id = Auth::user()->id; $logaction->note = e(Input::get('note')); diff --git a/app/Http/Controllers/UsersController.php b/app/Http/Controllers/UsersController.php index 6e4540ccd4..caaa4582d5 100755 --- a/app/Http/Controllers/UsersController.php +++ b/app/Http/Controllers/UsersController.php @@ -5,6 +5,7 @@ use App\Http\Requests\SetupUserRequest; use App\Http\Requests\AssetFileRequest; use App\Helpers\Helper; use App\Models\Accessory; +use App\Models\LicenseSeat; use App\Models\Actionlog; use App\Models\Asset; use App\Models\Group; @@ -29,6 +30,7 @@ use Str; use Symfony\Component\HttpFoundation\JsonResponse; use URL; use View; +use Request; /** * This controller handles all actions related to Users for @@ -388,9 +390,12 @@ class UsersController extends Controller } else { $statuslabel_list = Helper::statusLabelList(); $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'); - $users = Company::scopeCompanyables($users)->get(); + //print_r($licenses); + + $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')); } @@ -425,11 +430,14 @@ class UsersController extends Controller if (!config('app.lock_passwords')) { + $users = User::whereIn('id', $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(); + $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) { @@ -441,10 +449,10 @@ class UsersController extends Controller $logaction->checkedout_to = $asset->assigned_to; $logaction->asset_type = 'hardware'; $logaction->user_id = Auth::user()->id; - $logaction->note = 'Bulk checkin'; - $log = $logaction->logaction('checkin from'); + $logaction->note = 'Bulk checkin asset and delete user'; + $logaction->logaction('checkin from'); - $update_assets = Asset::whereIn('id', $asset_array)->update( + Asset::whereIn('id', $asset_array)->update( array( 'status_id' => e(Input::get('status_id')), 'assigned_to' => null, @@ -453,31 +461,46 @@ class UsersController extends Controller } foreach ($accessories as $accessory) { - $accessory_array[] = $accessory->id; + $accessory_array[] = $accessory->accessory_id; // Update the asset log $logaction = new Actionlog(); $logaction->accessory_id = $accessory->id; $logaction->checkedout_to = $accessory->assigned_to; $logaction->asset_type = 'accessory'; $logaction->user_id = Auth::user()->id; - $logaction->note = 'Bulk checkin'; - $log = $logaction->logaction('checkin from'); + $logaction->note = 'Bulk checkin accessory and delete user'; + $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.'); } else { 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 { // 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) { $duplicates .= $row[2] . ', '; } else { @@ -884,7 +907,7 @@ class UsersController extends Controller $rows[] = array( 'id' => $user->id, - 'checkbox' =>'', + 'checkbox' => ($status!='deleted') ? '' : '', 'name' => ''.e($user->fullName()).'', 'email' => ($user->email!='') ? '' diff --git a/app/Models/User.php b/app/Models/User.php index 289a5b3460..058a15e965 100755 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -62,12 +62,21 @@ class User extends Model implements AuthenticatableContract, CanResetPasswordCon } public function isSuperUser() { - $user_permissions = json_decode($this->permissions, true); - $user_groups = $this->groups(); + if (!$user_permissions = json_decode($this->permissions, true)) { + 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')) { return true; } else { + if ((array_key_exists('superuser', $group_array)) && ($group_array['superuser']=='1')) { + return true; + } return false; } diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 99afc1a899..f0432c7c8f 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -18,6 +18,7 @@ $factory->defineAs(App\Models\Asset::class, 'asset', function (Faker\Generator $ 'rtd_location_id' => $faker->numberBetween(1,5), 'serial' => $faker->uuid, 'status_id' => 1, + 'user_id' => 1, 'asset_tag' => $faker->unixTime('now'), 'notes' => $faker->sentence, 'purchase_date' => $faker->dateTime(), diff --git a/resources/views/hardware/view.blade.php b/resources/views/hardware/view.blade.php index bc602650fc..50891463fc 100755 --- a/resources/views/hardware/view.blade.php +++ b/resources/views/hardware/view.blade.php @@ -438,10 +438,12 @@ {{ $asset->created_at }} - @if (isset($asset->adminuser->id)) {{ $asset->adminuser->fullName() }} - @else - {{ trans('general.unknown_admin') }} - @endif + @if ($asset->adminuser) + {{ $asset->adminuser->fullName() }} + @else + @trans('general.unknown_admin') + @endif + {{ trans('general.created_asset') }} diff --git a/resources/views/users/confirm-bulk-delete.blade.php b/resources/views/users/confirm-bulk-delete.blade.php index e8505ecf72..05198ebc83 100644 --- a/resources/views/users/confirm-bulk-delete.blade.php +++ b/resources/views/users/confirm-bulk-delete.blade.php @@ -2,7 +2,7 @@ {{-- Page title --}} @section('title') -Bulk Edit/Delete +Bulk Checkin & Delete @parent @stop @@ -45,15 +45,23 @@ Bulk Edit/Delete Name Groups + Assets + Accessories + Licenses - + {{ Form::select('status_id', $statuslabel_list , Input::old('status_id'), array('class'=>'select2', 'style'=>'width:250px')) }} + + + + + @foreach ($users as $user) @@ -65,6 +73,7 @@ Bulk Edit/Delete @endif + id==$user->id ? ' style="text-decoration: line-through"' : '') }}>{{ $user->fullName() }} ({{ $user->username }}) @@ -72,8 +81,23 @@ Bulk Edit/Delete - + @foreach ($user->groups as $group) + + {{ $group->name }} + + @endforeach + + + {{ number_format($user->assets->count()) }} + + + {{ number_format($user->accessories->count()) }} + + + {{ number_format($user->licenses->count()) }} + + @endforeach diff --git a/resources/views/users/index.blade.php b/resources/views/users/index.blade.php index dea4e3cc7a..e420d2defb 100755 --- a/resources/views/users/index.blade.php +++ b/resources/views/users/index.blade.php @@ -42,12 +42,14 @@ 'route' => ['users/bulkedit'], 'class' => 'form-inline' ]) }} + @if (Input::get('status')!='deleted')
+ @endif - + diff --git a/resources/views/users/view.blade.php b/resources/views/users/view.blade.php index 624833c9a3..30d0cd5b1f 100755 --- a/resources/views/users/view.blade.php +++ b/resources/views/users/view.blade.php @@ -132,7 +132,12 @@ {{ trans('button.delete') }}
- {{ trans('button.checkin_and_delete') }} +
+ + + + +
@else
+ @if (Input::get('status')!='deleted') +
+ @endif +
{{ trans('general.id') }} {{ trans('admin/companies/table.title') }} {{ trans('admin/users/table.employee_num') }}