@drax/identity-back 0.5.1 → 0.5.3

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.
Files changed (78) hide show
  1. package/dist/controllers/TenantController.js +66 -0
  2. package/dist/graphql/resolvers/role.resolvers.js +1 -2
  3. package/dist/graphql/resolvers/tenant.resolvers.js +1 -2
  4. package/dist/graphql/resolvers/user-api-key.resolvers.js +1 -2
  5. package/dist/graphql/resolvers/user.resolvers.js +1 -1
  6. package/dist/index.js +7 -4
  7. package/dist/permissions/RolePermissions.js +11 -0
  8. package/dist/permissions/TenantPermissions.js +10 -0
  9. package/dist/permissions/UserApiKeyPermissions.js +11 -0
  10. package/dist/permissions/UserPermissions.js +10 -0
  11. package/dist/permissions/index.js +12 -0
  12. package/dist/rbac/Rbac.js +1 -1
  13. package/dist/repository/mongo/TenantMongoRepository.js +12 -0
  14. package/dist/repository/sqlite/TenantSqliteRepository.js +11 -0
  15. package/dist/routes/RoleRoutes.js +1 -2
  16. package/dist/routes/TenantRoutes.js +10 -181
  17. package/dist/routes/UserApiKeyRoutes.js +1 -2
  18. package/dist/routes/UserAvatarRoutes.js +1 -2
  19. package/dist/routes/UserRoutes.js +1 -2
  20. package/dist/services/TenantService.js +3 -1
  21. package/package.json +6 -5
  22. package/src/controllers/TenantController.ts +73 -0
  23. package/src/graphql/resolvers/role.resolvers.ts +1 -2
  24. package/src/graphql/resolvers/tenant.resolvers.ts +1 -2
  25. package/src/graphql/resolvers/user-api-key.resolvers.ts +1 -2
  26. package/src/graphql/resolvers/user.resolvers.ts +1 -1
  27. package/src/index.ts +12 -3
  28. package/src/permissions/IdentityPermissions.ts +1 -0
  29. package/src/permissions/RolePermissions.ts +12 -0
  30. package/src/permissions/TenantPermissions.ts +11 -0
  31. package/src/permissions/UserApiKeyPermissions.ts +12 -0
  32. package/src/permissions/UserPermissions.ts +11 -0
  33. package/src/permissions/index.ts +15 -0
  34. package/src/rbac/Rbac.ts +1 -1
  35. package/src/repository/mongo/TenantMongoRepository.ts +41 -16
  36. package/src/repository/sqlite/TenantSqliteRepository.ts +24 -1
  37. package/src/routes/RoleRoutes.ts +1 -2
  38. package/src/routes/TenantRoutes.ts +10 -164
  39. package/src/routes/UserApiKeyRoutes.ts +1 -2
  40. package/src/routes/UserAvatarRoutes.ts +1 -2
  41. package/src/routes/UserRoutes.ts +1 -2
  42. package/src/services/TenantService.ts +3 -1
  43. package/tsconfig.json +2 -2
  44. package/tsconfig.tsbuildinfo +1 -1
  45. package/types/controllers/TenantController.d.ts +12 -0
  46. package/types/controllers/TenantController.d.ts.map +1 -0
  47. package/types/graphql/resolvers/role.resolvers.d.ts.map +1 -1
  48. package/types/graphql/resolvers/tenant.resolvers.d.ts.map +1 -1
  49. package/types/graphql/resolvers/user-api-key.resolvers.d.ts.map +1 -1
  50. package/types/index.d.ts +6 -3
  51. package/types/index.d.ts.map +1 -1
  52. package/types/permissions/IdentityPermissions.d.ts.map +1 -1
  53. package/types/permissions/RolePermissions.d.ts +11 -0
  54. package/types/permissions/RolePermissions.d.ts.map +1 -0
  55. package/types/permissions/TenantPermissions.d.ts +10 -0
  56. package/types/permissions/TenantPermissions.d.ts.map +1 -0
  57. package/types/permissions/UserApiKeyPermissions.d.ts +11 -0
  58. package/types/permissions/UserApiKeyPermissions.d.ts.map +1 -0
  59. package/types/permissions/UserPermissions.d.ts +10 -0
  60. package/types/permissions/UserPermissions.d.ts.map +1 -0
  61. package/types/permissions/index.d.ts +14 -0
  62. package/types/permissions/index.d.ts.map +1 -0
  63. package/types/repository/mongo/TenantMongoRepository.d.ts +2 -1
  64. package/types/repository/mongo/TenantMongoRepository.d.ts.map +1 -1
  65. package/types/repository/sqlite/TenantSqliteRepository.d.ts +2 -1
  66. package/types/repository/sqlite/TenantSqliteRepository.d.ts.map +1 -1
  67. package/types/routes/RoleRoutes.d.ts.map +1 -1
  68. package/types/routes/TenantRoutes.d.ts.map +1 -1
  69. package/types/routes/UserApiKeyRoutes.d.ts.map +1 -1
  70. package/types/routes/UserAvatarRoutes.d.ts.map +1 -1
  71. package/types/routes/UserRoutes.d.ts.map +1 -1
  72. package/types/services/TenantService.d.ts +2 -1
  73. package/types/services/TenantService.d.ts.map +1 -1
  74. package/types/zod/UserZod.d.ts +6 -6
  75. package/dist/errors/UnauthorizedError.js +0 -10
  76. package/src/errors/UnauthorizedError.ts +0 -13
  77. package/types/errors/UnauthorizedError.d.ts +0 -6
  78. package/types/errors/UnauthorizedError.d.ts.map +0 -1
@@ -0,0 +1,66 @@
1
+ import { AbstractFastifyController } from "@drax/crud-back";
2
+ import { ValidationError } from "@drax/common-back";
3
+ import TenantServiceFactory from "../factory/TenantServiceFactory.js";
4
+ import TenantPermissions from "../permissions/TenantPermissions.js";
5
+ import { UnauthorizedError } from "@drax/common-back";
6
+ class TenantController extends AbstractFastifyController {
7
+ constructor() {
8
+ super(TenantServiceFactory(), TenantPermissions);
9
+ }
10
+ async findByName(request, reply) {
11
+ try {
12
+ request.rbac.assertPermission(this.permission.View);
13
+ if (!request.params.id) {
14
+ reply.statusCode = 400;
15
+ reply.send({ error: 'BAD REQUEST' });
16
+ }
17
+ const name = request.params.name;
18
+ let item = await this.service.findByName(name);
19
+ return item;
20
+ }
21
+ catch (e) {
22
+ console.error(e);
23
+ if (e instanceof ValidationError) {
24
+ reply.statusCode = e.statusCode;
25
+ reply.send({ error: e.message, inputErrors: e.errors });
26
+ }
27
+ else if (e instanceof UnauthorizedError) {
28
+ reply.statusCode = e.statusCode;
29
+ reply.send({ error: e.message });
30
+ }
31
+ else {
32
+ reply.statusCode = 500;
33
+ reply.send({ error: 'INTERNAL_SERVER_ERROR' });
34
+ }
35
+ }
36
+ }
37
+ async all(request, reply) {
38
+ try {
39
+ request.rbac.assertPermission(this.permission.View);
40
+ let tenants = await this.service.fetchAll();
41
+ if (request.rbac.getAuthUser.tenantId) {
42
+ return tenants.filter(t => t.id === request.rbac.getAuthUser.tenantId);
43
+ }
44
+ else {
45
+ return tenants;
46
+ }
47
+ }
48
+ catch (e) {
49
+ console.error(e);
50
+ if (e instanceof ValidationError) {
51
+ reply.statusCode = e.statusCode;
52
+ reply.send({ error: e.message, inputErrors: e.errors });
53
+ }
54
+ else if (e instanceof UnauthorizedError) {
55
+ reply.statusCode = e.statusCode;
56
+ reply.send({ error: e.message });
57
+ }
58
+ else {
59
+ reply.statusCode = 500;
60
+ reply.send({ error: 'INTERNAL_SERVER_ERROR' });
61
+ }
62
+ }
63
+ }
64
+ }
65
+ export default TenantController;
66
+ export { TenantController };
@@ -1,9 +1,8 @@
1
1
  import RoleServiceFactory from "../../factory/RoleServiceFactory.js";
2
2
  import { IdentityPermissions } from "../../permissions/IdentityPermissions.js";
3
- import { ValidationError, ValidationErrorToGraphQLError } from "@drax/common-back";
3
+ import { ValidationError, ValidationErrorToGraphQLError, UnauthorizedError } from "@drax/common-back";
4
4
  import { GraphQLError } from "graphql";
5
5
  import { PermissionService } from "../../services/PermissionService.js";
6
- import UnauthorizedError from "../../errors/UnauthorizedError.js";
7
6
  export default {
8
7
  Query: {
9
8
  findRoleById: async (_, { id }, { rbac }) => {
@@ -1,8 +1,7 @@
1
1
  import TenantServiceFactory from "../../factory/TenantServiceFactory.js";
2
2
  import { IdentityPermissions } from "../../permissions/IdentityPermissions.js";
3
- import { ValidationError, ValidationErrorToGraphQLError } from "@drax/common-back";
3
+ import { ValidationError, ValidationErrorToGraphQLError, UnauthorizedError } from "@drax/common-back";
4
4
  import { GraphQLError } from "graphql";
5
- import UnauthorizedError from "../../errors/UnauthorizedError.js";
6
5
  export default {
7
6
  Query: {
8
7
  findTenantById: async (_, { id }, { rbac }) => {
@@ -1,8 +1,7 @@
1
1
  import UserApiKeyServiceFactory from "../../factory/UserApiKeyServiceFactory.js";
2
2
  import { IdentityPermissions } from "../../permissions/IdentityPermissions.js";
3
- import { ValidationError, ValidationErrorToGraphQLError } from "@drax/common-back";
3
+ import { ValidationError, ValidationErrorToGraphQLError, UnauthorizedError } from "@drax/common-back";
4
4
  import { GraphQLError } from "graphql";
5
- import UnauthorizedError from "../../errors/UnauthorizedError.js";
6
5
  import * as crypto from "node:crypto";
7
6
  export default {
8
7
  Query: {
@@ -2,7 +2,7 @@ import UserServiceFactory from "../../factory/UserServiceFactory.js";
2
2
  import { GraphQLError } from "graphql";
3
3
  import { ValidationErrorToGraphQLError, ValidationError, StoreManager, DraxConfig, CommonConfig } from "@drax/common-back";
4
4
  import { IdentityPermissions } from "../../permissions/IdentityPermissions.js";
5
- import UnauthorizedError from "../../errors/UnauthorizedError.js";
5
+ import { UnauthorizedError } from "@drax/common-back";
6
6
  import BadCredentialsError from "../../errors/BadCredentialsError.js";
7
7
  import { join } from "path";
8
8
  import IdentityConfig from "../../config/IdentityConfig.js";
package/dist/index.js CHANGED
@@ -16,15 +16,18 @@ import AuthUtils from "./utils/AuthUtils.js";
16
16
  import { jwtMiddleware } from "./middleware/jwtMiddleware.js";
17
17
  import { rbacMiddleware } from "./middleware/rbacMiddleware.js";
18
18
  import { apiKeyMiddleware } from "./middleware/apiKeyMiddleware.js";
19
- import IdentityPermissions from "./permissions/IdentityPermissions.js";
20
19
  import IdentityConfig from "./config/IdentityConfig.js";
21
- import UnauthorizedError from "./errors/UnauthorizedError.js";
22
20
  import BadCredentialsError from "./errors/BadCredentialsError.js";
23
21
  import CreateUserIfNotExist from "./setup/CreateUserIfNotExist.js";
24
22
  import CreateOrUpdateRole from "./setup/CreateOrUpdateRole.js";
25
23
  import LoadPermissions from "./setup/LoadPermissions.js";
26
24
  import LoadIdentityConfigFromEnv from "./setup/LoadIdentityConfigFromEnv.js";
27
25
  import RecoveryUserPassword from "./setup/RecoveryUserPassword.js";
26
+ import IdentityPermissions from "./permissions/IdentityPermissions.js";
27
+ import { RolePermissions } from "./permissions/RolePermissions.js";
28
+ import { TenantPermissions } from "./permissions/TenantPermissions.js";
29
+ import { UserPermissions } from "./permissions/UserPermissions.js";
30
+ import { UserApiKeyPermissions } from "./permissions/UserApiKeyPermissions.js";
28
31
  const graphqlMergeResult = await GraphqlMerge();
29
32
  const identityTypeDefs = await graphqlMergeResult.typeDefs;
30
33
  const identityResolvers = await graphqlMergeResult.resolvers;
@@ -40,10 +43,10 @@ UserRoutes, RoleRoutes, TenantRoutes, UserAvatarRoutes, UserApiKeyRoutes, AuthUt
40
43
  //API MIDDLEWARE
41
44
  jwtMiddleware, rbacMiddleware, apiKeyMiddleware,
42
45
  //Permissions
43
- IdentityPermissions,
46
+ IdentityPermissions, RolePermissions, TenantPermissions, UserPermissions, UserApiKeyPermissions,
44
47
  //Config
45
48
  IdentityConfig,
46
49
  //Errors
47
- UnauthorizedError, BadCredentialsError,
50
+ BadCredentialsError,
48
51
  //Setup
49
52
  LoadIdentityConfigFromEnv, LoadPermissions, CreateOrUpdateRole, CreateUserIfNotExist, RecoveryUserPassword };
@@ -0,0 +1,11 @@
1
+ var RolePermissions;
2
+ (function (RolePermissions) {
3
+ RolePermissions["Create"] = "role:create";
4
+ RolePermissions["Update"] = "role:update";
5
+ RolePermissions["Delete"] = "role:delete";
6
+ RolePermissions["View"] = "role:view";
7
+ RolePermissions["Manage"] = "role:manage";
8
+ RolePermissions["Permissions"] = "role:permissions";
9
+ })(RolePermissions || (RolePermissions = {}));
10
+ export default RolePermissions;
11
+ export { RolePermissions };
@@ -0,0 +1,10 @@
1
+ var TenantPermissions;
2
+ (function (TenantPermissions) {
3
+ TenantPermissions["Create"] = "tenant:create";
4
+ TenantPermissions["Update"] = "tenant:update";
5
+ TenantPermissions["Delete"] = "tenant:delete";
6
+ TenantPermissions["View"] = "tenant:view";
7
+ TenantPermissions["Manage"] = "tenant:manage";
8
+ })(TenantPermissions || (TenantPermissions = {}));
9
+ export default TenantPermissions;
10
+ export { TenantPermissions };
@@ -0,0 +1,11 @@
1
+ var UserApiKeyPermissions;
2
+ (function (UserApiKeyPermissions) {
3
+ UserApiKeyPermissions["Create"] = "userApiKey:create";
4
+ UserApiKeyPermissions["Update"] = "userApiKey:update";
5
+ UserApiKeyPermissions["Delete"] = "userApiKey:delete";
6
+ UserApiKeyPermissions["View"] = "userApiKey:view";
7
+ UserApiKeyPermissions["ViewMy"] = "userApiKey:myView";
8
+ UserApiKeyPermissions["Manage"] = "userApiKey:manage";
9
+ })(UserApiKeyPermissions || (UserApiKeyPermissions = {}));
10
+ export default UserApiKeyPermissions;
11
+ export { UserApiKeyPermissions };
@@ -0,0 +1,10 @@
1
+ var UserPermissions;
2
+ (function (UserPermissions) {
3
+ UserPermissions["Create"] = "user:create";
4
+ UserPermissions["Update"] = "user:update";
5
+ UserPermissions["Delete"] = "user:delete";
6
+ UserPermissions["View"] = "user:view";
7
+ UserPermissions["Manage"] = "user:manage";
8
+ })(UserPermissions || (UserPermissions = {}));
9
+ export default UserPermissions;
10
+ export { UserPermissions };
@@ -0,0 +1,12 @@
1
+ import TenantPermissions from './TenantPermissions';
2
+ import UserPermissions from './UserPermissions';
3
+ import RolePermissions from './RolePermissions';
4
+ import UserApiKeyPermissions from './UserApiKeyPermissions';
5
+ const permissions = {
6
+ ...TenantPermissions,
7
+ ...UserPermissions,
8
+ ...RolePermissions,
9
+ ...UserApiKeyPermissions
10
+ };
11
+ export default permissions;
12
+ export { permissions };
package/dist/rbac/Rbac.js CHANGED
@@ -1,4 +1,4 @@
1
- import UnauthorizedError from "../errors/UnauthorizedError.js";
1
+ import { UnauthorizedError } from "@drax/common-back";
2
2
  class Rbac {
3
3
  constructor(authUser, role) {
4
4
  this.authUser = authUser;
@@ -44,5 +44,17 @@ class TenantMongoRepository {
44
44
  items: tenants.docs
45
45
  };
46
46
  }
47
+ async find({ cursor = false, limit = 0, orderBy = '', order = false, search = '', filters = [] }) {
48
+ const query = {};
49
+ if (search) {
50
+ query['$or'] = [
51
+ { name: new RegExp(search, 'i') },
52
+ ];
53
+ }
54
+ MongooseQueryFilter.applyFilters(query, filters);
55
+ const sort = MongooseSort.applySort(orderBy, order);
56
+ const items = await TenantModel.find(query).sort(sort);
57
+ return items;
58
+ }
47
59
  }
48
60
  export default TenantMongoRepository;
@@ -96,5 +96,16 @@ class TenantSqliteRepository {
96
96
  items: tenants
97
97
  };
98
98
  }
99
+ async find({ cursor = false, limit = 0, orderBy = '', order = false, search = '', filters = [] }) {
100
+ let where = "";
101
+ if (search) {
102
+ where = ` WHERE name LIKE '%${search}%'`;
103
+ }
104
+ where = SqlQueryFilter.applyFilters(where, filters);
105
+ const sort = SqlSort.applySort(orderBy, order);
106
+ where += sort;
107
+ const tenants = this.db.prepare('SELECT * FROM tenants ' + where).all();
108
+ return tenants;
109
+ }
99
110
  }
100
111
  export default TenantSqliteRepository;
@@ -1,8 +1,7 @@
1
- import { ValidationError } from "@drax/common-back";
1
+ import { ValidationError, UnauthorizedError } from "@drax/common-back";
2
2
  import RoleServiceFactory from "../factory/RoleServiceFactory.js";
3
3
  import { IdentityPermissions } from "../permissions/IdentityPermissions.js";
4
4
  import { PermissionService } from "../services/PermissionService.js";
5
- import UnauthorizedError from "../errors/UnauthorizedError.js";
6
5
  async function RoleRoutes(fastify, options) {
7
6
  fastify.get('/api/permissions', async (request, reply) => {
8
7
  try {
@@ -1,185 +1,14 @@
1
- import { ValidationError } from "@drax/common-back";
2
- import TenantServiceFactory from "../factory/TenantServiceFactory.js";
3
- import { IdentityPermissions } from "../permissions/IdentityPermissions.js";
4
- import UnauthorizedError from "../errors/UnauthorizedError.js";
1
+ import TenantController from '../controllers/TenantController.js';
5
2
  async function TenantRoutes(fastify, options) {
6
- fastify.get('/api/tenants/:id', async (request, reply) => {
7
- try {
8
- request.rbac.assertPermission(IdentityPermissions.ViewTenant);
9
- const id = request.params.id;
10
- const tenantService = TenantServiceFactory();
11
- let tenant = await tenantService.findById(id);
12
- return tenant;
13
- }
14
- catch (e) {
15
- console.error(e);
16
- if (e instanceof ValidationError) {
17
- reply.statusCode = e.statusCode;
18
- reply.send({ error: e.message, inputErrors: e.errors });
19
- }
20
- else if (e instanceof UnauthorizedError) {
21
- reply.statusCode = e.statusCode;
22
- reply.send({ error: e.message });
23
- }
24
- else {
25
- reply.statusCode = 500;
26
- reply.send({ error: 'INTERNAL_SERVER_ERROR' });
27
- }
28
- }
29
- });
30
- fastify.get('/api/tenants/name/:name', async (request, reply) => {
31
- try {
32
- request.rbac.assertPermission(IdentityPermissions.ViewTenant);
33
- const name = request.params.name;
34
- const tenantService = TenantServiceFactory();
35
- let tenant = await tenantService.findByName(name);
36
- return tenant;
37
- }
38
- catch (e) {
39
- console.error(e);
40
- if (e instanceof ValidationError) {
41
- reply.statusCode = e.statusCode;
42
- reply.send({ error: e.message, inputErrors: e.errors });
43
- }
44
- else if (e instanceof UnauthorizedError) {
45
- reply.statusCode = e.statusCode;
46
- reply.send({ error: e.message });
47
- }
48
- else {
49
- reply.statusCode = 500;
50
- reply.send({ error: 'INTERNAL_SERVER_ERROR' });
51
- }
52
- }
53
- });
54
- fastify.get('/api/tenants/all', async (request, reply) => {
55
- try {
56
- request.rbac.assertPermission(IdentityPermissions.ViewTenant);
57
- const tenantService = TenantServiceFactory();
58
- let tenants = await tenantService.fetchAll();
59
- if (request.rbac.getAuthUser.tenantId) {
60
- return tenants.filter(t => t.id === request.rbac.getAuthUser.tenantId);
61
- }
62
- else {
63
- return tenants;
64
- }
65
- }
66
- catch (e) {
67
- console.error(e);
68
- if (e instanceof ValidationError) {
69
- reply.statusCode = e.statusCode;
70
- reply.send({ error: e.message, inputErrors: e.errors });
71
- }
72
- else if (e instanceof UnauthorizedError) {
73
- reply.statusCode = e.statusCode;
74
- reply.send({ error: e.message });
75
- }
76
- else {
77
- reply.statusCode = 500;
78
- reply.send({ error: 'INTERNAL_SERVER_ERROR' });
79
- }
80
- }
81
- });
82
- fastify.get('/api/tenants', async (request, reply) => {
83
- try {
84
- request.rbac.assertPermission(IdentityPermissions.ViewTenant);
85
- const page = request.query.page;
86
- const limit = request.query.limit;
87
- const orderBy = request.query.orderBy;
88
- const order = request.query.order;
89
- const search = request.query.search;
90
- const tenantService = TenantServiceFactory();
91
- let paginateResult = await tenantService.paginate({ page, limit, orderBy, order, search });
92
- return paginateResult;
93
- }
94
- catch (e) {
95
- console.error(e);
96
- if (e instanceof ValidationError) {
97
- reply.statusCode = e.statusCode;
98
- reply.send({ error: e.message, inputErrors: e.errors });
99
- }
100
- else if (e instanceof UnauthorizedError) {
101
- reply.statusCode = e.statusCode;
102
- reply.send({ error: e.message });
103
- }
104
- else {
105
- reply.statusCode = 500;
106
- reply.send({ error: 'INTERNAL_SERVER_ERROR' });
107
- }
108
- }
109
- });
110
- fastify.post('/api/tenants', async (request, reply) => {
111
- try {
112
- request.rbac.assertPermission(IdentityPermissions.CreateTenant);
113
- const payload = request.body;
114
- const tenantService = TenantServiceFactory();
115
- let tenant = await tenantService.create(payload);
116
- return tenant;
117
- }
118
- catch (e) {
119
- console.error(e);
120
- if (e instanceof ValidationError) {
121
- reply.statusCode = e.statusCode;
122
- reply.send({ error: e.message, inputErrors: e.errors });
123
- }
124
- else if (e instanceof UnauthorizedError) {
125
- reply.statusCode = e.statusCode;
126
- reply.send({ error: e.message });
127
- }
128
- else {
129
- reply.statusCode = 500;
130
- reply.send({ error: 'INTERNAL_SERVER_ERROR' });
131
- }
132
- }
133
- });
134
- fastify.put('/api/tenants/:id', async (request, reply) => {
135
- try {
136
- request.rbac.assertPermission(IdentityPermissions.UpdateTenant);
137
- const id = request.params.id;
138
- const payload = request.body;
139
- const tenantService = TenantServiceFactory();
140
- let tenant = await tenantService.update(id, payload);
141
- return tenant;
142
- }
143
- catch (e) {
144
- console.error(e);
145
- if (e instanceof ValidationError) {
146
- reply.statusCode = e.statusCode;
147
- reply.send({ error: e.message, inputErrors: e.errors });
148
- }
149
- else if (e instanceof UnauthorizedError) {
150
- reply.statusCode = e.statusCode;
151
- reply.send({ error: e.message });
152
- }
153
- else {
154
- reply.statusCode = 500;
155
- reply.send({ error: 'INTERNAL_SERVER_ERROR' });
156
- }
157
- }
158
- });
159
- fastify.delete('/api/tenants/:id', async (request, reply) => {
160
- try {
161
- request.rbac.assertPermission(IdentityPermissions.DeleteTenant);
162
- const id = request.params.id;
163
- const tenantService = TenantServiceFactory();
164
- let r = await tenantService.delete(id);
165
- return r;
166
- }
167
- catch (e) {
168
- console.error(e);
169
- if (e instanceof ValidationError) {
170
- reply.statusCode = e.statusCode;
171
- reply.send({ error: e.message, inputErrors: e.errors });
172
- }
173
- else if (e instanceof UnauthorizedError) {
174
- reply.statusCode = e.statusCode;
175
- reply.send({ error: e.message });
176
- }
177
- else {
178
- reply.statusCode = 500;
179
- reply.send({ error: 'INTERNAL_SERVER_ERROR' });
180
- }
181
- }
182
- });
3
+ const controller = new TenantController();
4
+ fastify.get('/api/tenants/export', (req, rep) => controller.export(req, rep));
5
+ fastify.get('/api/tenants/:id', (req, rep) => controller.findById(req, rep));
6
+ fastify.get('/api/tenants/name/:name', (req, rep) => controller.findByName(req, rep));
7
+ fastify.get('/api/tenants/all', (req, rep) => controller.all(req, rep));
8
+ fastify.get('/api/tenants', (req, rep) => controller.paginate(req, rep));
9
+ fastify.post('/api/tenants', (req, rep) => controller.create(req, rep));
10
+ fastify.put('/api/tenants/:id', (req, rep) => controller.update(req, rep));
11
+ fastify.delete('/api/tenants/:id', (req, rep) => controller.delete(req, rep));
183
12
  }
184
13
  export default TenantRoutes;
185
14
  export { TenantRoutes };
@@ -1,7 +1,6 @@
1
1
  import UserApiKeyServiceFactory from "../factory/UserApiKeyServiceFactory.js";
2
- import { ValidationError } from "@drax/common-back";
2
+ import { ValidationError, UnauthorizedError } from "@drax/common-back";
3
3
  import { IdentityPermissions } from "../permissions/IdentityPermissions.js";
4
- import UnauthorizedError from "../errors/UnauthorizedError.js";
5
4
  async function UserApiKeyRoutes(fastify, options) {
6
5
  fastify.get('/api/user-api-keys', async (request, reply) => {
7
6
  try {
@@ -1,6 +1,5 @@
1
1
  import { join } from "path";
2
- import UnauthorizedError from "../errors/UnauthorizedError.js";
3
- import { StoreManager, UploadFileError, DraxConfig, CommonConfig } from "@drax/common-back";
2
+ import { StoreManager, UploadFileError, DraxConfig, CommonConfig, UnauthorizedError } from "@drax/common-back";
4
3
  import UserServiceFactory from "../factory/UserServiceFactory.js";
5
4
  import IdentityConfig from "../config/IdentityConfig.js";
6
5
  const BASE_FILE_DIR = DraxConfig.getOrLoad(CommonConfig.FileDir) || 'files';
@@ -1,7 +1,6 @@
1
1
  import UserServiceFactory from "../factory/UserServiceFactory.js";
2
- import { ValidationError } from "@drax/common-back";
2
+ import { ValidationError, UnauthorizedError } from "@drax/common-back";
3
3
  import { IdentityPermissions } from "../permissions/IdentityPermissions.js";
4
- import UnauthorizedError from "../errors/UnauthorizedError.js";
5
4
  import BadCredentialsError from "../errors/BadCredentialsError.js";
6
5
  async function UserRoutes(fastify, options) {
7
6
  fastify.post('/api/auth', async (request, reply) => {
@@ -1,8 +1,10 @@
1
1
  import { ZodErrorToValidationError } from "@drax/common-back";
2
2
  import { tenantSchema } from "../zod/TenantZod.js";
3
3
  import { ZodError } from "zod";
4
- class TenantService {
4
+ import { AbstractService } from "@drax/crud-back";
5
+ class TenantService extends AbstractService {
5
6
  constructor(tenantRepostitory) {
7
+ super(tenantRepostitory, tenantSchema);
6
8
  this._repository = tenantRepostitory;
7
9
  console.log("TenantService constructor");
8
10
  }
package/package.json CHANGED
@@ -3,10 +3,10 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "0.5.1",
6
+ "version": "0.5.3",
7
7
  "description": "Identity module for user management, authentication and authorization.",
8
8
  "main": "dist/index.js",
9
- "types": "types/index.d.ts",
9
+ "types": "dist/types/index.d.ts",
10
10
  "type": "module",
11
11
  "scripts": {
12
12
  "prepublish": "tsc && npm run copygql",
@@ -28,8 +28,9 @@
28
28
  "author": "Cristian Incarnato & Drax Team",
29
29
  "license": "ISC",
30
30
  "dependencies": {
31
- "@drax/common-back": "^0.5.1",
32
- "@drax/crud-share": "^0.5.1",
31
+ "@drax/common-back": "^0.5.3",
32
+ "@drax/crud-back": "^0.5.3",
33
+ "@drax/crud-share": "^0.5.3",
33
34
  "@drax/identity-share": "^0.5.1",
34
35
  "bcryptjs": "^2.4.3",
35
36
  "express-jwt": "^8.4.1",
@@ -61,5 +62,5 @@
61
62
  "debug": "0"
62
63
  }
63
64
  },
64
- "gitHead": "6f507c8b52e134f10839fd7698c2466ff65a3d08"
65
+ "gitHead": "56ea5a743bce196d6322ae6c26ea5b041450d7a3"
65
66
  }
@@ -0,0 +1,73 @@
1
+ import type {ITenant, ITenantBase} from "@drax/identity-share";
2
+ import {AbstractFastifyController} from "@drax/crud-back";
3
+ import {ValidationError} from "@drax/common-back";
4
+
5
+ import TenantServiceFactory from "../factory/TenantServiceFactory.js";
6
+ import TenantService from "../services/TenantService.js";
7
+ import TenantPermissions from "../permissions/TenantPermissions.js";
8
+ import {UnauthorizedError} from "@drax/common-back";
9
+
10
+ class TenantController extends AbstractFastifyController<ITenant, ITenantBase, ITenantBase> {
11
+
12
+ protected service: TenantService
13
+
14
+ constructor() {
15
+ super(TenantServiceFactory(), TenantPermissions)
16
+ }
17
+
18
+ async findByName(request, reply) {
19
+ try {
20
+ request.rbac.assertPermission(this.permission.View)
21
+ if(!request.params.id){
22
+ reply.statusCode = 400
23
+ reply.send({error: 'BAD REQUEST'})
24
+ }
25
+ const name = request.params.name
26
+ let item = await this.service.findByName(name)
27
+ return item
28
+ } catch (e) {
29
+ console.error(e)
30
+ if (e instanceof ValidationError) {
31
+ reply.statusCode = e.statusCode
32
+ reply.send({error: e.message, inputErrors: e.errors})
33
+ } else if (e instanceof UnauthorizedError) {
34
+ reply.statusCode = e.statusCode
35
+ reply.send({error: e.message})
36
+ } else {
37
+ reply.statusCode = 500
38
+ reply.send({error: 'INTERNAL_SERVER_ERROR'})
39
+ }
40
+ }
41
+ }
42
+
43
+ async all(request, reply) {
44
+ try {
45
+ request.rbac.assertPermission(this.permission.View)
46
+ let tenants = await this.service.fetchAll()
47
+ if(request.rbac.getAuthUser.tenantId){
48
+ return tenants.filter(t => t.id === request.rbac.getAuthUser.tenantId)
49
+ }else{
50
+ return tenants
51
+ }
52
+ } catch (e) {
53
+ console.error(e)
54
+ if (e instanceof ValidationError) {
55
+ reply.statusCode = e.statusCode
56
+ reply.send({error: e.message, inputErrors: e.errors})
57
+ } else if (e instanceof UnauthorizedError) {
58
+ reply.statusCode = e.statusCode
59
+ reply.send({error: e.message})
60
+ } else {
61
+ reply.statusCode = 500
62
+ reply.send({error: 'INTERNAL_SERVER_ERROR'})
63
+ }
64
+ }
65
+ }
66
+
67
+ }
68
+
69
+ export default TenantController;
70
+ export {
71
+ TenantController
72
+ }
73
+
@@ -1,9 +1,8 @@
1
1
  import RoleServiceFactory from "../../factory/RoleServiceFactory.js";
2
2
  import {IdentityPermissions} from "../../permissions/IdentityPermissions.js";
3
- import {ValidationError, ValidationErrorToGraphQLError} from "@drax/common-back";
3
+ import {ValidationError, ValidationErrorToGraphQLError, UnauthorizedError} from "@drax/common-back";
4
4
  import {GraphQLError} from "graphql";
5
5
  import {PermissionService} from "../../services/PermissionService.js";
6
- import UnauthorizedError from "../../errors/UnauthorizedError.js";
7
6
 
8
7
 
9
8
  export default {
@@ -1,8 +1,7 @@
1
1
  import TenantServiceFactory from "../../factory/TenantServiceFactory.js";
2
2
  import {IdentityPermissions} from "../../permissions/IdentityPermissions.js";
3
- import {ValidationError, ValidationErrorToGraphQLError} from "@drax/common-back";
3
+ import {ValidationError, ValidationErrorToGraphQLError, UnauthorizedError} from "@drax/common-back";
4
4
  import {GraphQLError} from "graphql";
5
- import UnauthorizedError from "../../errors/UnauthorizedError.js";
6
5
 
7
6
 
8
7
  export default {
@@ -1,8 +1,7 @@
1
1
  import UserApiKeyServiceFactory from "../../factory/UserApiKeyServiceFactory.js";
2
2
  import {IdentityPermissions} from "../../permissions/IdentityPermissions.js";
3
- import {ValidationError, ValidationErrorToGraphQLError} from "@drax/common-back";
3
+ import {ValidationError, ValidationErrorToGraphQLError, UnauthorizedError} from "@drax/common-back";
4
4
  import {GraphQLError} from "graphql";
5
- import UnauthorizedError from "../../errors/UnauthorizedError.js";
6
5
  import * as crypto from "node:crypto";
7
6
 
8
7
 
@@ -8,7 +8,7 @@ import {
8
8
  CommonConfig
9
9
  } from "@drax/common-back";
10
10
  import {IdentityPermissions} from "../../permissions/IdentityPermissions.js";
11
- import UnauthorizedError from "../../errors/UnauthorizedError.js";
11
+ import {UnauthorizedError} from "@drax/common-back";
12
12
  import BadCredentialsError from "../../errors/BadCredentialsError.js";
13
13
  import {join} from "path";
14
14
  import IdentityConfig from "../../config/IdentityConfig.js";