n8n/packages/cli/test/integration/shared/db/projects.ts
Csaba Tuncsik 596c472ecc
feat: RBAC (#8922)
Signed-off-by: Oleg Ivaniv <me@olegivaniv.com>
Co-authored-by: Val <68596159+valya@users.noreply.github.com>
Co-authored-by: कारतोफ्फेलस्क्रिप्ट™ <aditya@netroy.in>
Co-authored-by: Valya Bullions <valya@n8n.io>
Co-authored-by: Danny Martini <danny@n8n.io>
Co-authored-by: Danny Martini <despair.blue@gmail.com>
Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
Co-authored-by: Omar Ajoue <krynble@gmail.com>
Co-authored-by: oleg <me@olegivaniv.com>
Co-authored-by: Michael Kret <michael.k@radency.com>
Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com>
Co-authored-by: Elias Meire <elias@meire.dev>
Co-authored-by: Giulio Andreini <andreini@netseven.it>
Co-authored-by: Giulio Andreini <g.andreini@gmail.com>
Co-authored-by: Ayato Hayashi <go12limchangyong@gmail.com>
2024-05-17 10:53:15 +02:00

64 lines
1.8 KiB
TypeScript

import Container from 'typedi';
import { ProjectRepository } from '@/databases/repositories/project.repository';
import { randomName } from '../random';
import { ProjectRelationRepository } from '@/databases/repositories/projectRelation.repository';
import type { User } from '@/databases/entities/User';
import type { Project } from '@/databases/entities/Project';
import type { ProjectRelation, ProjectRole } from '@/databases/entities/ProjectRelation';
export const createTeamProject = async (name?: string, adminUser?: User) => {
const projectRepository = Container.get(ProjectRepository);
const project = await projectRepository.save(
projectRepository.create({
name: name ?? randomName(),
type: 'team',
}),
);
if (adminUser) {
await linkUserToProject(adminUser, project, 'project:admin');
}
return project;
};
export const linkUserToProject = async (user: User, project: Project, role: ProjectRole) => {
const projectRelationRepository = Container.get(ProjectRelationRepository);
await projectRelationRepository.save(
projectRelationRepository.create({
projectId: project.id,
userId: user.id,
role,
}),
);
};
export const getPersonalProject = async (user: User): Promise<Project> => {
return await Container.get(ProjectRepository).findOneOrFail({
where: {
projectRelations: {
userId: user.id,
role: 'project:personalOwner',
},
type: 'personal',
},
});
};
export const findProject = async (id: string): Promise<Project> => {
return await Container.get(ProjectRepository).findOneOrFail({
where: { id },
});
};
export const getProjectRelations = async ({
projectId,
userId,
role,
}: Partial<ProjectRelation>): Promise<ProjectRelation[]> => {
return await Container.get(ProjectRelationRepository).find({
where: { projectId, userId, role },
});
};