@drax/identity-back 0.11.5 → 0.12.2

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 (224) hide show
  1. package/dist/controllers/RoleController.js +8 -39
  2. package/dist/controllers/TenantController.js +1 -28
  3. package/dist/controllers/UserApiKeyController.js +3 -3
  4. package/dist/controllers/UserController.js +48 -209
  5. package/dist/errors/BadCredentialsError.js +12 -0
  6. package/dist/factory/RoleServiceFactory.js +1 -0
  7. package/dist/factory/TenantServiceFactory.js +1 -0
  8. package/dist/factory/UserApiKeyServiceFactory.js +5 -4
  9. package/dist/factory/UserServiceFactory.js +1 -0
  10. package/dist/graphql/resolvers/role.resolvers.js +2 -2
  11. package/dist/graphql/resolvers/tenant.resolvers.js +2 -2
  12. package/dist/graphql/resolvers/user-api-key.resolvers.js +2 -2
  13. package/dist/graphql/resolvers/user.resolvers.js +1 -1
  14. package/dist/index.js +6 -0
  15. package/dist/middleware/apiKeyMiddleware.js +2 -2
  16. package/dist/models/RoleModel.js +10 -7
  17. package/dist/models/TenantModel.js +11 -8
  18. package/dist/models/UserApiKeyModel.js +10 -7
  19. package/dist/models/UserGroupModel.js +7 -7
  20. package/dist/models/UserModel.js +10 -8
  21. package/dist/rbac/Rbac.js +10 -8
  22. package/dist/repository/mongo/RoleMongoRepository.js +20 -65
  23. package/dist/repository/mongo/TenantMongoRepository.js +18 -66
  24. package/dist/repository/mongo/UserApiKeyMongoRepository.js +29 -47
  25. package/dist/repository/mongo/UserMongoRepository.js +56 -85
  26. package/dist/repository/sqlite/RoleSqliteRepository.js +30 -115
  27. package/dist/repository/sqlite/TenantSqliteRepository.js +15 -105
  28. package/dist/repository/sqlite/UserApiKeySqliteRepository.js +42 -117
  29. package/dist/repository/sqlite/UserSqliteRepository.js +49 -130
  30. package/dist/routes/RoleRoutes.js +35 -10
  31. package/dist/routes/TenantRoutes.js +18 -9
  32. package/dist/routes/UserApiKeyRoutes.js +20 -4
  33. package/dist/routes/UserRoutes.js +92 -17
  34. package/dist/schemas/LoginSchema.js +9 -0
  35. package/dist/schemas/PasswordSchema.js +12 -0
  36. package/dist/schemas/RegisterSchema.js +19 -0
  37. package/dist/schemas/RoleSchema.js +23 -0
  38. package/dist/schemas/TenantSchema.js +13 -0
  39. package/dist/schemas/UserApiKeySchema.js +14 -0
  40. package/dist/schemas/UserSchema.js +39 -0
  41. package/dist/services/PermissionService.js +5 -5
  42. package/dist/services/RoleService.js +6 -6
  43. package/dist/services/TenantService.js +6 -6
  44. package/dist/services/UserApiKeyService.js +5 -5
  45. package/dist/services/UserService.js +14 -14
  46. package/dist/setup/CreateOrUpdateRole.js +5 -2
  47. package/dist/setup/CreateUserIfNotExist.js +3 -1
  48. package/dist/setup/RecoveryUserPassword.js +1 -1
  49. package/dist/zod/EndpointZod.js +9 -0
  50. package/dist/zod/TenantSchema.js +12 -0
  51. package/dist/zod/TenantZod.js +5 -3
  52. package/dist/zod/UserApiKeyZod.js +7 -3
  53. package/package.json +10 -9
  54. package/src/controllers/RoleController.ts +8 -36
  55. package/src/controllers/TenantController.ts +2 -25
  56. package/src/controllers/UserApiKeyController.ts +3 -3
  57. package/src/controllers/UserController.ts +50 -183
  58. package/src/errors/BadCredentialsError.ts +18 -1
  59. package/src/factory/RoleServiceFactory.ts +1 -0
  60. package/src/factory/TenantServiceFactory.ts +1 -0
  61. package/src/factory/UserApiKeyServiceFactory.ts +5 -4
  62. package/src/factory/UserServiceFactory.ts +1 -0
  63. package/src/graphql/resolvers/role.resolvers.ts +3 -2
  64. package/src/graphql/resolvers/tenant.resolvers.ts +3 -2
  65. package/src/graphql/resolvers/user-api-key.resolvers.ts +3 -2
  66. package/src/graphql/resolvers/user.resolvers.ts +2 -1
  67. package/src/index.ts +16 -0
  68. package/src/interfaces/ITenantRepository.ts +2 -2
  69. package/src/interfaces/IUserApiKeyRepository.ts +2 -2
  70. package/src/interfaces/IUserRepository.ts +3 -2
  71. package/src/middleware/apiKeyMiddleware.ts +2 -2
  72. package/src/models/RoleModel.ts +12 -7
  73. package/src/models/TenantModel.ts +13 -8
  74. package/src/models/UserApiKeyModel.ts +12 -7
  75. package/src/models/UserGroupModel.ts +7 -7
  76. package/src/models/UserModel.ts +10 -8
  77. package/src/rbac/Rbac.ts +12 -9
  78. package/src/repository/mongo/RoleMongoRepository.ts +23 -94
  79. package/src/repository/mongo/TenantMongoRepository.ts +19 -98
  80. package/src/repository/mongo/UserApiKeyMongoRepository.ts +31 -56
  81. package/src/repository/mongo/UserMongoRepository.ts +71 -130
  82. package/src/repository/sqlite/RoleSqliteRepository.ts +37 -146
  83. package/src/repository/sqlite/TenantSqliteRepository.ts +16 -156
  84. package/src/repository/sqlite/UserApiKeySqliteRepository.ts +46 -151
  85. package/src/repository/sqlite/UserSqliteRepository.ts +59 -173
  86. package/src/routes/RoleRoutes.ts +35 -12
  87. package/src/routes/TenantRoutes.ts +25 -9
  88. package/src/routes/UserApiKeyRoutes.ts +23 -7
  89. package/src/routes/UserRoutes.ts +117 -34
  90. package/src/schemas/LoginSchema.ts +12 -0
  91. package/src/schemas/PasswordSchema.ts +16 -0
  92. package/src/{zod/UserZod.ts → schemas/RegisterSchema.ts} +7 -10
  93. package/src/schemas/RoleSchema.ts +29 -0
  94. package/src/schemas/TenantSchema.ts +22 -0
  95. package/src/{zod/UserApiKeyZod.ts → schemas/UserApiKeySchema.ts} +8 -3
  96. package/src/schemas/UserSchema.ts +57 -0
  97. package/src/services/PermissionService.ts +6 -5
  98. package/src/services/RoleService.ts +6 -6
  99. package/src/services/TenantService.ts +10 -10
  100. package/src/services/UserApiKeyService.ts +5 -5
  101. package/src/services/UserService.ts +15 -16
  102. package/src/setup/CreateOrUpdateRole.ts +7 -4
  103. package/src/setup/CreateUserIfNotExist.ts +5 -3
  104. package/src/setup/RecoveryUserPassword.ts +1 -1
  105. package/test/data-obj/apikey/root-mongo-user-apikey.ts +2 -1
  106. package/test/data-obj/roles/admin-sqlite-role.ts +2 -2
  107. package/test/data-obj/roles/operator-sqlite-role.ts +1 -1
  108. package/test/data-obj/tenants/company-sqlite-tenant.ts +6 -0
  109. package/test/data-obj/users/root-sqlite-user.ts +2 -2
  110. package/test/initializers/RoleSqliteInitializer.ts +1 -1
  111. package/test/repository/mongo/role-mongo-repository.test.ts +3 -3
  112. package/test/repository/mongo/user-apikey-mongo-repository.test.ts +5 -4
  113. package/test/repository/mongo/user-mongo-repository.test.ts +4 -4
  114. package/test/repository/sqlite/role-sqlite-repository.test.ts +21 -9
  115. package/test/repository/sqlite/tenant-sqlite-repository.test.ts +74 -0
  116. package/test/repository/sqlite/user-sqlite-repository.test.ts +15 -9
  117. package/test/routes/data/admin-role.ts +10 -0
  118. package/test/routes/data/root-user.ts +13 -0
  119. package/test/routes/helpers/CreateRootUserAndAdminRole.ts +17 -0
  120. package/test/routes/helpers/FastifyTestServerFactory.ts +34 -0
  121. package/test/routes/helpers/InitializePermissions.ts +23 -0
  122. package/test/routes/helpers/SetupIdentityDrax.ts +22 -0
  123. package/test/routes/tenant-route.test.ts +336 -0
  124. package/test/routes/user-route.test.ts +186 -0
  125. package/test/schemas/lab-schema.test.ts +110 -0
  126. package/test/service/mock-service.test.ts +3 -3
  127. package/test/service/role-service.test.ts +3 -3
  128. package/test/service/user-service.test.ts +16 -25
  129. package/test.db +0 -0
  130. package/tsconfig.tsbuildinfo +1 -1
  131. package/types/controllers/RoleController.d.ts +0 -1
  132. package/types/controllers/RoleController.d.ts.map +1 -1
  133. package/types/controllers/TenantController.d.ts +0 -1
  134. package/types/controllers/TenantController.d.ts.map +1 -1
  135. package/types/controllers/UserController.d.ts +11 -4
  136. package/types/controllers/UserController.d.ts.map +1 -1
  137. package/types/errors/BadCredentialsError.d.ts +9 -1
  138. package/types/errors/BadCredentialsError.d.ts.map +1 -1
  139. package/types/factory/RoleServiceFactory.d.ts.map +1 -1
  140. package/types/factory/TenantServiceFactory.d.ts.map +1 -1
  141. package/types/factory/UserApiKeyServiceFactory.d.ts.map +1 -1
  142. package/types/factory/UserServiceFactory.d.ts.map +1 -1
  143. package/types/graphql/resolvers/role.resolvers.d.ts +3 -9
  144. package/types/graphql/resolvers/role.resolvers.d.ts.map +1 -1
  145. package/types/graphql/resolvers/tenant.resolvers.d.ts +3 -9
  146. package/types/graphql/resolvers/tenant.resolvers.d.ts.map +1 -1
  147. package/types/graphql/resolvers/user-api-key.resolvers.d.ts +3 -9
  148. package/types/graphql/resolvers/user-api-key.resolvers.d.ts.map +1 -1
  149. package/types/graphql/resolvers/user.resolvers.d.ts +3 -9
  150. package/types/graphql/resolvers/user.resolvers.d.ts.map +1 -1
  151. package/types/index.d.ts +5 -1
  152. package/types/index.d.ts.map +1 -1
  153. package/types/interfaces/ITenantRepository.d.ts +2 -2
  154. package/types/interfaces/ITenantRepository.d.ts.map +1 -1
  155. package/types/interfaces/IUserApiKeyRepository.d.ts +2 -2
  156. package/types/interfaces/IUserApiKeyRepository.d.ts.map +1 -1
  157. package/types/interfaces/IUserRepository.d.ts +3 -2
  158. package/types/interfaces/IUserRepository.d.ts.map +1 -1
  159. package/types/models/RoleModel.d.ts +7 -7
  160. package/types/models/RoleModel.d.ts.map +1 -1
  161. package/types/models/TenantModel.d.ts +7 -7
  162. package/types/models/TenantModel.d.ts.map +1 -1
  163. package/types/models/UserApiKeyModel.d.ts +7 -7
  164. package/types/models/UserApiKeyModel.d.ts.map +1 -1
  165. package/types/models/UserGroupModel.d.ts +2 -2
  166. package/types/models/UserGroupModel.d.ts.map +1 -1
  167. package/types/models/UserModel.d.ts +7 -7
  168. package/types/models/UserModel.d.ts.map +1 -1
  169. package/types/rbac/Rbac.d.ts +1 -1
  170. package/types/rbac/Rbac.d.ts.map +1 -1
  171. package/types/repository/mongo/RoleMongoRepository.d.ts +9 -11
  172. package/types/repository/mongo/RoleMongoRepository.d.ts.map +1 -1
  173. package/types/repository/mongo/TenantMongoRepository.d.ts +8 -11
  174. package/types/repository/mongo/TenantMongoRepository.d.ts.map +1 -1
  175. package/types/repository/mongo/UserApiKeyMongoRepository.d.ts +12 -5
  176. package/types/repository/mongo/UserApiKeyMongoRepository.d.ts.map +1 -1
  177. package/types/repository/mongo/UserMongoRepository.d.ts +11 -12
  178. package/types/repository/mongo/UserMongoRepository.d.ts.map +1 -1
  179. package/types/repository/sqlite/RoleSqliteRepository.d.ts +14 -14
  180. package/types/repository/sqlite/RoleSqliteRepository.d.ts.map +1 -1
  181. package/types/repository/sqlite/TenantSqliteRepository.d.ts +12 -14
  182. package/types/repository/sqlite/TenantSqliteRepository.d.ts.map +1 -1
  183. package/types/repository/sqlite/UserApiKeySqliteRepository.d.ts +15 -11
  184. package/types/repository/sqlite/UserApiKeySqliteRepository.d.ts.map +1 -1
  185. package/types/repository/sqlite/UserSqliteRepository.d.ts +15 -12
  186. package/types/repository/sqlite/UserSqliteRepository.d.ts.map +1 -1
  187. package/types/routes/RoleRoutes.d.ts.map +1 -1
  188. package/types/routes/TenantRoutes.d.ts.map +1 -1
  189. package/types/routes/UserApiKeyRoutes.d.ts.map +1 -1
  190. package/types/routes/UserRoutes.d.ts.map +1 -1
  191. package/types/schemas/LoginSchema.d.ts +20 -0
  192. package/types/schemas/LoginSchema.d.ts.map +1 -0
  193. package/types/schemas/PasswordSchema.d.ts +27 -0
  194. package/types/schemas/PasswordSchema.d.ts.map +1 -0
  195. package/types/schemas/RegisterSchema.d.ts +32 -0
  196. package/types/schemas/RegisterSchema.d.ts.map +1 -0
  197. package/types/schemas/RoleSchema.d.ts +67 -0
  198. package/types/schemas/RoleSchema.d.ts.map +1 -0
  199. package/types/schemas/TenantSchema.d.ts +29 -0
  200. package/types/schemas/TenantSchema.d.ts.map +1 -0
  201. package/types/schemas/UserApiKeySchema.d.ts +39 -0
  202. package/types/schemas/UserApiKeySchema.d.ts.map +1 -0
  203. package/types/schemas/UserSchema.d.ts +161 -0
  204. package/types/schemas/UserSchema.d.ts.map +1 -0
  205. package/types/services/PermissionService.d.ts +1 -0
  206. package/types/services/PermissionService.d.ts.map +1 -1
  207. package/types/services/TenantService.d.ts +3 -3
  208. package/types/services/TenantService.d.ts.map +1 -1
  209. package/types/services/UserService.d.ts.map +1 -1
  210. package/types/setup/CreateOrUpdateRole.d.ts +2 -2
  211. package/types/setup/CreateOrUpdateRole.d.ts.map +1 -1
  212. package/types/setup/CreateUserIfNotExist.d.ts +2 -2
  213. package/types/setup/CreateUserIfNotExist.d.ts.map +1 -1
  214. package/types/zod/EndpointZod.d.ts +20 -0
  215. package/types/zod/EndpointZod.d.ts.map +1 -0
  216. package/types/zod/TenantSchema.d.ts +26 -0
  217. package/types/zod/TenantSchema.d.ts.map +1 -0
  218. package/types/zod/TenantZod.d.ts +13 -3
  219. package/types/zod/TenantZod.d.ts.map +1 -1
  220. package/types/zod/UserApiKeyZod.d.ts +23 -3
  221. package/types/zod/UserApiKeyZod.d.ts.map +1 -1
  222. package/types/zod/UserZod.d.ts +6 -6
  223. package/src/zod/RoleZod.ts +0 -14
  224. package/src/zod/TenantZod.ts +0 -14
@@ -6,7 +6,7 @@ const Schema = mongoose.Schema
6
6
  import {PaginateModel} from "mongoose";
7
7
 
8
8
 
9
- const RoleSchema = new Schema<IRole>({
9
+ const RoleMongoSchema = new Schema<IRole>({
10
10
  name: {
11
11
  type: String, unique: true, required: true, index: true,
12
12
  },
@@ -17,21 +17,26 @@ const RoleSchema = new Schema<IRole>({
17
17
  required: false,
18
18
  }],
19
19
  readonly: {type: Boolean, required: false, default: false},
20
+ }, {timestamps: true, toJSON: { virtuals: true}, toObject: {virtuals: true} } );
21
+
22
+ RoleMongoSchema.virtual("id").get(function () {
23
+ return this._id.toString();
20
24
  });
21
25
 
22
- RoleSchema.plugin(uniqueValidator, {message: 'validation.unique'});
23
- RoleSchema.plugin(MongooseSoftDelete);
24
- RoleSchema.plugin(mongoosePaginate);
26
+
27
+ RoleMongoSchema.plugin(uniqueValidator, {message: 'validation.unique'});
28
+ RoleMongoSchema.plugin(MongooseSoftDelete);
29
+ RoleMongoSchema.plugin(mongoosePaginate);
25
30
 
26
31
 
27
- RoleSchema.set('toJSON', {getters: true});
32
+ RoleMongoSchema.set('toJSON', {getters: true, virtuals: true});
28
33
 
29
34
  const ROLE_MODEL_NAME = 'Role';
30
35
  const ROLE_COLLECTION_NAME = 'roles';
31
- const RoleModel = mongoose.model<IRole, PaginateModel<IRole>>(ROLE_MODEL_NAME, RoleSchema,ROLE_COLLECTION_NAME);
36
+ const RoleModel = mongoose.models.Role as mongoose.Model<IRole> & PaginateModel<IRole> || mongoose.model<IRole, PaginateModel<IRole>>(ROLE_MODEL_NAME, RoleMongoSchema,ROLE_COLLECTION_NAME);
32
37
 
33
38
  export {
34
- RoleSchema,
39
+ RoleMongoSchema,
35
40
  RoleModel
36
41
  }
37
42
 
@@ -6,23 +6,28 @@ const Schema = mongoose.Schema
6
6
  import {PaginateModel} from "mongoose";
7
7
 
8
8
 
9
- const TenantSchema = new Schema<ITenant>({
9
+ const TenantMongoSchema = new Schema<ITenant>({
10
10
  name: {type: String, unique: true, required: true, index: true,},
11
- }, {timestamps: true});
11
+ }, {timestamps: true, toJSON: { virtuals: true}, toObject: { virtuals: true}});
12
12
 
13
- TenantSchema.plugin(uniqueValidator, {message: 'validation.unique'});
14
- TenantSchema.plugin(MongooseSoftDelete);
15
- TenantSchema.plugin(mongoosePaginate);
16
13
 
14
+ TenantMongoSchema.virtual("id").get(function () {
15
+ return this._id.toString();
16
+ });
17
17
 
18
- TenantSchema.set('toJSON', {getters: true});
18
+ TenantMongoSchema.plugin(uniqueValidator, {message: 'validation.unique'});
19
+ TenantMongoSchema.plugin(MongooseSoftDelete);
20
+ TenantMongoSchema.plugin(mongoosePaginate);
21
+
22
+
23
+ TenantMongoSchema.set('toJSON', {getters: true, virtuals: true});
19
24
 
20
25
  const TENANT_MODEL_NAME = 'Tenant';
21
26
  const TENANT_COLLECTION_NAME = 'tenants';
22
- const TenantModel = mongoose.model<ITenant, PaginateModel<ITenant>>(TENANT_MODEL_NAME, TenantSchema,TENANT_COLLECTION_NAME);
27
+ const TenantModel = mongoose.models.Tenant as mongoose.Model<ITenant> & PaginateModel<ITenant> || mongoose.model<ITenant, PaginateModel<ITenant>>(TENANT_MODEL_NAME, TenantMongoSchema,TENANT_COLLECTION_NAME);
23
28
 
24
29
  export {
25
- TenantSchema,
30
+ TenantMongoSchema,
26
31
  TenantModel
27
32
  }
28
33
 
@@ -5,7 +5,7 @@ import mongoosePaginate from 'mongoose-paginate-v2'
5
5
  import {PaginateModel} from "mongoose";
6
6
 
7
7
  // Defining user Mongoose Schema
8
- const UserApiKeySchema = new mongoose.Schema<IUserApiKey>({
8
+ const UserApiKeyMongoSchema = new mongoose.Schema<IUserApiKey>({
9
9
  name: {
10
10
  type: String,
11
11
  unique: false,
@@ -42,21 +42,26 @@ const UserApiKeySchema = new mongoose.Schema<IUserApiKey>({
42
42
  },
43
43
  }, {timestamps: true});
44
44
 
45
- UserApiKeySchema.set('toJSON', {getters: true});
45
+ UserApiKeyMongoSchema.virtual("id").get(function () {
46
+ return this._id.toString();
47
+ });
46
48
 
47
- UserApiKeySchema.plugin(uniqueValidator, {message: 'validation.unique'});
48
49
 
49
- UserApiKeySchema.plugin(MongooseSoftDelete);
50
- UserApiKeySchema.plugin(mongoosePaginate);
50
+ UserApiKeyMongoSchema.set('toJSON', {getters: true, virtuals: true});
51
+
52
+ UserApiKeyMongoSchema.plugin(uniqueValidator, {message: 'validation.unique'});
53
+
54
+ UserApiKeyMongoSchema.plugin(MongooseSoftDelete);
55
+ UserApiKeyMongoSchema.plugin(mongoosePaginate);
51
56
 
52
57
  const MODEL_NAME = 'UserApiKey';
53
58
  const COLLECTION_NAME = 'userApiKeys';
54
59
 
55
- const UserApiKeyModel = mongoose.model<IUserApiKey,PaginateModel<IUserApiKey>>(MODEL_NAME, UserApiKeySchema,COLLECTION_NAME);
60
+ const UserApiKeyModel = mongoose.models.UserApiKey as mongoose.Model<IUserApiKey> & PaginateModel<IUserApiKey> || mongoose.model<IUserApiKey,PaginateModel<IUserApiKey>>(MODEL_NAME, UserApiKeyMongoSchema,COLLECTION_NAME);
56
61
 
57
62
 
58
63
  export {
59
- UserApiKeySchema,
64
+ UserApiKeyMongoSchema,
60
65
  UserApiKeyModel
61
66
  }
62
67
 
@@ -6,7 +6,7 @@ import {PaginateModel} from "mongoose";
6
6
 
7
7
 
8
8
  // Defining user Mongoose Schema
9
- const UserGroupSchema = new mongoose.Schema<IUserGroup>({
9
+ const UserGroupMongoSchema = new mongoose.Schema<IUserGroup>({
10
10
  name: {
11
11
  type: String,
12
12
  unique: true,
@@ -27,20 +27,20 @@ const UserGroupSchema = new mongoose.Schema<IUserGroup>({
27
27
  }],
28
28
  }, {timestamps: true});
29
29
 
30
- UserGroupSchema.set('toJSON', {getters: true});
30
+ UserGroupMongoSchema.set('toJSON', {getters: true});
31
31
 
32
- UserGroupSchema.plugin(uniqueValidator, {message: 'validation.unique'});
32
+ UserGroupMongoSchema.plugin(uniqueValidator, {message: 'validation.unique'});
33
33
 
34
- UserGroupSchema.plugin(MongooseSoftDelete);
35
- UserGroupSchema.plugin(mongoosePaginate);
34
+ UserGroupMongoSchema.plugin(MongooseSoftDelete);
35
+ UserGroupMongoSchema.plugin(mongoosePaginate);
36
36
 
37
37
  const USERGROUP_MODEL_NAME = 'UserGroup';
38
38
  const USERGROUP_COLLECTION_NAME = 'userGroups';
39
39
 
40
- const UserGroupModel = mongoose.model<IUserGroup,PaginateModel<IUserGroup>>(USERGROUP_MODEL_NAME, UserGroupSchema,USERGROUP_COLLECTION_NAME);
40
+ const UserGroupModel = mongoose.model<IUserGroup,PaginateModel<IUserGroup>>(USERGROUP_MODEL_NAME, UserGroupMongoSchema,USERGROUP_COLLECTION_NAME);
41
41
 
42
42
  export {
43
- UserGroupSchema,
43
+ UserGroupMongoSchema,
44
44
  UserGroupModel
45
45
  }
46
46
 
@@ -6,7 +6,7 @@ import {PaginateModel} from "mongoose";
6
6
 
7
7
 
8
8
  // Defining user Mongoose Schema
9
- const UserSchema = new mongoose.Schema<IUser>({
9
+ const UserMongoSchema = new mongoose.Schema<IUser>({
10
10
  username: {
11
11
  type: String,
12
12
  unique: true,
@@ -74,22 +74,24 @@ const UserSchema = new mongoose.Schema<IUser>({
74
74
  emailCode: {type: String, required: false, index: false, select: false},
75
75
  phoneCode: {type: String, required: false, index: false, select: false},
76
76
  recoveryCode: {type: String, required: false, index: false, select: false},
77
- }, {timestamps: true});
77
+ }, {timestamps: true, toJSON: { virtuals: true}, toObject: { virtuals: true} });
78
78
 
79
- UserSchema.set('toJSON', {getters: true});
79
+ UserMongoSchema.virtual("id").get(function () {
80
+ return this._id.toString();
81
+ });
80
82
 
81
- UserSchema.plugin(uniqueValidator, {message: 'validation.unique'});
83
+ UserMongoSchema.plugin(uniqueValidator, {message: 'validation.unique'});
82
84
 
83
- UserSchema.plugin(MongooseSoftDelete);
84
- UserSchema.plugin(mongoosePaginate);
85
+ UserMongoSchema.plugin(MongooseSoftDelete);
86
+ UserMongoSchema.plugin(mongoosePaginate);
85
87
 
86
88
  const USER_MODEL_NAME = 'User';
87
89
  const USER_COLLECTION_NAME = 'users';
88
90
 
89
- const UserModel = mongoose.model<IUser,PaginateModel<IUser>>(USER_MODEL_NAME, UserSchema,USER_COLLECTION_NAME);
91
+ const UserModel = mongoose.models.User as mongoose.Model<IUser> & PaginateModel<IUser> || mongoose.model<IUser,PaginateModel<IUser>>(USER_MODEL_NAME, UserMongoSchema,USER_COLLECTION_NAME);
90
92
 
91
93
  export {
92
- UserSchema,
94
+ UserMongoSchema,
93
95
  UserModel
94
96
  }
95
97
 
package/src/rbac/Rbac.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type {IJwtUser, IRole, IRbac} from "@drax/identity-share";
2
- import {UnauthorizedError} from "@drax/common-back";
2
+ import {UnauthorizedError, ForbiddenError} from "@drax/common-back";
3
3
 
4
4
  class Rbac implements IRbac{
5
5
  private role: IRole;
@@ -26,6 +26,12 @@ class Rbac implements IRbac{
26
26
  return this.authUser.tenantId
27
27
  }
28
28
 
29
+ assertAuthenticated() {
30
+ if (!this.authUser) {
31
+ throw new UnauthorizedError()
32
+ }
33
+ }
34
+
29
35
  hasPermission(requiredPermission: string): boolean {
30
36
  if (!this.authUser || !this.role || !this.role.permissions || this.role.permissions.length === 0) {
31
37
  return false;
@@ -35,27 +41,24 @@ class Rbac implements IRbac{
35
41
  }
36
42
 
37
43
  assertPermission(requiredPermission: string) {
44
+ this.assertAuthenticated()
38
45
  if (!this.hasPermission(requiredPermission)) {
39
- throw new UnauthorizedError()
46
+ throw new ForbiddenError()
40
47
  }
41
48
  }
42
49
 
43
50
  assertOrPermissions(requiredPermissions: string[]) {
44
-
51
+ this.assertAuthenticated()
45
52
  for(let requiredPermission of requiredPermissions){
46
53
  if (this.hasPermission(requiredPermission)) {
47
54
  return true
48
55
  }
49
56
  }
50
57
 
51
- throw new UnauthorizedError()
58
+ throw new ForbiddenError()
52
59
  }
53
60
 
54
- assertAuthenticated() {
55
- if (!this.authUser) {
56
- throw new UnauthorizedError()
57
- }
58
- }
61
+
59
62
 
60
63
  assertUserId(userId: string) {
61
64
  if (this.userId != userId) {
@@ -1,106 +1,34 @@
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 {Cursor, PaginateOptions, PaginateResult} from "mongoose";
5
- import {DeleteResult} from "mongodb";
6
- import {IDraxFindOptions, IDraxPaginateOptions, IDraxPaginateResult} from "@drax/crud-share";
4
+ import {Cursor} from "mongoose";
5
+ import {IDraxFindOptions} from "@drax/crud-share";
7
6
  import {IRoleBase, IRole} from "@drax/identity-share";
7
+ import {AbstractMongoRepository} from "@drax/crud-back";
8
8
 
9
- class RoleMongoRepository implements IRoleRepository {
9
+ class RoleMongoRepository extends AbstractMongoRepository<IRole, IRoleBase,IRoleBase> implements IRoleRepository {
10
10
 
11
- _searchFields = ['name']
11
+ protected _model = RoleModel
12
+ protected _searchFields = ['name']
13
+ protected _populateFields = ['childRoles']
14
+ protected _lean = true
12
15
 
13
- async create(roleData: IRoleBase): Promise<IRole> {
14
- const role: mongoose.HydratedDocument<IRole> = new RoleModel(roleData)
15
- await role.save()
16
- await role.populate('childRoles')
17
- return role
18
- }
19
-
20
- async update(id: string, roleData: IRoleBase): Promise<IRole> {
21
- const role: mongoose.HydratedDocument<IRole> = await RoleModel.findOneAndUpdate({_id: id}, roleData, {new: true}).populate('childRoles').exec()
22
- return role
23
- }
24
-
25
- async delete(id: string): Promise<boolean> {
26
- const result: DeleteResult = await RoleModel.deleteOne({_id: id}).exec()
27
- return result.deletedCount == 1
28
- }
29
-
30
- async findById(id: string): Promise<IRole | null> {
31
- const role: mongoose.HydratedDocument<IRole> | null = await RoleModel.findById(id).populate('childRoles').exec()
32
- return role
16
+ constructor() {
17
+ super();
18
+ this._populateFields = ['childRoles']
33
19
  }
34
20
 
35
21
  async findByName(name: string): Promise<IRole | null> {
36
- const role: mongoose.HydratedDocument<IRole> | null = await RoleModel.findOne({name}).populate('childRoles').exec()
37
- return role
38
- }
39
-
40
- async fetchAll(): Promise<IRole[]> {
41
- const roles: mongoose.HydratedDocument<IRole>[] = await RoleModel.find().populate('childRoles').exec()
42
- return roles
43
- }
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
-
54
- async paginate({
55
- page = 1,
56
- limit = 5,
57
- orderBy = '',
58
- order = false,
59
- search = '',
60
- filters = []
61
- }: IDraxPaginateOptions): Promise<IDraxPaginateResult<IRole>> {
62
- const query = {}
63
-
64
- if (search) {
65
- query['$or'] = this._searchFields.map(field => ({[field]: new RegExp(search.toString(), 'i')}))
66
- }
67
-
68
- MongooseQueryFilter.applyFilters(query, filters)
69
-
70
- const sort = MongooseSort.applySort(orderBy, order)
22
+ const item = await RoleModel
23
+ .findOne({name})
24
+ .populate(this._populateFields)
25
+ .lean(this._lean ? { virtuals: true } : false)
26
+ .exec()
71
27
 
72
- const options = {populate: ['childRoles'], page, limit, sort} as PaginateOptions
73
- const roles: PaginateResult<IRole> = await RoleModel.paginate(query, options)
74
- return {
75
- page: page,
76
- limit: limit,
77
- total: roles.totalDocs,
78
- items: roles.docs
79
- }
28
+ return item as IRole
80
29
  }
81
30
 
82
- async find({
83
- limit = 0,
84
- orderBy = '',
85
- order = false,
86
- search = '',
87
- filters = []
88
- }: IDraxFindOptions): Promise<IRole[]> {
89
-
90
- const query = {}
91
31
 
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
32
 
105
33
  async findCursor({
106
34
  limit = 0,
@@ -109,13 +37,14 @@ class RoleMongoRepository implements IRoleRepository {
109
37
  search = '',
110
38
  filters = []
111
39
  }: IDraxFindOptions): Promise<Cursor> {
112
- console.log("RoleMongoRepository.findCursor called")
113
40
  const query = {}
114
41
 
115
- if (search) {
116
- query['$or'] = [
117
- {name: new RegExp(search, 'i')},
118
- ]
42
+ if(search){
43
+ if(mongoose.Types.ObjectId.isValid(search)) {
44
+ query['_id'] = new mongoose.Types.ObjectId(search)
45
+ }else{
46
+ query['$or'] = this._searchFields.map(field => ({[field]: new RegExp(search.toString(), 'i')}))
47
+ }
119
48
  }
120
49
 
121
50
  MongooseQueryFilter.applyFilters(query, filters)
@@ -1,108 +1,27 @@
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 {Cursor, PaginateOptions, PaginateResult} from "mongoose";
5
- import {DeleteResult} from "mongodb";
6
- import {IDraxFindOptions, IDraxPaginateOptions, IDraxPaginateResult} from "@drax/crud-share";
4
+ import {Cursor} from "mongoose";
5
+ import {IDraxFindOptions} from "@drax/crud-share";
7
6
  import {ITenant, ITenantBase} from "@drax/identity-share";
7
+ import {AbstractMongoRepository} from "@drax/crud-back";
8
8
 
9
- class TenantMongoRepository implements ITenantRepository {
9
+ class TenantMongoRepository extends AbstractMongoRepository<ITenant, ITenantBase, ITenantBase> implements ITenantRepository {
10
10
 
11
- _searchFields = ['_id','name']
11
+ protected _model = TenantModel
12
+ protected _searchFields: string[] = ['name']
13
+ protected _populateFields: string[] = []
14
+ protected _lean: boolean = true
12
15
 
13
- async create(tenantData: ITenantBase): Promise<ITenant> {
14
- const tenant: mongoose.HydratedDocument<ITenant> = new TenantModel(tenantData)
15
- await tenant.save()
16
- return tenant
17
- }
18
-
19
- async update(id: string, tenantData: ITenantBase): Promise<ITenant> {
20
- const tenant: mongoose.HydratedDocument<ITenant> = await TenantModel.findOneAndUpdate({_id: id}, tenantData, {new: true}).exec()
21
- return tenant
22
- }
23
-
24
- async delete(id: string): Promise<boolean> {
25
- const result: DeleteResult = await TenantModel.deleteOne({_id: id}).exec()
26
- return result.deletedCount == 1
27
- }
28
-
29
- async findById(id: string): Promise<ITenant | null> {
30
- const tenant: mongoose.HydratedDocument<ITenant> | null = await TenantModel.findById(id).exec()
31
- return tenant
32
- }
33
16
 
34
17
  async findByName(name: string): Promise<ITenant | null> {
35
- const tenant: mongoose.HydratedDocument<ITenant> | null = await TenantModel.findOne({name}).exec()
36
- return tenant
37
- }
38
-
39
- async fetchAll(): Promise<ITenant[]> {
40
- const tenants: mongoose.HydratedDocument<ITenant>[] = await TenantModel.find().exec()
41
- return tenants
42
- }
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
-
53
- async paginate({
54
- page = 1,
55
- limit = 5,
56
- orderBy = '',
57
- order = false,
58
- search = '',
59
- filters = []
60
- }: IDraxPaginateOptions): Promise<IDraxPaginateResult<ITenant>> {
61
-
62
- const query = {}
63
-
64
- if (search) {
65
- query['$or'] = [
66
- {name: new RegExp(search, 'i')},
67
- ]
68
- }
69
-
70
- MongooseQueryFilter.applyFilters(query, filters)
71
-
72
- const sort = MongooseSort.applySort(orderBy, order)
73
-
74
- const options = {page, limit, sort} as PaginateOptions
75
- const tenants: PaginateResult<ITenant> = await TenantModel.paginate(query, options)
76
- return {
77
- page: page,
78
- limit: limit,
79
- total: tenants.totalDocs,
80
- items: tenants.docs
81
- }
18
+ const tenant = await TenantModel
19
+ .findOne({name})
20
+ .lean(this._lean)
21
+ .exec()
22
+ return tenant as ITenant
82
23
  }
83
24
 
84
- async find({
85
- limit = 0,
86
- orderBy = '',
87
- order = false,
88
- search = '',
89
- filters = []
90
- }: IDraxFindOptions): Promise<ITenant[]> {
91
-
92
- const query = {}
93
-
94
- if (search) {
95
- query['$or'] = [
96
- {name: new RegExp(search, 'i')},
97
- ]
98
- }
99
-
100
- MongooseQueryFilter.applyFilters(query, filters)
101
-
102
- const sort = MongooseSort.applySort(orderBy, order)
103
- const items: ITenant[] = await TenantModel.find(query).limit(limit).sort(sort)
104
- return items
105
- }
106
25
 
107
26
  async findCursor({
108
27
  limit = 0,
@@ -114,10 +33,12 @@ class TenantMongoRepository implements ITenantRepository {
114
33
  console.log("TenantMongoRepository.findCursor called")
115
34
  const query = {}
116
35
 
117
- if (search) {
118
- query['$or'] = [
119
- {name: new RegExp(search, 'i')},
120
- ]
36
+ if(search){
37
+ if(mongoose.Types.ObjectId.isValid(search)) {
38
+ query['_id'] = new mongoose.Types.ObjectId(search)
39
+ }else{
40
+ query['$or'] = this._searchFields.map(field => ({[field]: new RegExp(search.toString(), 'i')}))
41
+ }
121
42
  }
122
43
 
123
44
  MongooseQueryFilter.applyFilters(query, filters)
@@ -1,78 +1,48 @@
1
1
  import {UserApiKeyModel} from "../../models/UserApiKeyModel.js";
2
2
  import {
3
3
  mongoose,
4
- MongooseErrorToValidationError, MongooseQueryFilter, MongooseSort,
5
- MongoServerErrorToValidationError,
6
- ValidationError
4
+ MongooseQueryFilter,
5
+ MongooseSort,
7
6
  } from "@drax/common-back"
8
7
  import type {IUserApiKey, IUserApiKeyBase, IUserApiKeySoftDelete} from "@drax/identity-share";
9
- import {DeleteResult, MongoServerError} from "mongodb";
10
8
  import {PaginateResult} from "mongoose";
11
9
  import {IDraxPaginateOptions, IDraxPaginateResult} from "@drax/crud-share";
12
10
  import {IUserApiKeyRepository} from "../../interfaces/IUserApiKeyRepository";
11
+ import {AbstractMongoRepository} from "@drax/crud-back";
13
12
 
14
- class UserApiKeyMongoRepository implements IUserApiKeyRepository {
13
+ class UserApiKeyMongoRepository extends AbstractMongoRepository<IUserApiKey,IUserApiKeyBase,IUserApiKeyBase> implements IUserApiKeyRepository {
15
14
 
15
+ protected _searchFields=['name']
16
+ protected _populateFields = [
17
+ {path: 'user', populate: {path: 'tenant role'} },
18
+ {path: 'createdBy', populate: {path: 'tenant role'} },
19
+ ]
20
+ protected _model = UserApiKeyModel
21
+ protected _lean = false
16
22
 
17
- constructor() {
18
- }
19
-
20
- async create(data: IUserApiKeyBase): Promise<IUserApiKey> {
21
- try {
22
-
23
- const userApiKey: mongoose.HydratedDocument<IUserApiKey> = new UserApiKeyModel(data)
24
- await userApiKey.save()
25
- await userApiKey.populate([
26
- {path: 'user', populate: {path: 'tenant role'} },
27
- {path: 'createdBy', populate: {path: 'tenant role'} },
28
- ])
29
- return userApiKey
30
- } catch (e) {
31
- if (e instanceof mongoose.Error.ValidationError) {
32
- throw MongooseErrorToValidationError(e)
33
- }
34
- throw e
35
- }
36
-
37
- }
38
-
39
- async update(id: string, data: IUserApiKeyBase): Promise<IUserApiKey> {
40
- try {
41
- delete data.secret
42
- const userApiKey: mongoose.HydratedDocument<IUserApiKey> = await UserApiKeyModel.findOneAndUpdate({_id: id}, data, {new: true}).populate({path: 'user', populate: {path: 'tenant role'} }).exec()
43
- return userApiKey
44
- } catch (e) {
45
- if (e instanceof mongoose.Error.ValidationError) {
46
- throw MongooseErrorToValidationError(e)
47
- }
48
- if (e instanceof MongoServerError || e.name === 'MongoServerError') {
49
- throw MongoServerErrorToValidationError(e)
50
- }
51
- throw e
52
- }
53
- }
54
23
 
55
24
  async delete(id: string): Promise<boolean> {
56
- const userApiKey: mongoose.HydratedDocument<IUserApiKeySoftDelete> = await UserApiKeyModel.findById(id)
25
+ const userApiKey: mongoose.HydratedDocument<IUserApiKeySoftDelete> = await UserApiKeyModel
26
+ .findById(id)
57
27
  userApiKey.softDelete()
58
28
  return true
59
29
  }
60
30
 
61
- async findById(id: string): Promise<IUserApiKey> {
62
- const userApiKey: mongoose.HydratedDocument<IUserApiKey> = await UserApiKeyModel.findById(id).populate({path: 'user', populate: {path: 'tenant role'} }).exec()
63
- return userApiKey
64
- }
31
+
65
32
 
66
33
  async findBySecret(secret: string): Promise<IUserApiKey> {
67
- const userApiKey: mongoose.HydratedDocument<IUserApiKey> = await UserApiKeyModel.findOne({secret: {$eq: secret}, deleted: {$ne: true} }).populate({path: 'user', populate: {path: 'tenant role'}}).exec()
68
- return userApiKey
34
+ const userApiKey = await UserApiKeyModel
35
+ .findOne({secret: {$eq: secret}, deleted: {$ne: true} })
36
+ .populate(this._populateFields)
37
+ .exec()
38
+ return userApiKey as IUserApiKey
69
39
  }
70
40
 
71
41
  async paginate({
72
42
  page = 1,
73
43
  limit = 5,
74
44
  orderBy = '',
75
- order = false,
45
+ order = "asc",
76
46
  search = '',
77
47
  filters = []
78
48
  }: IDraxPaginateOptions): Promise<IDraxPaginateResult<IUserApiKey>> {
@@ -81,17 +51,22 @@ class UserApiKeyMongoRepository implements IUserApiKeyRepository {
81
51
  deleted: false
82
52
  }
83
53
 
84
- if (search) {
85
- query['$or'] = [
86
- {name: new RegExp(search, 'i')},
87
- ]
54
+ if(search){
55
+ if(mongoose.Types.ObjectId.isValid(search)) {
56
+ query['_id'] = new mongoose.Types.ObjectId(search)
57
+ }else{
58
+ query['$or'] = this._searchFields.map(field => ({[field]: new RegExp(search.toString(), 'i')}))
59
+ }
88
60
  }
89
61
 
90
62
  MongooseQueryFilter.applyFilters(query, filters)
91
63
 
92
64
  const sort = MongooseSort.applySort(orderBy, order)
93
-
94
- const options = {populate: ['user', 'user.tenant', 'user.role', 'createdBy'], page, limit, sort}
65
+ const populate = ['user', 'user.tenant', 'user.role', 'createdBy']
66
+ const lean = this._lean
67
+ const leanWithId = this._lean
68
+ const leanWithVirtuals = this._lean
69
+ const options = {populate , page, limit, sort, lean, leanWithId, leanWithVirtuals}
95
70
 
96
71
  const userApiKeyPaginated: PaginateResult<IUserApiKey> = await UserApiKeyModel.paginate(query, options)
97
72
  return {