@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
|
@@ -8,6 +8,8 @@ import {IRoleBase, IRole} from "@drax/identity-share";
|
|
|
8
8
|
|
|
9
9
|
class RoleMongoRepository implements IRoleRepository{
|
|
10
10
|
|
|
11
|
+
_searchFields = ['_id','name']
|
|
12
|
+
|
|
11
13
|
async create(roleData: IRoleBase): Promise<IRole> {
|
|
12
14
|
const role : mongoose.HydratedDocument<IRole> = new RoleModel(roleData)
|
|
13
15
|
await role.save()
|
|
@@ -40,6 +42,15 @@ class RoleMongoRepository implements IRoleRepository{
|
|
|
40
42
|
return roles
|
|
41
43
|
}
|
|
42
44
|
|
|
45
|
+
async search(value: string, limit: number = 1000): Promise<IRole[]> {
|
|
46
|
+
const query = {}
|
|
47
|
+
if(value){
|
|
48
|
+
query['$or'] = this._searchFields.map(field => ({[field]: new RegExp(value.toString(), 'i')}))
|
|
49
|
+
}
|
|
50
|
+
const items: mongoose.HydratedDocument<IRole>[] = await RoleModel.find(query).limit(limit).exec()
|
|
51
|
+
return items
|
|
52
|
+
}
|
|
53
|
+
|
|
43
54
|
async paginate({
|
|
44
55
|
page= 1,
|
|
45
56
|
limit= 5,
|
|
@@ -50,9 +61,7 @@ class RoleMongoRepository implements IRoleRepository{
|
|
|
50
61
|
const query = {}
|
|
51
62
|
|
|
52
63
|
if(search){
|
|
53
|
-
query['$or'] = [
|
|
54
|
-
{name: new RegExp(search, 'i')},
|
|
55
|
-
]
|
|
64
|
+
query['$or'] = this._searchFields.map(field => ({[field]: new RegExp(search.toString(), 'i')}))
|
|
56
65
|
}
|
|
57
66
|
|
|
58
67
|
MongooseQueryFilter.applyFilters(query, filters)
|
|
@@ -8,6 +8,8 @@ import {ITenant, ITenantBase} from "@drax/identity-share";
|
|
|
8
8
|
|
|
9
9
|
class TenantMongoRepository implements ITenantRepository {
|
|
10
10
|
|
|
11
|
+
_searchFields = ['_id','name']
|
|
12
|
+
|
|
11
13
|
async create(tenantData: ITenantBase): Promise<ITenant> {
|
|
12
14
|
const tenant: mongoose.HydratedDocument<ITenant> = new TenantModel(tenantData)
|
|
13
15
|
await tenant.save()
|
|
@@ -39,6 +41,15 @@ class TenantMongoRepository implements ITenantRepository {
|
|
|
39
41
|
return tenants
|
|
40
42
|
}
|
|
41
43
|
|
|
44
|
+
async search(value: string, limit: number = 1000): Promise<ITenant[]> {
|
|
45
|
+
const query = {}
|
|
46
|
+
if(value){
|
|
47
|
+
query['$or'] = this._searchFields.map(field => ({[field]: new RegExp(value.toString(), 'i')}))
|
|
48
|
+
}
|
|
49
|
+
const items: mongoose.HydratedDocument<ITenant>[] = await TenantModel.find(query).limit(limit).exec()
|
|
50
|
+
return items
|
|
51
|
+
}
|
|
52
|
+
|
|
42
53
|
async paginate({
|
|
43
54
|
page = 1,
|
|
44
55
|
limit = 5,
|
package/src/routes/RoleRoutes.ts
CHANGED
|
@@ -1,203 +1,28 @@
|
|
|
1
|
-
import
|
|
2
|
-
import RoleServiceFactory from "../factory/RoleServiceFactory.js";
|
|
3
|
-
import {IRole} from "@drax/identity-share";
|
|
4
|
-
import {IdentityPermissions} from "../permissions/IdentityPermissions.js";
|
|
5
|
-
import {PermissionService} from "../services/PermissionService.js";
|
|
6
|
-
import {IDraxPaginateResult} from "@drax/crud-share";
|
|
1
|
+
import RoleController from "../controllers/RoleController.js";
|
|
7
2
|
|
|
8
3
|
|
|
9
4
|
|
|
10
5
|
async function RoleRoutes(fastify, options) {
|
|
11
6
|
|
|
12
|
-
|
|
13
|
-
try {
|
|
14
|
-
request.rbac.assertPermission(IdentityPermissions.PermissionsRole)
|
|
15
|
-
let permissions = PermissionService.getPermissions()
|
|
16
|
-
return permissions
|
|
17
|
-
}catch (e){
|
|
18
|
-
console.error(e)
|
|
19
|
-
if (e instanceof UnauthorizedError) {
|
|
20
|
-
reply.statusCode = e.statusCode
|
|
21
|
-
reply.send({error: e.message})
|
|
22
|
-
} else {
|
|
23
|
-
reply.statusCode = 500
|
|
24
|
-
reply.send({error: 'INTERNAL_SERVER_ERROR'})
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
})
|
|
7
|
+
const controller: RoleController = new RoleController()
|
|
28
8
|
|
|
29
|
-
fastify.get('/api/
|
|
30
|
-
try {
|
|
31
|
-
request.rbac.assertPermission(IdentityPermissions.ViewRole)
|
|
32
|
-
const id = request.params.id
|
|
33
|
-
const roleService = RoleServiceFactory()
|
|
34
|
-
let role = await roleService.findById(id)
|
|
35
|
-
return role
|
|
36
|
-
} catch (e) {
|
|
37
|
-
console.error(e)
|
|
38
|
-
if (e instanceof ValidationError) {
|
|
39
|
-
reply.statusCode = e.statusCode
|
|
40
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
41
|
-
} else if (e instanceof UnauthorizedError) {
|
|
42
|
-
reply.statusCode = e.statusCode
|
|
43
|
-
reply.send({error: e.message})
|
|
44
|
-
} else {
|
|
45
|
-
reply.statusCode = 500
|
|
46
|
-
reply.send({error: 'INTERNAL_SERVER_ERROR'})
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
})
|
|
9
|
+
fastify.get('/api/permissions', (req,rep) => controller.permissions(req,rep))
|
|
50
10
|
|
|
51
|
-
fastify.get('/api/roles/
|
|
52
|
-
try {
|
|
53
|
-
request.rbac.assertPermission(IdentityPermissions.ViewRole)
|
|
54
|
-
const name = request.params.name
|
|
55
|
-
const roleService = RoleServiceFactory()
|
|
56
|
-
let role = await roleService.findByName(name)
|
|
57
|
-
return role
|
|
58
|
-
} catch (e) {
|
|
59
|
-
console.error(e)
|
|
60
|
-
if (e instanceof ValidationError) {
|
|
61
|
-
reply.statusCode = e.statusCode
|
|
62
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
63
|
-
} else if (e instanceof UnauthorizedError) {
|
|
64
|
-
reply.statusCode = e.statusCode
|
|
65
|
-
reply.send({error: e.message})
|
|
66
|
-
} else {
|
|
67
|
-
reply.statusCode = 500
|
|
68
|
-
reply.send({error: 'INTERNAL_SERVER_ERROR'})
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
})
|
|
11
|
+
fastify.get('/api/roles/search', (req,rep) => controller.search(req,rep))
|
|
72
12
|
|
|
73
|
-
fastify.get('/api/roles
|
|
74
|
-
try {
|
|
75
|
-
request.rbac.assertPermission(IdentityPermissions.ViewRole)
|
|
76
|
-
const roleService = RoleServiceFactory()
|
|
77
|
-
let roles = await roleService.fetchAll()
|
|
78
|
-
if(request.rbac.getRole?.childRoles?.length > 0) {
|
|
79
|
-
return roles.filter(role => request.rbac.getRole.childRoles.some(childRole => childRole.id === role.id));
|
|
80
|
-
}else{
|
|
81
|
-
return roles
|
|
82
|
-
}
|
|
83
|
-
} catch (e) {
|
|
84
|
-
console.error(e)
|
|
85
|
-
if (e instanceof ValidationError) {
|
|
86
|
-
reply.statusCode = e.statusCode
|
|
87
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
88
|
-
} else if (e instanceof UnauthorizedError) {
|
|
89
|
-
reply.statusCode = e.statusCode
|
|
90
|
-
reply.send({error: e.message})
|
|
91
|
-
} else {
|
|
92
|
-
reply.statusCode = 500
|
|
93
|
-
reply.send({error: 'INTERNAL_SERVER_ERROR'})
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
})
|
|
13
|
+
fastify.get('/api/roles/:id', (req,rep) => controller.findById(req,rep))
|
|
97
14
|
|
|
98
|
-
fastify.get('/api/roles',
|
|
99
|
-
try {
|
|
100
|
-
request.rbac.assertPermission(IdentityPermissions.ViewRole)
|
|
101
|
-
const page = request.query.page
|
|
102
|
-
const limit = request.query.limit
|
|
103
|
-
const orderBy = request.query.orderBy
|
|
104
|
-
const order = request.query.order
|
|
105
|
-
const search = request.query.search
|
|
106
|
-
const roleService = RoleServiceFactory()
|
|
107
|
-
let paginateResult = await roleService.paginate({page, limit, search, orderBy, order})
|
|
108
|
-
return paginateResult
|
|
109
|
-
} catch (e) {
|
|
110
|
-
console.error(e)
|
|
111
|
-
if (e instanceof ValidationError) {
|
|
112
|
-
reply.statusCode = e.statusCode
|
|
113
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
114
|
-
} else if (e instanceof UnauthorizedError) {
|
|
115
|
-
reply.statusCode = e.statusCode
|
|
116
|
-
reply.send({error: e.message})
|
|
117
|
-
} else {
|
|
118
|
-
reply.statusCode = 500
|
|
119
|
-
reply.send({error: 'INTERNAL_SERVER_ERROR'})
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
})
|
|
15
|
+
fastify.get('/api/roles/name/:name', (req,rep) => controller.findByName(req,rep))
|
|
123
16
|
|
|
124
|
-
fastify.
|
|
125
|
-
try {
|
|
126
|
-
request.rbac.assertPermission(IdentityPermissions.CreateRole)
|
|
127
|
-
const payload = request.body
|
|
128
|
-
const roleService = RoleServiceFactory()
|
|
129
|
-
let role = await roleService.create(payload)
|
|
130
|
-
return role
|
|
131
|
-
} catch (e) {
|
|
132
|
-
console.error(e)
|
|
133
|
-
if (e instanceof ValidationError) {
|
|
134
|
-
reply.statusCode = e.statusCode
|
|
135
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
136
|
-
} else if (e instanceof UnauthorizedError) {
|
|
137
|
-
reply.statusCode = e.statusCode
|
|
138
|
-
reply.send({error: e.message})
|
|
139
|
-
} else {
|
|
140
|
-
reply.statusCode = 500
|
|
141
|
-
reply.send({error: 'INTERNAL_SERVER_ERROR'})
|
|
142
|
-
}
|
|
143
|
-
}
|
|
17
|
+
fastify.get('/api/roles/all', (req,rep) => controller.all(req,rep))
|
|
144
18
|
|
|
145
|
-
|
|
19
|
+
fastify.get('/api/roles', (req,rep) => controller.paginate(req,rep))
|
|
146
20
|
|
|
147
|
-
fastify.
|
|
148
|
-
try {
|
|
149
|
-
request.rbac.assertPermission(IdentityPermissions.UpdateRole)
|
|
150
|
-
const id = request.params.id
|
|
151
|
-
const payload = request.body
|
|
152
|
-
const roleService = RoleServiceFactory()
|
|
153
|
-
const currentRole = await roleService.findById(id)
|
|
154
|
-
if(currentRole.readonly){
|
|
155
|
-
throw new ValidationError([{field:'name', reason:"role.readonly", value:payload.name}])
|
|
156
|
-
}
|
|
21
|
+
fastify.post('/api/roles', (req,rep) => controller.create(req,rep))
|
|
157
22
|
|
|
158
|
-
|
|
159
|
-
return role
|
|
160
|
-
} catch (e) {
|
|
161
|
-
console.error(e)
|
|
162
|
-
if (e instanceof ValidationError) {
|
|
163
|
-
reply.statusCode = e.statusCode
|
|
164
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
165
|
-
} else if (e instanceof UnauthorizedError) {
|
|
166
|
-
reply.statusCode = e.statusCode
|
|
167
|
-
reply.send({error: e.message})
|
|
168
|
-
} else {
|
|
169
|
-
reply.statusCode = 500
|
|
170
|
-
reply.send({error: 'INTERNAL_SERVER_ERROR'})
|
|
171
|
-
}
|
|
172
|
-
}
|
|
23
|
+
fastify.put('/api/roles/:id', (req,rep) => controller.update(req,rep))
|
|
173
24
|
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
fastify.delete('/api/roles/:id', async (request, reply): Promise<any> => {
|
|
177
|
-
try {
|
|
178
|
-
request.rbac.assertPermission(IdentityPermissions.DeleteRole)
|
|
179
|
-
const id = request.params.id
|
|
180
|
-
const roleService = RoleServiceFactory()
|
|
181
|
-
const currentRole = await roleService.findById(id)
|
|
182
|
-
if(currentRole.readonly){
|
|
183
|
-
throw new UnauthorizedError()
|
|
184
|
-
}
|
|
185
|
-
let r = await roleService.delete(id)
|
|
186
|
-
return r
|
|
187
|
-
} catch (e) {
|
|
188
|
-
console.error(e)
|
|
189
|
-
if (e instanceof ValidationError) {
|
|
190
|
-
reply.statusCode = e.statusCode
|
|
191
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
192
|
-
} else if (e instanceof UnauthorizedError) {
|
|
193
|
-
reply.statusCode = e.statusCode
|
|
194
|
-
reply.send({error: e.message})
|
|
195
|
-
} else {
|
|
196
|
-
reply.statusCode = 500
|
|
197
|
-
reply.send({error: 'INTERNAL_SERVER_ERROR'})
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
})
|
|
25
|
+
fastify.delete('/api/roles/:id', (req,rep) => controller.delete(req,rep))
|
|
201
26
|
|
|
202
27
|
}
|
|
203
28
|
|
|
@@ -6,6 +6,8 @@ async function TenantRoutes(fastify, options) {
|
|
|
6
6
|
|
|
7
7
|
fastify.get('/api/tenants/export', (req,rep) => controller.export(req,rep) )
|
|
8
8
|
|
|
9
|
+
fastify.get('/api/tenants/search', (req,rep) => controller.search(req,rep) )
|
|
10
|
+
|
|
9
11
|
fastify.get('/api/tenants/:id', (req,rep) => controller.findById(req,rep) )
|
|
10
12
|
|
|
11
13
|
fastify.get('/api/tenants/name/:name', (req,rep) => controller.findByName(req,rep))
|
|
@@ -1,124 +1,17 @@
|
|
|
1
|
-
import
|
|
2
|
-
import type {IUserApiKey} from "@drax/identity-share";
|
|
3
|
-
import {ValidationError, UnauthorizedError} from "@drax/common-back";
|
|
4
|
-
import {IdentityPermissions} from "../permissions/IdentityPermissions.js";
|
|
5
|
-
import {IDraxPaginateResult} from "@drax/crud-share";
|
|
1
|
+
import UserApiKeyController from "../controllers/UserApiKeyController.js";
|
|
6
2
|
|
|
7
3
|
|
|
8
4
|
async function UserApiKeyRoutes(fastify, options) {
|
|
9
5
|
|
|
10
|
-
|
|
6
|
+
const controller: UserApiKeyController = new UserApiKeyController()
|
|
11
7
|
|
|
12
|
-
|
|
13
|
-
request.rbac.assertAuthenticated()
|
|
8
|
+
fastify.get('/api/user-api-keys', (req,rep) => controller.paginate(req,rep) )
|
|
14
9
|
|
|
15
|
-
|
|
16
|
-
IdentityPermissions.ViewUserApiKey,
|
|
17
|
-
IdentityPermissions.ViewMyUserApiKey
|
|
18
|
-
])
|
|
10
|
+
fastify.post('/api/user-api-keys', (req,rep) => controller.create(req,rep))
|
|
19
11
|
|
|
20
|
-
|
|
12
|
+
fastify.put('/api/user-api-keys/:id', (req,rep) => controller.update(req,rep))
|
|
21
13
|
|
|
22
|
-
|
|
23
|
-
filters.push({field: "user", operator: "eq", value: request.rbac.authUser.id})
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const page = request.query.page
|
|
27
|
-
const limit = request.query.limit
|
|
28
|
-
const orderBy = request.query.orderBy
|
|
29
|
-
const order = request.query.order
|
|
30
|
-
const search = request.query.search
|
|
31
|
-
const userApiKeyService = UserApiKeyServiceFactory()
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
let paginateResult = await userApiKeyService.paginate({page, limit, orderBy, order, search, filters})
|
|
35
|
-
return paginateResult
|
|
36
|
-
} catch (e) {
|
|
37
|
-
console.log("/api/user-api-keys",e)
|
|
38
|
-
if (e instanceof ValidationError) {
|
|
39
|
-
reply.statusCode = e.statusCode
|
|
40
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
41
|
-
} else if (e instanceof UnauthorizedError) {
|
|
42
|
-
reply.statusCode = e.statusCode
|
|
43
|
-
reply.send({error: e.message})
|
|
44
|
-
} else {
|
|
45
|
-
reply.statusCode = 500
|
|
46
|
-
reply.send({error: 'error.server'})
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
fastify.post('/api/user-api-keys', async (request, reply): Promise<IUserApiKey> => {
|
|
52
|
-
try {
|
|
53
|
-
request.rbac.assertPermission(IdentityPermissions.CreateUserApiKey)
|
|
54
|
-
const payload = request.body
|
|
55
|
-
payload.user = request.rbac.authUser.id
|
|
56
|
-
|
|
57
|
-
const userApiKeyService = UserApiKeyServiceFactory()
|
|
58
|
-
|
|
59
|
-
let userApiKey = await userApiKeyService.create(payload)
|
|
60
|
-
return userApiKey
|
|
61
|
-
} catch (e) {
|
|
62
|
-
if (e instanceof ValidationError) {
|
|
63
|
-
reply.statusCode = e.statusCode
|
|
64
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
65
|
-
} else if (e instanceof UnauthorizedError) {
|
|
66
|
-
reply.statusCode = e.statusCode
|
|
67
|
-
reply.send({error: e.message})
|
|
68
|
-
} else {
|
|
69
|
-
reply.statusCode = 500
|
|
70
|
-
reply.send({error: 'error.server'})
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
})
|
|
75
|
-
|
|
76
|
-
fastify.put('/api/user-api-keys/:id', async (request, reply): Promise<IUserApiKey> => {
|
|
77
|
-
try {
|
|
78
|
-
request.rbac.assertPermission(IdentityPermissions.UpdateUserApiKey)
|
|
79
|
-
const id = request.params.id
|
|
80
|
-
const payload = request.body
|
|
81
|
-
const userApiKeyService = UserApiKeyServiceFactory()
|
|
82
|
-
let userApiKey = await userApiKeyService.update(id, payload)
|
|
83
|
-
return userApiKey
|
|
84
|
-
} catch (e) {
|
|
85
|
-
if (e instanceof ValidationError) {
|
|
86
|
-
reply.statusCode = e.statusCode
|
|
87
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
88
|
-
}
|
|
89
|
-
if (e instanceof UnauthorizedError) {
|
|
90
|
-
reply.statusCode = e.statusCode
|
|
91
|
-
reply.send({error: e.message})
|
|
92
|
-
} else if (e instanceof UnauthorizedError) {
|
|
93
|
-
reply.statusCode = e.statusCode
|
|
94
|
-
reply.send({error: e.message})
|
|
95
|
-
} else {
|
|
96
|
-
reply.statusCode = 500
|
|
97
|
-
reply.send({error: 'error.server'})
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
})
|
|
101
|
-
|
|
102
|
-
fastify.delete('/api/user-api-keys/:id', async (request, reply): Promise<any> => {
|
|
103
|
-
try {
|
|
104
|
-
request.rbac.assertPermission(IdentityPermissions.DeleteUserApiKey)
|
|
105
|
-
const id = request.params.id
|
|
106
|
-
const userApiKeyService = UserApiKeyServiceFactory()
|
|
107
|
-
let r = await userApiKeyService.delete(id)
|
|
108
|
-
return r
|
|
109
|
-
} catch (e) {
|
|
110
|
-
if (e instanceof ValidationError) {
|
|
111
|
-
reply.statusCode = e.statusCode
|
|
112
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
113
|
-
} else if (e instanceof UnauthorizedError) {
|
|
114
|
-
reply.statusCode = e.statusCode
|
|
115
|
-
reply.send({error: e.message})
|
|
116
|
-
} else {
|
|
117
|
-
reply.statusCode = 500
|
|
118
|
-
reply.send({error: 'error.server'})
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
})
|
|
14
|
+
fastify.delete('/api/user-api-keys/:id', (req,rep) => controller.delete(req,rep))
|
|
122
15
|
|
|
123
16
|
|
|
124
17
|
}
|
package/src/routes/UserRoutes.ts
CHANGED
|
@@ -1,218 +1,28 @@
|
|
|
1
|
-
import
|
|
2
|
-
import {IUser} from "@drax/identity-share";
|
|
3
|
-
import {ValidationError, UnauthorizedError} from "@drax/common-back";
|
|
4
|
-
import {IdentityPermissions} from "../permissions/IdentityPermissions.js";
|
|
5
|
-
import BadCredentialsError from "../errors/BadCredentialsError.js";
|
|
6
|
-
import {IDraxPaginateResult} from "@drax/crud-share";
|
|
1
|
+
import UserController from "../controllers/UserController.js";
|
|
7
2
|
|
|
8
3
|
async function UserRoutes(fastify, options) {
|
|
9
|
-
fastify.post('/api/auth', async (request, reply) => {
|
|
10
|
-
try {
|
|
11
|
-
const username = request.body.username
|
|
12
|
-
const password = request.body.password
|
|
13
|
-
const userService = UserServiceFactory()
|
|
14
|
-
return await userService.auth(username, password)
|
|
15
|
-
} catch (e) {
|
|
16
|
-
console.error('/api/auth error', e)
|
|
17
|
-
if (e instanceof BadCredentialsError) {
|
|
18
|
-
reply.code(401)
|
|
19
|
-
reply.send({error: e.message})
|
|
20
|
-
}
|
|
21
|
-
reply.code(500)
|
|
22
|
-
reply.send({error: 'error.server'})
|
|
23
|
-
}
|
|
24
|
-
})
|
|
25
4
|
|
|
5
|
+
const controller: UserController = new UserController()
|
|
26
6
|
|
|
7
|
+
fastify.post('/api/auth', (req,rep) => controller.auth(req,rep))
|
|
27
8
|
|
|
9
|
+
fastify.get('/api/me', (req,rep) => controller.me(req,rep))
|
|
28
10
|
|
|
29
|
-
fastify.get('/api/
|
|
30
|
-
try {
|
|
31
|
-
if (request.authUser) {
|
|
32
|
-
const userService = UserServiceFactory()
|
|
33
|
-
let user = await userService.findById(request.authUser.id)
|
|
34
|
-
user.password = undefined
|
|
35
|
-
delete user.password
|
|
36
|
-
return user
|
|
37
|
-
} else {
|
|
38
|
-
throw new UnauthorizedError()
|
|
11
|
+
fastify.get('/api/users', (req,rep) => controller.paginate(req,rep))
|
|
39
12
|
|
|
40
|
-
|
|
41
|
-
} catch (e) {
|
|
42
|
-
if (e instanceof UnauthorizedError) {
|
|
43
|
-
reply.code(401)
|
|
44
|
-
reply.send({error: "Unauthorized"})
|
|
45
|
-
} else if (e instanceof UnauthorizedError) {
|
|
46
|
-
reply.statusCode = e.statusCode
|
|
47
|
-
reply.send({error: e.message})
|
|
48
|
-
} else {
|
|
49
|
-
reply.statusCode = 500
|
|
50
|
-
reply.send({error: 'error.server'})
|
|
51
|
-
}
|
|
52
|
-
}
|
|
13
|
+
fastify.post('/api/users', (req,rep) => controller.create(req,rep))
|
|
53
14
|
|
|
15
|
+
fastify.put('/api/users/:id', (req,rep) => controller.update(req,rep))
|
|
54
16
|
|
|
55
|
-
|
|
17
|
+
fastify.delete('/api/users/:id', (req,rep) => controller.delete(req,rep))
|
|
56
18
|
|
|
57
|
-
fastify.
|
|
19
|
+
fastify.post('/api/password', (req,rep) => controller.myPassword(req,rep))
|
|
58
20
|
|
|
59
|
-
|
|
60
|
-
request.rbac.assertPermission(IdentityPermissions.ViewUser)
|
|
61
|
-
const page = request.query.page
|
|
62
|
-
const limit = request.query.limit
|
|
63
|
-
const orderBy = request.query.orderBy
|
|
64
|
-
const order = request.query.order
|
|
65
|
-
const search = request.query.search
|
|
66
|
-
const userService = UserServiceFactory()
|
|
67
|
-
const filters = []
|
|
68
|
-
if(request.rbac.getAuthUser.tenantId){
|
|
69
|
-
filters.push({field: 'tenant', operator: 'eq', value: request.rbac.getAuthUser.tenantId})
|
|
70
|
-
}
|
|
71
|
-
let paginateResult = await userService.paginate({page, limit, orderBy, order, search, filters})
|
|
72
|
-
for(let item of paginateResult.items){
|
|
73
|
-
item.password = undefined
|
|
74
|
-
delete item.password
|
|
75
|
-
}
|
|
76
|
-
return paginateResult
|
|
77
|
-
} catch (e) {
|
|
78
|
-
if (e instanceof ValidationError) {
|
|
79
|
-
reply.statusCode = e.statusCode
|
|
80
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
81
|
-
} else if (e instanceof UnauthorizedError) {
|
|
82
|
-
reply.statusCode = e.statusCode
|
|
83
|
-
reply.send({error: e.message})
|
|
84
|
-
} else {
|
|
85
|
-
reply.statusCode = 500
|
|
86
|
-
reply.send({error: 'error.server'})
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
})
|
|
21
|
+
fastify.post('/api/password/:id', (req,rep) => controller.password(req,rep))
|
|
90
22
|
|
|
91
|
-
fastify.post('/api/
|
|
92
|
-
try {
|
|
93
|
-
request.rbac.assertPermission(IdentityPermissions.CreateUser)
|
|
94
|
-
const payload = request.body
|
|
95
|
-
const userService = UserServiceFactory()
|
|
96
|
-
if(request.rbac.getAuthUser.tenantId){
|
|
97
|
-
payload.tenant = request.rbac.getAuthUser.tenantId
|
|
98
|
-
}
|
|
99
|
-
let user = await userService.create(payload)
|
|
100
|
-
return user
|
|
101
|
-
} catch (e) {
|
|
102
|
-
if (e instanceof ValidationError) {
|
|
103
|
-
reply.statusCode = e.statusCode
|
|
104
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
105
|
-
} else if (e instanceof UnauthorizedError) {
|
|
106
|
-
reply.statusCode = e.statusCode
|
|
107
|
-
reply.send({error: e.message})
|
|
108
|
-
} else {
|
|
109
|
-
reply.statusCode = 500
|
|
110
|
-
reply.send({error: 'error.server'})
|
|
111
|
-
}
|
|
112
|
-
}
|
|
23
|
+
fastify.post('/api/user/avatar', (req,rep) => controller.updateAvatar(req,rep))
|
|
113
24
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
fastify.put('/api/users/:id', async (request, reply): Promise<IUser> => {
|
|
117
|
-
try {
|
|
118
|
-
request.rbac.assertPermission(IdentityPermissions.UpdateUser)
|
|
119
|
-
const id = request.params.id
|
|
120
|
-
const payload = request.body
|
|
121
|
-
const userService = UserServiceFactory()
|
|
122
|
-
if(request.rbac.getAuthUser.tenantId){
|
|
123
|
-
payload.tenant = request.rbac.getAuthUser.tenantId
|
|
124
|
-
}
|
|
125
|
-
let user = await userService.update(id, payload)
|
|
126
|
-
return user
|
|
127
|
-
} catch (e) {
|
|
128
|
-
if (e instanceof ValidationError) {
|
|
129
|
-
reply.statusCode = e.statusCode
|
|
130
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
131
|
-
}
|
|
132
|
-
if (e instanceof UnauthorizedError) {
|
|
133
|
-
reply.statusCode = e.statusCode
|
|
134
|
-
reply.send({error: e.message})
|
|
135
|
-
} else if (e instanceof UnauthorizedError) {
|
|
136
|
-
reply.statusCode = e.statusCode
|
|
137
|
-
reply.send({error: e.message})
|
|
138
|
-
} else {
|
|
139
|
-
reply.statusCode = 500
|
|
140
|
-
reply.send({error: 'error.server'})
|
|
141
|
-
}
|
|
142
|
-
}
|
|
143
|
-
})
|
|
144
|
-
|
|
145
|
-
fastify.delete('/api/users/:id', async (request, reply): Promise<any> => {
|
|
146
|
-
try {
|
|
147
|
-
request.rbac.assertPermission(IdentityPermissions.DeleteUser)
|
|
148
|
-
const id = request.params.id
|
|
149
|
-
const userService = UserServiceFactory()
|
|
150
|
-
let r = await userService.delete(id)
|
|
151
|
-
return r
|
|
152
|
-
} catch (e) {
|
|
153
|
-
if (e instanceof ValidationError) {
|
|
154
|
-
reply.statusCode = e.statusCode
|
|
155
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
156
|
-
} else if (e instanceof UnauthorizedError) {
|
|
157
|
-
reply.statusCode = e.statusCode
|
|
158
|
-
reply.send({error: e.message})
|
|
159
|
-
} else {
|
|
160
|
-
reply.statusCode = 500
|
|
161
|
-
reply.send({error: 'error.server'})
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
})
|
|
165
|
-
|
|
166
|
-
fastify.post('/api/password', async (request, reply) => {
|
|
167
|
-
try {
|
|
168
|
-
if(!request.authUser){
|
|
169
|
-
throw new UnauthorizedError()
|
|
170
|
-
}
|
|
171
|
-
const userId = request.authUser.id
|
|
172
|
-
const currentPassword = request.body.currentPassword
|
|
173
|
-
const newPassword = request.body.newPassword
|
|
174
|
-
const userService = UserServiceFactory()
|
|
175
|
-
return await userService.changeOwnPassword(userId, currentPassword, newPassword)
|
|
176
|
-
} catch (e) {
|
|
177
|
-
console.error('/api/password error', e)
|
|
178
|
-
if (e instanceof ValidationError) {
|
|
179
|
-
reply.statusCode = e.statusCode
|
|
180
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
181
|
-
} else if (e instanceof UnauthorizedError) {
|
|
182
|
-
reply.statusCode = e.statusCode
|
|
183
|
-
reply.send({error: e.message})
|
|
184
|
-
} else {
|
|
185
|
-
reply.statusCode = 500
|
|
186
|
-
reply.send({error: 'error.server'})
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
})
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
fastify.post('/api/password/:id', async (request, reply) => {
|
|
193
|
-
try {
|
|
194
|
-
request.rbac.assertPermission(IdentityPermissions.UpdateUser)
|
|
195
|
-
const userId = request.params.id
|
|
196
|
-
if(!userId){
|
|
197
|
-
throw new UnauthorizedError()
|
|
198
|
-
}
|
|
199
|
-
const newPassword = request.body.newPassword
|
|
200
|
-
const userService = UserServiceFactory()
|
|
201
|
-
return await userService.changeUserPassword(userId, newPassword)
|
|
202
|
-
} catch (e) {
|
|
203
|
-
console.error('/api/password error', e)
|
|
204
|
-
if (e instanceof ValidationError) {
|
|
205
|
-
reply.statusCode = e.statusCode
|
|
206
|
-
reply.send({error: e.message, inputErrors: e.errors})
|
|
207
|
-
} else if (e instanceof UnauthorizedError) {
|
|
208
|
-
reply.statusCode = e.statusCode
|
|
209
|
-
reply.send({error: e.message})
|
|
210
|
-
} else {
|
|
211
|
-
reply.statusCode = 500
|
|
212
|
-
reply.send({error: 'error.server'})
|
|
213
|
-
}
|
|
214
|
-
}
|
|
215
|
-
})
|
|
25
|
+
fastify.get('/api/user/avatar/:filename', (req,rep) => controller.getAvatar(req,rep))
|
|
216
26
|
|
|
217
27
|
}
|
|
218
28
|
|