Skip to content

Commit 858530f

Browse files
ycdzjLucienShui
authored andcommitted
fix(resources): check edit permission level
1 parent 767d0bd commit 858530f

File tree

3 files changed

+33
-5
lines changed

3 files changed

+33
-5
lines changed

src/permissions/permission-level.enum.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,18 @@ export enum PermissionLevel {
55
CAN_EDIT = 'can_edit',
66
FULL_ACCESS = 'full_access',
77
}
8+
9+
const order = [
10+
PermissionLevel.NO_ACCESS,
11+
PermissionLevel.CAN_VIEW,
12+
PermissionLevel.CAN_COMMENT,
13+
PermissionLevel.CAN_EDIT,
14+
PermissionLevel.FULL_ACCESS,
15+
];
16+
17+
export function comparePermissionLevel(
18+
a: PermissionLevel,
19+
b: PermissionLevel,
20+
): number {
21+
return order.indexOf(a) - order.indexOf(b);
22+
}

src/permissions/permissions.service.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@ import { DataSource, EntityManager, IsNull, Repository } from 'typeorm';
44
import { PermissionDto } from './dto/permission.dto';
55
import { ListRespDto, UserPermissionDto } from './dto/list-resp.dto';
66
import { plainToInstance } from 'class-transformer';
7-
import { PermissionLevel } from './permission-level.enum';
7+
import {
8+
comparePermissionLevel,
9+
PermissionLevel,
10+
} from './permission-level.enum';
811
import { UserPermission } from './entities/user-permission.entity';
912
import { GroupPermission } from './entities/group-permission.entity';
1013
import { Resource } from 'src/resources/resources.entity';
@@ -334,20 +337,21 @@ export class PermissionsService {
334337
namespaceId: string,
335338
resourceId: string,
336339
userId: string,
340+
level: PermissionLevel = PermissionLevel.CAN_VIEW,
337341
) {
338342
const globalLevel = await this.getGlobalPermissionLevel(
339343
namespaceId,
340344
resourceId,
341345
);
342-
if (globalLevel != PermissionLevel.NO_ACCESS) {
346+
if (comparePermissionLevel(globalLevel, level) >= 0) {
343347
return true;
344348
}
345349
const userPermi = await this.getUserPermission(
346350
namespaceId,
347351
resourceId,
348352
userId,
349353
);
350-
if (userPermi.level != PermissionLevel.NO_ACCESS) {
354+
if (comparePermissionLevel(userPermi.level, level) >= 0) {
351355
return true;
352356
}
353357
const groups = await this.groupUserRepository.find({
@@ -363,7 +367,7 @@ export class PermissionsService {
363367
resourceId,
364368
group.group.id,
365369
);
366-
if (groupLevel != PermissionLevel.NO_ACCESS) {
370+
if (comparePermissionLevel(groupLevel, level) >= 0) {
367371
return true;
368372
}
369373
}

src/resources/resources.controller.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,14 @@ export class ResourcesController {
8484
if (!hasPermission) {
8585
throw new ForbiddenException('Not authorized');
8686
}
87-
return await this.resourcesService.get(resourceId);
87+
const canEdit = await this.permissionsService.userHasPermission(
88+
namespaceId,
89+
resourceId,
90+
req.user.id,
91+
PermissionLevel.CAN_EDIT,
92+
);
93+
const resource = await this.resourcesService.get(resourceId);
94+
return { ...resource, canEdit };
8895
}
8996

9097
@Patch(':resourceId')
@@ -98,6 +105,7 @@ export class ResourcesController {
98105
namespaceId,
99106
resourceId,
100107
req.user.id,
108+
PermissionLevel.CAN_EDIT,
101109
);
102110
if (!hasPermission) {
103111
throw new ForbiddenException('Not authorized');
@@ -115,6 +123,7 @@ export class ResourcesController {
115123
namespaceId,
116124
resourceId,
117125
req.user.id,
126+
PermissionLevel.CAN_EDIT,
118127
);
119128
if (!hasPermission) {
120129
throw new ForbiddenException('Not authorized');

0 commit comments

Comments
 (0)