From 9ac4efb912e07d79a067c3a0717cab64ea764bbe Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 17 Mar 2023 16:24:38 -0700 Subject: [PATCH 1/9] Better commenting Signed-off-by: snipe --- .../Controllers/Users/BulkUsersController.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Users/BulkUsersController.php b/app/Http/Controllers/Users/BulkUsersController.php index bbf3ced555..63fff037a9 100644 --- a/app/Http/Controllers/Users/BulkUsersController.php +++ b/app/Http/Controllers/Users/BulkUsersController.php @@ -22,7 +22,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 +36,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') { + + 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 != '')) { From 82dc57aa1818d67905b5ef7090dcbd6cb1493d31 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 17 Mar 2023 16:24:58 -0700 Subject: [PATCH 2/9] Renamed some variables, invoked the event listener Signed-off-by: snipe --- .../Controllers/Users/BulkUsersController.php | 38 ++++++++++--------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/app/Http/Controllers/Users/BulkUsersController.php b/app/Http/Controllers/Users/BulkUsersController.php index 63fff037a9..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; @@ -55,7 +56,7 @@ class BulkUsersController extends Controller } elseif ($request->input('bulk_actions') == 'merge') { if (($request->filled('ids')) && (count($request->input('ids')) > 1)) { - return view('users/confirm-merge')->with('users', $users); + 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])); @@ -287,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])); } From a54e50961e7ba733559c97d7fbb3138e1efa960e Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 17 Mar 2023 16:41:33 -0700 Subject: [PATCH 3/9] Added event and listener Signed-off-by: snipe --- app/Events/UserMerged.php | 24 ++++++++++++++++++++ app/Listeners/LogListener.php | 42 +++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 app/Events/UserMerged.php 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/Listeners/LogListener.php b/app/Listeners/LogListener.php index 791a84f57a..af329be869 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_users_log_merged_into', $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_users_log_merged_from', $to_from_array); + $logaction->user_id = $event->merged_to->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 ); } } + + } From 28cc8f9d69ea21729bc36a894ca87e89b0523d49 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 17 Mar 2023 16:41:43 -0700 Subject: [PATCH 4/9] Nicer user history display Signed-off-by: snipe --- resources/views/users/view.blade.php | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) 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 @@ Icon - {{ trans('general.date') }} - {{ trans('general.admin') }} - {{ trans('general.action') }} + {{ trans('general.date') }} + {{ trans('general.item') }} + {{ trans('general.action') }} + {{ trans('general.target') }} + {{ trans('general.notes') }} @if ($snipeSettings->require_accept_signature=='1') - {{ trans('general.signature') }} + {{ trans('general.signature') }} @endif - {{ trans('admin/hardware/table.serial') }} - {{ trans('general.item') }} - {{ trans('general.notes') }} - {{ trans('general.target') }} + {{ trans('admin/hardware/table.serial') }} + {{ trans('general.admin') }} + + From b39fa1d98762f05a44b9439cbc97b21772ee1a6e Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 17 Mar 2023 16:42:17 -0700 Subject: [PATCH 5/9] Added merge icon Signed-off-by: snipe --- app/Presenters/ActionlogPresenter.php | 2 ++ 1 file changed, 2 insertions(+) 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'; } } From fb54a21e12f2d71adb51f900e358cce5fa3af360 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 17 Mar 2023 16:42:22 -0700 Subject: [PATCH 6/9] Added strings Signed-off-by: snipe --- resources/lang/en/general.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php index 153e5d1da4..085dbbdafc 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_users_log_merged_from' => 'Merged this user (ID :to_id - :to_username) into user ID :from_id (:from_username) ', + 'merged_users_log_merged_into' => 'Merged user ID :from_id (:from_username) into this user (ID :to_id - :to_username)', ]; \ No newline at end of file From 820ae09a6f9c50aa94f09fea588dfe7f45f2e2ad Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 17 Mar 2023 16:42:41 -0700 Subject: [PATCH 7/9] Reworked the layout of the confirm merge form a little Signed-off-by: snipe --- resources/views/users/confirm-merge.blade.php | 36 ++++++++++++------- 1 file changed, 24 insertions(+), 12 deletions(-) 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"':'') !!}> - -
{{ 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 From e47fc9527b5186003a2d56583e955636e69d35bc Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 17 Mar 2023 16:50:02 -0700 Subject: [PATCH 8/9] Updated language Signed-off-by: snipe --- app/Listeners/LogListener.php | 4 ++-- resources/lang/en/general.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/Listeners/LogListener.php b/app/Listeners/LogListener.php index af329be869..d5e1550409 100644 --- a/app/Listeners/LogListener.php +++ b/app/Listeners/LogListener.php @@ -108,7 +108,7 @@ class LogListener $logaction->target_id = $event->merged_to->id; $logaction->target_type = User::class; $logaction->action_type = 'merged'; - $logaction->note = trans('general.merged_users_log_merged_into', $to_from_array); + $logaction->note = trans('general.merged_log_this_user_from', $to_from_array); $logaction->user_id = $event->admin->id; $logaction->save(); @@ -119,7 +119,7 @@ class LogListener $logaction->item_id = $event->merged_to->id; $logaction->item_type = User::class; $logaction->action_type = 'merged'; - $logaction->note = trans('general.merged_users_log_merged_from', $to_from_array); + $logaction->note = trans('general.merged_log_this_user_into', $to_from_array); $logaction->user_id = $event->merged_to->admin_id; $logaction->save(); diff --git a/resources/lang/en/general.php b/resources/lang/en/general.php index 085dbbdafc..159c1abad4 100644 --- a/resources/lang/en/general.php +++ b/resources/lang/en/general.php @@ -415,8 +415,8 @@ return [ 'not_enough_users_selected' => 'At least :count users must be selected', 'merge_success' => ':count users merged successfully into :into_username!', 'merged' => 'merged', - 'merged_users_log_merged_from' => 'Merged this user (ID :to_id - :to_username) into user ID :from_id (:from_username) ', - 'merged_users_log_merged_into' => 'Merged user ID :from_id (:from_username) into this user (ID :to_id - :to_username)', + '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 From 12a2d8ad49fdc1f54dfab9d6a11f9fb037b71d13 Mon Sep 17 00:00:00 2001 From: snipe Date: Fri, 17 Mar 2023 17:03:10 -0700 Subject: [PATCH 9/9] Fixed admin ID Signed-off-by: snipe --- app/Listeners/LogListener.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Listeners/LogListener.php b/app/Listeners/LogListener.php index d5e1550409..d14b674364 100644 --- a/app/Listeners/LogListener.php +++ b/app/Listeners/LogListener.php @@ -120,7 +120,7 @@ class LogListener $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->merged_to->admin_id; + $logaction->user_id = $event->admin->id; $logaction->save();