@drax/identity-back 0.5.3 → 0.5.4
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/dist/controllers/RoleController.js +84 -0
- package/dist/controllers/TenantController.js +1 -2
- package/dist/controllers/UserApiKeyController.js +127 -0
- package/dist/controllers/UserController.js +295 -0
- package/dist/index.js +1 -2
- package/dist/repository/mongo/RoleMongoRepository.js +12 -3
- package/dist/repository/mongo/TenantMongoRepository.js +11 -0
- package/dist/routes/RoleRoutes.js +11 -207
- package/dist/routes/TenantRoutes.js +1 -0
- package/dist/routes/UserApiKeyRoutes.js +6 -114
- package/dist/routes/UserRoutes.js +12 -218
- package/dist/services/RoleService.js +42 -2
- package/dist/services/TenantService.js +5 -0
- package/dist/services/UserApiKeyService.js +9 -1
- package/dist/services/UserService.js +14 -4
- package/dist/setup/CreateOrUpdateRole.js +1 -1
- package/package.json +4 -4
- package/src/controllers/RoleController.ts +94 -0
- package/src/controllers/TenantController.ts +1 -2
- package/src/controllers/UserApiKeyController.ts +144 -0
- package/src/controllers/UserController.ts +300 -0
- package/src/index.ts +0 -2
- package/src/repository/mongo/RoleMongoRepository.ts +12 -3
- package/src/repository/mongo/TenantMongoRepository.ts +11 -0
- package/src/routes/RoleRoutes.ts +11 -186
- package/src/routes/TenantRoutes.ts +2 -0
- package/src/routes/UserApiKeyRoutes.ts +6 -113
- package/src/routes/UserRoutes.ts +12 -202
- package/src/services/RoleService.ts +45 -4
- package/src/services/TenantService.ts +7 -1
- package/src/services/UserApiKeyService.ts +11 -1
- package/src/services/UserService.ts +16 -4
- package/src/setup/CreateOrUpdateRole.ts +1 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/types/controllers/RoleController.d.ts +14 -0
- package/types/controllers/RoleController.d.ts.map +1 -0
- package/types/controllers/TenantController.d.ts.map +1 -1
- package/types/controllers/UserApiKeyController.d.ts +14 -0
- package/types/controllers/UserApiKeyController.d.ts.map +1 -0
- package/types/controllers/UserController.d.ts +27 -0
- package/types/controllers/UserController.d.ts.map +1 -0
- package/types/index.d.ts +1 -2
- package/types/index.d.ts.map +1 -1
- package/types/repository/mongo/RoleMongoRepository.d.ts +2 -0
- package/types/repository/mongo/RoleMongoRepository.d.ts.map +1 -1
- package/types/repository/mongo/TenantMongoRepository.d.ts +2 -0
- package/types/repository/mongo/TenantMongoRepository.d.ts.map +1 -1
- package/types/routes/RoleRoutes.d.ts.map +1 -1
- package/types/routes/TenantRoutes.d.ts.map +1 -1
- package/types/routes/UserApiKeyRoutes.d.ts.map +1 -1
- package/types/routes/UserRoutes.d.ts.map +1 -1
- package/types/services/RoleService.d.ts +5 -1
- package/types/services/RoleService.d.ts.map +1 -1
- package/types/services/TenantService.d.ts +1 -0
- package/types/services/TenantService.d.ts.map +1 -1
- package/types/services/UserApiKeyService.d.ts +2 -1
- package/types/services/UserApiKeyService.d.ts.map +1 -1
- package/types/services/UserService.d.ts +3 -1
- package/types/services/UserService.d.ts.map +1 -1
- package/src/routes/UserAvatarRoutes.ts +0 -82
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import {IRoleRepository} from "../interfaces/IRoleRepository";
|
|
2
|
-
import { ZodErrorToValidationError} from "@drax/common-back"
|
|
2
|
+
import {UnauthorizedError, ValidationError, ZodErrorToValidationError} from "@drax/common-back"
|
|
3
|
+
import { AbstractService } from "@drax/crud-back"
|
|
3
4
|
import {roleSchema} from "../zod/RoleZod.js";
|
|
4
5
|
import {ZodError} from "zod";
|
|
5
6
|
import {IDraxPaginateOptions, IDraxPaginateResult} from "@drax/crud-share";
|
|
6
7
|
import type {IRoleBase, IRole} from "@drax/identity-share";
|
|
7
8
|
|
|
8
|
-
class RoleService {
|
|
9
|
+
class RoleService extends AbstractService<IRole, IRoleBase, IRoleBase> {
|
|
9
10
|
|
|
10
11
|
_repository: IRoleRepository
|
|
11
12
|
|
|
12
13
|
constructor(roleRepostitory: IRoleRepository) {
|
|
14
|
+
super(roleRepostitory, roleSchema)
|
|
13
15
|
this._repository = roleRepostitory
|
|
14
16
|
console.log("RoleService constructor")
|
|
15
17
|
}
|
|
@@ -29,11 +31,15 @@ class RoleService {
|
|
|
29
31
|
}
|
|
30
32
|
}
|
|
31
33
|
|
|
32
|
-
async update(id: string, roleData: IRoleBase) {
|
|
34
|
+
async update(id: string, roleData: IRoleBase): Promise<IRole> {
|
|
33
35
|
try {
|
|
34
36
|
roleData.name = roleData?.name?.trim()
|
|
35
37
|
await roleSchema.parseAsync(roleData)
|
|
36
|
-
const
|
|
38
|
+
const currentRole = await this.findById(id)
|
|
39
|
+
if(currentRole.readonly){
|
|
40
|
+
throw new ValidationError([{field:'name', reason:"role.readonly", value:roleData.name}])
|
|
41
|
+
}
|
|
42
|
+
const role: IRole = await this._repository.update(id, roleData)
|
|
37
43
|
return role
|
|
38
44
|
} catch (e) {
|
|
39
45
|
console.error("Error updating role", e)
|
|
@@ -44,7 +50,36 @@ class RoleService {
|
|
|
44
50
|
}
|
|
45
51
|
}
|
|
46
52
|
|
|
53
|
+
async systemUpdate(id: string, roleData: IRoleBase): Promise<IRole> {
|
|
54
|
+
try {
|
|
55
|
+
roleData.name = roleData?.name?.trim()
|
|
56
|
+
await roleSchema.parseAsync(roleData)
|
|
57
|
+
const role: IRole = await this._repository.update(id, roleData)
|
|
58
|
+
return role
|
|
59
|
+
} catch (e) {
|
|
60
|
+
console.error("Error systemUpdating role", e)
|
|
61
|
+
if (e instanceof ZodError) {
|
|
62
|
+
throw ZodErrorToValidationError(e, roleData)
|
|
63
|
+
}
|
|
64
|
+
throw e
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
47
68
|
async delete(id: string): Promise<boolean> {
|
|
69
|
+
try {
|
|
70
|
+
const currentRole = await this.findById(id)
|
|
71
|
+
if(currentRole.readonly){
|
|
72
|
+
throw new UnauthorizedError()
|
|
73
|
+
}
|
|
74
|
+
const deletedRole = await this._repository.delete(id);
|
|
75
|
+
return deletedRole;
|
|
76
|
+
} catch (e) {
|
|
77
|
+
console.error("Error deleting role", e)
|
|
78
|
+
throw e
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async systemDelete(id: string): Promise<boolean> {
|
|
48
83
|
try {
|
|
49
84
|
const deletedRole = await this._repository.delete(id);
|
|
50
85
|
return deletedRole;
|
|
@@ -81,6 +116,12 @@ class RoleService {
|
|
|
81
116
|
return roles
|
|
82
117
|
}
|
|
83
118
|
|
|
119
|
+
async search(value: any): Promise<IRole[]> {
|
|
120
|
+
const limit = 100
|
|
121
|
+
const roles: IRole[] = await this._repository.search(value, limit);
|
|
122
|
+
return roles;
|
|
123
|
+
}
|
|
124
|
+
|
|
84
125
|
async paginate({
|
|
85
126
|
page = 1,
|
|
86
127
|
limit = 5,
|
|
@@ -2,7 +2,7 @@ import {ITenantRepository} from "../interfaces/ITenantRepository";
|
|
|
2
2
|
import {ValidationError, ZodErrorToValidationError} from "@drax/common-back"
|
|
3
3
|
import {tenantSchema} from "../zod/TenantZod.js";
|
|
4
4
|
import {ZodError} from "zod";
|
|
5
|
-
import {ITenantBase, ITenant} from "@drax/identity-share";
|
|
5
|
+
import {ITenantBase, ITenant, IRole} from "@drax/identity-share";
|
|
6
6
|
import {IDraxPaginateOptions, IDraxPaginateResult} from "@drax/crud-share";
|
|
7
7
|
import {AbstractService} from "@drax/crud-back";
|
|
8
8
|
|
|
@@ -90,6 +90,12 @@ class TenantService extends AbstractService<ITenant,ITenantBase,ITenantBase> {
|
|
|
90
90
|
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
async search(value: any): Promise<ITenant[]> {
|
|
94
|
+
const limit = 100
|
|
95
|
+
const tenants: ITenant[] = await this._repository.search(value, limit);
|
|
96
|
+
return tenants;
|
|
97
|
+
}
|
|
98
|
+
|
|
93
99
|
async paginate({
|
|
94
100
|
page = 1,
|
|
95
101
|
limit = 5,
|
|
@@ -7,12 +7,14 @@ import {IDraxPaginateOptions, IDraxPaginateResult} from "@drax/crud-share";
|
|
|
7
7
|
import crypto from "node:crypto";
|
|
8
8
|
import AuthUtils from "../utils/AuthUtils.js";
|
|
9
9
|
import IdentityConfig from "../config/IdentityConfig.js";
|
|
10
|
+
import {AbstractService} from "@drax/crud-back";
|
|
10
11
|
|
|
11
|
-
class UserApiKeyService {
|
|
12
|
+
class UserApiKeyService extends AbstractService<IUserApiKey, IUserApiKeyBase, IUserApiKeyBase>{
|
|
12
13
|
|
|
13
14
|
_repository: IUserApiKeyRepository
|
|
14
15
|
|
|
15
16
|
constructor(userApiKeyRepostitory: IUserApiKeyRepository) {
|
|
17
|
+
super(userApiKeyRepostitory,userApiKeySchema)
|
|
16
18
|
this._repository = userApiKeyRepostitory
|
|
17
19
|
console.log("UserApiKeyService constructor")
|
|
18
20
|
}
|
|
@@ -22,6 +24,9 @@ class UserApiKeyService {
|
|
|
22
24
|
userApiKeyData.name = userApiKeyData?.name?.trim()
|
|
23
25
|
const secret = crypto.randomUUID()
|
|
24
26
|
const APIKEY_SECRET = DraxConfig.getOrLoad(IdentityConfig.ApiKeySecret)
|
|
27
|
+
if(!APIKEY_SECRET){
|
|
28
|
+
throw new Error('ApiKey miss configuration')
|
|
29
|
+
}
|
|
25
30
|
userApiKeyData.secret = AuthUtils.generateHMAC(APIKEY_SECRET, secret)
|
|
26
31
|
await userApiKeySchema.parseAsync(userApiKeyData)
|
|
27
32
|
const userApiKey = await this._repository.create(userApiKeyData)
|
|
@@ -74,9 +79,14 @@ class UserApiKeyService {
|
|
|
74
79
|
|
|
75
80
|
}
|
|
76
81
|
|
|
82
|
+
|
|
83
|
+
|
|
77
84
|
async findBySecret(secret: string): Promise<IUserApiKey | null> {
|
|
78
85
|
try{
|
|
79
86
|
const APIKEY_SECRET = DraxConfig.getOrLoad(IdentityConfig.ApiKeySecret)
|
|
87
|
+
if(!APIKEY_SECRET){
|
|
88
|
+
throw new Error('ApiKey miss configuration')
|
|
89
|
+
}
|
|
80
90
|
const hashedSecret = AuthUtils.generateHMAC(APIKEY_SECRET, secret)
|
|
81
91
|
const userApiKey: IUserApiKey = await this._repository.findBySecret(hashedSecret);
|
|
82
92
|
return userApiKey
|
|
@@ -3,15 +3,18 @@ import type {IUserRepository} from "../interfaces/IUserRepository";
|
|
|
3
3
|
import {ZodError} from "zod";
|
|
4
4
|
import {ValidationError, ZodErrorToValidationError} from "@drax/common-back";
|
|
5
5
|
import AuthUtils from "../utils/AuthUtils.js";
|
|
6
|
-
import {createUserSchema, editUserSchema,} from "../zod/UserZod.js";
|
|
6
|
+
import {createUserSchema, editUserSchema, userBaseSchema} from "../zod/UserZod.js";
|
|
7
7
|
import BadCredentialsError from "../errors/BadCredentialsError.js";
|
|
8
8
|
import {IDraxPaginateOptions, IDraxPaginateResult} from "@drax/crud-share";
|
|
9
|
+
import {AbstractService} from "@drax/crud-back";
|
|
10
|
+
import {ITenant} from "@drax/identity-share";
|
|
9
11
|
|
|
10
|
-
class UserService {
|
|
12
|
+
class UserService extends AbstractService<IUser, IUserCreate, IUserUpdate>{
|
|
11
13
|
|
|
12
14
|
_repository: IUserRepository
|
|
13
15
|
|
|
14
16
|
constructor(userRepository: IUserRepository) {
|
|
17
|
+
super(userRepository,userBaseSchema);
|
|
15
18
|
this._repository = userRepository;
|
|
16
19
|
console.log("UserService constructor")
|
|
17
20
|
}
|
|
@@ -123,8 +126,11 @@ class UserService {
|
|
|
123
126
|
|
|
124
127
|
async delete(id: string): Promise<boolean> {
|
|
125
128
|
try {
|
|
126
|
-
const
|
|
127
|
-
|
|
129
|
+
const result: boolean = await this._repository.delete(id);
|
|
130
|
+
if(!result){
|
|
131
|
+
throw new Error("error.deletionFailed")
|
|
132
|
+
}
|
|
133
|
+
return result;
|
|
128
134
|
} catch (e) {
|
|
129
135
|
console.error("Error deleting user", e)
|
|
130
136
|
throw e
|
|
@@ -171,6 +177,12 @@ class UserService {
|
|
|
171
177
|
}
|
|
172
178
|
|
|
173
179
|
}
|
|
180
|
+
|
|
181
|
+
async search(value: any): Promise<IUser[]> {
|
|
182
|
+
const limit = 100
|
|
183
|
+
const users: IUser[] = await this._repository.search(value, limit);
|
|
184
|
+
return users;
|
|
185
|
+
}
|
|
174
186
|
}
|
|
175
187
|
|
|
176
188
|
export default UserService
|
|
@@ -18,7 +18,7 @@ async function CreateOrUpdateRole(roleData: IRoleBase) {
|
|
|
18
18
|
}
|
|
19
19
|
|
|
20
20
|
if(role){
|
|
21
|
-
const r = await roleService.
|
|
21
|
+
const r = await roleService.systemUpdate(role.id, roleData)
|
|
22
22
|
console.log("Role Updated. Name: "+ roleData.name)
|
|
23
23
|
}else{
|
|
24
24
|
const r = await roleService.create(roleData)
|