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: Implement runtine check for enterprise features #4676

Merged
merged 10 commits into from
Nov 22, 2022
29 changes: 19 additions & 10 deletions packages/cli/src/UserManagement/PermissionChecker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { INode, NodeOperationError, Workflow } from 'n8n-workflow';
import { In } from 'typeorm';
import { FindManyOptions, In, ObjectLiteral } from 'typeorm';
import * as Db from '@/Db';
import config from '@/config';
import type { SharedCredentials } from '@db/entities/SharedCredentials';
import { getRole } from './UserManagementHelper';

export class PermissionChecker {
/**
Expand All @@ -26,23 +29,29 @@ export class PermissionChecker {
// allow if all creds used in this workflow are a subset of
// all creds accessible to users who have access to this workflow

let workflowUserIds: string[] = [];
let workflowUserIds = [userId];

if (workflow.id) {
if (workflow.id && config.getEnv('enterprise.workflowSharingEnabled')) {
const workflowSharings = await Db.collections.SharedWorkflow.find({
relations: ['workflow'],
where: { workflow: { id: Number(workflow.id) } },
});

workflowUserIds = workflowSharings.map((s) => s.userId);
} else {
// unsaved workflows have no id, so only get credentials for current user
workflowUserIds = [userId];
}

const credentialSharings = await Db.collections.SharedCredentials.find({
where: { user: In(workflowUserIds) },
});
const credentialsWhereCondition: FindManyOptions<SharedCredentials> & { where: ObjectLiteral } =
{
where: { user: In(workflowUserIds) },
};

if (!config.getEnv('enterprise.features.sharing')) {
// If credential sharing is not enabled, get only credentials owned by this user
credentialsWhereCondition.where.role = await getRole('credential', 'owner');
}

const credentialSharings = await Db.collections.SharedCredentials.find(
credentialsWhereCondition,
);

const accessibleCredIds = credentialSharings.map((s) => s.credentialId.toString());

Expand Down
9 changes: 9 additions & 0 deletions packages/cli/src/UserManagement/UserManagementHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ export async function getInstanceOwner(): Promise<User> {
return owner;
}

export async function getRole(scope: Role['scope'], name: Role['name']): Promise<Role> {
return Db.collections.Role.findOneOrFail({
where: {
name,
scope,
},
});
}

/**
* Return the n8n instance base URL without trailing slash.
*/
Expand Down