feat(editor): Replace middleware for Role checks with Scope checks (#7847)

This commit is contained in:
Alex Grozav
2023-11-29 10:35:40 +02:00
committed by GitHub
parent d4970410e1
commit 72852a60eb
18 changed files with 267 additions and 111 deletions

View File

@@ -1,6 +1,6 @@
import { useRBACStore } from '@/stores/rbac.store';
import { hasScope } from '@/rbac/checks/hasScope';
import type { HasScopeOptions } from '@n8n/permissions';
import type { ScopeOptions } from '@n8n/permissions';
vi.mock('@/stores/rbac.store', () => ({
useRBACStore: vi.fn(),
@@ -19,7 +19,7 @@ describe('Checks', () => {
} as unknown as ReturnType<typeof useRBACStore>);
const scope = 'workflow:read';
const options: HasScopeOptions = { mode: 'allOf' };
const options: ScopeOptions = { mode: 'allOf' };
const projectId = 'proj123';
const resourceType = 'workflow';
const resourceId = 'res123';

View File

@@ -0,0 +1,27 @@
import { useUsersStore } from '@/stores/users.store';
import { isDefaultUser } from '@/rbac/checks/isDefaultUser';
vi.mock('@/stores/users.store', () => ({
useUsersStore: vi.fn(),
}));
describe('Checks', () => {
describe('isDefaultUser()', () => {
it('should return false if user not logged in', () => {
vi.mocked(useUsersStore).mockReturnValue({ currentUser: null } as ReturnType<
typeof useUsersStore
>);
expect(isDefaultUser()).toBe(false);
});
it('should return true if user is default user', () => {
const mockUser = { id: 'user123', name: 'Test User', isDefaultUser: true };
vi.mocked(useUsersStore).mockReturnValue({ currentUser: mockUser } as unknown as ReturnType<
typeof useUsersStore
>);
expect(isDefaultUser()).toBe(mockUser.isDefaultUser);
});
});
});

View File

@@ -1,6 +1,7 @@
export * from './hasRole';
export * from './hasScope';
export * from './isAuthenticated';
export * from './isDefaultUser';
export * from './isEnterpriseFeatureEnabled';
export * from './isGuest';
export * from './isValid';

View File

@@ -0,0 +1,12 @@
import { useUsersStore } from '@/stores/users.store';
import type { DefaultUserMiddlewareOptions, RBACPermissionCheck } from '@/types/rbac';
export const isDefaultUser: RBACPermissionCheck<DefaultUserMiddlewareOptions> = () => {
const usersStore = useUsersStore();
const currentUser = usersStore.currentUser;
if (currentUser) {
return currentUser.isDefaultUser;
}
return false;
};