mirror of
https://github.com/n8n-io/n8n.git
synced 2026-10-21 20:20:03 +02:00
Co-authored-by: Svetoslav Dekov <svetoslav.dekov@n8n.io> Co-authored-by: Artem Sorokin <artem@n8n.io> Co-authored-by: Charlie Kolb <charlie@n8n.io> Co-authored-by: Declan Carroll <declan@n8n.io> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> Co-authored-by: claude[bot] <209825114+claude[bot]@users.noreply.github.com> Co-authored-by: Svetoslav Dekov <scdekov@users.noreply.github.com> Co-authored-by: Robert Squires <robtf9@icloud.com> Co-authored-by: Michael Drury <me@michaeldrury.co.uk>
43 lines
1.3 KiB
TypeScript
43 lines
1.3 KiB
TypeScript
import type { User } from '@n8n/db';
|
|
import { UserRepository } from '@n8n/db';
|
|
import { Service } from '@n8n/di';
|
|
import type { Workflow } from 'n8n-workflow';
|
|
|
|
import { WorkflowFinderService } from '@/workflows/workflow-finder.service';
|
|
|
|
/**
|
|
* Responsible for checking whether a user has access to a resource.
|
|
*/
|
|
@Service()
|
|
export class AccessService {
|
|
constructor(
|
|
private readonly userRepository: UserRepository,
|
|
private readonly workflowFinderService: WorkflowFinderService,
|
|
) {}
|
|
|
|
/** Whether a user has read access to a workflow based on their project and scope. */
|
|
async hasReadAccess(userId: User['id'], workflowId: Workflow['id']) {
|
|
const user = await this.userRepository.findOne({ where: { id: userId }, relations: ['role'] });
|
|
|
|
if (!user) return false;
|
|
|
|
const workflow = await this.workflowFinderService.findWorkflowForUser(workflowId, user, [
|
|
'workflow:read',
|
|
]);
|
|
|
|
return workflow !== null;
|
|
}
|
|
|
|
async hasWriteAccess(userId: User['id'], workflowId: Workflow['id']) {
|
|
const user = await this.userRepository.findOne({ where: { id: userId }, relations: ['role'] });
|
|
|
|
if (!user) return false;
|
|
|
|
const workflow = await this.workflowFinderService.findWorkflowForUser(workflowId, user, [
|
|
'workflow:update',
|
|
]);
|
|
|
|
return workflow !== null;
|
|
}
|
|
}
|