@modular-rest/server 1.6.5 → 1.7.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.
- package/README.md +2 -0
- package/package.json +8 -3
- package/src/application.js +32 -68
- package/src/class/collection_definition.js +32 -24
- package/src/class/combinator.js +82 -71
- package/src/class/database_trigger.js +18 -12
- package/src/class/db_schemas.js +11 -11
- package/src/class/security.js +105 -31
- package/src/class/trigger_operator.js +35 -31
- package/src/index.js +2 -25
- package/src/middlewares.js +14 -1
- package/src/services/data_provider/service.js +173 -177
- package/src/services/user_manager/service.js +139 -30
- package/tsconfig.json +10 -0
|
@@ -8,13 +8,19 @@ class UserManager {
|
|
|
8
8
|
}
|
|
9
9
|
|
|
10
10
|
/**
|
|
11
|
-
*
|
|
12
|
-
* @param {function} method
|
|
11
|
+
* Set a custom method for generating verification codes.
|
|
12
|
+
* @param {function} method - A method that returns a random verification code.
|
|
13
13
|
*/
|
|
14
14
|
setCustomVerificationCodeGeneratorMethod(method) {
|
|
15
15
|
this.verificationCodeGeneratorMethod = method;
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* Generate a verification code.
|
|
20
|
+
* @param {string} id - The ID for which to generate the verification code.
|
|
21
|
+
* @param {string} idType - The type of the ID.
|
|
22
|
+
* @returns {string} The generated verification code.
|
|
23
|
+
*/
|
|
18
24
|
generateVerificationCode(id, idType) {
|
|
19
25
|
if (this.verificationCodeGeneratorMethod)
|
|
20
26
|
return this.verificationCodeGeneratorMethod(id, idType);
|
|
@@ -24,9 +30,10 @@ class UserManager {
|
|
|
24
30
|
}
|
|
25
31
|
|
|
26
32
|
/**
|
|
27
|
-
* Get a user by
|
|
28
|
-
*
|
|
29
|
-
* @
|
|
33
|
+
* Get a user by their ID.
|
|
34
|
+
* @param {string} id - The ID of the user.
|
|
35
|
+
* @returns {Promise<User>} A promise that resolves to the user.
|
|
36
|
+
* @throws {string} If the user is not found.
|
|
30
37
|
*/
|
|
31
38
|
getUserById(id) {
|
|
32
39
|
return new Promise(async (done, reject) => {
|
|
@@ -39,7 +46,43 @@ class UserManager {
|
|
|
39
46
|
.exec()
|
|
40
47
|
.catch(reject);
|
|
41
48
|
|
|
42
|
-
if (!userDoc)
|
|
49
|
+
if (!userDoc) {
|
|
50
|
+
reject("user not found");
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
let user = User.loadFromModel(userDoc);
|
|
55
|
+
done(user);
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Get a user by their identity.
|
|
61
|
+
* @param {string} id - The identity of the user.
|
|
62
|
+
* @param {string} idType - The type of the identity (phone or email).
|
|
63
|
+
* @returns {Promise<User>} A promise that resolves to the user.
|
|
64
|
+
* @throws {string} If the user is not found.
|
|
65
|
+
*/
|
|
66
|
+
getUserByIdentity(id, idType) {
|
|
67
|
+
return new Promise(async (done, reject) => {
|
|
68
|
+
let userModel = DataProvider.getCollection("cms", "auth");
|
|
69
|
+
|
|
70
|
+
let query = {};
|
|
71
|
+
|
|
72
|
+
if (idType == "phone") query["phone"] = id;
|
|
73
|
+
else if (idType == "email") query["email"] = id;
|
|
74
|
+
|
|
75
|
+
let userDoc = await userModel
|
|
76
|
+
.findOne(query)
|
|
77
|
+
.select({ password: 0 })
|
|
78
|
+
.populate("permission")
|
|
79
|
+
.exec()
|
|
80
|
+
.catch(reject);
|
|
81
|
+
|
|
82
|
+
if (!userDoc) {
|
|
83
|
+
reject("user not found");
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
43
86
|
|
|
44
87
|
let user = User.loadFromModel(userDoc);
|
|
45
88
|
done(user);
|
|
@@ -47,9 +90,10 @@ class UserManager {
|
|
|
47
90
|
}
|
|
48
91
|
|
|
49
92
|
/**
|
|
50
|
-
* Get user by token.
|
|
51
|
-
*
|
|
52
|
-
* @
|
|
93
|
+
* Get a user by their token.
|
|
94
|
+
* @param {string} token - The token of the user.
|
|
95
|
+
* @returns {Promise<User>} A promise that resolves to the user.
|
|
96
|
+
* @throws {string} If the user has a wrong permission.
|
|
53
97
|
*/
|
|
54
98
|
getUserByToken(token) {
|
|
55
99
|
return JWT.main.verify(token).then(async (payload) => {
|
|
@@ -67,10 +111,10 @@ class UserManager {
|
|
|
67
111
|
}
|
|
68
112
|
|
|
69
113
|
/**
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
* @param {string}
|
|
73
|
-
* @
|
|
114
|
+
* Check if a verification code is valid.
|
|
115
|
+
* @param {string} id - The ID of the user.
|
|
116
|
+
* @param {string} code - The verification code.
|
|
117
|
+
* @returns {boolean} Whether the verification code is valid.
|
|
74
118
|
*/
|
|
75
119
|
isCodeValid(id, code) {
|
|
76
120
|
let key = false;
|
|
@@ -85,23 +129,24 @@ class UserManager {
|
|
|
85
129
|
}
|
|
86
130
|
|
|
87
131
|
/**
|
|
88
|
-
* Login and return
|
|
89
|
-
*
|
|
90
|
-
* @param {string}
|
|
91
|
-
* @param {string}
|
|
92
|
-
* @
|
|
132
|
+
* Login a user and return their token.
|
|
133
|
+
* @param {string} id - The ID of the user.
|
|
134
|
+
* @param {string} idType - The type of the ID (phone or email).
|
|
135
|
+
* @param {string} password - The password of the user.
|
|
136
|
+
* @returns {Promise<string>} A promise that resolves to the token of the user.
|
|
137
|
+
* @throws {string} If the user is not found.
|
|
93
138
|
*/
|
|
94
139
|
loginUser(id = "", idType = "", password = "") {
|
|
95
140
|
let token;
|
|
96
141
|
|
|
97
142
|
return new Promise(async (done, reject) => {
|
|
98
143
|
// Get user model
|
|
99
|
-
|
|
144
|
+
const userModel = DataProvider.getCollection("cms", "auth");
|
|
100
145
|
|
|
101
146
|
/**
|
|
102
147
|
* Setup query to find by phone or email
|
|
103
148
|
*/
|
|
104
|
-
|
|
149
|
+
const query = {
|
|
105
150
|
password: Buffer.from(password).toString("base64"),
|
|
106
151
|
type: "user",
|
|
107
152
|
};
|
|
@@ -110,7 +155,7 @@ class UserManager {
|
|
|
110
155
|
else if (idType == "email") query["email"] = id;
|
|
111
156
|
|
|
112
157
|
// Get from database
|
|
113
|
-
|
|
158
|
+
const gottenFromDB = await userModel
|
|
114
159
|
.findOne(query)
|
|
115
160
|
.populate("permission")
|
|
116
161
|
.exec()
|
|
@@ -120,11 +165,13 @@ class UserManager {
|
|
|
120
165
|
// Token
|
|
121
166
|
else {
|
|
122
167
|
// Load user
|
|
123
|
-
|
|
168
|
+
const user = await User.loadFromModel(gottenFromDB)
|
|
169
|
+
.then()
|
|
170
|
+
.catch(reject);
|
|
124
171
|
|
|
125
172
|
// Get token payload
|
|
126
173
|
// This is some information about the user.
|
|
127
|
-
|
|
174
|
+
const payload = user.getBrief();
|
|
128
175
|
|
|
129
176
|
// Generate json web token
|
|
130
177
|
token = await JWT.main.sign(payload).then().catch(reject);
|
|
@@ -135,7 +182,40 @@ class UserManager {
|
|
|
135
182
|
}
|
|
136
183
|
|
|
137
184
|
/**
|
|
138
|
-
*
|
|
185
|
+
* Issue a token for a user.
|
|
186
|
+
* @param {string} email - The email of the user.
|
|
187
|
+
* @returns {Promise<string>} A promise that resolves to the token of the user.
|
|
188
|
+
* @throws {string} If the user is not found.
|
|
189
|
+
*/
|
|
190
|
+
issueTokenForUser(email) {
|
|
191
|
+
return new Promise(async (done, reject) => {
|
|
192
|
+
const userModel = DataProvider.getCollection("cms", "auth");
|
|
193
|
+
const query = { email: email };
|
|
194
|
+
|
|
195
|
+
// Get from database
|
|
196
|
+
const gottenFromDB = await userModel
|
|
197
|
+
.findOne(query)
|
|
198
|
+
.populate("permission")
|
|
199
|
+
.exec()
|
|
200
|
+
.catch(reject);
|
|
201
|
+
|
|
202
|
+
if (!gottenFromDB) reject("user not found");
|
|
203
|
+
|
|
204
|
+
const user = await User.loadFromModel(gottenFromDB).then().catch(reject);
|
|
205
|
+
|
|
206
|
+
// Get token payload
|
|
207
|
+
// This is some information about the user.
|
|
208
|
+
const payload = user.getBrief();
|
|
209
|
+
|
|
210
|
+
// Generate json web token
|
|
211
|
+
await JWT.main.sign(payload).then(done).catch(reject);
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Login as an anonymous user.
|
|
217
|
+
* @returns {Promise<string>} A promise that resolves to the token of the anonymous user.
|
|
218
|
+
* @throws {string} If the anonymous user is not found.
|
|
139
219
|
*/
|
|
140
220
|
loginAnonymous() {
|
|
141
221
|
let token;
|
|
@@ -180,16 +260,22 @@ class UserManager {
|
|
|
180
260
|
}
|
|
181
261
|
|
|
182
262
|
/**
|
|
183
|
-
*
|
|
184
|
-
*
|
|
185
|
-
* @param {string}
|
|
186
|
-
* @param {string}
|
|
187
|
-
* @param {string} code code is a string being sent to user and he/she must return it back.
|
|
263
|
+
* Register a temporary ID.
|
|
264
|
+
* @param {string} id - The ID to register.
|
|
265
|
+
* @param {string} type - The type of the ID.
|
|
266
|
+
* @param {string} code - The verification code.
|
|
188
267
|
*/
|
|
189
268
|
registerTemporaryID(id, type, code) {
|
|
190
269
|
this.tempIds[id] = { id: id, type: type, code: code };
|
|
191
270
|
}
|
|
192
271
|
|
|
272
|
+
/**
|
|
273
|
+
* Submit a password for a temporary ID.
|
|
274
|
+
* @param {string} id - The ID.
|
|
275
|
+
* @param {string} password - The password.
|
|
276
|
+
* @param {string} code - The verification code.
|
|
277
|
+
* @returns {Promise<boolean>} A promise that resolves to whether the operation was successful.
|
|
278
|
+
*/
|
|
193
279
|
async submitPasswordForTemporaryID(id, password, code) {
|
|
194
280
|
let key = false;
|
|
195
281
|
|
|
@@ -213,6 +299,13 @@ class UserManager {
|
|
|
213
299
|
return key;
|
|
214
300
|
}
|
|
215
301
|
|
|
302
|
+
/**
|
|
303
|
+
* Change the password for a temporary ID.
|
|
304
|
+
* @param {string} id - The ID.
|
|
305
|
+
* @param {string} password - The new password.
|
|
306
|
+
* @param {string} code - The verification code.
|
|
307
|
+
* @returns {Promise<boolean>} A promise that resolves to whether the operation was successful.
|
|
308
|
+
*/
|
|
216
309
|
async changePasswordForTemporaryID(id, password, code) {
|
|
217
310
|
let key = false;
|
|
218
311
|
|
|
@@ -231,6 +324,12 @@ class UserManager {
|
|
|
231
324
|
return key;
|
|
232
325
|
}
|
|
233
326
|
|
|
327
|
+
/**
|
|
328
|
+
* Register a user.
|
|
329
|
+
* @param {Object} detail - The details of the user.
|
|
330
|
+
* @returns {Promise<string>} A promise that resolves to the ID of the new user.
|
|
331
|
+
* @throws {string} If the user could not be registered.
|
|
332
|
+
*/
|
|
234
333
|
registerUser(detail) {
|
|
235
334
|
return new Promise(async (done, reject) => {
|
|
236
335
|
// get default permission
|
|
@@ -263,13 +362,23 @@ class UserManager {
|
|
|
263
362
|
});
|
|
264
363
|
}
|
|
265
364
|
|
|
365
|
+
/**
|
|
366
|
+
* Change the password of a user.
|
|
367
|
+
* @param {Object} query - The query to find the user.
|
|
368
|
+
* @param {string} newPass - The new password.
|
|
369
|
+
* @returns {Promise<void>} A promise that resolves when the operation is complete.
|
|
370
|
+
*/
|
|
266
371
|
changePassword(query, newPass) {
|
|
267
372
|
let update = { $set: { password: newPass } };
|
|
268
373
|
let authM = DataProvider.getCollection("cms", "auth");
|
|
269
374
|
return authM.updateOne(query, update).exec().then();
|
|
270
375
|
}
|
|
376
|
+
|
|
377
|
+
static get instance() {
|
|
378
|
+
return instance;
|
|
379
|
+
}
|
|
271
380
|
}
|
|
272
381
|
|
|
273
|
-
|
|
382
|
+
const instance = new UserManager();
|
|
274
383
|
module.exports.name = "userManager";
|
|
275
384
|
module.exports.main = UserManager.instance;
|