@drax/identity-back 0.5.3 → 0.5.5
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 +35 -3
- package/dist/repository/mongo/TenantMongoRepository.js +25 -2
- package/dist/repository/mongo/UserMongoRepository.js +23 -0
- package/dist/repository/sqlite/TenantSqliteRepository.js +1 -1
- package/dist/routes/RoleRoutes.js +12 -207
- package/dist/routes/TenantRoutes.js +1 -0
- package/dist/routes/UserApiKeyRoutes.js +6 -114
- package/dist/routes/UserRoutes.js +13 -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/interfaces/IRoleRepository.ts +2 -2
- package/src/repository/mongo/RoleMongoRepository.ts +75 -19
- package/src/repository/mongo/TenantMongoRepository.ts +36 -3
- package/src/repository/mongo/UserMongoRepository.ts +49 -2
- package/src/repository/sqlite/TenantSqliteRepository.ts +0 -1
- package/src/routes/RoleRoutes.ts +22 -195
- package/src/routes/TenantRoutes.ts +2 -0
- package/src/routes/UserApiKeyRoutes.ts +6 -113
- package/src/routes/UserRoutes.ts +13 -201
- 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/interfaces/IRoleRepository.d.ts +2 -2
- package/types/interfaces/IRoleRepository.d.ts.map +1 -1
- package/types/repository/mongo/RoleMongoRepository.d.ts +33 -1
- package/types/repository/mongo/RoleMongoRepository.d.ts.map +1 -1
- package/types/repository/mongo/TenantMongoRepository.d.ts +32 -1
- package/types/repository/mongo/TenantMongoRepository.d.ts.map +1 -1
- package/types/repository/mongo/UserMongoRepository.d.ts +31 -1
- package/types/repository/mongo/UserMongoRepository.d.ts.map +1 -1
- package/types/repository/sqlite/TenantSqliteRepository.d.ts +1 -1
- package/types/repository/sqlite/TenantSqliteRepository.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,58 +1,68 @@
|
|
|
1
1
|
import {RoleModel} from "../../models/RoleModel.js";
|
|
2
2
|
import {IRoleRepository} from '../../interfaces/IRoleRepository'
|
|
3
3
|
import {mongoose, MongooseQueryFilter, MongooseSort} from "@drax/common-back";
|
|
4
|
-
import {PaginateOptions, PaginateResult} from "mongoose";
|
|
4
|
+
import {Cursor, PaginateOptions, PaginateResult} from "mongoose";
|
|
5
5
|
import {DeleteResult} from "mongodb";
|
|
6
|
-
import {IDraxPaginateOptions, IDraxPaginateResult} from "@drax/crud-share";
|
|
6
|
+
import {IDraxFindOptions, IDraxPaginateOptions, IDraxPaginateResult} from "@drax/crud-share";
|
|
7
7
|
import {IRoleBase, IRole} from "@drax/identity-share";
|
|
8
8
|
|
|
9
|
-
class RoleMongoRepository implements IRoleRepository{
|
|
9
|
+
class RoleMongoRepository implements IRoleRepository {
|
|
10
|
+
|
|
11
|
+
_searchFields = ['name']
|
|
10
12
|
|
|
11
13
|
async create(roleData: IRoleBase): Promise<IRole> {
|
|
12
|
-
const role
|
|
14
|
+
const role: mongoose.HydratedDocument<IRole> = new RoleModel(roleData)
|
|
13
15
|
await role.save()
|
|
14
16
|
await role.populate('childRoles')
|
|
15
17
|
return role
|
|
16
18
|
}
|
|
17
19
|
|
|
18
20
|
async update(id: string, roleData: IRoleBase): Promise<IRole> {
|
|
19
|
-
const role
|
|
21
|
+
const role: mongoose.HydratedDocument<IRole> = await RoleModel.findOneAndUpdate({_id: id}, roleData, {new: true}).populate('childRoles').exec()
|
|
20
22
|
return role
|
|
21
23
|
}
|
|
22
24
|
|
|
23
25
|
async delete(id: string): Promise<boolean> {
|
|
24
|
-
const result
|
|
26
|
+
const result: DeleteResult = await RoleModel.deleteOne({_id: id}).exec()
|
|
25
27
|
return result.deletedCount == 1
|
|
26
28
|
}
|
|
27
29
|
|
|
28
|
-
async findById(id: string): Promise<IRole | null>{
|
|
30
|
+
async findById(id: string): Promise<IRole | null> {
|
|
29
31
|
const role: mongoose.HydratedDocument<IRole> | null = await RoleModel.findById(id).populate('childRoles').exec()
|
|
30
32
|
return role
|
|
31
33
|
}
|
|
32
34
|
|
|
33
|
-
async findByName(name: string): Promise<IRole | null>{
|
|
35
|
+
async findByName(name: string): Promise<IRole | null> {
|
|
34
36
|
const role: mongoose.HydratedDocument<IRole> | null = await RoleModel.findOne({name}).populate('childRoles').exec()
|
|
35
37
|
return role
|
|
36
38
|
}
|
|
37
39
|
|
|
38
|
-
async fetchAll(): Promise<IRole[]>{
|
|
40
|
+
async fetchAll(): Promise<IRole[]> {
|
|
39
41
|
const roles: mongoose.HydratedDocument<IRole>[] = await RoleModel.find().populate('childRoles').exec()
|
|
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
|
-
page= 1,
|
|
45
|
-
limit= 5,
|
|
46
|
-
orderBy= '',
|
|
47
|
-
order= false,
|
|
48
|
-
search= '',
|
|
49
|
-
filters= []
|
|
55
|
+
page = 1,
|
|
56
|
+
limit = 5,
|
|
57
|
+
orderBy = '',
|
|
58
|
+
order = false,
|
|
59
|
+
search = '',
|
|
60
|
+
filters = []
|
|
61
|
+
}: IDraxPaginateOptions): Promise<IDraxPaginateResult<IRole>> {
|
|
50
62
|
const query = {}
|
|
51
63
|
|
|
52
|
-
if(search){
|
|
53
|
-
query['$or'] = [
|
|
54
|
-
{name: new RegExp(search, 'i')},
|
|
55
|
-
]
|
|
64
|
+
if (search) {
|
|
65
|
+
query['$or'] = this._searchFields.map(field => ({[field]: new RegExp(search.toString(), 'i')}))
|
|
56
66
|
}
|
|
57
67
|
|
|
58
68
|
MongooseQueryFilter.applyFilters(query, filters)
|
|
@@ -68,6 +78,52 @@ class RoleMongoRepository implements IRoleRepository{
|
|
|
68
78
|
items: roles.docs
|
|
69
79
|
}
|
|
70
80
|
}
|
|
81
|
+
|
|
82
|
+
async find({
|
|
83
|
+
limit = 0,
|
|
84
|
+
orderBy = '',
|
|
85
|
+
order = false,
|
|
86
|
+
search = '',
|
|
87
|
+
filters = []
|
|
88
|
+
}: IDraxFindOptions): Promise<IRole[]> {
|
|
89
|
+
|
|
90
|
+
const query = {}
|
|
91
|
+
|
|
92
|
+
if (search) {
|
|
93
|
+
query['$or'] = [
|
|
94
|
+
{name: new RegExp(search, 'i')},
|
|
95
|
+
]
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
MongooseQueryFilter.applyFilters(query, filters)
|
|
99
|
+
|
|
100
|
+
const sort = MongooseSort.applySort(orderBy, order)
|
|
101
|
+
|
|
102
|
+
return await RoleModel.find(query).limit(limit).sort(sort)
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async findCursor({
|
|
106
|
+
limit = 0,
|
|
107
|
+
orderBy = '',
|
|
108
|
+
order = false,
|
|
109
|
+
search = '',
|
|
110
|
+
filters = []
|
|
111
|
+
}: IDraxFindOptions): Promise<Cursor> {
|
|
112
|
+
console.log("RoleMongoRepository.findCursor called")
|
|
113
|
+
const query = {}
|
|
114
|
+
|
|
115
|
+
if (search) {
|
|
116
|
+
query['$or'] = [
|
|
117
|
+
{name: new RegExp(search, 'i')},
|
|
118
|
+
]
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
MongooseQueryFilter.applyFilters(query, filters)
|
|
122
|
+
|
|
123
|
+
const sort = MongooseSort.applySort(orderBy, order)
|
|
124
|
+
|
|
125
|
+
return RoleModel.find(query).limit(limit).sort(sort).cursor() as Cursor;
|
|
126
|
+
}
|
|
71
127
|
}
|
|
72
128
|
|
|
73
129
|
export default RoleMongoRepository
|
|
@@ -1,13 +1,15 @@
|
|
|
1
1
|
import {TenantModel} from "../../models/TenantModel.js";
|
|
2
2
|
import {ITenantRepository} from '../../interfaces/ITenantRepository'
|
|
3
3
|
import {mongoose, MongooseSort, MongooseQueryFilter} from "@drax/common-back";
|
|
4
|
-
import {PaginateOptions, PaginateResult} from "mongoose";
|
|
4
|
+
import {Cursor, PaginateOptions, PaginateResult} from "mongoose";
|
|
5
5
|
import {DeleteResult} from "mongodb";
|
|
6
6
|
import {IDraxFindOptions, IDraxPaginateOptions, IDraxPaginateResult} from "@drax/crud-share";
|
|
7
7
|
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,
|
|
@@ -71,7 +82,6 @@ class TenantMongoRepository implements ITenantRepository {
|
|
|
71
82
|
}
|
|
72
83
|
|
|
73
84
|
async find({
|
|
74
|
-
cursor = false,
|
|
75
85
|
limit = 0,
|
|
76
86
|
orderBy = '',
|
|
77
87
|
order = false,
|
|
@@ -90,9 +100,32 @@ class TenantMongoRepository implements ITenantRepository {
|
|
|
90
100
|
MongooseQueryFilter.applyFilters(query, filters)
|
|
91
101
|
|
|
92
102
|
const sort = MongooseSort.applySort(orderBy, order)
|
|
93
|
-
const items: ITenant[] = await TenantModel.find(query).sort(sort)
|
|
103
|
+
const items: ITenant[] = await TenantModel.find(query).limit(limit).sort(sort)
|
|
94
104
|
return items
|
|
95
105
|
}
|
|
106
|
+
|
|
107
|
+
async findCursor({
|
|
108
|
+
limit = 0,
|
|
109
|
+
orderBy = '',
|
|
110
|
+
order = false,
|
|
111
|
+
search = '',
|
|
112
|
+
filters = []
|
|
113
|
+
}: IDraxFindOptions): Promise<Cursor> {
|
|
114
|
+
console.log("TenantMongoRepository.findCursor called")
|
|
115
|
+
const query = {}
|
|
116
|
+
|
|
117
|
+
if (search) {
|
|
118
|
+
query['$or'] = [
|
|
119
|
+
{name: new RegExp(search, 'i')},
|
|
120
|
+
]
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
MongooseQueryFilter.applyFilters(query, filters)
|
|
124
|
+
|
|
125
|
+
const sort = MongooseSort.applySort(orderBy, order)
|
|
126
|
+
|
|
127
|
+
return TenantModel.find(query).limit(limit).sort(sort).cursor() as Cursor;
|
|
128
|
+
}
|
|
96
129
|
}
|
|
97
130
|
|
|
98
131
|
export default TenantMongoRepository
|
|
@@ -9,9 +9,10 @@ import {
|
|
|
9
9
|
import type {IUser, IUserCreate, IUserUpdate} from "@drax/identity-share";
|
|
10
10
|
import {DeleteResult, MongoServerError} from "mongodb";
|
|
11
11
|
import type {IUserRepository} from "../../interfaces/IUserRepository";
|
|
12
|
-
import {PaginateResult} from "mongoose";
|
|
12
|
+
import {Cursor, PaginateResult} from "mongoose";
|
|
13
13
|
import RoleMongoRepository from "./RoleMongoRepository.js";
|
|
14
|
-
import {IDraxPaginateOptions, IDraxPaginateResult} from "@drax/crud-share";
|
|
14
|
+
import {IDraxFindOptions, IDraxPaginateOptions, IDraxPaginateResult} from "@drax/crud-share";
|
|
15
|
+
import {IRole} from "@drax/identity-share";
|
|
15
16
|
|
|
16
17
|
class UserMongoRepository implements IUserRepository {
|
|
17
18
|
private roleRepository: RoleMongoRepository;
|
|
@@ -124,6 +125,52 @@ class UserMongoRepository implements IUserRepository {
|
|
|
124
125
|
return false
|
|
125
126
|
}
|
|
126
127
|
}
|
|
128
|
+
|
|
129
|
+
async find({
|
|
130
|
+
limit = 0,
|
|
131
|
+
orderBy = '',
|
|
132
|
+
order = false,
|
|
133
|
+
search = '',
|
|
134
|
+
filters = []
|
|
135
|
+
}: IDraxFindOptions): Promise<IUser[]> {
|
|
136
|
+
|
|
137
|
+
const query = {}
|
|
138
|
+
|
|
139
|
+
if (search) {
|
|
140
|
+
query['$or'] = [
|
|
141
|
+
{name: new RegExp(search, 'i')},
|
|
142
|
+
]
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
MongooseQueryFilter.applyFilters(query, filters)
|
|
146
|
+
|
|
147
|
+
const sort = MongooseSort.applySort(orderBy, order)
|
|
148
|
+
|
|
149
|
+
return await UserModel.find(query).populate(['role','tenant']).limit(limit).sort(sort)
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async findCursor({
|
|
153
|
+
limit = 0,
|
|
154
|
+
orderBy = '',
|
|
155
|
+
order = false,
|
|
156
|
+
search = '',
|
|
157
|
+
filters = []
|
|
158
|
+
}: IDraxFindOptions): Promise<Cursor> {
|
|
159
|
+
console.log("RoleMongoRepository.findCursor called")
|
|
160
|
+
const query = {}
|
|
161
|
+
|
|
162
|
+
if (search) {
|
|
163
|
+
query['$or'] = [
|
|
164
|
+
{name: new RegExp(search, 'i')},
|
|
165
|
+
]
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
MongooseQueryFilter.applyFilters(query, filters)
|
|
169
|
+
|
|
170
|
+
const sort = MongooseSort.applySort(orderBy, order)
|
|
171
|
+
|
|
172
|
+
return UserModel.find(query).populate(['role','tenant']).limit(limit).sort(sort).cursor() as Cursor;
|
|
173
|
+
}
|
|
127
174
|
}
|
|
128
175
|
|
|
129
176
|
export default UserMongoRepository
|
package/src/routes/RoleRoutes.ts
CHANGED
|
@@ -1,203 +1,30 @@
|
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
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
|
-
})
|
|
50
|
-
|
|
51
|
-
fastify.get('/api/roles/name/:name', async (request, reply): Promise<IRole> => {
|
|
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
|
-
})
|
|
72
|
-
|
|
73
|
-
fastify.get('/api/roles/all', async (request, reply): Promise<IRole[]> => {
|
|
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
|
-
})
|
|
97
|
-
|
|
98
|
-
fastify.get('/api/roles', async (request, reply): Promise<IDraxPaginateResult<IRole>> => {
|
|
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
|
-
})
|
|
123
|
-
|
|
124
|
-
fastify.post('/api/roles', async (request, reply): Promise<IRole> => {
|
|
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
|
-
}
|
|
144
|
-
|
|
145
|
-
})
|
|
146
|
-
|
|
147
|
-
fastify.put('/api/roles/:id', async (request, reply): Promise<IRole> => {
|
|
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
|
-
}
|
|
157
|
-
|
|
158
|
-
let role = await roleService.update(id, payload)
|
|
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
|
-
}
|
|
173
|
-
|
|
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
|
-
})
|
|
7
|
+
const controller: RoleController = new RoleController()
|
|
8
|
+
|
|
9
|
+
fastify.get('/api/permissions', (req,rep) => controller.permissions(req,rep))
|
|
10
|
+
|
|
11
|
+
fastify.get('/api/roles/export', (req,rep) => controller.export(req,rep) )
|
|
12
|
+
|
|
13
|
+
fastify.get('/api/roles/search', (req,rep) => controller.search(req,rep))
|
|
14
|
+
|
|
15
|
+
fastify.get('/api/roles/:id', (req,rep) => controller.findById(req,rep))
|
|
16
|
+
|
|
17
|
+
fastify.get('/api/roles/name/:name', (req,rep) => controller.findByName(req,rep))
|
|
18
|
+
|
|
19
|
+
fastify.get('/api/roles/all', (req,rep) => controller.all(req,rep))
|
|
20
|
+
|
|
21
|
+
fastify.get('/api/roles', (req,rep) => controller.paginate(req,rep))
|
|
22
|
+
|
|
23
|
+
fastify.post('/api/roles', (req,rep) => controller.create(req,rep))
|
|
24
|
+
|
|
25
|
+
fastify.put('/api/roles/:id', (req,rep) => controller.update(req,rep))
|
|
26
|
+
|
|
27
|
+
fastify.delete('/api/roles/:id', (req,rep) => controller.delete(req,rep))
|
|
201
28
|
|
|
202
29
|
}
|
|
203
30
|
|
|
@@ -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
|
}
|