fix: Add role check for upgrade path (#7374)

Github issue / Community forum post (link here to close automatically):

---------

Co-authored-by: Mutasem <mutdmour@gmail.com>
This commit is contained in:
Ricardo Espinoza 2023-10-09 11:18:08 +02:00 committed by GitHub
parent 3fa27647d8
commit a43f720658
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 9 deletions

View file

@ -18,5 +18,5 @@ export async function confirmEmail(context: IRestApiContext): Promise<Cloud.User
} }
export async function getAdminPanelLoginCode(context: IRestApiContext): Promise<{ code: string }> { export async function getAdminPanelLoginCode(context: IRestApiContext): Promise<{ code: string }> {
return get(context.baseUrl, '/admin/login/code'); return get(context.baseUrl, '/cloud/proxy/login/code');
} }

View file

@ -12,20 +12,21 @@ import {
getUserCloudInfo, getUserCloudInfo,
getNotTrialingUserResponse, getNotTrialingUserResponse,
} from './utils/cloudStoreUtils'; } from './utils/cloudStoreUtils';
import type { IRole } from '@/Interface';
let uiStore: ReturnType<typeof useUIStore>; let uiStore: ReturnType<typeof useUIStore>;
let settingsStore: ReturnType<typeof useSettingsStore>; let settingsStore: ReturnType<typeof useSettingsStore>;
let rootStore: ReturnType<typeof useRootStore>; let rootStore: ReturnType<typeof useRootStore>;
let cloudPlanStore: ReturnType<typeof useCloudPlanStore>; let cloudPlanStore: ReturnType<typeof useCloudPlanStore>;
function setOwnerUser() { function setUser(role: IRole) {
useUsersStore().addUsers([ useUsersStore().addUsers([
{ {
id: '1', id: '1',
isPending: false, isPending: false,
globalRole: { globalRole: {
id: '1', id: '1',
name: 'owner', name: role,
createdAt: new Date(), createdAt: new Date(),
}, },
}, },
@ -35,7 +36,7 @@ function setOwnerUser() {
} }
function setupOwnerAndCloudDeployment() { function setupOwnerAndCloudDeployment() {
setOwnerUser(); setUser('owner');
settingsStore.setSettings( settingsStore.setSettings(
merge({}, SETTINGS_STORE_DEFAULT_STATE.settings, { merge({}, SETTINGS_STORE_DEFAULT_STATE.settings, {
n8nMetadata: { n8nMetadata: {
@ -77,23 +78,34 @@ describe('UI store', () => {
[ [
'default', 'default',
'production', 'production',
'owner',
'https://n8n.io/pricing?utm_campaign=utm-test-campaign&source=test_source', 'https://n8n.io/pricing?utm_campaign=utm-test-campaign&source=test_source',
], ],
[ [
'default', 'default',
'development', 'development',
'owner',
'https://n8n.io/pricing?utm_campaign=utm-test-campaign&source=test_source', 'https://n8n.io/pricing?utm_campaign=utm-test-campaign&source=test_source',
], ],
[ [
'cloud', 'cloud',
'production', 'production',
'owner',
`https://app.n8n.cloud/login?code=123&returnPath=${encodeURIComponent( `https://app.n8n.cloud/login?code=123&returnPath=${encodeURIComponent(
'/account/change-plan', '/account/change-plan',
)}&utm_campaign=utm-test-campaign&source=test_source`, )}&utm_campaign=utm-test-campaign&source=test_source`,
], ],
[
'cloud',
'production',
'member',
'https://n8n.io/pricing?utm_campaign=utm-test-campaign&source=test_source',
],
])( ])(
'"upgradeLinkUrl" should generate the correct URL for "%s" deployment and "%s" license environment', '"upgradeLinkUrl" should generate the correct URL for "%s" deployment and "%s" license environment and user role "%s"',
async (type, environment, expectation) => { async (type, environment, role, expectation) => {
setUser(role as IRole);
settingsStore.setSettings( settingsStore.setSettings(
merge({}, SETTINGS_STORE_DEFAULT_STATE.settings, { merge({}, SETTINGS_STORE_DEFAULT_STATE.settings, {
deployment: { deployment: {

View file

@ -55,7 +55,7 @@ import { defineStore } from 'pinia';
import { useRootStore } from '@/stores/n8nRoot.store'; import { useRootStore } from '@/stores/n8nRoot.store';
import { getCurlToJson } from '@/api/curlHelper'; import { getCurlToJson } from '@/api/curlHelper';
import { useWorkflowsStore } from '@/stores/workflows.store'; import { useWorkflowsStore } from '@/stores/workflows.store';
import { useSettingsStore } from '@/stores/settings.store'; import { useSettingsStore, useUsersStore } from '@/stores/settings.store';
import { useCloudPlanStore } from '@/stores/cloudPlan.store'; import { useCloudPlanStore } from '@/stores/cloudPlan.store';
import { useTelemetryStore } from '@/stores/telemetry.store'; import { useTelemetryStore } from '@/stores/telemetry.store';
import { getStyleTokenValue } from '@/utils/htmlUtils'; import { getStyleTokenValue } from '@/utils/htmlUtils';
@ -346,9 +346,11 @@ export const useUIStore = defineStore(STORES.UI, {
const searchParams = new URLSearchParams(); const searchParams = new URLSearchParams();
if (deploymentType === 'cloud') { const isOwner = useUsersStore().isInstanceOwner;
const { code } = await useCloudPlanStore().getAutoLoginCode();
if (deploymentType === 'cloud' && isOwner) {
const adminPanelHost = new URL(window.location.href).host.split('.').slice(1).join('.'); const adminPanelHost = new URL(window.location.href).host.split('.').slice(1).join('.');
const { code } = await useCloudPlanStore().getAutoLoginCode();
linkUrl = `https://${adminPanelHost}/login`; linkUrl = `https://${adminPanelHost}/login`;
searchParams.set('code', code); searchParams.set('code', code);
searchParams.set('returnPath', '/account/change-plan'); searchParams.set('returnPath', '/account/change-plan');