Skip to content

Commit

Permalink
fix: Add role check for upgrade path (n8n-io#7374)
Browse files Browse the repository at this point in the history
Github issue / Community forum post (link here to close automatically):

---------

Co-authored-by: Mutasem <mutdmour@gmail.com>
  • Loading branch information
RicardoE105 and mutdmour authored Oct 9, 2023
1 parent 3fa2764 commit a43f720
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/editor-ui/src/api/cloudPlans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ export async function confirmEmail(context: IRestApiContext): Promise<Cloud.User
}

export async function getAdminPanelLoginCode(context: IRestApiContext): Promise<{ code: string }> {
return get(context.baseUrl, '/admin/login/code');
return get(context.baseUrl, '/cloud/proxy/login/code');
}
22 changes: 17 additions & 5 deletions packages/editor-ui/src/stores/__tests__/ui.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ import {
getUserCloudInfo,
getNotTrialingUserResponse,
} from './utils/cloudStoreUtils';
import type { IRole } from '@/Interface';

let uiStore: ReturnType<typeof useUIStore>;
let settingsStore: ReturnType<typeof useSettingsStore>;
let rootStore: ReturnType<typeof useRootStore>;
let cloudPlanStore: ReturnType<typeof useCloudPlanStore>;

function setOwnerUser() {
function setUser(role: IRole) {
useUsersStore().addUsers([
{
id: '1',
isPending: false,
globalRole: {
id: '1',
name: 'owner',
name: role,
createdAt: new Date(),
},
},
Expand All @@ -35,7 +36,7 @@ function setOwnerUser() {
}

function setupOwnerAndCloudDeployment() {
setOwnerUser();
setUser('owner');
settingsStore.setSettings(
merge({}, SETTINGS_STORE_DEFAULT_STATE.settings, {
n8nMetadata: {
Expand Down Expand Up @@ -77,23 +78,34 @@ describe('UI store', () => {
[
'default',
'production',
'owner',
'https://n8n.io/pricing?utm_campaign=utm-test-campaign&source=test_source',
],
[
'default',
'development',
'owner',
'https://n8n.io/pricing?utm_campaign=utm-test-campaign&source=test_source',
],
[
'cloud',
'production',
'owner',
`https://app.n8n.cloud/login?code=123&returnPath=${encodeURIComponent(
'/account/change-plan',
)}&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',
async (type, environment, expectation) => {
'"upgradeLinkUrl" should generate the correct URL for "%s" deployment and "%s" license environment and user role "%s"',
async (type, environment, role, expectation) => {
setUser(role as IRole);

settingsStore.setSettings(
merge({}, SETTINGS_STORE_DEFAULT_STATE.settings, {
deployment: {
Expand Down
8 changes: 5 additions & 3 deletions packages/editor-ui/src/stores/ui.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ import { defineStore } from 'pinia';
import { useRootStore } from '@/stores/n8nRoot.store';
import { getCurlToJson } from '@/api/curlHelper';
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 { useTelemetryStore } from '@/stores/telemetry.store';
import { getStyleTokenValue } from '@/utils/htmlUtils';
Expand Down Expand Up @@ -346,9 +346,11 @@ export const useUIStore = defineStore(STORES.UI, {

const searchParams = new URLSearchParams();

if (deploymentType === 'cloud') {
const { code } = await useCloudPlanStore().getAutoLoginCode();
const isOwner = useUsersStore().isInstanceOwner;

if (deploymentType === 'cloud' && isOwner) {
const adminPanelHost = new URL(window.location.href).host.split('.').slice(1).join('.');
const { code } = await useCloudPlanStore().getAutoLoginCode();
linkUrl = `https://${adminPanelHost}/login`;
searchParams.set('code', code);
searchParams.set('returnPath', '/account/change-plan');
Expand Down

0 comments on commit a43f720

Please sign in to comment.