refactor(base): use PageAccessService for BasePropertyController

This commit is contained in:
Philipinho
2026-04-27 01:28:28 +01:00
parent fd6a208235
commit 43cf1665f5
@@ -1,7 +1,6 @@
import { import {
Body, Body,
Controller, Controller,
ForbiddenException,
HttpCode, HttpCode,
HttpStatus, HttpStatus,
NotFoundException, NotFoundException,
@@ -20,11 +19,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/properties') @Controller('bases/properties')
@@ -32,7 +27,7 @@ export class BasePropertyController {
constructor( constructor(
private readonly basePropertyService: BasePropertyService, private readonly basePropertyService: BasePropertyService,
private readonly baseRepo: BaseRepo, private readonly baseRepo: BaseRepo,
private readonly spaceAbility: SpaceAbilityFactory, private readonly pageAccessService: PageAccessService,
) {} ) {}
@HttpCode(HttpStatus.OK) @HttpCode(HttpStatus.OK)
@@ -42,15 +37,12 @@ export class BasePropertyController {
@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.basePropertyService.create(workspace.id, dto, user.id); return this.basePropertyService.create(workspace.id, dto, user.id);
} }
@@ -62,15 +54,12 @@ export class BasePropertyController {
@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.basePropertyService.update(dto, workspace.id, user.id); return this.basePropertyService.update(dto, workspace.id, user.id);
} }
@@ -82,15 +71,12 @@ export class BasePropertyController {
@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.basePropertyService.delete(dto, workspace.id, user.id); await this.basePropertyService.delete(dto, workspace.id, user.id);
} }
@@ -102,15 +88,12 @@ export class BasePropertyController {
@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.basePropertyService.reorder(dto, workspace.id, user.id); await this.basePropertyService.reorder(dto, workspace.id, user.id);
} }