@forklaunch/implementation-iam-base 0.1.13 → 0.1.15
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/lib/__test__/schemaEquality.test.d.ts +1 -1
- package/lib/__test__/schemaEquality.test.js +235 -135
- package/lib/jest.config.d.ts +1 -1
- package/lib/jest.config.js +16 -16
- package/lib/schemas/index.d.ts +1 -1
- package/lib/schemas/organization.schema.d.ts +375 -152
- package/lib/schemas/organization.schema.js +4 -1
- package/lib/schemas/permission.schema.d.ts +88 -34
- package/lib/schemas/permission.schema.js +4 -1
- package/lib/schemas/role.schema.d.ts +139 -50
- package/lib/schemas/role.schema.js +4 -1
- package/lib/schemas/typebox/organization.schema.d.ts +373 -103
- package/lib/schemas/typebox/organization.schema.js +27 -17
- package/lib/schemas/typebox/permission.schema.d.ts +114 -38
- package/lib/schemas/typebox/permission.schema.js +24 -17
- package/lib/schemas/typebox/role.schema.d.ts +174 -50
- package/lib/schemas/typebox/role.schema.js +24 -17
- package/lib/schemas/typebox/user.schema.d.ts +306 -94
- package/lib/schemas/typebox/user.schema.js +40 -32
- package/lib/schemas/user.schema.d.ts +270 -116
- package/lib/schemas/user.schema.js +4 -1
- package/lib/schemas/zod/organization.schema.d.ts +355 -221
- package/lib/schemas/zod/organization.schema.js +27 -17
- package/lib/schemas/zod/permission.schema.d.ts +62 -38
- package/lib/schemas/zod/permission.schema.js +24 -17
- package/lib/schemas/zod/role.schema.d.ts +112 -66
- package/lib/schemas/zod/role.schema.js +24 -17
- package/lib/schemas/zod/user.schema.d.ts +214 -144
- package/lib/schemas/zod/user.schema.js +40 -32
- package/lib/services/index.d.ts +1 -1
- package/lib/services/organization.service.d.ts +106 -33
- package/lib/services/organization.service.js +47 -32
- package/lib/services/permission.service.d.ts +117 -41
- package/lib/services/permission.service.js +193 -149
- package/lib/services/role.service.d.ts +104 -35
- package/lib/services/role.service.js +70 -54
- package/lib/services/user.service.d.ts +121 -37
- package/lib/services/user.service.js +121 -80
- package/lib/tsconfig.tsbuildinfo +1 -1
- package/lib/vitest.config.d.ts +2 -2
- package/lib/vitest.config.js +4 -4
- package/package.json +8 -8
|
@@ -1,35 +1,50 @@
|
|
|
1
1
|
import { transformIntoInternalDtoMapper } from '@forklaunch/core/mappers';
|
|
2
2
|
export class BaseOrganizationService {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
3
|
+
em;
|
|
4
|
+
openTelemetryCollector;
|
|
5
|
+
schemaValidator;
|
|
6
|
+
mapperss;
|
|
7
|
+
#mapperss;
|
|
8
|
+
constructor(em, openTelemetryCollector, schemaValidator, mapperss) {
|
|
9
|
+
this.em = em;
|
|
10
|
+
this.openTelemetryCollector = openTelemetryCollector;
|
|
11
|
+
this.schemaValidator = schemaValidator;
|
|
12
|
+
this.mapperss = mapperss;
|
|
13
|
+
this.#mapperss = transformIntoInternalDtoMapper(mapperss, schemaValidator);
|
|
14
|
+
}
|
|
15
|
+
async createOrganization(organizationDto, em) {
|
|
16
|
+
this.openTelemetryCollector.log('info', 'Creating organization');
|
|
17
|
+
const organization =
|
|
18
|
+
this.#mapperss.CreateOrganizationDtoMapper.deserializeDtoToEntity(
|
|
19
|
+
organizationDto
|
|
20
|
+
);
|
|
21
|
+
await (em ?? this.em).transactional(async (innerEm) => {
|
|
22
|
+
await innerEm.persist(organization);
|
|
23
|
+
});
|
|
24
|
+
return this.#mapperss.OrganizationDtoMapper.serializeEntityToDto(
|
|
25
|
+
organization
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
async getOrganization(idDto, em) {
|
|
29
|
+
const organization = await (em ?? this.em).findOneOrFail(
|
|
30
|
+
'Organization',
|
|
31
|
+
idDto
|
|
32
|
+
);
|
|
33
|
+
return this.#mapperss.OrganizationDtoMapper.serializeEntityToDto(
|
|
34
|
+
organization
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
async updateOrganization(organizationDto, em) {
|
|
38
|
+
const updatedOrganization =
|
|
39
|
+
this.#mapperss.UpdateOrganizationDtoMapper.deserializeDtoToEntity(
|
|
40
|
+
organizationDto
|
|
41
|
+
);
|
|
42
|
+
await (em ?? this.em).upsert(updatedOrganization);
|
|
43
|
+
return this.#mapperss.OrganizationDtoMapper.serializeEntityToDto(
|
|
44
|
+
updatedOrganization
|
|
45
|
+
);
|
|
46
|
+
}
|
|
47
|
+
async deleteOrganization(idDto, em) {
|
|
48
|
+
await (em ?? this.em).nativeDelete('Organization', idDto);
|
|
49
|
+
}
|
|
35
50
|
}
|
|
@@ -1,62 +1,138 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
PermissionService,
|
|
3
|
+
RoleService
|
|
4
|
+
} from '@forklaunch/interfaces-iam/interfaces';
|
|
2
5
|
import { IdDto, IdsDto } from '@forklaunch/common';
|
|
3
|
-
import {
|
|
4
|
-
|
|
6
|
+
import {
|
|
7
|
+
RequestDtoMapperConstructor,
|
|
8
|
+
ResponseDtoMapperConstructor
|
|
9
|
+
} from '@forklaunch/core/mappers';
|
|
10
|
+
import {
|
|
11
|
+
MetricsDefinition,
|
|
12
|
+
OpenTelemetryCollector
|
|
13
|
+
} from '@forklaunch/core/http';
|
|
5
14
|
import { MapNestedDtoArraysToCollections } from '@forklaunch/core/services';
|
|
6
|
-
import {
|
|
15
|
+
import {
|
|
16
|
+
CreatePermissionDto,
|
|
17
|
+
PermissionDto,
|
|
18
|
+
RoleDto,
|
|
19
|
+
UpdatePermissionDto
|
|
20
|
+
} from '@forklaunch/interfaces-iam/types';
|
|
7
21
|
import { AnySchemaValidator } from '@forklaunch/validator';
|
|
8
22
|
import { EntityManager } from '@mikro-orm/core';
|
|
9
|
-
export declare class BasePermissionService<
|
|
23
|
+
export declare class BasePermissionService<
|
|
24
|
+
SchemaValidator extends AnySchemaValidator,
|
|
25
|
+
Metrics extends MetricsDefinition = MetricsDefinition,
|
|
26
|
+
Dto extends {
|
|
10
27
|
PermissionDtoMapper: PermissionDto;
|
|
11
28
|
CreatePermissionDtoMapper: CreatePermissionDto;
|
|
12
29
|
UpdatePermissionDtoMapper: UpdatePermissionDto;
|
|
13
30
|
RoleDtoMapper: RoleDto;
|
|
14
|
-
} = {
|
|
31
|
+
} = {
|
|
15
32
|
PermissionDtoMapper: PermissionDto;
|
|
16
33
|
CreatePermissionDtoMapper: CreatePermissionDto;
|
|
17
34
|
UpdatePermissionDtoMapper: UpdatePermissionDto;
|
|
18
35
|
RoleDtoMapper: RoleDto;
|
|
19
|
-
},
|
|
36
|
+
},
|
|
37
|
+
Entities extends {
|
|
20
38
|
PermissionDtoMapper: PermissionDto;
|
|
21
39
|
CreatePermissionDtoMapper: PermissionDto;
|
|
22
40
|
UpdatePermissionDtoMapper: PermissionDto;
|
|
23
41
|
RoleDtoMapper: MapNestedDtoArraysToCollections<RoleDto, 'permissions'>;
|
|
24
|
-
} = {
|
|
42
|
+
} = {
|
|
25
43
|
PermissionDtoMapper: PermissionDto;
|
|
26
44
|
CreatePermissionDtoMapper: PermissionDto;
|
|
27
45
|
UpdatePermissionDtoMapper: PermissionDto;
|
|
28
46
|
RoleDtoMapper: MapNestedDtoArraysToCollections<RoleDto, 'permissions'>;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
47
|
+
}
|
|
48
|
+
> implements PermissionService
|
|
49
|
+
{
|
|
50
|
+
#private;
|
|
51
|
+
em: EntityManager;
|
|
52
|
+
protected roleServiceFactory: () => RoleService;
|
|
53
|
+
protected openTelemetryCollector: OpenTelemetryCollector<Metrics>;
|
|
54
|
+
protected schemaValidator: SchemaValidator;
|
|
55
|
+
protected mapperss: {
|
|
56
|
+
PermissionDtoMapper: ResponseDtoMapperConstructor<
|
|
57
|
+
SchemaValidator,
|
|
58
|
+
Dto['PermissionDtoMapper'],
|
|
59
|
+
Entities['PermissionDtoMapper']
|
|
60
|
+
>;
|
|
61
|
+
CreatePermissionDtoMapper: RequestDtoMapperConstructor<
|
|
62
|
+
SchemaValidator,
|
|
63
|
+
Dto['CreatePermissionDtoMapper'],
|
|
64
|
+
Entities['CreatePermissionDtoMapper']
|
|
65
|
+
>;
|
|
66
|
+
UpdatePermissionDtoMapper: RequestDtoMapperConstructor<
|
|
67
|
+
SchemaValidator,
|
|
68
|
+
Dto['UpdatePermissionDtoMapper'],
|
|
69
|
+
Entities['UpdatePermissionDtoMapper']
|
|
70
|
+
>;
|
|
71
|
+
RoleDtoMapper: RequestDtoMapperConstructor<
|
|
72
|
+
SchemaValidator,
|
|
73
|
+
Dto['RoleDtoMapper'],
|
|
74
|
+
Entities['RoleDtoMapper']
|
|
75
|
+
>;
|
|
76
|
+
};
|
|
77
|
+
constructor(
|
|
78
|
+
em: EntityManager,
|
|
79
|
+
roleServiceFactory: () => RoleService,
|
|
80
|
+
openTelemetryCollector: OpenTelemetryCollector<Metrics>,
|
|
81
|
+
schemaValidator: SchemaValidator,
|
|
82
|
+
mapperss: {
|
|
83
|
+
PermissionDtoMapper: ResponseDtoMapperConstructor<
|
|
84
|
+
SchemaValidator,
|
|
85
|
+
Dto['PermissionDtoMapper'],
|
|
86
|
+
Entities['PermissionDtoMapper']
|
|
87
|
+
>;
|
|
88
|
+
CreatePermissionDtoMapper: RequestDtoMapperConstructor<
|
|
89
|
+
SchemaValidator,
|
|
90
|
+
Dto['CreatePermissionDtoMapper'],
|
|
91
|
+
Entities['CreatePermissionDtoMapper']
|
|
92
|
+
>;
|
|
93
|
+
UpdatePermissionDtoMapper: RequestDtoMapperConstructor<
|
|
94
|
+
SchemaValidator,
|
|
95
|
+
Dto['UpdatePermissionDtoMapper'],
|
|
96
|
+
Entities['UpdatePermissionDtoMapper']
|
|
97
|
+
>;
|
|
98
|
+
RoleDtoMapper: RequestDtoMapperConstructor<
|
|
99
|
+
SchemaValidator,
|
|
100
|
+
Dto['RoleDtoMapper'],
|
|
101
|
+
Entities['RoleDtoMapper']
|
|
102
|
+
>;
|
|
103
|
+
}
|
|
104
|
+
);
|
|
105
|
+
private updateRolesWithPermissions;
|
|
106
|
+
private removePermissionsFromRoles;
|
|
107
|
+
private getBatchRoles;
|
|
108
|
+
private createPermissionDto;
|
|
109
|
+
private extractCreatePermissionDtoToEntityData;
|
|
110
|
+
createPermission(
|
|
111
|
+
createPermissionDto: Dto['CreatePermissionDtoMapper'],
|
|
112
|
+
em?: EntityManager
|
|
113
|
+
): Promise<Dto['PermissionDtoMapper']>;
|
|
114
|
+
createBatchPermissions(
|
|
115
|
+
permissionDtos: Dto['CreatePermissionDtoMapper'][],
|
|
116
|
+
em?: EntityManager
|
|
117
|
+
): Promise<Dto['PermissionDtoMapper'][]>;
|
|
118
|
+
getPermission(
|
|
119
|
+
idDto: IdDto,
|
|
120
|
+
em?: EntityManager
|
|
121
|
+
): Promise<Dto['PermissionDtoMapper']>;
|
|
122
|
+
getBatchPermissions(
|
|
123
|
+
idsDto: IdsDto,
|
|
124
|
+
em?: EntityManager
|
|
125
|
+
): Promise<Dto['PermissionDtoMapper'][]>;
|
|
126
|
+
private updatePermissionDto;
|
|
127
|
+
updatePermission(
|
|
128
|
+
permissionDto: Dto['UpdatePermissionDtoMapper'],
|
|
129
|
+
em?: EntityManager
|
|
130
|
+
): Promise<Dto['PermissionDtoMapper']>;
|
|
131
|
+
updateBatchPermissions(
|
|
132
|
+
permissionDtos: Dto['UpdatePermissionDtoMapper'][],
|
|
133
|
+
em?: EntityManager
|
|
134
|
+
): Promise<Dto['PermissionDtoMapper'][]>;
|
|
135
|
+
deletePermission(idDto: IdDto, em?: EntityManager): Promise<void>;
|
|
136
|
+
deleteBatchPermissions(idsDto: IdsDto, em?: EntityManager): Promise<void>;
|
|
61
137
|
}
|
|
62
|
-
//# sourceMappingURL=permission.service.d.ts.map
|
|
138
|
+
//# sourceMappingURL=permission.service.d.ts.map
|
|
@@ -1,159 +1,203 @@
|
|
|
1
1
|
import { transformIntoInternalDtoMapper } from '@forklaunch/core/mappers';
|
|
2
2
|
export class BasePermissionService {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
3
|
+
em;
|
|
4
|
+
roleServiceFactory;
|
|
5
|
+
openTelemetryCollector;
|
|
6
|
+
schemaValidator;
|
|
7
|
+
mapperss;
|
|
8
|
+
#mapperss;
|
|
9
|
+
constructor(
|
|
10
|
+
em,
|
|
11
|
+
roleServiceFactory,
|
|
12
|
+
openTelemetryCollector,
|
|
13
|
+
schemaValidator,
|
|
14
|
+
mapperss
|
|
15
|
+
) {
|
|
16
|
+
this.em = em;
|
|
17
|
+
this.roleServiceFactory = roleServiceFactory;
|
|
18
|
+
this.openTelemetryCollector = openTelemetryCollector;
|
|
19
|
+
this.schemaValidator = schemaValidator;
|
|
20
|
+
this.mapperss = mapperss;
|
|
21
|
+
this.#mapperss = transformIntoInternalDtoMapper(mapperss, schemaValidator);
|
|
22
|
+
}
|
|
23
|
+
// start: global helper functions
|
|
24
|
+
async updateRolesWithPermissions(roles, permissions) {
|
|
25
|
+
return await Promise.all(
|
|
26
|
+
roles.map(async (role) => {
|
|
27
|
+
permissions.forEach((permission) => role.permissions.add(permission));
|
|
28
|
+
return role;
|
|
29
|
+
})
|
|
30
|
+
);
|
|
31
|
+
}
|
|
32
|
+
async removePermissionsFromRoles(roles, permissions) {
|
|
33
|
+
return await Promise.all(
|
|
34
|
+
roles.map(async (role) => {
|
|
35
|
+
permissions.forEach((permission) =>
|
|
36
|
+
role.permissions.remove(permission)
|
|
37
|
+
);
|
|
38
|
+
return role;
|
|
39
|
+
})
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
async getBatchRoles(roleIds, em) {
|
|
43
|
+
return roleIds
|
|
44
|
+
? (await this.roleServiceFactory().getBatchRoles(roleIds, em)).map(
|
|
45
|
+
(role) => {
|
|
46
|
+
return (em ?? this.em).merge(
|
|
47
|
+
this.#mapperss.RoleDtoMapper.deserializeDtoToEntity(role)
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
)
|
|
51
|
+
: [];
|
|
52
|
+
}
|
|
53
|
+
// end: global helper functions
|
|
54
|
+
// start: createPermission helper functions
|
|
55
|
+
async createPermissionDto({ permission, addToRoles }) {
|
|
56
|
+
let roles = [];
|
|
57
|
+
if (addToRoles) {
|
|
58
|
+
roles = await this.updateRolesWithPermissions(addToRoles, [permission]);
|
|
16
59
|
}
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
async createBatchPermissions(permissionDtos, em) {
|
|
63
|
-
const rolesCache = {};
|
|
64
|
-
const permissions = [];
|
|
65
|
-
await (em ?? this.em).transactional(async (em) => {
|
|
66
|
-
permissionDtos.map(async (createPermissionDto) => {
|
|
67
|
-
const { permission, roles } = await this.createPermissionDto(await this.extractCreatePermissionDtoToEntityData(createPermissionDto, em));
|
|
68
|
-
roles.forEach((role) => {
|
|
69
|
-
if (rolesCache[role.id] &&
|
|
70
|
-
role.permissions !== rolesCache[role.id].permissions) {
|
|
71
|
-
role.permissions.getItems().forEach((permission) => {
|
|
72
|
-
if (!rolesCache[role.id].permissions.contains(permission)) {
|
|
73
|
-
rolesCache[role.id].permissions.add(permission);
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
rolesCache[role.id] = role;
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
permissions.push(permission);
|
|
60
|
+
return { permission, roles };
|
|
61
|
+
}
|
|
62
|
+
async extractCreatePermissionDtoToEntityData(permissionDto, em) {
|
|
63
|
+
return {
|
|
64
|
+
permission: (em ?? this.em).merge(
|
|
65
|
+
this.#mapperss.CreatePermissionDtoMapper.deserializeDtoToEntity(
|
|
66
|
+
permissionDto
|
|
67
|
+
)
|
|
68
|
+
),
|
|
69
|
+
addToRoles: permissionDto.addToRolesIds
|
|
70
|
+
? await this.getBatchRoles({ ids: permissionDto.addToRolesIds }, em)
|
|
71
|
+
: []
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
// end: createPermission helper functions
|
|
75
|
+
async createPermission(createPermissionDto, em) {
|
|
76
|
+
const { permission, roles } = await this.createPermissionDto(
|
|
77
|
+
await this.extractCreatePermissionDtoToEntityData(createPermissionDto, em)
|
|
78
|
+
);
|
|
79
|
+
await (em ?? this.em).transactional(async (innerEm) => {
|
|
80
|
+
await innerEm.persist([permission, ...roles]);
|
|
81
|
+
});
|
|
82
|
+
return this.#mapperss.PermissionDtoMapper.serializeEntityToDto(permission);
|
|
83
|
+
}
|
|
84
|
+
async createBatchPermissions(permissionDtos, em) {
|
|
85
|
+
const rolesCache = {};
|
|
86
|
+
const permissions = [];
|
|
87
|
+
await (em ?? this.em).transactional(async (em) => {
|
|
88
|
+
permissionDtos.map(async (createPermissionDto) => {
|
|
89
|
+
const { permission, roles } = await this.createPermissionDto(
|
|
90
|
+
await this.extractCreatePermissionDtoToEntityData(
|
|
91
|
+
createPermissionDto,
|
|
92
|
+
em
|
|
93
|
+
)
|
|
94
|
+
);
|
|
95
|
+
roles.forEach((role) => {
|
|
96
|
+
if (
|
|
97
|
+
rolesCache[role.id] &&
|
|
98
|
+
role.permissions !== rolesCache[role.id].permissions
|
|
99
|
+
) {
|
|
100
|
+
role.permissions.getItems().forEach((permission) => {
|
|
101
|
+
if (!rolesCache[role.id].permissions.contains(permission)) {
|
|
102
|
+
rolesCache[role.id].permissions.add(permission);
|
|
103
|
+
}
|
|
82
104
|
});
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
]);
|
|
105
|
+
} else {
|
|
106
|
+
rolesCache[role.id] = role;
|
|
107
|
+
}
|
|
87
108
|
});
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
109
|
+
permissions.push(permission);
|
|
110
|
+
});
|
|
111
|
+
await (em ?? this.em).persist([
|
|
112
|
+
...permissions,
|
|
113
|
+
...Object.values(rolesCache)
|
|
114
|
+
]);
|
|
115
|
+
});
|
|
116
|
+
return permissions.map((permission) =>
|
|
117
|
+
this.#mapperss.PermissionDtoMapper.serializeEntityToDto(permission)
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
async getPermission(idDto, em) {
|
|
121
|
+
const permission = await (em ?? this.em).findOneOrFail('Permission', idDto);
|
|
122
|
+
return this.#mapperss.PermissionDtoMapper.serializeEntityToDto(permission);
|
|
123
|
+
}
|
|
124
|
+
async getBatchPermissions(idsDto, em) {
|
|
125
|
+
return (await (em ?? this.em).find('Permission', idsDto)).map(
|
|
126
|
+
(permission) =>
|
|
127
|
+
this.#mapperss.PermissionDtoMapper.serializeEntityToDto(permission)
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
// start: updatePermission helper functions
|
|
131
|
+
updatePermissionDto = async (permissionDto, em) => {
|
|
132
|
+
const permission =
|
|
133
|
+
this.#mapperss.UpdatePermissionDtoMapper.deserializeDtoToEntity(
|
|
134
|
+
permissionDto
|
|
135
|
+
);
|
|
136
|
+
const addToRoles = permissionDto.addToRolesIds
|
|
137
|
+
? await this.getBatchRoles({ ids: permissionDto.addToRolesIds }, em)
|
|
138
|
+
: [];
|
|
139
|
+
const removeFromRoles = permissionDto.removeFromRolesIds
|
|
140
|
+
? await this.getBatchRoles({ ids: permissionDto.removeFromRolesIds }, em)
|
|
141
|
+
: [];
|
|
142
|
+
let roles = [];
|
|
143
|
+
roles = roles.concat(
|
|
144
|
+
await this.updateRolesWithPermissions(addToRoles, [permission])
|
|
145
|
+
);
|
|
146
|
+
roles = roles.concat(
|
|
147
|
+
await this.removePermissionsFromRoles(removeFromRoles, [permission])
|
|
148
|
+
);
|
|
149
|
+
return {
|
|
150
|
+
permission,
|
|
151
|
+
roles
|
|
113
152
|
};
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
return this.#mapperss.PermissionDtoMapper.serializeEntityToDto(permission);
|
|
153
|
+
};
|
|
154
|
+
// end: updatePermission helper functions
|
|
155
|
+
async updatePermission(permissionDto, em) {
|
|
156
|
+
const { permission, roles } = await this.updatePermissionDto(permissionDto);
|
|
157
|
+
await (em ?? this.em).upsertMany([permission, ...roles]);
|
|
158
|
+
if (!em) {
|
|
159
|
+
this.em.flush();
|
|
122
160
|
}
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
});
|
|
142
|
-
permissions.push(permission);
|
|
161
|
+
return this.#mapperss.PermissionDtoMapper.serializeEntityToDto(permission);
|
|
162
|
+
}
|
|
163
|
+
async updateBatchPermissions(permissionDtos, em) {
|
|
164
|
+
const rolesCache = {};
|
|
165
|
+
const permissions = [];
|
|
166
|
+
await (em ?? this.em).transactional(async (em) => {
|
|
167
|
+
permissionDtos.map(async (updatePermissionDto) => {
|
|
168
|
+
const { permission, roles } =
|
|
169
|
+
await this.updatePermissionDto(updatePermissionDto);
|
|
170
|
+
roles.forEach((role) => {
|
|
171
|
+
if (
|
|
172
|
+
rolesCache[role.id] &&
|
|
173
|
+
role.permissions !== rolesCache[role.id].permissions
|
|
174
|
+
) {
|
|
175
|
+
role.permissions.getItems().forEach((permission) => {
|
|
176
|
+
if (!rolesCache[role.id].permissions.contains(permission)) {
|
|
177
|
+
rolesCache[role.id].permissions.add(permission);
|
|
178
|
+
}
|
|
143
179
|
});
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
]);
|
|
180
|
+
} else {
|
|
181
|
+
rolesCache[role.id] = role;
|
|
182
|
+
}
|
|
148
183
|
});
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
184
|
+
permissions.push(permission);
|
|
185
|
+
});
|
|
186
|
+
await (em ?? this.em).persist([
|
|
187
|
+
...permissions,
|
|
188
|
+
...Object.values(rolesCache)
|
|
189
|
+
]);
|
|
190
|
+
});
|
|
191
|
+
return permissions.map((permission) =>
|
|
192
|
+
this.#mapperss.PermissionDtoMapper.serializeEntityToDto(permission)
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
async deletePermission(idDto, em) {
|
|
196
|
+
await (em ?? this.em).nativeDelete('Permission', idDto);
|
|
197
|
+
}
|
|
198
|
+
async deleteBatchPermissions(idsDto, em) {
|
|
199
|
+
await (em ?? this.em).nativeDelete('Permission', {
|
|
200
|
+
id: { $in: idsDto.ids }
|
|
201
|
+
});
|
|
202
|
+
}
|
|
159
203
|
}
|