-
Notifications
You must be signed in to change notification settings - Fork 703
[6.x] Modernize elevated session confirmation #19237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f581b79
Fix Testbench workbench serving
riasvdv 51937e5
Modernize elevated session confirmation
riasvdv c59155a
Merge branch '6.x' into feature/elevated-sessions
riasvdv 3985ef2
Fix elevated session auth tests
riasvdv 7120d32
Address elevated session review feedback
riasvdv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
resources/js/modules/auth/elevated-session/ElevatedSessionHost.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| import {createApp, nextTick} from 'vue'; | ||
| import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; | ||
|
|
||
| const manager = vi.hoisted(() => ({ | ||
| state: { | ||
| active: true, | ||
| checking: false, | ||
| loginName: 'admin@example.com', | ||
| alternativeLoginMethods: null, | ||
| }, | ||
| cancel: vi.fn(), | ||
| confirm: vi.fn(), | ||
| })); | ||
|
|
||
| vi.mock('./manager', async () => { | ||
| const {reactive, readonly} = await import('vue'); | ||
|
|
||
| return { | ||
| elevatedSessionManager: { | ||
| state: readonly(reactive(manager.state)), | ||
| cancel: manager.cancel, | ||
| confirm: manager.confirm, | ||
| }, | ||
| }; | ||
| }); | ||
|
|
||
| vi.mock('@craftcms/cp', () => ({ | ||
| ConfigService: { | ||
| getInstance: () => ({ | ||
| getCpUrl: (path: string) => `https://example.test/admin/${path}`, | ||
| get: (key: string, fallback: unknown) => | ||
| key === 'useEmailAsUsername' ? true : fallback, | ||
| }), | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('@/common/components/Modal.vue', () => ({ | ||
| default: { | ||
| props: {isActive: Boolean}, | ||
| emits: ['close', 'opened'], | ||
| template: | ||
| '<div v-if="isActive"><button class="overlay" @click="$emit(\'close\')">Close</button><slot /></div>', | ||
| }, | ||
| })); | ||
|
|
||
| vi.mock('@/modules/auth/components/login/login-form.js', () => ({})); | ||
|
|
||
| vi.mock('@/common/components/Pane.vue', () => ({ | ||
| default: {template: '<div><slot /></div>'}, | ||
| })); | ||
|
|
||
| vi.mock('@/common/components/HtmlFragmentRenderer.vue', () => ({ | ||
| default: {template: '<div></div>'}, | ||
| })); | ||
|
|
||
| import ElevatedSessionHost from './ElevatedSessionHost.vue'; | ||
|
|
||
| describe('ElevatedSessionHost', () => { | ||
| let app: ReturnType<typeof createApp>; | ||
| let container: HTMLElement; | ||
|
|
||
| beforeEach(async () => { | ||
| manager.cancel.mockClear(); | ||
| manager.confirm.mockClear(); | ||
| container = document.createElement('div'); | ||
| document.body.append(container); | ||
| app = createApp(ElevatedSessionHost); | ||
| app.mount(container); | ||
| await nextTick(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| app.unmount(); | ||
| container.remove(); | ||
| }); | ||
|
|
||
| it('renders the server-selected identity and confirms login success', () => { | ||
| const loginForm = container.querySelector('craft-login-form')!; | ||
| const event = new CustomEvent('craft:login:success', { | ||
| bubbles: true, | ||
| cancelable: true, | ||
| }); | ||
|
|
||
| loginForm.dispatchEvent(event); | ||
|
|
||
| expect(loginForm.getAttribute('static-email')).toBe('admin@example.com'); | ||
| expect(event.defaultPrevented).toBe(true); | ||
| expect(manager.confirm).toHaveBeenCalledOnce(); | ||
| }); | ||
|
|
||
| it('cancels when the modal requests dismissal', () => { | ||
| container.querySelector<HTMLButtonElement>('.overlay')!.click(); | ||
|
|
||
| expect(manager.cancel).toHaveBeenCalledOnce(); | ||
| }); | ||
| }); |
108 changes: 108 additions & 0 deletions
108
resources/js/modules/auth/elevated-session/ElevatedSessionHost.vue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| <script setup lang="ts"> | ||
| import {ConfigService} from '@craftcms/cp'; | ||
| import {releaseFocusWithin, trapFocusWithin} from '@craftcms/garnish'; | ||
| import {t} from '@craftcms/cp/utilities/translate'; | ||
| import {nextTick, onBeforeUnmount, watch} from 'vue'; | ||
| import HtmlFragmentRenderer from '@/common/components/HtmlFragmentRenderer.vue'; | ||
| import Modal from '@/common/components/Modal.vue'; | ||
| import Pane from '@/common/components/Pane.vue'; | ||
| import {elevatedSessionManager} from './manager'; | ||
| import '@/modules/auth/components/login/login-form.js'; | ||
|
|
||
| const {state} = elevatedSessionManager; | ||
| let previouslyFocused: HTMLElement | null = null; | ||
| let dialog: Element | null = null; | ||
|
|
||
| watch( | ||
| () => state.active, | ||
| async (active) => { | ||
| if (active) { | ||
| previouslyFocused = document.activeElement as HTMLElement | null; | ||
| await nextTick(); | ||
| document | ||
| .querySelector<HTMLElement>( | ||
| '[data-elevated-session-dialog] craft-login-form' | ||
| ) | ||
| ?.focus(); | ||
| } else { | ||
| if (dialog) { | ||
| releaseFocusWithin(dialog); | ||
| dialog = null; | ||
| } | ||
| previouslyFocused?.focus(); | ||
| previouslyFocused = null; | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| onBeforeUnmount(() => { | ||
| if (state.active) { | ||
| elevatedSessionManager.cancel(); | ||
| } | ||
| }); | ||
|
|
||
| function handleSuccess(event: Event): void { | ||
| event.preventDefault(); | ||
| elevatedSessionManager.confirm(); | ||
| } | ||
|
|
||
| function close(): void { | ||
| if (state.active) { | ||
| elevatedSessionManager.cancel(); | ||
| } | ||
| } | ||
|
|
||
| function opened(element: Element): void { | ||
| dialog = element; | ||
| trapFocusWithin(element); | ||
| } | ||
| </script> | ||
|
|
||
| <template> | ||
| <Modal :is-active="state.active" width="md" @close="close" @opened="opened"> | ||
| <Pane | ||
| data-elevated-session-dialog | ||
| role="dialog" | ||
| aria-modal="true" | ||
| aria-labelledby="elevated-session-title" | ||
| aria-describedby="elevated-session-description" | ||
| class="p-6" | ||
| > | ||
| <div class="flex items-start gap-4 mb-5"> | ||
| <div> | ||
| <h1 id="elevated-session-title" class="text-xl"> | ||
| {{ t('Confirm your identity.') }} | ||
| </h1> | ||
| <p id="elevated-session-description"> | ||
| {{ t('You must reverify your identity before proceeding.') }} | ||
| </p> | ||
| </div> | ||
| <craft-button | ||
| icon | ||
| appearance="plain" | ||
| type="button" | ||
| class="ml-auto" | ||
| @click="close" | ||
| > | ||
| <craft-icon name="xmark" :label="t('Cancel')"></craft-icon> | ||
| </craft-button> | ||
| </div> | ||
|
|
||
| <craft-login-form | ||
| tabindex="0" | ||
| :action="ConfigService.getInstance().getCpUrl('login')" | ||
| :static-email="state.loginName" | ||
| :use-email-as-username=" | ||
| ConfigService.getInstance().get('useEmailAsUsername', false) | ||
| " | ||
| @craft:login:success="handleSuccess" | ||
| > | ||
| <HtmlFragmentRenderer | ||
| v-if="state.alternativeLoginMethods" | ||
| slot="alternative-methods" | ||
| :fragment="state.alternativeLoginMethods" | ||
| /> | ||
|
riasvdv marked this conversation as resolved.
|
||
| </craft-login-form> | ||
| </Pane> | ||
| </Modal> | ||
| </template> | ||
66 changes: 66 additions & 0 deletions
66
resources/js/modules/auth/elevated-session/elevated-session-form.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| import {beforeEach, describe, expect, it, vi} from 'vitest'; | ||
| import {ElevatedSessionForm} from './elevated-session-form'; | ||
|
|
||
| describe('ElevatedSessionForm', () => { | ||
| beforeEach(() => { | ||
| document.body.innerHTML = ''; | ||
| }); | ||
|
|
||
| it('only requires confirmation when a tracked input changes', () => { | ||
| document.body.innerHTML = ` | ||
| <form id="settings"> | ||
| <input name="name" value="Original"> | ||
| <input class="permission" name="permissions[]" value="edit"> | ||
| </form> | ||
| `; | ||
| const form = document.querySelector<HTMLFormElement>('#settings')!; | ||
| const elevatedForm = new ElevatedSessionForm(form, [ | ||
| '[name="name"]', | ||
| '.permission', | ||
| ]); | ||
|
|
||
| expect(elevatedForm.inputsChanged()).toBe(false); | ||
| form.querySelector<HTMLInputElement>('[name="name"]')!.value = 'Changed'; | ||
| expect(elevatedForm.inputsChanged()).toBe(true); | ||
| }); | ||
|
|
||
| it('detects inputs added after initialization', () => { | ||
| document.body.innerHTML = '<form id="settings"></form>'; | ||
| const form = document.querySelector<HTMLFormElement>('#settings')!; | ||
| const elevatedForm = new ElevatedSessionForm(form, '.permission'); | ||
|
|
||
| expect(elevatedForm.inputsChanged()).toBe(false); | ||
| form.insertAdjacentHTML( | ||
| 'beforeend', | ||
| '<input class="permission" name="permissions[]" value="edit">' | ||
| ); | ||
| expect(elevatedForm.inputsChanged()).toBe(true); | ||
| }); | ||
|
|
||
| it('resubmits with the original submitter after confirmation', async () => { | ||
| document.body.innerHTML = ` | ||
| <form id="settings"> | ||
| <input name="name" value="Original"> | ||
| <button type="submit" name="redirect" value="1">Save</button> | ||
| </form> | ||
| `; | ||
| const form = document.querySelector<HTMLFormElement>('#settings')!; | ||
| const button = form.querySelector<HTMLButtonElement>('button')!; | ||
| const manager = {require: vi.fn().mockResolvedValue(true)}; | ||
| new ElevatedSessionForm(form, '[name="name"]', manager); | ||
| const submitted = vi.fn(); | ||
| form.addEventListener('submit', (event) => { | ||
| event.preventDefault(); | ||
| submitted((event as SubmitEvent).submitter); | ||
| }); | ||
| form.querySelector<HTMLInputElement>('[name="name"]')!.value = 'Changed'; | ||
|
|
||
| form.requestSubmit(button); | ||
| await Promise.resolve(); | ||
| await Promise.resolve(); | ||
|
|
||
| expect(manager.require).toHaveBeenCalledOnce(); | ||
| expect(submitted).toHaveBeenCalledOnce(); | ||
| expect(submitted).toHaveBeenCalledWith(button); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.