n8n/packages/cli/src/services/access.service.ts
Daria 37aa56e6ff
feat: Autosave workflows (#23036)
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>
2026-01-09 18:40:24 +00:00

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;
}
}