diff --git a/app/Events/UserMerged.php b/app/Events/UserMerged.php new file mode 100644 index 0000000000..b045fdef03 --- /dev/null +++ b/app/Events/UserMerged.php @@ -0,0 +1,24 @@ +merged_from = $from_user; + $this->merged_to = $to_user; + $this->admin = $admin; + } +} diff --git a/app/Http/Controllers/Users/BulkUsersController.php b/app/Http/Controllers/Users/BulkUsersController.php index bbf3ced555..b12683efaf 100644 --- a/app/Http/Controllers/Users/BulkUsersController.php +++ b/app/Http/Controllers/Users/BulkUsersController.php @@ -2,6 +2,7 @@ namespace App\Http\Controllers\Users; +use App\Events\UserMerged; use App\Helpers\Helper; use App\Http\Controllers\Controller; use App\Models\Accessory; @@ -22,7 +23,7 @@ use Illuminate\Support\Facades\Password; class BulkUsersController extends Controller { /** - * Returns a view that confirms the user's a bulk delete will be applied to. + * Returns a view that confirms the user's a bulk action will be applied to. * * @author [A. Gianotto] [] * @since [v1.7] @@ -36,18 +37,32 @@ class BulkUsersController extends Controller // Make sure there were users selected if (($request->filled('ids')) && (count($request->input('ids')) > 0)) { + // Get the list of affected users $user_raw_array = request('ids'); $users = User::whereIn('id', $user_raw_array) ->with('groups', 'assets', 'licenses', 'accessories')->get(); + // bulk edit, display the bulk edit form if ($request->input('bulk_actions') == 'edit') { return view('users/bulk-edit', compact('users')) ->with('groups', Group::pluck('name', 'id')); + + // bulk delete, display the bulk delete confirmation form } elseif ($request->input('bulk_actions') == 'delete') { return view('users/confirm-bulk-delete')->with('users', $users)->with('statuslabel_list', Helper::statusLabelList()); + + // merge, confirm they have at least 2 users selected and display the merge screen } elseif ($request->input('bulk_actions') == 'merge') { - return view('users/confirm-merge')->with('users', $users); + + if (($request->filled('ids')) && (count($request->input('ids')) > 1)) { + return view('users/confirm-merge')->with('users', $users); + // Not enough users selected, send them back + } else { + return redirect()->back()->with('error', trans('general.not_enough_users_selected', ['count' => 2])); + } + + // bulk password reset, just do the thing } elseif ($request->input('bulk_actions') == 'bulkpasswordreset') { foreach ($users as $user) { if (($user->activated == '1') && ($user->email != '')) { @@ -273,55 +288,58 @@ class BulkUsersController extends Controller $user_ids_to_merge = $request->input('ids_to_merge'); $user_ids_to_merge = array_diff($user_ids_to_merge, array($request->input('merge_into_id'))); - if ((!$request->filled('merge_into_id')) || (count($user_ids_to_merge) < 0)) { + if ((!$request->filled('merge_into_id')) || (count($user_ids_to_merge) < 1)) { return redirect()->back()->with('error', trans('general.no_users_selected')); } // Get the users $merge_into_user = User::find($request->input('merge_into_id')); $users_to_merge = User::whereIn('id', $user_ids_to_merge)->with('assets', 'licenses', 'consumables','accessories')->get(); + $admin = User::find(Auth::user()->id); // Walk users - foreach ($users_to_merge as $user) { + foreach ($users_to_merge as $user_to_merge) { - foreach ($user->assets as $asset) { + foreach ($user_to_merge->assets as $asset) { \Log::debug('Updating asset: '.$asset->asset_tag . ' to '.$merge_into_user->id); $asset->assigned_to = $request->input('merge_into_id'); $asset->save(); } - foreach ($user->licenses as $license) { + foreach ($user_to_merge->licenses as $license) { \Log::debug('Updating license pivot: '.$license->id . ' to '.$merge_into_user->id); - $user->licenses()->updateExistingPivot($license->id, ['assigned_to' => $merge_into_user->id]); + $user_to_merge->licenses()->updateExistingPivot($license->id, ['assigned_to' => $merge_into_user->id]); } - foreach ($user->consumables as $consumable) { + foreach ($user_to_merge->consumables as $consumable) { \Log::debug('Updating consumable pivot: '.$consumable->id . ' to '.$merge_into_user->id); - $user->consumables()->updateExistingPivot($consumable->id, ['assigned_to' => $merge_into_user->id]); + $user_to_merge->consumables()->updateExistingPivot($consumable->id, ['assigned_to' => $merge_into_user->id]); } - foreach ($user->accessories as $accessory) { - $user->accessories()->updateExistingPivot($accessory->id, ['assigned_to' => $merge_into_user->id]); + foreach ($user_to_merge->accessories as $accessory) { + $user_to_merge->accessories()->updateExistingPivot($accessory->id, ['assigned_to' => $merge_into_user->id]); } - foreach ($user->userlog as $log) { - $log->target_id = $user->id; + foreach ($user_to_merge->userlog as $log) { + $log->target_id = $user_to_merge->id; $log->save(); } - User::where('manager_id', '=', $user->id)->update(['manager_id' => $merge_into_user->id]); + User::where('manager_id', '=', $user_to_merge->id)->update(['manager_id' => $merge_into_user->id]); - foreach ($user->managedLocations as $managedLocation) { + foreach ($user_to_merge->managedLocations as $managedLocation) { $managedLocation->manager_id = $merge_into_user->id; $managedLocation->save(); } - $user->deleted_at = Carbon::now()->timestamp; - $user->save(); + $user_to_merge->delete(); + //$user_to_merge->save(); + + event(new UserMerged($user_to_merge, $merge_into_user, $admin)); } - return redirect()->route('users.index')->with('success', trans('general.merge_success')); + return redirect()->route('users.index')->with('success', trans('general.merge_success', ['count' => $users_to_merge->count(), 'into_username' => $merge_into_user->username])); } diff --git a/app/Listeners/LogListener.php b/app/Listeners/LogListener.php index 791a84f57a..d14b674364 100644 --- a/app/Listeners/LogListener.php +++ b/app/Listeners/LogListener.php @@ -18,7 +18,9 @@ use App\Events\ItemDeclined; use App\Events\LicenseCheckedIn; use App\Events\LicenseCheckedOut; use App\Models\Actionlog; +use App\Models\User; use App\Models\LicenseSeat; +use App\Events\UserMerged; class LogListener { @@ -87,6 +89,43 @@ class LogListener $logaction->save(); } + + public function onUserMerged(UserMerged $event) + { + + $to_from_array = [ + 'to_id' => $event->merged_to->id, + 'to_username' => $event->merged_to->username, + 'from_id' => $event->merged_from->id, + 'from_username' => $event->merged_from->username, + ]; + + // Add a record to the users being merged FROM + \Log::debug('Users merged: '.$event->merged_from->id .' ('.$event->merged_from->username.') merged into '. $event->merged_to->id. ' ('.$event->merged_to->username.')'); + $logaction = new Actionlog(); + $logaction->item_id = $event->merged_from->id; + $logaction->item_type = User::class; + $logaction->target_id = $event->merged_to->id; + $logaction->target_type = User::class; + $logaction->action_type = 'merged'; + $logaction->note = trans('general.merged_log_this_user_from', $to_from_array); + $logaction->user_id = $event->admin->id; + $logaction->save(); + + // Add a record to the users being merged TO + $logaction = new Actionlog(); + $logaction->target_id = $event->merged_from->id; + $logaction->target_type = User::class; + $logaction->item_id = $event->merged_to->id; + $logaction->item_type = User::class; + $logaction->action_type = 'merged'; + $logaction->note = trans('general.merged_log_this_user_into', $to_from_array); + $logaction->user_id = $event->admin->id; + $logaction->save(); + + + } + /** * Register the listeners for the subscriber. * @@ -99,6 +138,7 @@ class LogListener 'CheckoutableCheckedOut', 'CheckoutAccepted', 'CheckoutDeclined', + 'UserMerged', ]; foreach ($list as $event) { @@ -108,4 +148,6 @@ class LogListener ); } } + + } diff --git a/app/Presenters/ActionlogPresenter.php b/app/Presenters/ActionlogPresenter.php index 617cdb6a2a..cd581d33ce 100644 --- a/app/Presenters/ActionlogPresenter.php +++ b/app/Presenters/ActionlogPresenter.php @@ -50,6 +50,8 @@ class ActionlogPresenter extends Presenter return 'far fa-save'; } elseif ($this->itemType() == 'component') { return 'far fa-hdd'; + } elseif ($this->itemType() == 'user') { + return 'fa-solid fa-people-arrows'; } } diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php index 153e5d1da4..159c1abad4 100644 --- a/resources/lang/en/general.php +++ b/resources/lang/en/general.php @@ -409,9 +409,14 @@ return [ 'integration_option' => 'Integration Option', 'log_does_not_exist' => 'No matching log record exists.', 'merge_users' => 'Merge Users', - 'warning_merge_information' => 'This will merge the selected :count users into a single user. Select the user you wish to merge the others into into below. THIS ACTION CANNOT BE UNDONE and should ONLY be used when you need to merge users because of a bad import or sync.', + 'merge_information' => 'This will merge the :count users into a single user. Select the user you wish to merge the others into below, and the associated assets, licences, etc will be moved over to the selected user and the other users will be marked as deleted.', + 'warning_merge_information' => 'This action CANNOT be undone and should ONLY be used when you need to merge users because of a bad import or sync. Be sure to run a backup first.', 'no_users_selected' => 'No users selected', - 'merge_success' => 'Users merged successfully', + 'not_enough_users_selected' => 'At least :count users must be selected', + 'merge_success' => ':count users merged successfully into :into_username!', + 'merged' => 'merged', + 'merged_log_this_user_into' => 'Merged this user (ID :to_id - :to_username) into user ID :from_id (:from_username) ', + 'merged_log_this_user_from' => 'Merged user ID :from_id (:from_username) into this user (ID :to_id - :to_username)', ]; \ No newline at end of file diff --git a/resources/views/users/confirm-merge.blade.php b/resources/views/users/confirm-merge.blade.php index f423d74dd8..c8d87916a4 100644 --- a/resources/views/users/confirm-merge.blade.php +++ b/resources/views/users/confirm-merge.blade.php @@ -7,19 +7,25 @@ {{-- Page content --}} @section('content') - +
+ +
+

+ {{ trans('general.merge_information', array('count' => count($users))) }} +

+
{{csrf_field()}}
- {{ trans('general.warning_merge_information', array('count' => count($users))) }} + {{ trans('general.warning_merge_information') }}
@@ -39,7 +45,6 @@ - @@ -66,14 +71,8 @@ @foreach ($users as $user) isSuperUser() ? ' class="danger"':'') !!}> - - - - - + + + + + @if ($snipeSettings->require_accept_signature=='1') - + @endif - - - - + + + +
{{ trans('general.name') }} {{ trans('general.email') }} {{ trans('general.username') }}
- - - id==$user->id ? ' style="text-decoration: line-through"' : '') !!}> - {{ $user->present()->fullName() }} - - {{ (Auth::id()==$user->id ? ' (cannot delete yourself)' : '') }} + + {{ $user->email }} @@ -112,7 +111,7 @@ @foreach ($users as $user) @@ -128,4 +127,17 @@ @section('moar_scripts') + @if (!(config('app.lock_passwords'))) + + + @endif + @stop \ No newline at end of file diff --git a/resources/views/users/view.blade.php b/resources/views/users/view.blade.php index 3757dec079..a130d1629d 100755 --- a/resources/views/users/view.blade.php +++ b/resources/views/users/view.blade.php @@ -958,16 +958,18 @@
{{ trans('general.date') }}{{ trans('general.admin') }}{{ trans('general.action') }}{{ trans('general.date') }}{{ trans('general.item') }}{{ trans('general.action') }}{{ trans('general.target') }}{{ trans('general.notes') }}{{ trans('general.signature') }}{{ trans('general.signature') }}{{ trans('admin/hardware/table.serial') }}{{ trans('general.item') }}{{ trans('general.notes') }}{{ trans('general.target') }}{{ trans('admin/hardware/table.serial') }}{{ trans('general.admin') }}