@forklaunch/implementation-iam-base 0.3.1 → 0.3.3
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/domain/schemas/index.d.mts +847 -0
- package/lib/domain/schemas/index.d.ts +847 -0
- package/lib/{schemas → domain/schemas}/index.js +138 -104
- package/lib/{schemas → domain/schemas}/index.mjs +81 -77
- package/lib/eject/domain/schemas/organization.schema.ts +3 -3
- package/lib/eject/domain/schemas/permission.schema.ts +3 -3
- package/lib/eject/domain/schemas/role.schema.ts +3 -3
- package/lib/eject/domain/schemas/user.schema.ts +3 -3
- package/lib/eject/domain/types/iamDto.types.ts +43 -0
- package/lib/eject/domain/types/iamEntities.types.ts +49 -0
- package/lib/eject/services/organization.service.ts +41 -77
- package/lib/eject/services/permission.service.ts +80 -104
- package/lib/eject/services/role.service.ts +51 -81
- package/lib/eject/services/user.service.ts +51 -74
- package/lib/services/index.d.mts +495 -191
- package/lib/services/index.d.ts +495 -191
- package/lib/services/index.js +249 -212
- package/lib/services/index.mjs +222 -218
- package/package.json +20 -13
- package/lib/schemas/index.d.mts +0 -474
- package/lib/schemas/index.d.ts +0 -474
package/lib/services/index.mjs
CHANGED
|
@@ -1,111 +1,112 @@
|
|
|
1
1
|
// services/organization.service.ts
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
} from "@forklaunch/core/http";
|
|
5
|
-
import {
|
|
6
|
-
transformIntoInternalDtoMapper
|
|
7
|
-
} from "@forklaunch/core/mappers";
|
|
2
|
+
import { evaluateTelemetryOptions } from '@forklaunch/core/http';
|
|
3
|
+
import { transformIntoInternalMapper } from '@forklaunch/internal';
|
|
8
4
|
var BaseOrganizationService = class {
|
|
9
5
|
constructor(em, openTelemetryCollector, schemaValidator, mappers, options) {
|
|
10
6
|
this.em = em;
|
|
11
7
|
this.openTelemetryCollector = openTelemetryCollector;
|
|
12
8
|
this.schemaValidator = schemaValidator;
|
|
13
9
|
this.mappers = mappers;
|
|
14
|
-
this
|
|
15
|
-
this.evaluatedTelemetryOptions = options?.telemetry
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
10
|
+
this._mappers = transformIntoInternalMapper(mappers, schemaValidator);
|
|
11
|
+
this.evaluatedTelemetryOptions = options?.telemetry
|
|
12
|
+
? evaluateTelemetryOptions(options.telemetry).enabled
|
|
13
|
+
: {
|
|
14
|
+
logging: false,
|
|
15
|
+
metrics: false,
|
|
16
|
+
tracing: false
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
_mappers;
|
|
22
20
|
evaluatedTelemetryOptions;
|
|
23
21
|
async createOrganization(organizationDto, em) {
|
|
24
22
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
25
23
|
this.openTelemetryCollector.info(
|
|
26
|
-
|
|
24
|
+
'Creating organization',
|
|
27
25
|
organizationDto
|
|
28
26
|
);
|
|
29
27
|
}
|
|
30
|
-
const organization =
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
28
|
+
const organization =
|
|
29
|
+
await this._mappers.CreateOrganizationMapper.deserializeDtoToEntity(
|
|
30
|
+
organizationDto,
|
|
31
|
+
em ?? this.em
|
|
32
|
+
);
|
|
34
33
|
if (em) {
|
|
35
34
|
await em.persist(organization);
|
|
36
35
|
} else {
|
|
37
36
|
await this.em.persistAndFlush(organization);
|
|
38
37
|
}
|
|
39
|
-
return this
|
|
40
|
-
organization
|
|
41
|
-
);
|
|
38
|
+
return this._mappers.OrganizationMapper.serializeEntityToDto(organization);
|
|
42
39
|
}
|
|
43
40
|
async getOrganization(idDto, em) {
|
|
44
41
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
45
|
-
this.openTelemetryCollector.info(
|
|
42
|
+
this.openTelemetryCollector.info('Getting organization', idDto);
|
|
46
43
|
}
|
|
47
44
|
const organization = await (em ?? this.em).findOneOrFail(
|
|
48
|
-
|
|
45
|
+
'Organization',
|
|
49
46
|
idDto
|
|
50
47
|
);
|
|
51
|
-
return this
|
|
52
|
-
organization
|
|
53
|
-
);
|
|
48
|
+
return this._mappers.OrganizationMapper.serializeEntityToDto(organization);
|
|
54
49
|
}
|
|
55
50
|
async updateOrganization(organizationDto, em) {
|
|
56
51
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
57
52
|
this.openTelemetryCollector.info(
|
|
58
|
-
|
|
53
|
+
'Updating organization',
|
|
59
54
|
organizationDto
|
|
60
55
|
);
|
|
61
56
|
}
|
|
62
|
-
const updatedOrganization =
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
57
|
+
const updatedOrganization =
|
|
58
|
+
await this._mappers.UpdateOrganizationMapper.deserializeDtoToEntity(
|
|
59
|
+
organizationDto,
|
|
60
|
+
em ?? this.em
|
|
61
|
+
);
|
|
66
62
|
if (em) {
|
|
67
63
|
await em.persist(updatedOrganization);
|
|
68
64
|
} else {
|
|
69
65
|
await this.em.persistAndFlush(updatedOrganization);
|
|
70
66
|
}
|
|
71
|
-
return this
|
|
67
|
+
return this._mappers.OrganizationMapper.serializeEntityToDto(
|
|
72
68
|
updatedOrganization
|
|
73
69
|
);
|
|
74
70
|
}
|
|
75
71
|
async deleteOrganization(idDto, em) {
|
|
76
72
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
77
|
-
this.openTelemetryCollector.info(
|
|
73
|
+
this.openTelemetryCollector.info('Deleting organization', idDto);
|
|
78
74
|
}
|
|
79
75
|
if (em) {
|
|
80
|
-
await em.nativeDelete(
|
|
76
|
+
await em.nativeDelete('Organization', idDto);
|
|
81
77
|
} else {
|
|
82
|
-
await this.em.nativeDelete(
|
|
78
|
+
await this.em.nativeDelete('Organization', idDto);
|
|
83
79
|
}
|
|
84
80
|
}
|
|
85
81
|
};
|
|
86
82
|
|
|
87
83
|
// services/permission.service.ts
|
|
88
|
-
import {
|
|
89
|
-
|
|
90
|
-
} from "@forklaunch/core/http";
|
|
91
|
-
import {
|
|
92
|
-
transformIntoInternalDtoMapper as transformIntoInternalDtoMapper2
|
|
93
|
-
} from "@forklaunch/core/mappers";
|
|
84
|
+
import { evaluateTelemetryOptions as evaluateTelemetryOptions2 } from '@forklaunch/core/http';
|
|
85
|
+
import { transformIntoInternalMapper as transformIntoInternalMapper2 } from '@forklaunch/internal';
|
|
94
86
|
var BasePermissionService = class {
|
|
95
|
-
constructor(
|
|
87
|
+
constructor(
|
|
88
|
+
em,
|
|
89
|
+
roleServiceFactory,
|
|
90
|
+
openTelemetryCollector,
|
|
91
|
+
schemaValidator,
|
|
92
|
+
mappers,
|
|
93
|
+
options
|
|
94
|
+
) {
|
|
96
95
|
this.em = em;
|
|
97
96
|
this.roleServiceFactory = roleServiceFactory;
|
|
98
97
|
this.openTelemetryCollector = openTelemetryCollector;
|
|
99
98
|
this.schemaValidator = schemaValidator;
|
|
100
99
|
this.mappers = mappers;
|
|
101
|
-
this
|
|
102
|
-
this.evaluatedTelemetryOptions = options?.telemetry
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
100
|
+
this._mappers = transformIntoInternalMapper2(mappers, schemaValidator);
|
|
101
|
+
this.evaluatedTelemetryOptions = options?.telemetry
|
|
102
|
+
? evaluateTelemetryOptions2(options.telemetry).enabled
|
|
103
|
+
: {
|
|
104
|
+
logging: false,
|
|
105
|
+
metrics: false,
|
|
106
|
+
tracing: false
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
_mappers;
|
|
109
110
|
evaluatedTelemetryOptions;
|
|
110
111
|
// start: global helper functions
|
|
111
112
|
async updateRolesWithPermissions(roles, permissions) {
|
|
@@ -119,33 +120,32 @@ var BasePermissionService = class {
|
|
|
119
120
|
async removePermissionsFromRoles(roles, permissions) {
|
|
120
121
|
return Promise.all(
|
|
121
122
|
roles.map(async (role) => {
|
|
122
|
-
permissions.forEach(
|
|
123
|
-
|
|
123
|
+
permissions.forEach((permission) =>
|
|
124
|
+
role.permissions.remove(permission)
|
|
124
125
|
);
|
|
125
126
|
return role;
|
|
126
127
|
})
|
|
127
128
|
);
|
|
128
129
|
}
|
|
129
130
|
async getBatchRoles(roleIds, em) {
|
|
130
|
-
return roleIds
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
131
|
+
return roleIds
|
|
132
|
+
? await Promise.all(
|
|
133
|
+
(await this.roleServiceFactory().getBatchRoles(roleIds, em)).map(
|
|
134
|
+
async (role) => {
|
|
135
|
+
return (em ?? this.em).merge(
|
|
136
|
+
await this._mappers.RoleEntityMapper.deserializeDtoToEntity(
|
|
137
|
+
role,
|
|
138
|
+
em ?? this.em
|
|
139
|
+
)
|
|
140
|
+
);
|
|
141
|
+
}
|
|
142
|
+
)
|
|
143
|
+
)
|
|
144
|
+
: [];
|
|
142
145
|
}
|
|
143
146
|
// end: global helper functions
|
|
144
147
|
// start: createPermission helper functions
|
|
145
|
-
async createPermissionDto({
|
|
146
|
-
permission,
|
|
147
|
-
addToRoles
|
|
148
|
-
}) {
|
|
148
|
+
async createPermissionDto({ permission, addToRoles }) {
|
|
149
149
|
let roles = [];
|
|
150
150
|
if (addToRoles) {
|
|
151
151
|
roles = await this.updateRolesWithPermissions(addToRoles, [permission]);
|
|
@@ -155,19 +155,21 @@ var BasePermissionService = class {
|
|
|
155
155
|
async extractCreatePermissionDtoToEntityData(permissionDto, em) {
|
|
156
156
|
return {
|
|
157
157
|
permission: (em ?? this.em).merge(
|
|
158
|
-
await this
|
|
158
|
+
await this._mappers.CreatePermissionMapper.deserializeDtoToEntity(
|
|
159
159
|
permissionDto,
|
|
160
160
|
em ?? this.em
|
|
161
161
|
)
|
|
162
162
|
),
|
|
163
|
-
addToRoles: permissionDto.addToRolesIds
|
|
163
|
+
addToRoles: permissionDto.addToRolesIds
|
|
164
|
+
? await this.getBatchRoles({ ids: permissionDto.addToRolesIds }, em)
|
|
165
|
+
: []
|
|
164
166
|
};
|
|
165
167
|
}
|
|
166
168
|
// end: createPermission helper functions
|
|
167
169
|
async createPermission(createPermissionDto, em) {
|
|
168
170
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
169
171
|
this.openTelemetryCollector.info(
|
|
170
|
-
|
|
172
|
+
'Creating permission',
|
|
171
173
|
createPermissionDto
|
|
172
174
|
);
|
|
173
175
|
}
|
|
@@ -179,12 +181,12 @@ var BasePermissionService = class {
|
|
|
179
181
|
} else {
|
|
180
182
|
await this.em.persistAndFlush([permission, ...roles]);
|
|
181
183
|
}
|
|
182
|
-
return this
|
|
184
|
+
return this._mappers.PermissionMapper.serializeEntityToDto(permission);
|
|
183
185
|
}
|
|
184
186
|
async createBatchPermissions(permissionDtos, em) {
|
|
185
187
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
186
188
|
this.openTelemetryCollector.info(
|
|
187
|
-
|
|
189
|
+
'Creating batch permissions',
|
|
188
190
|
permissionDtos
|
|
189
191
|
);
|
|
190
192
|
}
|
|
@@ -198,7 +200,10 @@ var BasePermissionService = class {
|
|
|
198
200
|
)
|
|
199
201
|
);
|
|
200
202
|
roles.forEach((role) => {
|
|
201
|
-
if (
|
|
203
|
+
if (
|
|
204
|
+
rolesCache[role.id] &&
|
|
205
|
+
role.permissions !== rolesCache[role.id].permissions
|
|
206
|
+
) {
|
|
202
207
|
role.permissions.getItems().forEach((permission2) => {
|
|
203
208
|
if (!rolesCache[role.id].permissions.contains(permission2)) {
|
|
204
209
|
rolesCache[role.id].permissions.add(permission2);
|
|
@@ -217,40 +222,41 @@ var BasePermissionService = class {
|
|
|
217
222
|
await this.em.persistAndFlush(entities);
|
|
218
223
|
}
|
|
219
224
|
return Promise.all(
|
|
220
|
-
permissions.map(
|
|
221
|
-
|
|
225
|
+
permissions.map(async (permission) =>
|
|
226
|
+
this._mappers.PermissionMapper.serializeEntityToDto(permission)
|
|
222
227
|
)
|
|
223
228
|
);
|
|
224
229
|
}
|
|
225
230
|
async getPermission(idDto, em) {
|
|
226
231
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
227
|
-
this.openTelemetryCollector.info(
|
|
232
|
+
this.openTelemetryCollector.info('Getting permission', idDto);
|
|
228
233
|
}
|
|
229
|
-
const permission = await (em ?? this.em).findOneOrFail(
|
|
230
|
-
return this
|
|
231
|
-
permission
|
|
232
|
-
);
|
|
234
|
+
const permission = await (em ?? this.em).findOneOrFail('Permission', idDto);
|
|
235
|
+
return this._mappers.PermissionMapper.serializeEntityToDto(permission);
|
|
233
236
|
}
|
|
234
237
|
async getBatchPermissions(idsDto, em) {
|
|
235
238
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
236
|
-
this.openTelemetryCollector.info(
|
|
239
|
+
this.openTelemetryCollector.info('Getting batch permissions', idsDto);
|
|
237
240
|
}
|
|
238
241
|
return Promise.all(
|
|
239
|
-
(await (em ?? this.em).find(
|
|
240
|
-
|
|
241
|
-
permission
|
|
242
|
-
)
|
|
242
|
+
(await (em ?? this.em).find('Permission', idsDto)).map((permission) =>
|
|
243
|
+
this._mappers.PermissionMapper.serializeEntityToDto(permission)
|
|
243
244
|
)
|
|
244
245
|
);
|
|
245
246
|
}
|
|
246
247
|
// start: updatePermission helper functions
|
|
247
248
|
updatePermissionDto = async (permissionDto, em) => {
|
|
248
|
-
const permission =
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
const
|
|
249
|
+
const permission =
|
|
250
|
+
await this._mappers.UpdatePermissionMapper.deserializeDtoToEntity(
|
|
251
|
+
permissionDto,
|
|
252
|
+
em ?? this.em
|
|
253
|
+
);
|
|
254
|
+
const addToRoles = permissionDto.addToRolesIds
|
|
255
|
+
? await this.getBatchRoles({ ids: permissionDto.addToRolesIds }, em)
|
|
256
|
+
: [];
|
|
257
|
+
const removeFromRoles = permissionDto.removeFromRolesIds
|
|
258
|
+
? await this.getBatchRoles({ ids: permissionDto.removeFromRolesIds }, em)
|
|
259
|
+
: [];
|
|
254
260
|
let roles = [];
|
|
255
261
|
roles = roles.concat(
|
|
256
262
|
await this.updateRolesWithPermissions(addToRoles, [permission])
|
|
@@ -266,7 +272,7 @@ var BasePermissionService = class {
|
|
|
266
272
|
// end: updatePermission helper functions
|
|
267
273
|
async updatePermission(permissionDto, em) {
|
|
268
274
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
269
|
-
this.openTelemetryCollector.info(
|
|
275
|
+
this.openTelemetryCollector.info('Updating permission', permissionDto);
|
|
270
276
|
}
|
|
271
277
|
const { permission, roles } = await this.updatePermissionDto(permissionDto);
|
|
272
278
|
const entities = await (em ?? this.em).upsertMany([permission, ...roles]);
|
|
@@ -275,12 +281,12 @@ var BasePermissionService = class {
|
|
|
275
281
|
} else {
|
|
276
282
|
await this.em.persistAndFlush(entities);
|
|
277
283
|
}
|
|
278
|
-
return this
|
|
284
|
+
return this._mappers.PermissionMapper.serializeEntityToDto(permission);
|
|
279
285
|
}
|
|
280
286
|
async updateBatchPermissions(permissionDtos, em) {
|
|
281
287
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
282
288
|
this.openTelemetryCollector.info(
|
|
283
|
-
|
|
289
|
+
'Updating batch permissions',
|
|
284
290
|
permissionDtos
|
|
285
291
|
);
|
|
286
292
|
}
|
|
@@ -288,9 +294,13 @@ var BasePermissionService = class {
|
|
|
288
294
|
const permissions = [];
|
|
289
295
|
await (em ?? this.em).transactional(async (em2) => {
|
|
290
296
|
permissionDtos.map(async (updatePermissionDto) => {
|
|
291
|
-
const { permission, roles } =
|
|
297
|
+
const { permission, roles } =
|
|
298
|
+
await this.updatePermissionDto(updatePermissionDto);
|
|
292
299
|
roles.forEach((role) => {
|
|
293
|
-
if (
|
|
300
|
+
if (
|
|
301
|
+
rolesCache[role.id] &&
|
|
302
|
+
role.permissions !== rolesCache[role.id].permissions
|
|
303
|
+
) {
|
|
294
304
|
role.permissions.getItems().forEach((permission2) => {
|
|
295
305
|
if (!rolesCache[role.id].permissions.contains(permission2)) {
|
|
296
306
|
rolesCache[role.id].permissions.add(permission2);
|
|
@@ -310,54 +320,52 @@ var BasePermissionService = class {
|
|
|
310
320
|
}
|
|
311
321
|
});
|
|
312
322
|
return Promise.all(
|
|
313
|
-
permissions.map(
|
|
314
|
-
|
|
323
|
+
permissions.map((permission) =>
|
|
324
|
+
this._mappers.PermissionMapper.serializeEntityToDto(permission)
|
|
315
325
|
)
|
|
316
326
|
);
|
|
317
327
|
}
|
|
318
328
|
async deletePermission(idDto, em) {
|
|
319
329
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
320
|
-
this.openTelemetryCollector.info(
|
|
330
|
+
this.openTelemetryCollector.info('Deleting permission', idDto);
|
|
321
331
|
}
|
|
322
|
-
await (em ?? this.em).nativeDelete(
|
|
332
|
+
await (em ?? this.em).nativeDelete('Permission', idDto);
|
|
323
333
|
}
|
|
324
334
|
async deleteBatchPermissions(idsDto, em) {
|
|
325
335
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
326
|
-
this.openTelemetryCollector.info(
|
|
336
|
+
this.openTelemetryCollector.info('Deleting batch permissions', idsDto);
|
|
327
337
|
}
|
|
328
|
-
await (em ?? this.em).nativeDelete(
|
|
338
|
+
await (em ?? this.em).nativeDelete('Permission', {
|
|
329
339
|
id: { $in: idsDto.ids }
|
|
330
340
|
});
|
|
331
341
|
}
|
|
332
342
|
};
|
|
333
343
|
|
|
334
344
|
// services/role.service.ts
|
|
335
|
-
import {
|
|
336
|
-
|
|
337
|
-
} from "@forklaunch/core/http";
|
|
338
|
-
import {
|
|
339
|
-
transformIntoInternalDtoMapper as transformIntoInternalDtoMapper3
|
|
340
|
-
} from "@forklaunch/core/mappers";
|
|
345
|
+
import { evaluateTelemetryOptions as evaluateTelemetryOptions3 } from '@forklaunch/core/http';
|
|
346
|
+
import { transformIntoInternalMapper as transformIntoInternalMapper3 } from '@forklaunch/internal';
|
|
341
347
|
var BaseRoleService = class {
|
|
342
348
|
constructor(em, openTelemetryCollector, schemaValidator, mappers, options) {
|
|
343
349
|
this.em = em;
|
|
344
350
|
this.openTelemetryCollector = openTelemetryCollector;
|
|
345
351
|
this.schemaValidator = schemaValidator;
|
|
346
352
|
this.mappers = mappers;
|
|
347
|
-
this
|
|
348
|
-
this.evaluatedTelemetryOptions = options?.telemetry
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
353
|
+
this._mappers = transformIntoInternalMapper3(mappers, schemaValidator);
|
|
354
|
+
this.evaluatedTelemetryOptions = options?.telemetry
|
|
355
|
+
? evaluateTelemetryOptions3(options.telemetry).enabled
|
|
356
|
+
: {
|
|
357
|
+
logging: false,
|
|
358
|
+
metrics: false,
|
|
359
|
+
tracing: false
|
|
360
|
+
};
|
|
361
|
+
}
|
|
362
|
+
_mappers;
|
|
355
363
|
evaluatedTelemetryOptions;
|
|
356
364
|
async createRole(roleDto, em) {
|
|
357
365
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
358
|
-
this.openTelemetryCollector.info(
|
|
366
|
+
this.openTelemetryCollector.info('Creating role', roleDto);
|
|
359
367
|
}
|
|
360
|
-
const role = await this
|
|
368
|
+
const role = await this._mappers.CreateRoleMapper.deserializeDtoToEntity(
|
|
361
369
|
roleDto,
|
|
362
370
|
em ?? this.em
|
|
363
371
|
);
|
|
@@ -366,15 +374,15 @@ var BaseRoleService = class {
|
|
|
366
374
|
} else {
|
|
367
375
|
await this.em.persistAndFlush(role);
|
|
368
376
|
}
|
|
369
|
-
return this
|
|
377
|
+
return this._mappers.RoleMapper.serializeEntityToDto(role);
|
|
370
378
|
}
|
|
371
379
|
async createBatchRoles(roleDtos, em) {
|
|
372
380
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
373
|
-
this.openTelemetryCollector.info(
|
|
381
|
+
this.openTelemetryCollector.info('Creating batch roles', roleDtos);
|
|
374
382
|
}
|
|
375
383
|
const roles = await Promise.all(
|
|
376
|
-
roleDtos.map(
|
|
377
|
-
|
|
384
|
+
roleDtos.map(async (roleDto) =>
|
|
385
|
+
this._mappers.CreateRoleMapper.deserializeDtoToEntity(
|
|
378
386
|
roleDto,
|
|
379
387
|
em ?? this.em
|
|
380
388
|
)
|
|
@@ -386,47 +394,41 @@ var BaseRoleService = class {
|
|
|
386
394
|
await this.em.persistAndFlush(roles);
|
|
387
395
|
}
|
|
388
396
|
return Promise.all(
|
|
389
|
-
roles.map(
|
|
390
|
-
(role) => this.#mappers.RoleDtoMapper.serializeEntityToDto(role)
|
|
391
|
-
)
|
|
397
|
+
roles.map((role) => this._mappers.RoleMapper.serializeEntityToDto(role))
|
|
392
398
|
);
|
|
393
399
|
}
|
|
394
400
|
async getRole({ id }, em) {
|
|
395
401
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
396
|
-
this.openTelemetryCollector.info(
|
|
402
|
+
this.openTelemetryCollector.info('Getting role', { id });
|
|
397
403
|
}
|
|
398
|
-
const role = await (em ?? this.em).findOneOrFail(
|
|
399
|
-
populate: [
|
|
404
|
+
const role = await (em ?? this.em).findOneOrFail('Role', id, {
|
|
405
|
+
populate: ['id', '*']
|
|
400
406
|
});
|
|
401
|
-
return this
|
|
402
|
-
role
|
|
403
|
-
);
|
|
407
|
+
return this._mappers.RoleMapper.serializeEntityToDto(role);
|
|
404
408
|
}
|
|
405
409
|
async getBatchRoles({ ids }, em) {
|
|
406
410
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
407
|
-
this.openTelemetryCollector.info(
|
|
411
|
+
this.openTelemetryCollector.info('Getting batch roles', { ids });
|
|
408
412
|
}
|
|
409
413
|
return Promise.all(
|
|
410
|
-
(
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
(role) => this.#mappers.RoleDtoMapper.serializeEntityToDto(
|
|
420
|
-
role
|
|
414
|
+
(
|
|
415
|
+
await (em ?? this.em).find(
|
|
416
|
+
'Role',
|
|
417
|
+
{
|
|
418
|
+
id: { $in: ids }
|
|
419
|
+
},
|
|
420
|
+
{
|
|
421
|
+
populate: ['id', '*']
|
|
422
|
+
}
|
|
421
423
|
)
|
|
422
|
-
)
|
|
424
|
+
).map((role) => this._mappers.RoleMapper.serializeEntityToDto(role))
|
|
423
425
|
);
|
|
424
426
|
}
|
|
425
427
|
async updateRole(roleDto, em) {
|
|
426
428
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
427
|
-
this.openTelemetryCollector.info(
|
|
429
|
+
this.openTelemetryCollector.info('Updating role', roleDto);
|
|
428
430
|
}
|
|
429
|
-
const role = await this
|
|
431
|
+
const role = await this._mappers.UpdateRoleMapper.deserializeDtoToEntity(
|
|
430
432
|
roleDto,
|
|
431
433
|
em ?? this.em
|
|
432
434
|
);
|
|
@@ -435,15 +437,15 @@ var BaseRoleService = class {
|
|
|
435
437
|
} else {
|
|
436
438
|
await this.em.persistAndFlush(role);
|
|
437
439
|
}
|
|
438
|
-
return this
|
|
440
|
+
return this._mappers.RoleMapper.serializeEntityToDto(role);
|
|
439
441
|
}
|
|
440
442
|
async updateBatchRoles(roleDtos, em) {
|
|
441
443
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
442
|
-
this.openTelemetryCollector.info(
|
|
444
|
+
this.openTelemetryCollector.info('Updating batch roles', roleDtos);
|
|
443
445
|
}
|
|
444
446
|
const roles = await Promise.all(
|
|
445
|
-
roleDtos.map(
|
|
446
|
-
|
|
447
|
+
roleDtos.map(async (roleDto) =>
|
|
448
|
+
this._mappers.UpdateRoleMapper.deserializeDtoToEntity(
|
|
447
449
|
roleDto,
|
|
448
450
|
em ?? this.em
|
|
449
451
|
)
|
|
@@ -455,36 +457,37 @@ var BaseRoleService = class {
|
|
|
455
457
|
await this.em.persistAndFlush(roles);
|
|
456
458
|
}
|
|
457
459
|
return Promise.all(
|
|
458
|
-
roles.map(
|
|
459
|
-
(role) => this.#mappers.RoleDtoMapper.serializeEntityToDto(
|
|
460
|
-
role
|
|
461
|
-
)
|
|
462
|
-
)
|
|
460
|
+
roles.map((role) => this._mappers.RoleMapper.serializeEntityToDto(role))
|
|
463
461
|
);
|
|
464
462
|
}
|
|
465
463
|
async deleteRole(idDto, em) {
|
|
466
464
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
467
|
-
this.openTelemetryCollector.info(
|
|
465
|
+
this.openTelemetryCollector.info('Deleting role', idDto);
|
|
468
466
|
}
|
|
469
|
-
await (em ?? this.em).nativeDelete(
|
|
467
|
+
await (em ?? this.em).nativeDelete('Role', idDto);
|
|
470
468
|
}
|
|
471
469
|
async deleteBatchRoles(idsDto, em) {
|
|
472
470
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
473
|
-
this.openTelemetryCollector.info(
|
|
471
|
+
this.openTelemetryCollector.info('Deleting batch roles', idsDto);
|
|
474
472
|
}
|
|
475
|
-
await (em ?? this.em).nativeDelete(
|
|
473
|
+
await (em ?? this.em).nativeDelete('Role', { id: { $in: idsDto.ids } });
|
|
476
474
|
}
|
|
477
475
|
};
|
|
478
476
|
|
|
479
477
|
// services/user.service.ts
|
|
480
|
-
import {
|
|
481
|
-
|
|
482
|
-
} from "@forklaunch/core/http";
|
|
483
|
-
import {
|
|
484
|
-
transformIntoInternalDtoMapper as transformIntoInternalDtoMapper4
|
|
485
|
-
} from "@forklaunch/core/mappers";
|
|
478
|
+
import { evaluateTelemetryOptions as evaluateTelemetryOptions4 } from '@forklaunch/core/http';
|
|
479
|
+
import { transformIntoInternalMapper as transformIntoInternalMapper4 } from '@forklaunch/internal';
|
|
486
480
|
var BaseUserService = class {
|
|
487
|
-
constructor(
|
|
481
|
+
constructor(
|
|
482
|
+
em,
|
|
483
|
+
passwordEncryptionPublicKeyPath,
|
|
484
|
+
roleServiceFactory,
|
|
485
|
+
organizationServiceFactory,
|
|
486
|
+
openTelemetryCollector,
|
|
487
|
+
schemaValidator,
|
|
488
|
+
mappers,
|
|
489
|
+
options
|
|
490
|
+
) {
|
|
488
491
|
this.em = em;
|
|
489
492
|
this.passwordEncryptionPublicKeyPath = passwordEncryptionPublicKeyPath;
|
|
490
493
|
this.roleServiceFactory = roleServiceFactory;
|
|
@@ -493,20 +496,22 @@ var BaseUserService = class {
|
|
|
493
496
|
this.schemaValidator = schemaValidator;
|
|
494
497
|
this.mappers = mappers;
|
|
495
498
|
this.options = options;
|
|
496
|
-
this
|
|
497
|
-
this.evaluatedTelemetryOptions = options?.telemetry
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
499
|
+
this._mappers = transformIntoInternalMapper4(mappers, schemaValidator);
|
|
500
|
+
this.evaluatedTelemetryOptions = options?.telemetry
|
|
501
|
+
? evaluateTelemetryOptions4(options.telemetry).enabled
|
|
502
|
+
: {
|
|
503
|
+
logging: false,
|
|
504
|
+
metrics: false,
|
|
505
|
+
tracing: false
|
|
506
|
+
};
|
|
507
|
+
}
|
|
508
|
+
_mappers;
|
|
504
509
|
evaluatedTelemetryOptions;
|
|
505
510
|
async createUser(userDto, em) {
|
|
506
511
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
507
|
-
this.openTelemetryCollector.info(
|
|
512
|
+
this.openTelemetryCollector.info('Creating user', userDto);
|
|
508
513
|
}
|
|
509
|
-
const user = await this
|
|
514
|
+
const user = await this._mappers.CreateUserMapper.deserializeDtoToEntity(
|
|
510
515
|
userDto,
|
|
511
516
|
em ?? this.em
|
|
512
517
|
);
|
|
@@ -515,15 +520,15 @@ var BaseUserService = class {
|
|
|
515
520
|
} else {
|
|
516
521
|
await this.em.persistAndFlush(user);
|
|
517
522
|
}
|
|
518
|
-
return this
|
|
523
|
+
return this._mappers.UserMapper.serializeEntityToDto(user);
|
|
519
524
|
}
|
|
520
525
|
async createBatchUsers(userDtos, em) {
|
|
521
526
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
522
|
-
this.openTelemetryCollector.info(
|
|
527
|
+
this.openTelemetryCollector.info('Creating batch users', userDtos);
|
|
523
528
|
}
|
|
524
529
|
const users = await Promise.all(
|
|
525
|
-
userDtos.map(
|
|
526
|
-
|
|
530
|
+
userDtos.map(async (createUserDto) =>
|
|
531
|
+
this._mappers.CreateUserMapper.deserializeDtoToEntity(
|
|
527
532
|
createUserDto,
|
|
528
533
|
em ?? this.em
|
|
529
534
|
)
|
|
@@ -535,41 +540,35 @@ var BaseUserService = class {
|
|
|
535
540
|
await this.em.persistAndFlush(users);
|
|
536
541
|
}
|
|
537
542
|
return Promise.all(
|
|
538
|
-
users.map(
|
|
539
|
-
(user) => this.#mappers.UserDtoMapper.serializeEntityToDto(user)
|
|
540
|
-
)
|
|
543
|
+
users.map((user) => this._mappers.UserMapper.serializeEntityToDto(user))
|
|
541
544
|
);
|
|
542
545
|
}
|
|
543
546
|
async getUser(idDto, em) {
|
|
544
547
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
545
|
-
this.openTelemetryCollector.info(
|
|
548
|
+
this.openTelemetryCollector.info('Getting user', idDto);
|
|
546
549
|
}
|
|
547
|
-
const user = await (em ?? this.em).findOneOrFail(
|
|
548
|
-
populate: [
|
|
550
|
+
const user = await (em ?? this.em).findOneOrFail('User', idDto, {
|
|
551
|
+
populate: ['id', '*']
|
|
549
552
|
});
|
|
550
|
-
return this
|
|
551
|
-
user
|
|
552
|
-
);
|
|
553
|
+
return this._mappers.UserMapper.serializeEntityToDto(user);
|
|
553
554
|
}
|
|
554
555
|
async getBatchUsers(idsDto, em) {
|
|
555
556
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
556
|
-
this.openTelemetryCollector.info(
|
|
557
|
+
this.openTelemetryCollector.info('Getting batch users', idsDto);
|
|
557
558
|
}
|
|
558
559
|
return Promise.all(
|
|
559
|
-
(
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
)
|
|
565
|
-
)
|
|
560
|
+
(
|
|
561
|
+
await (em ?? this.em).find('User', idsDto, {
|
|
562
|
+
populate: ['id', '*']
|
|
563
|
+
})
|
|
564
|
+
).map((user) => this._mappers.UserMapper.serializeEntityToDto(user))
|
|
566
565
|
);
|
|
567
566
|
}
|
|
568
567
|
async updateUser(userDto, em) {
|
|
569
568
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
570
|
-
this.openTelemetryCollector.info(
|
|
569
|
+
this.openTelemetryCollector.info('Updating user', userDto);
|
|
571
570
|
}
|
|
572
|
-
const user = await this
|
|
571
|
+
const user = await this._mappers.UpdateUserMapper.deserializeDtoToEntity(
|
|
573
572
|
userDto,
|
|
574
573
|
em ?? this.em
|
|
575
574
|
);
|
|
@@ -578,15 +577,15 @@ var BaseUserService = class {
|
|
|
578
577
|
} else {
|
|
579
578
|
await this.em.persistAndFlush(user);
|
|
580
579
|
}
|
|
581
|
-
return this
|
|
580
|
+
return this._mappers.UserMapper.serializeEntityToDto(user);
|
|
582
581
|
}
|
|
583
582
|
async updateBatchUsers(userDtos, em) {
|
|
584
583
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
585
|
-
this.openTelemetryCollector.info(
|
|
584
|
+
this.openTelemetryCollector.info('Updating batch users', userDtos);
|
|
586
585
|
}
|
|
587
586
|
const users = await Promise.all(
|
|
588
|
-
userDtos.map(
|
|
589
|
-
|
|
587
|
+
userDtos.map(async (updateUserDto) =>
|
|
588
|
+
this._mappers.UpdateUserMapper.deserializeDtoToEntity(
|
|
590
589
|
updateUserDto,
|
|
591
590
|
em ?? this.em
|
|
592
591
|
)
|
|
@@ -598,46 +597,51 @@ var BaseUserService = class {
|
|
|
598
597
|
await this.em.persistAndFlush(users);
|
|
599
598
|
}
|
|
600
599
|
return Promise.all(
|
|
601
|
-
users.map(
|
|
602
|
-
(user) => this.#mappers.UserDtoMapper.serializeEntityToDto(user)
|
|
603
|
-
)
|
|
600
|
+
users.map((user) => this._mappers.UserMapper.serializeEntityToDto(user))
|
|
604
601
|
);
|
|
605
602
|
}
|
|
606
603
|
async deleteUser(idDto, em) {
|
|
607
604
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
608
|
-
this.openTelemetryCollector.info(
|
|
605
|
+
this.openTelemetryCollector.info('Deleting user', idDto);
|
|
609
606
|
}
|
|
610
|
-
await (em ?? this.em).nativeDelete(
|
|
607
|
+
await (em ?? this.em).nativeDelete('User', idDto);
|
|
611
608
|
}
|
|
612
609
|
async deleteBatchUsers(idsDto, em) {
|
|
613
610
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
614
|
-
this.openTelemetryCollector.info(
|
|
611
|
+
this.openTelemetryCollector.info('Deleting batch users', idsDto);
|
|
615
612
|
}
|
|
616
|
-
await (em ?? this.em).nativeDelete(
|
|
613
|
+
await (em ?? this.em).nativeDelete('User', idsDto);
|
|
617
614
|
}
|
|
618
615
|
async verifyHasRole(idDto, roleId) {
|
|
619
616
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
620
|
-
this.openTelemetryCollector.info(
|
|
617
|
+
this.openTelemetryCollector.info('Verifying user has role', {
|
|
621
618
|
idDto,
|
|
622
619
|
roleId
|
|
623
620
|
});
|
|
624
621
|
}
|
|
625
622
|
const user = await this.getUser(idDto);
|
|
626
|
-
if (
|
|
627
|
-
|
|
628
|
-
|
|
623
|
+
if (
|
|
624
|
+
user.roles.filter((role) => {
|
|
625
|
+
return roleId == role.id;
|
|
626
|
+
}).length === 0
|
|
627
|
+
) {
|
|
629
628
|
throw new Error(`User ${idDto.id} does not have role ${roleId}`);
|
|
630
629
|
}
|
|
631
630
|
}
|
|
632
631
|
async verifyHasPermission(idDto, permissionId) {
|
|
633
632
|
if (this.evaluatedTelemetryOptions.logging) {
|
|
634
|
-
this.openTelemetryCollector.info(
|
|
633
|
+
this.openTelemetryCollector.info('Verifying user has permission', {
|
|
635
634
|
idDto,
|
|
636
635
|
permissionId
|
|
637
636
|
});
|
|
638
637
|
}
|
|
639
638
|
const user = await this.getUser(idDto);
|
|
640
|
-
if (
|
|
639
|
+
if (
|
|
640
|
+
user.roles
|
|
641
|
+
.map((role) => role.permissions.map((permission) => permission.id))
|
|
642
|
+
.flat()
|
|
643
|
+
.filter((id) => id == permissionId).length === 0
|
|
644
|
+
) {
|
|
641
645
|
throw new Error(
|
|
642
646
|
`User ${idDto.id} does not have permission ${permissionId}`
|
|
643
647
|
);
|