Skip to content

Commit 275f443

Browse files
authored
Merge pull request #57 from import-ai/refactor/resources
fix(resources): check edit permission level
2 parents 767d0bd + 2aaefea commit 275f443

File tree

3 files changed

+73
-5
lines changed

3 files changed

+73
-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: 49 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,13 +367,54 @@ 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
}
370374
return false;
371375
}
372376

377+
async getCurrentLevel(
378+
namespaceId: string,
379+
resourceId: string,
380+
userId: string,
381+
): Promise<PermissionLevel> {
382+
let level = PermissionLevel.NO_ACCESS;
383+
const globalLevel = await this.getGlobalPermissionLevel(
384+
namespaceId,
385+
resourceId,
386+
);
387+
if (comparePermissionLevel(globalLevel, level) >= 0) {
388+
level = globalLevel;
389+
}
390+
const userPermi = await this.getUserPermission(
391+
namespaceId,
392+
resourceId,
393+
userId,
394+
);
395+
if (comparePermissionLevel(userPermi.level, level) >= 0) {
396+
level = userPermi.level;
397+
}
398+
const groups = await this.groupUserRepository.find({
399+
where: {
400+
namespace: { id: namespaceId },
401+
user: { id: userId },
402+
},
403+
relations: ['group'],
404+
});
405+
for (const group of groups) {
406+
const groupLevel = await this.getGroupPermissionLevel(
407+
namespaceId,
408+
resourceId,
409+
group.group.id,
410+
);
411+
if (comparePermissionLevel(groupLevel, level) >= 0) {
412+
level = groupLevel;
413+
}
414+
}
415+
return level;
416+
}
417+
373418
async getParentId(
374419
namespaceId: string,
375420
resourceId: string,

src/resources/resources.controller.ts

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

9096
@Patch(':resourceId')
@@ -98,6 +104,7 @@ export class ResourcesController {
98104
namespaceId,
99105
resourceId,
100106
req.user.id,
107+
PermissionLevel.CAN_EDIT,
101108
);
102109
if (!hasPermission) {
103110
throw new ForbiddenException('Not authorized');
@@ -115,6 +122,7 @@ export class ResourcesController {
115122
namespaceId,
116123
resourceId,
117124
req.user.id,
125+
PermissionLevel.CAN_EDIT,
118126
);
119127
if (!hasPermission) {
120128
throw new ForbiddenException('Not authorized');

0 commit comments

Comments
 (0)