@forklaunch/implementation-iam-base 0.1.13 → 0.1.14
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 +357 -146
- 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 +137 -48
- package/lib/schemas/role.schema.js +4 -1
- package/lib/schemas/typebox/organization.schema.d.ts +405 -103
- package/lib/schemas/typebox/organization.schema.js +27 -17
- package/lib/schemas/typebox/permission.schema.d.ts +122 -38
- package/lib/schemas/typebox/permission.schema.js +24 -17
- package/lib/schemas/typebox/role.schema.d.ts +190 -50
- package/lib/schemas/typebox/role.schema.js +24 -17
- package/lib/schemas/typebox/user.schema.d.ts +330 -94
- package/lib/schemas/typebox/user.schema.js +40 -32
- package/lib/schemas/user.schema.d.ts +260 -114
- package/lib/schemas/user.schema.js +4 -1
- package/lib/schemas/zod/organization.schema.d.ts +301 -211
- package/lib/schemas/zod/organization.schema.js +27 -17
- package/lib/schemas/zod/permission.schema.d.ts +54 -38
- package/lib/schemas/zod/permission.schema.js +24 -17
- package/lib/schemas/zod/role.schema.d.ts +94 -64
- package/lib/schemas/zod/role.schema.js +24 -17
- package/lib/schemas/zod/user.schema.d.ts +184 -138
- 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,85 +1,126 @@
|
|
|
1
1
|
import { transformIntoInternalDtoMapper } from '@forklaunch/core/mappers';
|
|
2
2
|
export class BaseUserService {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
3
|
+
em;
|
|
4
|
+
passwordEncryptionPublicKeyPath;
|
|
5
|
+
roleServiceFactory;
|
|
6
|
+
organizationServiceFactory;
|
|
7
|
+
openTelemetryCollector;
|
|
8
|
+
schemaValidator;
|
|
9
|
+
mapperss;
|
|
10
|
+
#mapperss;
|
|
11
|
+
constructor(
|
|
12
|
+
em,
|
|
13
|
+
passwordEncryptionPublicKeyPath,
|
|
14
|
+
roleServiceFactory,
|
|
15
|
+
organizationServiceFactory,
|
|
16
|
+
openTelemetryCollector,
|
|
17
|
+
schemaValidator,
|
|
18
|
+
mapperss
|
|
19
|
+
) {
|
|
20
|
+
this.em = em;
|
|
21
|
+
this.passwordEncryptionPublicKeyPath = passwordEncryptionPublicKeyPath;
|
|
22
|
+
this.roleServiceFactory = roleServiceFactory;
|
|
23
|
+
this.organizationServiceFactory = organizationServiceFactory;
|
|
24
|
+
this.openTelemetryCollector = openTelemetryCollector;
|
|
25
|
+
this.schemaValidator = schemaValidator;
|
|
26
|
+
this.mapperss = mapperss;
|
|
27
|
+
this.#mapperss = transformIntoInternalDtoMapper(mapperss, schemaValidator);
|
|
28
|
+
}
|
|
29
|
+
async createUser(userDto, em) {
|
|
30
|
+
const user =
|
|
31
|
+
await this.#mapperss.CreateUserDtoMapper.deserializeDtoToEntity(
|
|
32
|
+
userDto,
|
|
33
|
+
this.passwordEncryptionPublicKeyPath
|
|
34
|
+
);
|
|
35
|
+
((await em) ?? this.em).transactional(async (em) => {
|
|
36
|
+
await em.persist(user);
|
|
37
|
+
});
|
|
38
|
+
return this.#mapperss.UserDtoMapper.serializeEntityToDto(user);
|
|
39
|
+
}
|
|
40
|
+
async createBatchUsers(userDtos, em) {
|
|
41
|
+
const users = await Promise.all(
|
|
42
|
+
userDtos.map(async (createUserDto) =>
|
|
43
|
+
this.#mapperss.CreateUserDtoMapper.deserializeDtoToEntity(
|
|
44
|
+
createUserDto,
|
|
45
|
+
this.passwordEncryptionPublicKeyPath
|
|
46
|
+
)
|
|
47
|
+
)
|
|
48
|
+
);
|
|
49
|
+
await (em ?? this.em).transactional(async (em) => {
|
|
50
|
+
await em.persist(users);
|
|
51
|
+
});
|
|
52
|
+
return users.map((user) =>
|
|
53
|
+
this.#mapperss.UserDtoMapper.serializeEntityToDto(user)
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
async getUser(idDto, em) {
|
|
57
|
+
const user = await (em ?? this.em).findOneOrFail('User', idDto, {
|
|
58
|
+
populate: ['id', '*']
|
|
59
|
+
});
|
|
60
|
+
return this.#mapperss.UserDtoMapper.serializeEntityToDto(user);
|
|
61
|
+
}
|
|
62
|
+
async getBatchUsers(idsDto, em) {
|
|
63
|
+
return (
|
|
64
|
+
await (em ?? this.em).find('User', idsDto, {
|
|
65
|
+
populate: ['id', '*']
|
|
66
|
+
})
|
|
67
|
+
).map((user) => this.#mapperss.UserDtoMapper.serializeEntityToDto(user));
|
|
68
|
+
}
|
|
69
|
+
async updateUser(userDto, em) {
|
|
70
|
+
let user = this.#mapperss.UpdateUserDtoMapper.deserializeDtoToEntity(
|
|
71
|
+
userDto,
|
|
72
|
+
this.passwordEncryptionPublicKeyPath
|
|
73
|
+
);
|
|
74
|
+
await (em ?? this.em).transactional(async (localEm) => {
|
|
75
|
+
user = await localEm.upsert(user);
|
|
76
|
+
});
|
|
77
|
+
return this.#mapperss.UserDtoMapper.serializeEntityToDto(user);
|
|
78
|
+
}
|
|
79
|
+
async updateBatchUsers(userDtos, em) {
|
|
80
|
+
let users = await Promise.all(
|
|
81
|
+
userDtos.map(async (updateUserDto) =>
|
|
82
|
+
this.#mapperss.UpdateUserDtoMapper.deserializeDtoToEntity(
|
|
83
|
+
updateUserDto,
|
|
84
|
+
this.passwordEncryptionPublicKeyPath
|
|
85
|
+
)
|
|
86
|
+
)
|
|
87
|
+
);
|
|
88
|
+
await (em ?? this.em).transactional(async (localEm) => {
|
|
89
|
+
users = await localEm.upsertMany(users);
|
|
90
|
+
});
|
|
91
|
+
return users.map((user) =>
|
|
92
|
+
this.#mapperss.UserDtoMapper.serializeEntityToDto(user)
|
|
93
|
+
);
|
|
94
|
+
}
|
|
95
|
+
async deleteUser(idDto, em) {
|
|
96
|
+
const entityManager = em || this.em;
|
|
97
|
+
await entityManager.nativeDelete('User', idDto);
|
|
98
|
+
}
|
|
99
|
+
async deleteBatchUsers(idsDto, em) {
|
|
100
|
+
const entityManager = em || this.em;
|
|
101
|
+
await entityManager.nativeDelete('User', idsDto);
|
|
102
|
+
}
|
|
103
|
+
async verifyHasRole(idDto, roleId) {
|
|
104
|
+
const user = await this.getUser(idDto);
|
|
105
|
+
if (
|
|
106
|
+
user.roles.filter((role) => {
|
|
107
|
+
return roleId == role.id;
|
|
108
|
+
}).length === 0
|
|
109
|
+
) {
|
|
110
|
+
throw new Error(`User ${idDto.id} does not have role ${roleId}`);
|
|
20
111
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
return users.map((user) => this.#mapperss.UserDtoMapper.serializeEntityToDto(user));
|
|
34
|
-
}
|
|
35
|
-
async getUser(idDto, em) {
|
|
36
|
-
const user = await (em ?? this.em).findOneOrFail('User', idDto, {
|
|
37
|
-
populate: ['id', '*']
|
|
38
|
-
});
|
|
39
|
-
return this.#mapperss.UserDtoMapper.serializeEntityToDto(user);
|
|
40
|
-
}
|
|
41
|
-
async getBatchUsers(idsDto, em) {
|
|
42
|
-
return (await (em ?? this.em).find('User', idsDto, {
|
|
43
|
-
populate: ['id', '*']
|
|
44
|
-
})).map((user) => this.#mapperss.UserDtoMapper.serializeEntityToDto(user));
|
|
45
|
-
}
|
|
46
|
-
async updateUser(userDto, em) {
|
|
47
|
-
let user = this.#mapperss.UpdateUserDtoMapper.deserializeDtoToEntity(userDto, this.passwordEncryptionPublicKeyPath);
|
|
48
|
-
await (em ?? this.em).transactional(async (localEm) => {
|
|
49
|
-
user = await localEm.upsert(user);
|
|
50
|
-
});
|
|
51
|
-
return this.#mapperss.UserDtoMapper.serializeEntityToDto(user);
|
|
52
|
-
}
|
|
53
|
-
async updateBatchUsers(userDtos, em) {
|
|
54
|
-
let users = await Promise.all(userDtos.map(async (updateUserDto) => this.#mapperss.UpdateUserDtoMapper.deserializeDtoToEntity(updateUserDto, this.passwordEncryptionPublicKeyPath)));
|
|
55
|
-
await (em ?? this.em).transactional(async (localEm) => {
|
|
56
|
-
users = await localEm.upsertMany(users);
|
|
57
|
-
});
|
|
58
|
-
return users.map((user) => this.#mapperss.UserDtoMapper.serializeEntityToDto(user));
|
|
59
|
-
}
|
|
60
|
-
async deleteUser(idDto, em) {
|
|
61
|
-
const entityManager = em || this.em;
|
|
62
|
-
await entityManager.nativeDelete('User', idDto);
|
|
63
|
-
}
|
|
64
|
-
async deleteBatchUsers(idsDto, em) {
|
|
65
|
-
const entityManager = em || this.em;
|
|
66
|
-
await entityManager.nativeDelete('User', idsDto);
|
|
67
|
-
}
|
|
68
|
-
async verifyHasRole(idDto, roleId) {
|
|
69
|
-
const user = await this.getUser(idDto);
|
|
70
|
-
if (user.roles.filter((role) => {
|
|
71
|
-
return roleId == role.id;
|
|
72
|
-
}).length === 0) {
|
|
73
|
-
throw new Error(`User ${idDto.id} does not have role ${roleId}`);
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
async verifyHasPermission(idDto, permissionId) {
|
|
77
|
-
const user = await this.getUser(idDto);
|
|
78
|
-
if (user.roles
|
|
79
|
-
.map((role) => role.permissions.map((permission) => permission.id))
|
|
80
|
-
.flat()
|
|
81
|
-
.filter((id) => id == permissionId).length === 0) {
|
|
82
|
-
throw new Error(`User ${idDto.id} does not have permission ${permissionId}`);
|
|
83
|
-
}
|
|
112
|
+
}
|
|
113
|
+
async verifyHasPermission(idDto, permissionId) {
|
|
114
|
+
const user = await this.getUser(idDto);
|
|
115
|
+
if (
|
|
116
|
+
user.roles
|
|
117
|
+
.map((role) => role.permissions.map((permission) => permission.id))
|
|
118
|
+
.flat()
|
|
119
|
+
.filter((id) => id == permissionId).length === 0
|
|
120
|
+
) {
|
|
121
|
+
throw new Error(
|
|
122
|
+
`User ${idDto.id} does not have permission ${permissionId}`
|
|
123
|
+
);
|
|
84
124
|
}
|
|
125
|
+
}
|
|
85
126
|
}
|