refactor(base): use PageAccessService for BaseViewController

This commit is contained in:
Philipinho
2026-04-27 01:29:23 +01:00
parent 2e7fe5bbb4
commit 78d450a238
@@ -1,7 +1,6 @@
import { import {
Body, Body,
Controller, Controller,
ForbiddenException,
HttpCode, HttpCode,
HttpStatus, HttpStatus,
NotFoundException, NotFoundException,
@@ -17,11 +16,7 @@ import { AuthUser } from '../../../common/decorators/auth-user.decorator';
import { AuthWorkspace } from '../../../common/decorators/auth-workspace.decorator'; import { AuthWorkspace } from '../../../common/decorators/auth-workspace.decorator';
import { JwtAuthGuard } from '../../../common/guards/jwt-auth.guard'; import { JwtAuthGuard } from '../../../common/guards/jwt-auth.guard';
import { User, Workspace } from '@docmost/db/types/entity.types'; import { User, Workspace } from '@docmost/db/types/entity.types';
import { import { PageAccessService } from '../../page/page-access/page-access.service';
SpaceCaslAction,
SpaceCaslSubject,
} from '../../casl/interfaces/space-ability.type';
import SpaceAbilityFactory from '../../casl/abilities/space-ability.factory';
@UseGuards(JwtAuthGuard) @UseGuards(JwtAuthGuard)
@Controller('bases/views') @Controller('bases/views')
@@ -29,7 +24,7 @@ export class BaseViewController {
constructor( constructor(
private readonly baseViewService: BaseViewService, private readonly baseViewService: BaseViewService,
private readonly baseRepo: BaseRepo, private readonly baseRepo: BaseRepo,
private readonly spaceAbility: SpaceAbilityFactory, private readonly pageAccessService: PageAccessService,
) {} ) {}
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@@ -39,15 +34,12 @@ export class BaseViewController {
@AuthUser() user: User, @AuthUser() user: User,
@AuthWorkspace() workspace: Workspace, @AuthWorkspace() workspace: Workspace,
) { ) {
const base = await this.baseRepo.findById(dto.baseId); const base = await this.baseRepo.findById(dto.pageId);
if (!base) { if (!base) {
throw new NotFoundException('Base not found'); throw new NotFoundException('Base not found');
} }
const ability = await this.spaceAbility.createForUser(user, base.spaceId); await this.pageAccessService.validateCanEdit(base, user);
if (ability.cannot(SpaceCaslAction.Edit, SpaceCaslSubject.Base)) {
throw new ForbiddenException();
}
return this.baseViewService.create(user.id, workspace.id, dto); return this.baseViewService.create(user.id, workspace.id, dto);
} }
@@ -59,15 +51,12 @@ export class BaseViewController {
@AuthUser() user: User, @AuthUser() user: User,
@AuthWorkspace() workspace: Workspace, @AuthWorkspace() workspace: Workspace,
) { ) {
const base = await this.baseRepo.findById(dto.baseId); const base = await this.baseRepo.findById(dto.pageId);
if (!base) { if (!base) {
throw new NotFoundException('Base not found'); throw new NotFoundException('Base not found');
} }
const ability = await this.spaceAbility.createForUser(user, base.spaceId); await this.pageAccessService.validateCanEdit(base, user);
if (ability.cannot(SpaceCaslAction.Edit, SpaceCaslSubject.Base)) {
throw new ForbiddenException();
}
return this.baseViewService.update(dto, workspace.id, user.id); return this.baseViewService.update(dto, workspace.id, user.id);
} }
@@ -79,15 +68,12 @@ export class BaseViewController {
@AuthUser() user: User, @AuthUser() user: User,
@AuthWorkspace() workspace: Workspace, @AuthWorkspace() workspace: Workspace,
) { ) {
const base = await this.baseRepo.findById(dto.baseId); const base = await this.baseRepo.findById(dto.pageId);
if (!base) { if (!base) {
throw new NotFoundException('Base not found'); throw new NotFoundException('Base not found');
} }
const ability = await this.spaceAbility.createForUser(user, base.spaceId); await this.pageAccessService.validateCanEdit(base, user);
if (ability.cannot(SpaceCaslAction.Edit, SpaceCaslSubject.Base)) {
throw new ForbiddenException();
}
await this.baseViewService.delete(dto, workspace.id, user.id); await this.baseViewService.delete(dto, workspace.id, user.id);
} }
@@ -99,16 +85,13 @@ export class BaseViewController {
@AuthUser() user: User, @AuthUser() user: User,
@AuthWorkspace() workspace: Workspace, @AuthWorkspace() workspace: Workspace,
) { ) {
const base = await this.baseRepo.findById(dto.baseId); const base = await this.baseRepo.findById(dto.pageId);
if (!base) { if (!base) {
throw new NotFoundException('Base not found'); throw new NotFoundException('Base not found');
} }
const ability = await this.spaceAbility.createForUser(user, base.spaceId); await this.pageAccessService.validateCanView(base, user);
if (ability.cannot(SpaceCaslAction.Read, SpaceCaslSubject.Base)) {
throw new ForbiddenException();
}
return this.baseViewService.listByBaseId(dto.baseId, workspace.id); return this.baseViewService.listByBaseId(dto.pageId, workspace.id);
} }
} }