From 90371ecadaaf193a40f2b932d13704b6373154b8 Mon Sep 17 00:00:00 2001 From: Alexander Onnikov Date: Mon, 3 Mar 2025 09:47:15 +0300 Subject: [PATCH] fix: check guest access with cookie token (#8122) Signed-off-by: Alexander Onnikov --- plugins/guest-resources/src/utils.ts | 27 +++++++++++++++++----- tests/sanity/tests/tracker/tracker.spec.ts | 4 ++-- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/plugins/guest-resources/src/utils.ts b/plugins/guest-resources/src/utils.ts index 30e15b4653..2e63271e7d 100644 --- a/plugins/guest-resources/src/utils.ts +++ b/plugins/guest-resources/src/utils.ts @@ -1,22 +1,37 @@ +import { + type AccountClient, + type WorkspaceLoginInfo, + getClient as getAccountClientRaw +} from '@hcengineering/account-client' import client from '@hcengineering/client' import { type Doc, AccountRole } from '@hcengineering/core' import login from '@hcengineering/login' -import { getMetadata, getResource } from '@hcengineering/platform' +import { getMetadata, getResource, setMetadata } from '@hcengineering/platform' import presentation from '@hcengineering/presentation' import { getCurrentLocation, navigate } from '@hcengineering/ui' import view from '@hcengineering/view' import { getObjectLinkFragment } from '@hcengineering/view-resources' import { workbenchId } from '@hcengineering/workbench' +function getAccountClient (token: string | undefined | null): AccountClient { + const accountsUrl = getMetadata(login.metadata.AccountsUrl) + return getAccountClientRaw(accountsUrl, token !== null ? token : undefined) +} + export async function checkAccess (doc: Doc): Promise { const loc = getCurrentLocation() const ws = loc.path[1] - const selectWorkspace = await getResource(login.function.SelectWorkspace) - const wsLoginInfo = (await selectWorkspace(ws, null))[1] - if (wsLoginInfo === undefined || wsLoginInfo.role === AccountRole.DocGuest) return + let wsLoginInfo: WorkspaceLoginInfo | undefined - const token = wsLoginInfo.token + try { + wsLoginInfo = await getAccountClient(null).selectWorkspace(ws) + if (wsLoginInfo === undefined || wsLoginInfo.role === AccountRole.DocGuest) return + } catch (err: any) { + return + } + + const token = wsLoginInfo?.token const endpoint = getMetadata(presentation.metadata.Endpoint) if (token === undefined || endpoint === undefined) return @@ -33,7 +48,7 @@ export async function checkAccess (doc: Doc): Promise { loc.path[0] = workbenchId loc.path[1] = ws // We have access, let's set correct tokens and redirect) - // setMetadata(presentation.metadata.Token, token) + setMetadata(presentation.metadata.Token, token) navigate(loc) } } diff --git a/tests/sanity/tests/tracker/tracker.spec.ts b/tests/sanity/tests/tracker/tracker.spec.ts index b119b24fab..217fc22dd5 100644 --- a/tests/sanity/tests/tracker/tracker.spec.ts +++ b/tests/sanity/tests/tracker/tracker.spec.ts @@ -159,7 +159,7 @@ test.describe('Tracker tests', () => { await issuesPage.clickAssignee() await issuesPage.setEstimation() await issuesPage.inputTextPlaceholderFill('1') - await issuesPage.setDueDate('24') + await issuesPage.setDueDate('19') await issuesPage.pressEscapeTwice() await issuesPage.clickOnNewIssue() await checkIssueDraft(page, { @@ -169,7 +169,7 @@ test.describe('Tracker tests', () => { priority: 'Urgent', assignee: 'Appleseed John', estimation: '1', - dueDate: '24' + dueDate: '19' }) })