mirror of
https://github.com/n8n-io/n8n.git
synced 2024-09-27 18:27:36 -07:00
74 lines
2 KiB
TypeScript
74 lines
2 KiB
TypeScript
|
import { DeleteResult, EntityManager, In, Not } from 'typeorm';
|
||
|
import { Db } from '..';
|
||
|
import { SharedWorkflow } from '../databases/entities/SharedWorkflow';
|
||
|
import { User } from '../databases/entities/User';
|
||
|
import { WorkflowEntity } from '../databases/entities/WorkflowEntity';
|
||
|
import { RoleService } from '../role/role.service';
|
||
|
import { UserService } from '../user/user.service';
|
||
|
import { WorkflowsService } from './workflows.services';
|
||
|
|
||
|
export class EEWorkflowsService extends WorkflowsService {
|
||
|
static async isOwned(
|
||
|
user: User,
|
||
|
workflowId: string,
|
||
|
): Promise<{ ownsWorkflow: boolean; workflow?: WorkflowEntity }> {
|
||
|
const sharing = await this.getSharing(user, workflowId, ['workflow', 'role'], {
|
||
|
allowGlobalOwner: false,
|
||
|
});
|
||
|
|
||
|
if (!sharing || sharing.role.name !== 'owner') return { ownsWorkflow: false };
|
||
|
|
||
|
const { workflow } = sharing;
|
||
|
|
||
|
return { ownsWorkflow: true, workflow };
|
||
|
}
|
||
|
|
||
|
static async getSharings(
|
||
|
transaction: EntityManager,
|
||
|
workflowId: string,
|
||
|
): Promise<SharedWorkflow[]> {
|
||
|
const workflow = await transaction.findOne(WorkflowEntity, workflowId, {
|
||
|
relations: ['shared'],
|
||
|
});
|
||
|
return workflow?.shared ?? [];
|
||
|
}
|
||
|
|
||
|
static async pruneSharings(
|
||
|
transaction: EntityManager,
|
||
|
workflowId: string,
|
||
|
userIds: string[],
|
||
|
): Promise<DeleteResult> {
|
||
|
return transaction.delete(SharedWorkflow, {
|
||
|
workflow: { id: workflowId },
|
||
|
user: { id: Not(In(userIds)) },
|
||
|
});
|
||
|
}
|
||
|
|
||
|
static async share(
|
||
|
transaction: EntityManager,
|
||
|
workflow: WorkflowEntity,
|
||
|
shareWithIds: string[],
|
||
|
): Promise<SharedWorkflow[]> {
|
||
|
const [users, role] = await Promise.all([
|
||
|
UserService.getByIds(transaction, shareWithIds),
|
||
|
RoleService.trxGet(transaction, { scope: 'workflow', name: 'editor' }),
|
||
|
]);
|
||
|
|
||
|
const newSharedWorkflows = users.reduce<SharedWorkflow[]>((acc, user) => {
|
||
|
if (user.isPending) {
|
||
|
return acc;
|
||
|
}
|
||
|
acc.push(
|
||
|
Db.collections.SharedWorkflow.create({
|
||
|
workflow,
|
||
|
user,
|
||
|
role,
|
||
|
}),
|
||
|
);
|
||
|
return acc;
|
||
|
}, []);
|
||
|
|
||
|
return transaction.save(newSharedWorkflows);
|
||
|
}
|
||
|
}
|