Skip to content

A free open source IT asset/license management system

Make impersonation note required

snipe2ee655e7396e
diff --git a/app/Http/Controllers/Users/ImpersonateController.php b/app/Http/Controllers/Users/ImpersonateController.php
index f4bfd44f8c..f5faec46bd 100644
--- a/app/Http/Controllers/Users/ImpersonateController.php
+++ b/app/Http/Controllers/Users/ImpersonateController.php
@@ -39,6 +39,15 @@ class ImpersonateController extends Controller
->with('error', trans('admin/users/message.impersonate.target_not_active'));
}
+ $note = trim((string) $request->input('note', ''));
+ if ($note === '') {
+ return redirect()->route('users.show', $user)
+ ->with('error', trans('admin/users/general.impersonate_note_required'));
+ }
+ // Cap length defensively to match the textarea's maxlength; anything longer
+ // is almost certainly a paste error or an abuse attempt.
+ $note = mb_substr($note, 0, 500);
+
$log = new Actionlog;
$log->item_type = User::class;
$log->item_id = $user->id;
@@ -46,6 +55,7 @@ class ImpersonateController extends Controller
$log->target_id = $user->id;
$log->created_at = date('Y-m-d H:i:s');
$log->created_by = $actor->id;
+ $log->note = $note;
$log->logaction('impersonated');
$impersonatorId = $actor->id;
diff --git a/app/Http/Transformers/ActionlogsTransformer.php b/app/Http/Transformers/ActionlogsTransformer.php
index eb1e372347..e882171d7d 100644
--- a/app/Http/Transformers/ActionlogsTransformer.php
+++ b/app/Http/Transformers/ActionlogsTransformer.php
@@ -81,6 +81,17 @@ class ActionlogsTransformer
continue;
}
+ // Some log_meta entries carry a scalar value rather than the
+ // {old, new} field-diff shape (e.g. the "reason" attached to
+ // an impersonation start). Copy scalars through untouched so
+ // the transformer doesn't blow up trying to read ->old on a
+ // string.
+ if (! is_object($fieldata)) {
+ $clean_meta[$fieldname] = $fieldata;
+
+ continue;
+ }
+
$clean_meta[$fieldname]['old'] = $this->clean_field($fieldata->old);
$clean_meta[$fieldname]['new'] = $this->clean_field($fieldata->new);
diff --git a/resources/lang/en-US/admin/users/general.php b/resources/lang/en-US/admin/users/general.php
index 953cbf8764..0ba94122d3 100644
--- a/resources/lang/en-US/admin/users/general.php
+++ b/resources/lang/en-US/admin/users/general.php
@@ -68,6 +68,9 @@ return [
'impersonate_user' => 'Login as :name',
'impersonate_confirm_title' => 'Confirm Impersonation',
'impersonate_confirm_body' => 'You are about to log in as :name. Any action you take will be recorded as if :name performed it. Continue?',
+ 'impersonate_note_label' => 'Note',
+ 'impersonate_note_placeholder' => 'e.g. Investigating ticket #12345, verifying user-reported permission issue',
+ 'impersonate_note_required' => 'A note is required to impersonate another user.',
'impersonating_banner_title' => 'Impersonating:',
'impersonating_banner_text' => 'You are currently logged in as :name. Anything you do will be recorded as if :name did it. Your real account is :impersonator.',
'impersonating_stop_link' => 'Switch back to :name',
diff --git a/resources/views/layouts/basic.blade.php b/resources/views/layouts/basic.blade.php
index 72667ad28e..359a0aac5c 100644
--- a/resources/views/layouts/basic.blade.php
+++ b/resources/views/layouts/basic.blade.php
@@ -45,6 +45,8 @@
<body class="hold-transition login-page">
+ @include('partials.impersonation-banner')
+
@if (($snipeSettings) && ($snipeSettings->logo!=''))
<div class="text-center">
<a href="{{ config('app.url') }}">
diff --git a/resources/views/users/impersonate-confirm-modal.blade.php b/resources/views/users/impersonate-confirm-modal.blade.php
new file mode 100644
index 0000000000..c137c8c4d0
--- /dev/null
+++ b/resources/views/users/impersonate-confirm-modal.blade.php
@@ -0,0 +1,41 @@
+<div class="modal fade modal-danger" id="confirmImpersonateModal" tabindex="-1" role="dialog" aria-labelledby="confirmImpersonateModalLabel" aria-hidden="true">
+ <div class="modal-dialog">
+ <div class="modal-content">
+ <div class="modal-header">
+ <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
+ <h4 class="modal-title" id="confirmImpersonateModalLabel">{{ trans('admin/users/general.impersonate_confirm_title') }}</h4>
+ </div>
+ <form action="{{ route('users.impersonate.start', $user->id) }}" method="POST" class="form-horizontal">
+ {{ csrf_field() }}
+ <div class="modal-body">
+ <div class="row">
+ <div class="col-md-12">
+ {{ trans('admin/users/general.impersonate_confirm_body', ['name' => $user->display_name]) }}
+ </div>
+ </div>
+
+ <div class="row" style="margin-top: 15px;">
+ <div class="col-md-12">
+ <label for="impersonate_note" class="control-label" style="color: #ffffff;">
+ {{ trans('admin/users/general.impersonate_note_label') }} <span aria-hidden="true">*</span>
+ </label>
+ <textarea
+ name="note"
+ id="impersonate_note"
+ class="form-control"
+ rows="3"
+ maxlength="500"
+ required
+ aria-required="true"
+ placeholder="{{ trans('admin/users/general.impersonate_note_placeholder') }}"></textarea>
+ </div>
+ </div>
+ </div>
+ <div class="modal-footer">
+ <button type="button" class="btn btn-default pull-left" data-dismiss="modal">{{ trans('button.cancel') }}</button>
+ <button type="submit" class="btn btn-outline">{{ trans('general.yes') }}</button>
+ </div>
+ </form>
+ </div>
+ </div>
+</div>
diff --git a/resources/views/users/view.blade.php b/resources/views/users/view.blade.php
index d2c5bcc363..f2af359307 100755
--- a/resources/views/users/view.blade.php
+++ b/resources/views/users/view.blade.php
@@ -661,15 +661,7 @@
</x-container>
@if (Auth::check() && Auth::user()->mayImpersonate($user))
- @include('modals.confirm-action', [
- 'modal_name' => 'confirmImpersonateModal',
- 'modal_class' => 'modal-danger',
- 'button_class' => 'btn-outline',
- 'button_label' => trans('general.yes'),
- 'route' => route('users.impersonate.start', $user->id),
- 'title' => trans('admin/users/general.impersonate_confirm_title'),
- 'body' => trans('admin/users/general.impersonate_confirm_body', ['name' => $user->display_name]),
- ])
+ @include('users.impersonate-confirm-modal')
@endif
@if (auth()->user()->isSuperUser() && $user->twoFactorResettable())
diff --git a/tests/Feature/Users/ImpersonateUserTest.php b/tests/Feature/Users/ImpersonateUserTest.php
index f15a45367a..b21f4afd0d 100644
--- a/tests/Feature/Users/ImpersonateUserTest.php
+++ b/tests/Feature/Users/ImpersonateUserTest.php
@@ -2,6 +2,7 @@
namespace Tests\Feature\Users;
+use App\Models\Actionlog;
use App\Models\Company;
use App\Models\User;
use Tests\TestCase;
@@ -61,18 +62,44 @@ class ImpersonateUserTest extends TestCase
$this->allow($actor);
$response = $this->actingAs($actor)
- ->post(route('users.impersonate.start', $target));
+ ->post(route('users.impersonate.start', $target), ['note' => 'Investigating ticket #4242']);
$response->assertRedirect(route('home'));
$this->assertSame($target->id, auth()->id());
$this->assertSame($actor->id, session('impersonator_id'));
- $this->assertDatabaseHas('action_logs', [
- 'item_type' => User::class,
- 'item_id' => $target->id,
- 'created_by' => $actor->id,
- 'action_type' => 'impersonated',
- ]);
+ $log = Actionlog::where('item_type', User::class)
+ ->where('item_id', $target->id)
+ ->where('created_by', $actor->id)
+ ->where('action_type', 'impersonated')
+ ->first();
+
+ $this->assertNotNull($log, 'impersonated action log entry not written');
+ $this->assertSame('Investigating ticket #4242', $log->note);
+ }
+
+ public function test_impersonate_requires_a_note()
+ {
+ $actor = User::factory()->superuser()->create();
+ $target = User::factory()->create(['activated' => 1]);
+ $this->allow($actor);
+
+ // No note
+ $this->actingAs($actor)
+ ->post(route('users.impersonate.start', $target))
+ ->assertRedirect(route('users.show', $target))
+ ->assertSessionHas('error', trans('admin/users/general.impersonate_note_required'));
+
+ $this->assertSame($actor->id, auth()->id());
+ $this->assertNull(session('impersonator_id'));
+
+ // Whitespace-only note
+ $this->actingAs($actor)
+ ->post(route('users.impersonate.start', $target), ['note' => ' '])
+ ->assertRedirect(route('users.show', $target))
+ ->assertSessionHas('error', trans('admin/users/general.impersonate_note_required'));
+
+ $this->assertNull(session('impersonator_id'));
}
public function test_allowlisted_superuser_cannot_impersonate_deactivated_user()
@@ -125,7 +152,9 @@ class ImpersonateUserTest extends TestCase
->get(route('users.show', $target))
->assertOk()
->assertSee('confirmImpersonateModal')
- ->assertSee(trans('admin/users/general.impersonate_confirm_title'));
+ ->assertSee(trans('admin/users/general.impersonate_confirm_title'))
+ ->assertSee('name="note"', false)
+ ->assertSee('required', false);
}
public function test_allowlisted_superuser_cannot_impersonate_themselves()
@@ -147,7 +176,7 @@ class ImpersonateUserTest extends TestCase
$this->allow($actor);
$this->actingAs($actor)
- ->post(route('users.impersonate.start', $target))
+ ->post(route('users.impersonate.start', $target), ['note' => 'test note'])
->assertRedirect(route('home'));
$this->assertSame($target->id, auth()->id());
@@ -173,7 +202,7 @@ class ImpersonateUserTest extends TestCase
$this->allow($actor);
$this->actingAs($actor)
- ->post(route('users.impersonate.start', $target))
+ ->post(route('users.impersonate.start', $target), ['note' => 'test note'])
->assertRedirect(route('home'));
$this->assertSame($actor->id, session('impersonator_id'));
@@ -200,7 +229,7 @@ class ImpersonateUserTest extends TestCase
$this->allow($actor);
$this->actingAs($actor)
- ->post(route('users.impersonate.start', $target))
+ ->post(route('users.impersonate.start', $target), ['note' => 'test note'])
->assertRedirect(route('home'));
$this->assertSame($target->id, auth()->id());
diff --git a/tests/Feature/Users/Ui/TwoFactorResetTest.php b/tests/Feature/Users/Ui/TwoFactorResetTest.php
index c763e5989c..770166a354 100644
--- a/tests/Feature/Users/Ui/TwoFactorResetTest.php
+++ b/tests/Feature/Users/Ui/TwoFactorResetTest.php
@@ -2,7 +2,6 @@
namespace Tests\Feature\Users\Ui;
-use App\Models\Setting;
use App\Models\User;
use Tests\TestCase;
@@ -13,8 +12,7 @@ class TwoFactorResetTest extends TestCase
// Optional-mode ("1") lets the actor through the CheckForTwoFactor middleware
// as long as they don't opt in, while still satisfying the resetTwoFactor policy
// for an opted-in-and-enrolled target.
- Setting::unguarded(fn () => Setting::getSettings()->update(['two_factor_enabled' => 1]));
- Setting::$_cache = null;
+ $this->settings->set(['two_factor_enabled' => 1]);
}
protected function makeEnrolledTarget(): User
@@ -89,8 +87,7 @@ class TwoFactorResetTest extends TestCase
{
// Empty string is the CheckForTwoFactor middleware's real "off" signal —
// the middleware treats "0" as on-but-misconfigured and still redirects.
- Setting::unguarded(fn () => Setting::getSettings()->update(['two_factor_enabled' => '']));
- Setting::$_cache = null;
+ $this->settings->set(['two_factor_enabled' => '']);
$actor = User::factory()->superuser()->create();
$target = User::factory()->create([