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