@drax/identity-back 0.1.0 → 0.1.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/routes/UserApiKeyRoutes.js +3 -3
- package/dist/services/RoleService.js +34 -8
- package/dist/services/TenantService.js +42 -11
- package/dist/services/UserApiKeyService.js +36 -11
- package/dist/services/UserService.js +34 -8
- package/package.json +2 -2
- package/src/routes/UserApiKeyRoutes.ts +3 -3
- package/src/services/RoleService.ts +41 -16
- package/src/services/TenantService.ts +49 -17
- package/src/services/UserApiKeyService.ts +42 -17
- package/src/services/UserService.ts +74 -48
- package/tsconfig.tsbuildinfo +1 -1
- package/types/config/IdentityConfig.d.ts +12 -0
- package/types/config/IdentityConfig.d.ts.map +1 -0
- package/types/errors/BadCredentialsError.d.ts +6 -0
- package/types/errors/BadCredentialsError.d.ts.map +1 -0
- package/types/errors/UnauthorizedError.d.ts +6 -0
- package/types/errors/UnauthorizedError.d.ts.map +1 -0
- package/types/factory/RoleServiceFactory.d.ts +4 -0
- package/types/factory/TenantServiceFactory.d.ts +4 -0
- package/types/factory/UserApiKeyServiceFactory.d.ts +4 -0
- package/types/factory/UserServiceFactory.d.ts +4 -0
- package/types/graphql/index.d.ts +6 -0
- package/types/graphql/resolvers/role.resolvers.d.ts +52 -0
- package/types/graphql/resolvers/tenant.resolvers.d.ts +49 -0
- package/types/graphql/resolvers/user-api-key.resolvers.d.ts +37 -0
- package/types/graphql/resolvers/user.resolvers.d.ts +67 -0
- package/types/index.d.ts +35 -0
- package/types/interfaces/IRoleRepository.d.ts +9 -0
- package/types/interfaces/ITenantRepository.d.ts +9 -0
- package/types/interfaces/IUserApiKeyRepository.d.ts +7 -0
- package/types/interfaces/IUserRepository.d.ts +10 -0
- package/types/middleware/apiKeyMiddleware.d.ts +4 -0
- package/types/middleware/jwtMiddleware.d.ts +4 -0
- package/types/middleware/rbacMiddleware.d.ts +4 -0
- package/types/models/RoleModel.d.ts +16 -0
- package/types/models/TenantModel.d.ts +16 -0
- package/types/models/UserApiKeyModel.d.ts +16 -0
- package/types/models/UserGroupModel.d.ts +16 -0
- package/types/models/UserModel.d.ts +16 -0
- package/types/permissions/IdentityPermissions.d.ts +27 -0
- package/types/permissions/IdentityPermissions.d.ts.map +1 -0
- package/types/repository/mongo/RoleMongoRepository.d.ts +14 -0
- package/types/repository/mongo/TenantMongoRepository.d.ts +14 -0
- package/types/repository/mongo/UserApiKeyMongoRepository.d.ts +14 -0
- package/types/repository/mongo/UserMongoRepository.d.ts +17 -0
- package/types/repository/sqlite/RoleSqliteRepository.d.ts +22 -0
- package/types/repository/sqlite/TenantSqliteRepository.d.ts +19 -0
- package/types/repository/sqlite/UserSqliteRepository.d.ts +25 -0
- package/types/routes/RoleRoutes.d.ts +4 -0
- package/types/routes/TenantRoutes.d.ts +4 -0
- package/types/routes/UserApiKeyRoutes.d.ts +4 -0
- package/types/routes/UserAvatarRoutes.d.ts +4 -0
- package/types/routes/UserRoutes.d.ts +4 -0
- package/types/services/PermissionService.d.ts +9 -0
- package/types/services/PermissionService.d.ts.map +1 -0
- package/types/services/RoleService.d.ts +16 -0
- package/types/services/RoleService.d.ts.map +1 -1
- package/types/services/TenantService.d.ts +16 -0
- package/types/services/TenantService.d.ts.map +1 -1
- package/types/services/UserApiKeyService.d.ts +15 -0
- package/types/services/UserApiKeyService.d.ts.map +1 -1
- package/types/services/UserService.d.ts +21 -0
- package/types/services/UserService.d.ts.map +1 -1
- package/types/setup/CreateOrUpdateRole.d.ts +5 -0
- package/types/setup/CreateUserIfNotExist.d.ts +5 -0
- package/types/setup/LoadIdentityConfigFromEnv.d.ts +4 -0
- package/types/setup/LoadPermissions.d.ts +4 -0
- package/types/setup/LoadPermissions.d.ts.map +1 -0
- package/types/setup/RecoveryUserPassword.d.ts +4 -0
- package/types/utils/AuthUtils.d.ts +18 -0
- package/types/zod/RoleZod.d.ts +10 -0
- package/types/zod/RoleZod.d.ts.map +1 -0
- package/types/zod/TenantZod.d.ts +10 -0
- package/types/zod/TenantZod.d.ts.map +1 -0
- package/types/zod/UserZod.d.ts +53 -0
- package/types/zod/UserZod.d.ts.map +1 -0
|
@@ -16,68 +16,68 @@ class UserService {
|
|
|
16
16
|
console.log("UserService constructor")
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
async auth(username
|
|
19
|
+
async auth(username: string, password: string) {
|
|
20
20
|
let user = null
|
|
21
|
-
console.log("auth username",username)
|
|
21
|
+
console.log("auth username", username)
|
|
22
22
|
user = await this.findByUsername(username)
|
|
23
23
|
if (user && user.active && AuthUtils.checkPassword(password, user.password)) {
|
|
24
24
|
//TODO: Generar Sesion
|
|
25
25
|
const session = '123'
|
|
26
26
|
const accessToken = AuthUtils.generateToken(user.id.toString(), user.username, user.role.id, user.tenant?.id, session)
|
|
27
27
|
return {accessToken: accessToken}
|
|
28
|
-
}else{
|
|
28
|
+
} else {
|
|
29
29
|
throw new BadCredentialsError()
|
|
30
30
|
}
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
async changeUserPassword(userId
|
|
33
|
+
async changeUserPassword(userId: string, newPassword: string) {
|
|
34
34
|
const user = await this.findById(userId)
|
|
35
|
-
if(user){
|
|
36
|
-
newPassword =
|
|
35
|
+
if (user) {
|
|
36
|
+
newPassword = AuthUtils.hashPassword(newPassword)
|
|
37
37
|
await this._repository.changePassword(userId, newPassword)
|
|
38
38
|
return true
|
|
39
|
-
}else{
|
|
39
|
+
} else {
|
|
40
40
|
throw new ValidationError([{field: 'userId', reason: 'validation.notFound'}])
|
|
41
41
|
}
|
|
42
42
|
}
|
|
43
43
|
|
|
44
44
|
|
|
45
|
-
async changeOwnPassword(userId
|
|
45
|
+
async changeOwnPassword(userId: string, currentPassword: string, newPassword: string) {
|
|
46
46
|
const user = await this.findById(userId)
|
|
47
47
|
|
|
48
48
|
|
|
49
|
-
if(user && user.active){
|
|
49
|
+
if (user && user.active) {
|
|
50
50
|
|
|
51
|
-
if(currentPassword === newPassword){
|
|
51
|
+
if (currentPassword === newPassword) {
|
|
52
52
|
throw new ValidationError([{field: 'newPassword', reason: 'validation.password.currentDifferent'}])
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
if (AuthUtils.checkPassword(currentPassword, user.password)) {
|
|
56
|
-
newPassword =
|
|
56
|
+
newPassword = AuthUtils.hashPassword(newPassword)
|
|
57
57
|
await this._repository.changePassword(userId, newPassword)
|
|
58
58
|
return true
|
|
59
|
-
}else{
|
|
59
|
+
} else {
|
|
60
60
|
throw new ValidationError([{field: 'currentPassword', reason: 'validation.notMatch'}])
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
}else{
|
|
63
|
+
} else {
|
|
64
64
|
throw new BadCredentialsError()
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
async changeAvatar(userId
|
|
68
|
+
async changeAvatar(userId: string, avatar: string) {
|
|
69
69
|
const user = await this.findById(userId)
|
|
70
|
-
if(user && user.active){
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
}else{
|
|
70
|
+
if (user && user.active) {
|
|
71
|
+
await this._repository.changeAvatar(userId, avatar)
|
|
72
|
+
return true
|
|
73
|
+
} else {
|
|
74
74
|
throw new BadCredentialsError()
|
|
75
75
|
}
|
|
76
76
|
}
|
|
77
77
|
|
|
78
78
|
|
|
79
79
|
async create(userData: IUserCreate): Promise<IUser> {
|
|
80
|
-
try{
|
|
80
|
+
try {
|
|
81
81
|
userData.name = userData?.name?.trim()
|
|
82
82
|
userData.username = userData.username.trim()
|
|
83
83
|
userData.password = userData.password.trim()
|
|
@@ -89,9 +89,10 @@ class UserService {
|
|
|
89
89
|
|
|
90
90
|
const user: IUser = await this._repository.create(userData)
|
|
91
91
|
return user
|
|
92
|
-
}catch (e){
|
|
93
|
-
|
|
94
|
-
|
|
92
|
+
} catch (e) {
|
|
93
|
+
console.error("Error creating user", e)
|
|
94
|
+
if (e instanceof ZodError) {
|
|
95
|
+
throw ZodErrorToValidationError(e, userData)
|
|
95
96
|
}
|
|
96
97
|
throw e
|
|
97
98
|
}
|
|
@@ -100,50 +101,75 @@ class UserService {
|
|
|
100
101
|
}
|
|
101
102
|
|
|
102
103
|
async update(id: string, userData: IUserUpdate) {
|
|
103
|
-
try{
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
104
|
+
try {
|
|
105
|
+
userData.name = userData.name.trim()
|
|
106
|
+
userData.username = userData.username.trim()
|
|
107
|
+
delete userData.password
|
|
108
|
+
userData.tenant = userData.tenant === "" ? null : userData.tenant
|
|
108
109
|
|
|
109
|
-
|
|
110
|
+
await editUserSchema.parseAsync(userData)
|
|
110
111
|
|
|
111
112
|
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
}catch (e){
|
|
115
|
-
|
|
116
|
-
|
|
113
|
+
const user: IUser = await this._repository.update(id, userData)
|
|
114
|
+
return user
|
|
115
|
+
} catch (e) {
|
|
116
|
+
console.error("Error updating user", e)
|
|
117
|
+
if (e instanceof ZodError) {
|
|
118
|
+
throw ZodErrorToValidationError(e, userData)
|
|
117
119
|
}
|
|
118
120
|
throw e
|
|
119
121
|
}
|
|
120
122
|
}
|
|
121
123
|
|
|
122
124
|
async delete(id: string): Promise<boolean> {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
+
try {
|
|
126
|
+
const deletedRole: boolean = await this._repository.delete(id);
|
|
127
|
+
return deletedRole;
|
|
128
|
+
} catch (e) {
|
|
129
|
+
console.error("Error deleting user", e)
|
|
130
|
+
throw e
|
|
131
|
+
}
|
|
132
|
+
|
|
125
133
|
}
|
|
126
134
|
|
|
127
135
|
async findById(id: string): Promise<IUser> {
|
|
128
|
-
|
|
129
|
-
|
|
136
|
+
try {
|
|
137
|
+
const user: IUser = await this._repository.findById(id);
|
|
138
|
+
return user
|
|
139
|
+
} catch (e) {
|
|
140
|
+
console.error("Error finding user by id", e)
|
|
141
|
+
throw e
|
|
142
|
+
}
|
|
143
|
+
|
|
130
144
|
}
|
|
131
145
|
|
|
132
146
|
async findByUsername(username: string): Promise<IUser | null> {
|
|
133
|
-
|
|
134
|
-
|
|
147
|
+
try {
|
|
148
|
+
const user: IUser = await this._repository.findByUsername(username);
|
|
149
|
+
return user
|
|
150
|
+
} catch (e) {
|
|
151
|
+
console.error("Error finding user by username", e)
|
|
152
|
+
throw e
|
|
153
|
+
}
|
|
154
|
+
|
|
135
155
|
}
|
|
136
156
|
|
|
137
157
|
async paginate({
|
|
138
|
-
page= 1,
|
|
139
|
-
limit= 5,
|
|
140
|
-
orderBy= '',
|
|
141
|
-
orderDesc= false,
|
|
142
|
-
search= '',
|
|
143
|
-
filters= []
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
158
|
+
page = 1,
|
|
159
|
+
limit = 5,
|
|
160
|
+
orderBy = '',
|
|
161
|
+
orderDesc = false,
|
|
162
|
+
search = '',
|
|
163
|
+
filters = []
|
|
164
|
+
}: IDraxPaginateOptions): Promise<IDraxPaginateResult<IUser>> {
|
|
165
|
+
try {
|
|
166
|
+
const pagination = await this._repository.paginate({page, limit, orderBy, orderDesc, search, filters});
|
|
167
|
+
return pagination;
|
|
168
|
+
} catch (e) {
|
|
169
|
+
console.error("Error paginating users", e)
|
|
170
|
+
throw e;
|
|
171
|
+
}
|
|
172
|
+
|
|
147
173
|
}
|
|
148
174
|
}
|
|
149
175
|
|