Kind: Service
Source: atloria-monorepo/apps/api/src/doc-version/version-access.guard.ts
VersionAccessGuard is a NestJS authorization guard that determines whether the current request is allowed to access a document version. It runs before protected route handlers, using request context and document-version ownership or permission data to allow the request or reject it with an authorization error.
Methods
| Method | Signature | Returns |
|---|---|---|
canActivate | canActivate(context: ExecutionContext) | Promise<boolean> |
Dependencies
PrismaService
Where it refuses work
VersionAccessGuardstops the work withForbiddenExceptionwhen!request.user— “Authentication required”, in 2 places.VersionAccessGuardstops the work withForbiddenExceptionwhen!pwd— “Password required”.VersionAccessGuardstops the work withForbiddenExceptionwhenpwd !== access.password— “Invalid password”.VersionAccessGuardstops the work with an early return when!versionId.VersionAccessGuardstops the work with an early return when!access || access.accessType === 'PUBLIC'.VersionAccessGuardstops the work with an early return whenaccess.allowedUserIds.includes(userId).
Diagram
mermaidsequenceDiagram participant Client participant Controller participant Guard as VersionAccessGuard participant Service as Document Version Service participant Handler as Route Handler Client->>Controller: Request protected version endpoint Controller->>Guard: canActivate(context) Guard->>Guard: Read authenticated user and route parameters Guard->>Service: Resolve version and verify access Service-->>Guard: Access result alt Access granted Guard-->>Controller: true Controller->>Handler: Execute handler Handler-->>Client: Return version data else Access denied Guard-->>Controller: false / throw exception Controller-->>Client: 403 Forbidden end
Usage
tsimport { Controller, Get, Param, UseGuards } from '@nestjs/common';
import { VersionAccessGuard } from './version-access.guard';
@Controller('document-versions')
export class DocumentVersionController {
@Get(':versionId')
@UseGuards(VersionAccessGuard)
async getVersion(@Param('versionId') versionId: string) {
// The guard has already verified that the authenticated user
// can access this document version.
return {
id: versionId,
};
}
}
AI Coding Instructions
- Apply
VersionAccessGuardwith@UseGuards()on every endpoint that exposes, modifies, or deletes protected document-version data. - Keep authorization logic inside the guard or delegated domain services; controllers should assume access has already been validated.
- Read authenticated user data from the NestJS execution context consistently with the application's authentication strategy.
- Ensure route parameter names used by the guard match the controller route, such as
:versionId. - Return
trueonly after verifying access; use NestJS authorization exceptions for denied access when detailed error handling is required.
Relationships
- DEPENDS_ON →
PrismaService
Referenced By
DocVersionModule(MODULE_PROVIDES)DocVersionModule(MODULE_EXPORTS)
Was this page helpful?