Skip to content
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

feat(editor): Skip RBAC checks in preview mode (no-changelog) #8506

Merged
merged 7 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/cli/src/services/frontend.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export class FrontendService {
}

this.settings = {
previewMode: process.env.N8N_PREVIEW_MODE === 'true',
endpointForm: config.getEnv('endpoints.form'),
endpointFormTest: config.getEnv('endpoints.formTest'),
endpointFormWaiting: config.getEnv('endpoints.formWaiting'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ vi.mock('@/stores/users.store', () => ({

describe('Checks', () => {
describe('isAuthenticated()', () => {
const mockUser = { id: 'user123', name: 'Test User' };

it('should return true if there is a current user', () => {
const mockUser = { id: 'user123', name: 'Test User' };
vi.mocked(useUsersStore).mockReturnValue({ currentUser: mockUser } as unknown as ReturnType<
typeof useUsersStore
>);
Expand All @@ -23,5 +24,29 @@ describe('Checks', () => {

expect(isAuthenticated()).toBe(false);
});

it('should return true if there is a current user and bypass returns false', () => {
vi.mocked(useUsersStore).mockReturnValue({ currentUser: mockUser } as ReturnType<
typeof useUsersStore
>);

expect(isAuthenticated({ bypass: () => false })).toBe(true);
});

it('should return true if there is no current user and bypass returns true', () => {
vi.mocked(useUsersStore).mockReturnValue({ currentUser: null } as ReturnType<
typeof useUsersStore
>);

expect(isAuthenticated({ bypass: () => true })).toBe(true);
});

it('should return false if there is no current user and bypass returns false', () => {
vi.mocked(useUsersStore).mockReturnValue({ currentUser: null } as ReturnType<
typeof useUsersStore
>);

expect(isAuthenticated({ bypass: () => false })).toBe(false);
});
});
});
6 changes: 5 additions & 1 deletion packages/editor-ui/src/rbac/checks/isAuthenticated.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { useUsersStore } from '@/stores/users.store';
import type { RBACPermissionCheck, AuthenticatedPermissionOptions } from '@/types/rbac';

export const isAuthenticated: RBACPermissionCheck<AuthenticatedPermissionOptions> = () => {
export const isAuthenticated: RBACPermissionCheck<AuthenticatedPermissionOptions> = (options) => {
if (options?.bypass?.()) {
return true;
}

const usersStore = useUsersStore();
return !!usersStore.currentUser;
};
3 changes: 2 additions & 1 deletion packages/editor-ui/src/rbac/middleware/authenticated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ export const authenticatedMiddleware: RouterMiddleware<AuthenticatedPermissionOp
to,
from,
next,
options,
) => {
const valid = isAuthenticated();
const valid = isAuthenticated(options);
if (!valid) {
const redirect =
to.query.redirect ??
Expand Down
8 changes: 8 additions & 0 deletions packages/editor-ui/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ export const routes = [
},
meta: {
middleware: ['authenticated'],
middlewareOptions: {
authenticated: {
bypass: () => {
const settingsStore = useSettingsStore();
return settingsStore.isPreviewMode;
},
},
},
},
},
{
Expand Down
3 changes: 3 additions & 0 deletions packages/editor-ui/src/stores/settings.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ export const useSettingsStore = defineStore(STORES.SETTINGS, {
isSwaggerUIEnabled(): boolean {
return this.api.swaggerUi.enabled;
},
isPreviewMode(): boolean {
return this.settings.previewMode;
},
publicApiLatestVersion(): number {
return this.api.latestVersion;
},
Expand Down
4 changes: 3 additions & 1 deletion packages/editor-ui/src/types/rbac.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import type { EnterpriseEditionFeature } from '@/constants';
import type { Resource, ScopeOptions, Scope } from '@n8n/permissions';
import type { IRole } from '@/Interface';

export type AuthenticatedPermissionOptions = {};
export type AuthenticatedPermissionOptions = {
bypass?: () => boolean;
};
export type CustomPermissionOptions<C = {}> = RBACPermissionCheck<C>;
export type DefaultUserMiddlewareOptions = {};
export type InstanceOwnerMiddlewareOptions = {};
Expand Down
17 changes: 10 additions & 7 deletions packages/editor-ui/src/views/NodeView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -805,13 +805,16 @@ export default defineComponent({
this.clipboard.onPaste.value = this.onClipboardPasteEvent;

this.canvasStore.startLoading();
const loadPromises = [
this.loadActiveWorkflows(),
this.loadCredentials(),
this.loadCredentialTypes(),
this.loadVariables(),
this.loadSecrets(),
];
const loadPromises =
this.settingsStore.isPreviewMode && this.isDemo
? []
: [
this.loadActiveWorkflows(),
this.loadCredentials(),
this.loadCredentialTypes(),
this.loadVariables(),
this.loadSecrets(),
];

if (this.nodeTypesStore.allNodeTypes.length === 0) {
loadPromises.push(this.loadNodeTypes());
Expand Down
1 change: 1 addition & 0 deletions packages/workflow/src/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2505,6 +2505,7 @@ export interface IN8nUISettings {
workflowTagsDisabled: boolean;
logLevel: LogLevel;
hiringBannerEnabled: boolean;
previewMode: boolean;
templates: {
enabled: boolean;
host: string;
Expand Down
Loading