@boarteam/boar-pack-users-backend 6.1.0 → 6.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@boarteam/boar-pack-users-backend",
|
|
3
|
-
"version": "6.
|
|
3
|
+
"version": "6.2.0",
|
|
4
4
|
"description": "NestJS Users module including permissions system, authentication strategies etc",
|
|
5
5
|
"main": "src/index",
|
|
6
6
|
"files": [
|
|
@@ -64,5 +64,5 @@
|
|
|
64
64
|
"yalc:push": "yalc push",
|
|
65
65
|
"gen-types": "SWAGGER=true JWT_SECRET=swagger nest start"
|
|
66
66
|
},
|
|
67
|
-
"gitHead": "
|
|
67
|
+
"gitHead": "8dcc9ac7eeadf49314ca1c8dc9ea6c150281adec"
|
|
68
68
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { IPolicyHandler } from "../../casl/policies.guard";
|
|
2
|
+
import { AppAbility } from "../../casl/casl-ability.factory";
|
|
3
|
+
import { Action } from "../../casl/action.enum";
|
|
4
|
+
import { User } from "../entities/user.entity";
|
|
5
|
+
|
|
6
|
+
export class ManageUsersPolicy implements IPolicyHandler {
|
|
7
|
+
handle(ability: AppAbility) {
|
|
8
|
+
return ability.can(Action.Manage, User);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -9,11 +9,18 @@ import {
|
|
|
9
9
|
import { Request } from 'express';
|
|
10
10
|
import { getAction } from "@dataui/crud";
|
|
11
11
|
import { isEqual } from 'lodash';
|
|
12
|
+
import { Action, CaslAbilityFactory } from "../casl";
|
|
13
|
+
import { Roles } from "./entities/user.entity";
|
|
12
14
|
|
|
13
15
|
@Injectable()
|
|
14
16
|
export class UsersEditingGuard implements CanActivate {
|
|
15
17
|
private readonly logger = new Logger(UsersEditingGuard.name);
|
|
16
18
|
|
|
19
|
+
constructor(
|
|
20
|
+
private readonly calsAbilityFactory: CaslAbilityFactory,
|
|
21
|
+
) {
|
|
22
|
+
}
|
|
23
|
+
|
|
17
24
|
async canActivate(context: ExecutionContext): Promise<boolean> {
|
|
18
25
|
const request = context.switchToHttp().getRequest<Request>();
|
|
19
26
|
const user = request.user;
|
|
@@ -25,7 +32,16 @@ export class UsersEditingGuard implements CanActivate {
|
|
|
25
32
|
|
|
26
33
|
const editingUserId = request.params['id'];
|
|
27
34
|
switch (getAction(context.getHandler())) {
|
|
28
|
-
case '
|
|
35
|
+
case 'Create-One': {
|
|
36
|
+
const ability = await this.calsAbilityFactory.createForUser(user);
|
|
37
|
+
if (request.body['role'] === Roles.ADMIN && !ability.can(Action.Manage, 'all')) {
|
|
38
|
+
this.logger.warn(`User can't create admin`);
|
|
39
|
+
throw new ForbiddenException(`User can't create admin`);
|
|
40
|
+
}
|
|
41
|
+
break;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
case 'Update-One': {
|
|
29
45
|
if (editingUserId === user.id) {
|
|
30
46
|
const newRole = request.body['role'];
|
|
31
47
|
if (newRole !== user.role && newRole !== undefined) {
|
|
@@ -45,14 +61,23 @@ export class UsersEditingGuard implements CanActivate {
|
|
|
45
61
|
throw new ForbiddenException(`User can't change his permissions`);
|
|
46
62
|
}
|
|
47
63
|
}
|
|
64
|
+
|
|
65
|
+
const ability = await this.calsAbilityFactory.createForUser(user);
|
|
66
|
+
if (request.body['role'] === Roles.ADMIN && !ability.can(Action.Manage, 'all')) {
|
|
67
|
+
this.logger.warn(`User can't change role to admin`);
|
|
68
|
+
throw new ForbiddenException(`User can't change role to admin`);
|
|
69
|
+
}
|
|
70
|
+
|
|
48
71
|
break;
|
|
72
|
+
}
|
|
49
73
|
|
|
50
|
-
case 'Delete-One':
|
|
74
|
+
case 'Delete-One': {
|
|
51
75
|
if (editingUserId === user.id) {
|
|
52
76
|
this.logger.warn(`User can't delete himself`);
|
|
53
77
|
throw new ForbiddenException(`User can't delete himself`);
|
|
54
78
|
}
|
|
55
79
|
break;
|
|
80
|
+
}
|
|
56
81
|
}
|
|
57
82
|
|
|
58
83
|
return true;
|
|
@@ -11,6 +11,7 @@ import { PermissionDto } from "./dto/permission.dto";
|
|
|
11
11
|
import { UsersEditingGuard } from "./users-editing.guard";
|
|
12
12
|
import { ViewUsersPolicy } from "./policies/view-users.policy";
|
|
13
13
|
import { Tools } from "@boarteam/boar-pack-common-backend";
|
|
14
|
+
import { ManageUsersPolicy } from "./policies/manage-users.policy";
|
|
14
15
|
|
|
15
16
|
@Crud({
|
|
16
17
|
model: {
|
|
@@ -44,6 +45,9 @@ import { Tools } from "@boarteam/boar-pack-common-backend";
|
|
|
44
45
|
interceptors: [
|
|
45
46
|
HashPasswordInterceptor,
|
|
46
47
|
],
|
|
48
|
+
decorators: [
|
|
49
|
+
UseGuards(UsersEditingGuard),
|
|
50
|
+
]
|
|
47
51
|
},
|
|
48
52
|
updateOneBase: {
|
|
49
53
|
interceptors: [
|
|
@@ -64,7 +68,7 @@ import { Tools } from "@boarteam/boar-pack-common-backend";
|
|
|
64
68
|
update: UserUpdateDto,
|
|
65
69
|
},
|
|
66
70
|
})
|
|
67
|
-
@CheckPolicies(new
|
|
71
|
+
@CheckPolicies(new ManageUsersPolicy())
|
|
68
72
|
@UseFilters(Tools.TypeOrmExceptionFilter)
|
|
69
73
|
@ApiTags('Users')
|
|
70
74
|
@ApiExtraModels(PermissionDto)
|