mirror of
https://github.com/solidtime-io/solidtime.git
synced 2026-12-06 17:52:16 +00:00
Add exit impersonation to the user menu
This commit is contained in:
committed by
Gregor Vostrak
parent
cc23cdc76b
commit
7912b0a237
@@ -8,6 +8,7 @@ use App\Service\BillingContract;
|
||||
use Illuminate\Http\Request;
|
||||
use Inertia\Middleware;
|
||||
use Nwidart\Modules\Facades\Module;
|
||||
use STS\FilamentImpersonate\ImpersonateManager;
|
||||
|
||||
class HandleInertiaRequests extends Middleware
|
||||
{
|
||||
@@ -62,6 +63,7 @@ class HandleInertiaRequests extends Middleware
|
||||
'goal_limit' => $billing->getGoalLimit($currentOrganization),
|
||||
'can_use_billable_rates' => $billing->canUseBillableRates($currentOrganization),
|
||||
] : null,
|
||||
'is_impersonating' => app(ImpersonateManager::class)->isImpersonating(),
|
||||
'flash' => [
|
||||
'message' => fn () => $request->session()->get('message'),
|
||||
'bannerText' => fn () => $request->session()->get('bannerText'),
|
||||
|
||||
57
e2e/impersonation.spec.ts
Normal file
57
e2e/impersonation.spec.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import { adminTest as test, expect } from '../playwright/fixtures';
|
||||
import { PLAYWRIGHT_BASE_URL } from '../playwright/config';
|
||||
import { registerUser } from './utils/members';
|
||||
|
||||
test.describe('Impersonation', () => {
|
||||
test.beforeEach(async ({ page }) => {
|
||||
// The seeded super admin's timezone differs from the browser's, which opens a modal over the app.
|
||||
await page.addInitScript(() => {
|
||||
window.sessionStorage.setItem('hide-timezone-mismatch-modal', 'true');
|
||||
});
|
||||
});
|
||||
|
||||
test('test that a super admin can exit an impersonation from the user menu', async ({
|
||||
page,
|
||||
browser,
|
||||
}) => {
|
||||
const userName = 'Impersonated User';
|
||||
const userEmail = `impersonated+${Date.now()}_${Math.floor(Math.random() * 10000)}@doe.com`;
|
||||
// Contexts inherit the super admin's storage state, so the new user registers in an empty one.
|
||||
const user = await registerUser(browser, userName, userEmail, {
|
||||
storageState: { cookies: [], origins: [] },
|
||||
});
|
||||
await user.close();
|
||||
|
||||
try {
|
||||
await page.goto(
|
||||
PLAYWRIGHT_BASE_URL + '/admin/users?search=' + encodeURIComponent(userEmail)
|
||||
);
|
||||
const userRow = page.getByRole('row').filter({ hasText: userEmail });
|
||||
await userRow.getByRole('button', { name: 'Impersonate' }).click();
|
||||
|
||||
await expect(page.getByTestId('dashboard_view')).toBeVisible();
|
||||
|
||||
await page.getByRole('button', { name: userName }).click();
|
||||
await page.getByRole('menuitem', { name: 'Exit impersonation' }).click();
|
||||
|
||||
await expect(page).toHaveURL(new RegExp('/admin/users'));
|
||||
await expect(page.getByRole('heading', { name: 'Users', level: 1 })).toBeVisible();
|
||||
} finally {
|
||||
// The admin session is shared by all tests of this worker, so it must never stay impersonating.
|
||||
await page.goto(PLAYWRIGHT_BASE_URL + '/filament-impersonate/leave');
|
||||
}
|
||||
});
|
||||
|
||||
test('test that the exit impersonation menu item is hidden without an impersonation', async ({
|
||||
page,
|
||||
}) => {
|
||||
await page.goto(PLAYWRIGHT_BASE_URL + '/dashboard');
|
||||
await expect(page.getByTestId('dashboard_view')).toBeVisible();
|
||||
|
||||
// The seeded super admin's name is unknown here, so the trigger has no stable accessible name.
|
||||
await page.getByTestId('current_user_button').click();
|
||||
|
||||
await expect(page.getByRole('menuitem', { name: 'Log Out' })).toBeVisible();
|
||||
await expect(page.getByRole('menuitem', { name: 'Exit impersonation' })).toHaveCount(0);
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { expect } from '@playwright/test';
|
||||
import type { Browser, Page } from '@playwright/test';
|
||||
import type { Browser, BrowserContextOptions, Page } from '@playwright/test';
|
||||
import { PLAYWRIGHT_BASE_URL } from '../../playwright/config';
|
||||
import { getInvitationAcceptUrl } from './mailpit';
|
||||
import type { TestContext } from './api';
|
||||
@@ -10,9 +10,10 @@ import type { TestContext } from './api';
|
||||
export async function registerUser(
|
||||
browser: Browser,
|
||||
name: string,
|
||||
email: string
|
||||
email: string,
|
||||
contextOptions: BrowserContextOptions = {}
|
||||
): Promise<{ page: Page; close: () => Promise<void> }> {
|
||||
const context = await browser.newContext();
|
||||
const context = await browser.newContext(contextOptions);
|
||||
const page = await context.newPage();
|
||||
|
||||
await page.goto(PLAYWRIGHT_BASE_URL + '/register');
|
||||
|
||||
@@ -12,12 +12,14 @@ import {
|
||||
UserCircleIcon,
|
||||
ArrowLeftOnRectangleIcon,
|
||||
ChatBubbleLeftRightIcon,
|
||||
ArrowUturnLeftIcon,
|
||||
} from '@heroicons/vue/24/solid';
|
||||
import { openFeedback } from '@/utils/feedback';
|
||||
|
||||
const page = usePage<{
|
||||
has_services_extension?: boolean;
|
||||
has_billing_extension?: boolean;
|
||||
is_impersonating?: boolean;
|
||||
auth: {
|
||||
user: User & {
|
||||
all_teams: Organization[];
|
||||
@@ -64,6 +66,16 @@ const logout = () => {
|
||||
</button>
|
||||
</DropdownMenuItem>
|
||||
|
||||
<!-- A full page load, since leaving returns to the admin panel, which is not an Inertia page. -->
|
||||
<DropdownMenuItem v-if="page.props.is_impersonating" as-child>
|
||||
<a
|
||||
:href="route('filament-impersonate.leave')"
|
||||
class="inline-flex items-center gap-2.5 w-full">
|
||||
<ArrowUturnLeftIcon class="w-5 h-5 text-icon-default" />
|
||||
<span>Exit impersonation</span>
|
||||
</a>
|
||||
</DropdownMenuItem>
|
||||
|
||||
<form class="w-full" @submit.prevent="logout">
|
||||
<DropdownMenuItem as-child class="inline-flex items-center gap-2.5 w-full">
|
||||
<button type="submit" data-testid="logout_button">
|
||||
|
||||
Reference in New Issue
Block a user