@modular-rest/server 1.11.12 → 1.12.0

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 (181) hide show
  1. package/.nvmrc +1 -0
  2. package/.prettierrc.json +9 -0
  3. package/.releaserc.json +24 -0
  4. package/README.md +79 -94
  5. package/dist/application.d.ts +29 -0
  6. package/dist/application.js +217 -0
  7. package/dist/class/cms_trigger.d.ts +61 -0
  8. package/dist/class/cms_trigger.js +47 -0
  9. package/dist/class/collection_definition.d.ts +112 -0
  10. package/dist/class/collection_definition.js +87 -0
  11. package/dist/class/combinator.d.ts +43 -0
  12. package/dist/class/combinator.js +174 -0
  13. package/dist/class/database_trigger.d.ts +84 -0
  14. package/dist/class/database_trigger.js +64 -0
  15. package/dist/class/db_schemas.d.ts +25 -0
  16. package/dist/class/db_schemas.js +28 -0
  17. package/dist/class/directory.d.ts +20 -0
  18. package/dist/class/directory.js +87 -0
  19. package/dist/class/paginator.d.ts +31 -0
  20. package/dist/class/paginator.js +43 -0
  21. package/dist/class/reply.d.ts +29 -0
  22. package/dist/class/reply.js +44 -0
  23. package/dist/class/security.d.ts +186 -0
  24. package/dist/class/security.js +178 -0
  25. package/dist/class/trigger_operator.d.ts +92 -0
  26. package/dist/class/trigger_operator.js +99 -0
  27. package/dist/class/user.d.ts +81 -0
  28. package/dist/class/user.js +151 -0
  29. package/dist/class/validator.d.ts +19 -0
  30. package/dist/class/validator.js +101 -0
  31. package/dist/config.d.ts +112 -0
  32. package/dist/config.js +26 -0
  33. package/dist/defult-permissions.d.ts +2 -0
  34. package/dist/defult-permissions.js +31 -0
  35. package/dist/events.d.ts +23 -0
  36. package/dist/events.js +47 -0
  37. package/dist/helper/data_insertion.d.ts +38 -0
  38. package/dist/helper/data_insertion.js +110 -0
  39. package/dist/helper/presetup_services.d.ts +60 -0
  40. package/dist/helper/presetup_services.js +108 -0
  41. package/dist/index.d.ts +118 -0
  42. package/dist/index.js +79 -0
  43. package/dist/middlewares.d.ts +53 -0
  44. package/dist/middlewares.js +106 -0
  45. package/dist/play-test.d.ts +1 -0
  46. package/dist/play-test.js +9 -0
  47. package/dist/services/data_provider/router.d.ts +4 -0
  48. package/dist/services/data_provider/router.js +187 -0
  49. package/dist/services/data_provider/service.d.ts +131 -0
  50. package/dist/services/data_provider/service.js +252 -0
  51. package/dist/services/data_provider/typeCasters.d.ts +9 -0
  52. package/dist/services/data_provider/typeCasters.js +18 -0
  53. package/dist/services/file/db.d.ts +1 -0
  54. package/dist/services/file/db.js +31 -0
  55. package/dist/services/file/router.d.ts +4 -0
  56. package/dist/services/file/router.js +115 -0
  57. package/dist/services/file/service.d.ts +204 -0
  58. package/dist/services/file/service.js +341 -0
  59. package/dist/services/functions/router.d.ts +4 -0
  60. package/dist/services/functions/router.js +67 -0
  61. package/dist/services/functions/service.d.ts +132 -0
  62. package/dist/services/functions/service.js +159 -0
  63. package/dist/services/jwt/router.d.ts +4 -0
  64. package/dist/services/jwt/router.js +99 -0
  65. package/dist/services/jwt/service.d.ts +97 -0
  66. package/dist/services/jwt/service.js +135 -0
  67. package/dist/services/user_manager/db.d.ts +1 -0
  68. package/dist/services/user_manager/db.js +75 -0
  69. package/dist/services/user_manager/permissionManager.d.ts +19 -0
  70. package/dist/services/user_manager/permissionManager.js +42 -0
  71. package/dist/services/user_manager/router.d.ts +4 -0
  72. package/dist/services/user_manager/router.js +195 -0
  73. package/dist/services/user_manager/service.d.ts +317 -0
  74. package/dist/services/user_manager/service.js +628 -0
  75. package/docs/.keep +0 -0
  76. package/docs/system-access-type.md +26 -0
  77. package/package.json +58 -45
  78. package/src/application.ts +206 -0
  79. package/src/class/cms_trigger.ts +68 -0
  80. package/src/class/collection_definition.ts +134 -0
  81. package/src/class/combinator.ts +176 -0
  82. package/src/class/database_trigger.ts +99 -0
  83. package/src/class/db_schemas.ts +44 -0
  84. package/src/class/{directory.js → directory.ts} +40 -18
  85. package/src/class/paginator.ts +51 -0
  86. package/src/class/reply.ts +59 -0
  87. package/src/class/security.ts +250 -0
  88. package/src/class/trigger_operator.ts +142 -0
  89. package/src/class/user.ts +199 -0
  90. package/src/class/validator.ts +123 -0
  91. package/src/config.ts +121 -0
  92. package/src/defult-permissions.ts +31 -0
  93. package/src/events.ts +59 -0
  94. package/src/helper/data_insertion.ts +94 -0
  95. package/src/helper/presetup_services.ts +96 -0
  96. package/src/index.ts +146 -0
  97. package/src/middlewares.ts +75 -0
  98. package/src/play-test.ts +8 -0
  99. package/src/services/data_provider/router.ts +191 -0
  100. package/src/services/data_provider/service.ts +305 -0
  101. package/src/services/data_provider/typeCasters.ts +15 -0
  102. package/src/services/file/db.ts +29 -0
  103. package/src/services/file/router.ts +88 -0
  104. package/src/services/file/service.ts +387 -0
  105. package/src/services/functions/router.ts +34 -0
  106. package/src/services/functions/service.ts +203 -0
  107. package/src/services/jwt/router.ts +73 -0
  108. package/src/services/jwt/service.ts +139 -0
  109. package/src/services/user_manager/db.ts +87 -0
  110. package/src/services/user_manager/permissionManager.ts +49 -0
  111. package/src/services/user_manager/router.ts +193 -0
  112. package/src/services/user_manager/service.ts +698 -0
  113. package/tsconfig.json +16 -9
  114. package/typedoc.mjs +41 -0
  115. package/LICENSE +0 -21
  116. package/package-lock.json +0 -1373
  117. package/src/application.js +0 -239
  118. package/src/class/cms_trigger.js +0 -20
  119. package/src/class/collection_definition.js +0 -33
  120. package/src/class/combinator.js +0 -133
  121. package/src/class/database_trigger.js +0 -20
  122. package/src/class/db_schemas.js +0 -18
  123. package/src/class/paginator.js +0 -31
  124. package/src/class/reply.js +0 -37
  125. package/src/class/security.js +0 -141
  126. package/src/class/trigger_operator.js +0 -39
  127. package/src/class/user.js +0 -112
  128. package/src/class/validator.js +0 -91
  129. package/src/config.js +0 -67
  130. package/src/events.js +0 -15
  131. package/src/helper/data_insertion.js +0 -64
  132. package/src/helper/presetup_services.js +0 -31
  133. package/src/index.js +0 -66
  134. package/src/middlewares.js +0 -44
  135. package/src/services/data_provider/router.js +0 -552
  136. package/src/services/data_provider/service.js +0 -262
  137. package/src/services/data_provider/typeCasters.js +0 -10
  138. package/src/services/file/db.js +0 -29
  139. package/src/services/file/router.js +0 -92
  140. package/src/services/file/service.js +0 -231
  141. package/src/services/functions/router.js +0 -37
  142. package/src/services/functions/service.js +0 -74
  143. package/src/services/jwt/router.js +0 -70
  144. package/src/services/jwt/service.js +0 -37
  145. package/src/services/user_manager/db.js +0 -83
  146. package/src/services/user_manager/permissionManager.js +0 -43
  147. package/src/services/user_manager/router.js +0 -176
  148. package/src/services/user_manager/service.js +0 -377
  149. package/types/application.d.ts +0 -97
  150. package/types/class/cms_trigger.d.ts +0 -24
  151. package/types/class/collection_definition.d.ts +0 -36
  152. package/types/class/combinator.d.ts +0 -30
  153. package/types/class/database_trigger.d.ts +0 -28
  154. package/types/class/db_schemas.d.ts +0 -2
  155. package/types/class/directory.d.ts +0 -2
  156. package/types/class/paginator.d.ts +0 -8
  157. package/types/class/reply.d.ts +0 -8
  158. package/types/class/security.d.ts +0 -109
  159. package/types/class/trigger_operator.d.ts +0 -19
  160. package/types/class/user.d.ts +0 -24
  161. package/types/class/validator.d.ts +0 -9
  162. package/types/config.d.ts +0 -101
  163. package/types/events.d.ts +0 -7
  164. package/types/helper/data_insertion.d.ts +0 -4
  165. package/types/helper/presetup_services.d.ts +0 -5
  166. package/types/index.d.ts +0 -72
  167. package/types/middlewares.d.ts +0 -9
  168. package/types/services/data_provider/router.d.ts +0 -3
  169. package/types/services/data_provider/service.d.ts +0 -40
  170. package/types/services/data_provider/typeCasters.d.ts +0 -3
  171. package/types/services/file/db.d.ts +0 -3
  172. package/types/services/file/router.d.ts +0 -3
  173. package/types/services/file/service.d.ts +0 -81
  174. package/types/services/functions/router.d.ts +0 -3
  175. package/types/services/functions/service.d.ts +0 -23
  176. package/types/services/jwt/router.d.ts +0 -3
  177. package/types/services/jwt/service.d.ts +0 -10
  178. package/types/services/user_manager/db.d.ts +0 -3
  179. package/types/services/user_manager/permissionManager.d.ts +0 -3
  180. package/types/services/user_manager/router.d.ts +0 -3
  181. package/types/services/user_manager/service.d.ts +0 -131
@@ -0,0 +1,628 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.main = exports.name = void 0;
37
+ const user_1 = require("../../class/user");
38
+ const DataProvider = __importStar(require("../data_provider/service"));
39
+ const JWT = __importStar(require("../jwt/service"));
40
+ const permissionManager_1 = require("./permissionManager");
41
+ /**
42
+ * Service name constant
43
+ * @constant {string}
44
+ */
45
+ exports.name = 'userManager';
46
+ /**
47
+ * User manager class for handling user operations
48
+
49
+ * This service provides functionality for managing users, including:
50
+ * - User registration and authentication
51
+ * - Password management
52
+ * - Token generation and verification
53
+ * - Temporary ID handling for password reset and verification
54
+ *
55
+ */
56
+ class UserManager {
57
+ /**
58
+ * @hidden
59
+ */
60
+ constructor() {
61
+ /**
62
+ * @hidden
63
+ */
64
+ this.tempIds = {};
65
+ }
66
+ /**
67
+ * Sets a custom method for generating verification codes
68
+ * @param {Function} generatorMethod - Function that generates verification codes
69
+ * @example
70
+ * ```typescript
71
+ * import { userManager } from '@modular-rest/server';
72
+ *
73
+ * userManager.setCustomVerificationCodeGeneratorMethod((id, type) => {
74
+ * return Math.random().toString(36).substring(2, 8).toUpperCase();
75
+ * });
76
+ * ```
77
+ */
78
+ setCustomVerificationCodeGeneratorMethod(generatorMethod) {
79
+ this.verificationCodeGeneratorMethod = generatorMethod;
80
+ }
81
+ /**
82
+ * Generates a verification code for a user
83
+ * @param {string} id - User ID or identifier
84
+ * @param {string} idType - Type of ID (email, phone)
85
+ * @returns {string} Verification code
86
+ * @example
87
+ * ```typescript
88
+ * import { userManager } from '@modular-rest/server';
89
+ *
90
+ * const code = userManager.generateVerificationCode('user@example.com', 'email');
91
+ * // Returns: '123' (default) or custom generated code
92
+ * ```
93
+ */
94
+ generateVerificationCode(id, idType) {
95
+ if (this.verificationCodeGeneratorMethod)
96
+ return this.verificationCodeGeneratorMethod(id, idType);
97
+ // this is default code
98
+ return '123';
99
+ }
100
+ /**
101
+ * Gets a user by their ID
102
+ * @param {string} id - The ID of the user
103
+ * @returns {Promise<User>} Promise resolving to the user
104
+ * @throws {Error} If user model is not found or user is not found
105
+ * @example
106
+ * ```typescript
107
+ * import { userManager } from '@modular-rest/server';
108
+ *
109
+ * try {
110
+ * const user = await userManager.getUserById('user123');
111
+ * console.log('User details:', user);
112
+ * } catch (error) {
113
+ * console.error('Failed to get user:', error);
114
+ * }
115
+ * ```
116
+ */
117
+ getUserById(id) {
118
+ return new Promise(async (done, reject) => {
119
+ const userModel = DataProvider.getCollection('cms', 'auth');
120
+ if (!userModel) {
121
+ return reject(new Error('User model not found'));
122
+ }
123
+ let userDoc;
124
+ try {
125
+ userDoc = await userModel.findOne({ _id: id }).select({ password: 0 }).exec();
126
+ }
127
+ catch (error) {
128
+ return reject(error);
129
+ }
130
+ if (!userDoc) {
131
+ return reject(new Error('User not found'));
132
+ }
133
+ try {
134
+ const user = await user_1.User.loadFromModel(userDoc);
135
+ done(user);
136
+ }
137
+ catch (error) {
138
+ reject(error);
139
+ }
140
+ });
141
+ }
142
+ /**
143
+ * Gets a user by their identity (email or phone)
144
+ * @param {string} id - The identity of the user
145
+ * @param {string} idType - The type of the identity (phone or email)
146
+ * @returns {Promise<User>} Promise resolving to the user
147
+ * @throws {Error} If user model is not found or user is not found
148
+ * @example
149
+ * ```typescript
150
+ * import { userManager } from '@modular-rest/server';
151
+ *
152
+ * // Get user by email
153
+ * const user = await userManager.getUserByIdentity('user@example.com', 'email');
154
+ *
155
+ * // Get user by phone
156
+ * const user = await userManager.getUserByIdentity('+1234567890', 'phone');
157
+ * ```
158
+ */
159
+ getUserByIdentity(id, idType) {
160
+ return new Promise(async (done, reject) => {
161
+ const userModel = DataProvider.getCollection('cms', 'auth');
162
+ if (!userModel) {
163
+ return reject(new Error('User model not found'));
164
+ }
165
+ const query = {};
166
+ if (idType === 'phone')
167
+ query['phone'] = id;
168
+ else if (idType === 'email')
169
+ query['email'] = id;
170
+ let userDoc;
171
+ try {
172
+ userDoc = await userModel.findOne(query).select({ password: 0 }).exec();
173
+ }
174
+ catch (error) {
175
+ return reject(error);
176
+ }
177
+ if (!userDoc) {
178
+ return reject(new Error('User not found'));
179
+ }
180
+ try {
181
+ const user = await user_1.User.loadFromModel(userDoc);
182
+ done(user);
183
+ }
184
+ catch (error) {
185
+ reject(error);
186
+ }
187
+ });
188
+ }
189
+ /**
190
+ * Gets a user by their JWT token
191
+ * @param {string} token - The JWT token of the user
192
+ * @returns {Promise<User>} Promise resolving to the user
193
+ * @throws {Error} If token is invalid or user is not found
194
+ * @example
195
+ * ```typescript
196
+ * import { userManager } from '@modular-rest/server';
197
+ *
198
+ * try {
199
+ * const user = await userManager.getUserByToken('jwt.token.here');
200
+ * console.log('Authenticated user:', user);
201
+ * } catch (error) {
202
+ * console.error('Invalid token:', error);
203
+ * }
204
+ * ```
205
+ */
206
+ async getUserByToken(token) {
207
+ const decoded = await JWT.main.verify(token);
208
+ return this.getUserById(decoded.id);
209
+ }
210
+ /**
211
+ * Checks if a verification code is valid
212
+ * @param {string} id - The ID of the user
213
+ * @param {string} code - The verification code
214
+ * @returns {boolean} Whether the verification code is valid
215
+ * @example
216
+ * ```typescript
217
+ * import { userManager } from '@modular-rest/server';
218
+ *
219
+ * const isValid = userManager.isCodeValid('user123', '123');
220
+ * if (isValid) {
221
+ * // Proceed with verification
222
+ * }
223
+ * ```
224
+ */
225
+ isCodeValid(id, code) {
226
+ return this.tempIds.hasOwnProperty(id) && this.tempIds[id].code.toString() === code.toString();
227
+ }
228
+ /**
229
+ * Logs in a user and returns their JWT token
230
+ * @param {string} [id=''] - The ID of the user (email or phone)
231
+ * @param {string} [idType=''] - The type of the ID (phone or email)
232
+ * @param {string} [password=''] - The password of the user
233
+ * @returns {Promise<string>} Promise resolving to the JWT token
234
+ * @throws {Error} If user is not found or credentials are invalid
235
+ * @example
236
+ * ```typescript
237
+ * import { userManager } from '@modular-rest/server';
238
+ *
239
+ * try {
240
+ * // Login with email
241
+ * const token = await userManager.loginUser('user@example.com', 'email', 'password123');
242
+ *
243
+ * // Login with phone
244
+ * const token = await userManager.loginUser('+1234567890', 'phone', 'password123');
245
+ * } catch (error) {
246
+ * console.error('Login failed:', error);
247
+ * }
248
+ * ```
249
+ */
250
+ loginUser(id = '', idType = '', password = '') {
251
+ return new Promise(async (done, reject) => {
252
+ // Get user model
253
+ const userModel = DataProvider.getCollection('cms', 'auth');
254
+ if (!userModel) {
255
+ return reject(new Error('User model not found'));
256
+ }
257
+ /**
258
+ * Setup query to find by phone or email
259
+ */
260
+ const query = {
261
+ password: Buffer.from(password).toString('base64'),
262
+ type: 'user',
263
+ };
264
+ if (idType === 'phone')
265
+ query['phone'] = id;
266
+ else if (idType === 'email')
267
+ query['email'] = id;
268
+ // Get from database
269
+ let gottenFromDB;
270
+ try {
271
+ gottenFromDB = await userModel.findOne(query).exec();
272
+ }
273
+ catch (error) {
274
+ return reject(error);
275
+ }
276
+ if (!gottenFromDB) {
277
+ return reject(new Error('User not found'));
278
+ }
279
+ try {
280
+ // Load user
281
+ const user = await user_1.User.loadFromModel(gottenFromDB);
282
+ // Get token payload
283
+ // This is some information about the user.
284
+ const payload = user.getBrief();
285
+ // Generate json web token
286
+ const token = await JWT.main.sign(payload);
287
+ done(token);
288
+ }
289
+ catch (error) {
290
+ reject(error);
291
+ }
292
+ });
293
+ }
294
+ /**
295
+ * Issues a JWT token for a user by email
296
+ * @param {string} email - The email of the user
297
+ * @returns {Promise<string>} Promise resolving to the JWT token
298
+ * @throws {Error} If user is not found
299
+ * @example
300
+ * ```typescript
301
+ * import { userManager } from '@modular-rest/server';
302
+ *
303
+ * try {
304
+ * const token = await userManager.issueTokenForUser('user@example.com');
305
+ * console.log('Issued token:', token);
306
+ * } catch (error) {
307
+ * console.error('Failed to issue token:', error);
308
+ * }
309
+ * ```
310
+ */
311
+ issueTokenForUser(email) {
312
+ return new Promise(async (done, reject) => {
313
+ const userModel = DataProvider.getCollection('cms', 'auth');
314
+ if (!userModel) {
315
+ return reject(new Error('User model not found'));
316
+ }
317
+ const query = { email: email };
318
+ // Get from database
319
+ let gottenFromDB;
320
+ try {
321
+ gottenFromDB = await userModel.findOne(query).exec();
322
+ }
323
+ catch (error) {
324
+ return reject(error);
325
+ }
326
+ if (!gottenFromDB) {
327
+ return reject(new Error('User not found'));
328
+ }
329
+ try {
330
+ // Load user
331
+ const user = await user_1.User.loadFromModel(gottenFromDB);
332
+ // Get token payload
333
+ const payload = user.getBrief();
334
+ // Generate json web token
335
+ const token = await JWT.main.sign(payload);
336
+ done(token);
337
+ }
338
+ catch (error) {
339
+ reject(error);
340
+ }
341
+ });
342
+ }
343
+ /**
344
+ * Logs in an anonymous user and returns their JWT token
345
+ * @returns {Promise<string>} Promise resolving to the JWT token
346
+ * @example
347
+ * ```typescript
348
+ * import { userManager } from '@modular-rest/server';
349
+ *
350
+ * const token = await userManager.loginAnonymous();
351
+ * console.log('Anonymous token:', token);
352
+ * ```
353
+ */
354
+ loginAnonymous() {
355
+ return new Promise(async (done, reject) => {
356
+ const userModel = DataProvider.getCollection('cms', 'auth');
357
+ if (!userModel) {
358
+ return reject(new Error('User model not found'));
359
+ }
360
+ try {
361
+ // Create anonymous user document
362
+ const anonymousUserDoc = await userModel.create({
363
+ type: 'anonymous',
364
+ permissionGroup: (0, permissionManager_1.getDefaultAnonymousPermissionGroup)().title,
365
+ phone: '',
366
+ email: '',
367
+ password: '',
368
+ });
369
+ // Load user from document
370
+ const user = await user_1.User.loadFromModel(anonymousUserDoc);
371
+ // Get token payload
372
+ const payload = user.getBrief();
373
+ // Generate json web token
374
+ const token = await JWT.main.sign(payload);
375
+ done(token);
376
+ }
377
+ catch (error) {
378
+ reject(error);
379
+ }
380
+ });
381
+ }
382
+ /**
383
+ * Registers a temporary ID for verification or password reset
384
+ * @param {string} id - The ID to register
385
+ * @param {string} type - The type of temporary ID
386
+ * @param {string} code - The verification code
387
+ * @returns {string} The registered ID
388
+ * @example
389
+ * ```typescript
390
+ * import { userManager } from '@modular-rest/server';
391
+ *
392
+ * const tempId = userManager.registerTemporaryID('user@example.com', 'password_reset', '123456');
393
+ * ```
394
+ */
395
+ registerTemporaryID(id, type, code) {
396
+ this.tempIds[id] = {
397
+ type,
398
+ code,
399
+ timestamp: Date.now(),
400
+ };
401
+ return id;
402
+ }
403
+ /**
404
+ * Submits a password for a temporary ID
405
+ * @param {string} id - The temporary ID
406
+ * @param {string} password - The new password
407
+ * @param {string} code - The verification code
408
+ * @returns {Promise<string>} Promise resolving to the JWT token
409
+ * @throws {Error} If verification code is invalid or user is not found
410
+ * @example
411
+ * ```typescript
412
+ * import { userManager } from '@modular-rest/server';
413
+ *
414
+ * try {
415
+ * const token = await userManager.submitPasswordForTemporaryID(
416
+ * 'user@example.com',
417
+ * 'newpassword123',
418
+ * '123456'
419
+ * );
420
+ * console.log('Password set successfully');
421
+ * } catch (error) {
422
+ * console.error('Failed to set password:', error);
423
+ * }
424
+ * ```
425
+ */
426
+ async submitPasswordForTemporaryID(id, password, code) {
427
+ if (!this.isCodeValid(id, code)) {
428
+ throw new Error('Invalid verification code');
429
+ }
430
+ const userModel = DataProvider.getCollection('cms', 'auth');
431
+ if (!userModel) {
432
+ throw new Error('User model not found');
433
+ }
434
+ const query = { email: id };
435
+ // Get from database
436
+ let gottenFromDB;
437
+ try {
438
+ gottenFromDB = await userModel.findOne(query).exec();
439
+ }
440
+ catch (error) {
441
+ throw error;
442
+ }
443
+ if (!gottenFromDB) {
444
+ throw new Error('User not found');
445
+ }
446
+ try {
447
+ // Load user
448
+ const user = await user_1.User.loadFromModel(gottenFromDB);
449
+ // Update password
450
+ user.password = Buffer.from(password).toString('base64');
451
+ // Save to database
452
+ await user.save();
453
+ // Get token payload
454
+ const payload = user.getBrief();
455
+ // Generate json web token
456
+ const token = await JWT.main.sign(payload);
457
+ // Remove temporary ID
458
+ delete this.tempIds[id];
459
+ return token;
460
+ }
461
+ catch (error) {
462
+ throw error;
463
+ }
464
+ }
465
+ /**
466
+ * Changes password for a temporary ID
467
+ * @param {string} id - The temporary ID
468
+ * @param {string} password - The new password
469
+ * @param {string} code - The verification code
470
+ * @returns {Promise<string>} Promise resolving to the JWT token
471
+ * @throws {Error} If verification code is invalid or user is not found
472
+ * @example
473
+ * ```typescript
474
+ * import { userManager } from '@modular-rest/server';
475
+ *
476
+ * try {
477
+ * const token = await userManager.changePasswordForTemporaryID(
478
+ * 'user@example.com',
479
+ * 'newpassword123',
480
+ * '123456'
481
+ * );
482
+ * console.log('Password changed successfully');
483
+ * } catch (error) {
484
+ * console.error('Failed to change password:', error);
485
+ * }
486
+ * ```
487
+ */
488
+ async changePasswordForTemporaryID(id, password, code) {
489
+ if (!this.isCodeValid(id, code)) {
490
+ throw new Error('Invalid verification code');
491
+ }
492
+ const userModel = DataProvider.getCollection('cms', 'auth');
493
+ if (!userModel) {
494
+ throw new Error('User model not found');
495
+ }
496
+ const query = { email: id };
497
+ // Get from database
498
+ let gottenFromDB;
499
+ try {
500
+ gottenFromDB = await userModel.findOne(query).exec();
501
+ }
502
+ catch (error) {
503
+ throw error;
504
+ }
505
+ if (!gottenFromDB) {
506
+ throw new Error('User not found');
507
+ }
508
+ try {
509
+ // Load user
510
+ const user = await user_1.User.loadFromModel(gottenFromDB);
511
+ // Update password
512
+ user.password = Buffer.from(password).toString('base64');
513
+ // Save to database
514
+ await user.save();
515
+ // Get token payload
516
+ const payload = user.getBrief();
517
+ // Generate json web token
518
+ const token = await JWT.main.sign(payload);
519
+ // Remove temporary ID
520
+ delete this.tempIds[id];
521
+ return token;
522
+ }
523
+ catch (error) {
524
+ throw error;
525
+ }
526
+ }
527
+ /**
528
+ * Registers a new user
529
+ * @param {UserRegistrationDetail} detail - User registration details
530
+ * @returns {Promise<string>} Promise resolving to the JWT token
531
+ * @throws {Error} If user model is not found or registration fails
532
+ * @example
533
+ * ```typescript
534
+ * import { userManager } from '@modular-rest/server';
535
+ *
536
+ * try {
537
+ * const token = await userManager.registerUser({
538
+ * email: 'user@example.com',
539
+ * password: 'secure123',
540
+ * permissionGroup: 'user',
541
+ * phone: '+1234567890'
542
+ * });
543
+ * console.log('User registered successfully');
544
+ * } catch (error) {
545
+ * console.error('Registration failed:', error);
546
+ * }
547
+ * ```
548
+ */
549
+ registerUser(detail) {
550
+ return new Promise(async (done, reject) => {
551
+ const userModel = DataProvider.getCollection('cms', 'auth');
552
+ if (!userModel) {
553
+ return reject(new Error('User model not found'));
554
+ }
555
+ try {
556
+ // Create user document
557
+ const userDoc = await userModel.create({
558
+ ...detail,
559
+ type: detail.type || 'user',
560
+ permissionGroup: detail.permissionGroup || (0, permissionManager_1.getDefaultPermissionGroups)().title,
561
+ phone: detail.phone || '',
562
+ email: detail.email || '',
563
+ password: detail.password ? Buffer.from(detail.password).toString('base64') : '',
564
+ });
565
+ // Load user from document
566
+ const user = await user_1.User.loadFromModel(userDoc);
567
+ // Get token payload
568
+ const payload = user.getBrief();
569
+ // Generate json web token
570
+ const token = await JWT.main.sign(payload);
571
+ done(token);
572
+ }
573
+ catch (error) {
574
+ reject(error);
575
+ }
576
+ });
577
+ }
578
+ /**
579
+ * Changes a user's password
580
+ * @param {Record<string, any>} query - Query to find the user
581
+ * @param {string} newPass - The new password
582
+ * @returns {Promise<void>} Promise resolving when password is changed
583
+ * @throws {Error} If user is not found or password change fails
584
+ * @example
585
+ * ```typescript
586
+ * import { userManager } from '@modular-rest/server';
587
+ *
588
+ * try {
589
+ * await userManager.changePassword(
590
+ * { email: 'user@example.com' },
591
+ * 'newpassword123'
592
+ * );
593
+ * console.log('Password changed successfully');
594
+ * } catch (error) {
595
+ * console.error('Failed to change password:', error);
596
+ * }
597
+ * ```
598
+ */
599
+ async changePassword(query, newPass) {
600
+ const userModel = DataProvider.getCollection('cms', 'auth');
601
+ if (!userModel) {
602
+ throw new Error('User model not found');
603
+ }
604
+ const userDoc = await userModel.findOne(query).exec();
605
+ if (!userDoc) {
606
+ throw new Error('User not found');
607
+ }
608
+ const user = await user_1.User.loadFromModel(userDoc);
609
+ user.password = Buffer.from(newPass).toString('base64');
610
+ await user.save();
611
+ }
612
+ /**
613
+ * Gets the singleton instance of UserManager
614
+ * @returns {UserManager} The UserManager instance
615
+ * @hidden
616
+ */
617
+ static get instance() {
618
+ if (!this._instance) {
619
+ this._instance = new UserManager();
620
+ }
621
+ return this._instance;
622
+ }
623
+ }
624
+ /**
625
+ * Main user manager instance
626
+ * @constant {UserManager}
627
+ */
628
+ exports.main = UserManager.instance;
package/docs/.keep ADDED
File without changes
@@ -0,0 +1,26 @@
1
+ ### System Access Types
2
+
3
+ The permission system works by matching the user's assigned permission types (stored in their permission group) against the required permissions defined on collections and functions. This allows for flexible and granular access control.
4
+
5
+ For example, a collection could be configured to:
6
+
7
+ - Allow read access for users with 'user_access' permission
8
+ - Restrict write operations to users with 'advanced_settings' permission
9
+ - Only let users who has the 'upload_file_access' access type in their permission group to upload new files
10
+
11
+ **Built-in Permission Types**
12
+
13
+ The system comes with the following pre-defined permission types:
14
+
15
+ | Permission Type | Description |
16
+ | -------------------- | -------------------------------------------------------------------------------------------------------------------------------------- |
17
+ | `god_access` | Grants unrestricted super admin access. Users with this permission can perform any operation and access all collections and functions. |
18
+ | `user_access` | Basic user-level access for authenticated users. Typically grants read access to most collections and basic functionality. |
19
+ | `upload_file_access` | Required specifically for file upload operations. Users need this permission to upload files to the system. |
20
+ | `remove_file_access` | Required for file deletion operations. Users need this permission to remove files from the system. |
21
+ | `anonymous_access` | Used for unauthenticated access. Defines what operations are available to users who are not logged in. |
22
+ | `advanced_settings` | Grants access to advanced system configuration and administrative features. More privileged than basic user access. |
23
+
24
+ These permission types can be combined in permission groups to create different access levels. For example, an admin user might have both `advanced_settings` and `user_access`, while a basic user would only have `user_access`.
25
+
26
+ You can also define custom permission types for specific needs in your application. The permission system is flexible enough to accommodate any additional access types you require.