@steedos/accounts 3.0.13-beta.8 → 3.0.13
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/lib/core/index.js +177 -249
- package/lib/core/index.js.map +1 -1
- package/lib/database-mongo/index.js +1 -1
- package/lib/database-mongo/index.js.map +1 -1
- package/lib/database-mongo/mongo.js +760 -1278
- package/lib/database-mongo/mongo.js.map +1 -1
- package/lib/db.js +2 -2
- package/lib/db.js.map +1 -1
- package/lib/index.js +107 -143
- package/lib/index.js.map +1 -1
- package/lib/mail.js +36 -47
- package/lib/mail.js.map +1 -1
- package/lib/password/accounts-password.js +505 -763
- package/lib/password/accounts-password.js.map +1 -1
- package/lib/password/index.js +2 -2
- package/lib/password/index.js.map +1 -1
- package/lib/password/utils/encryption.js +10 -22
- package/lib/password/utils/encryption.js.map +1 -1
- package/lib/password/utils/is-email.js +2 -2
- package/lib/password/utils/is-email.js.map +1 -1
- package/lib/password/utils/user.js +3 -3
- package/lib/password/utils/user.js.map +1 -1
- package/lib/rest-express/endpoints/authorize.js +47 -54
- package/lib/rest-express/endpoints/authorize.js.map +1 -1
- package/lib/rest-express/endpoints/geetestV3/geetest-init.js +85 -131
- package/lib/rest-express/endpoints/geetestV3/geetest-init.js.map +1 -1
- package/lib/rest-express/endpoints/geetestV3/sdk/geetest_config.js +1 -1
- package/lib/rest-express/endpoints/geetestV3/sdk/geetest_config.js.map +1 -1
- package/lib/rest-express/endpoints/geetestV3/sdk/geetest_lib.js +112 -160
- package/lib/rest-express/endpoints/geetestV3/sdk/geetest_lib.js.map +1 -1
- package/lib/rest-express/endpoints/geetestV3/sdk/geetest_lib_result.js +8 -9
- package/lib/rest-express/endpoints/geetestV3/sdk/geetest_lib_result.js.map +1 -1
- package/lib/rest-express/endpoints/get-user.js +7 -12
- package/lib/rest-express/endpoints/get-user.js.map +1 -1
- package/lib/rest-express/endpoints/impersonate.js +14 -27
- package/lib/rest-express/endpoints/impersonate.js.map +1 -1
- package/lib/rest-express/endpoints/initServer.js +9 -13
- package/lib/rest-express/endpoints/initServer.js.map +1 -1
- package/lib/rest-express/endpoints/login.js +82 -104
- package/lib/rest-express/endpoints/login.js.map +1 -1
- package/lib/rest-express/endpoints/logout.js +68 -86
- package/lib/rest-express/endpoints/logout.js.map +1 -1
- package/lib/rest-express/endpoints/oauth/provider-callback.js +29 -37
- package/lib/rest-express/endpoints/oauth/provider-callback.js.map +1 -1
- package/lib/rest-express/endpoints/password/change-password.js +73 -102
- package/lib/rest-express/endpoints/password/change-password.js.map +1 -1
- package/lib/rest-express/endpoints/password/register.js +63 -95
- package/lib/rest-express/endpoints/password/register.js.map +1 -1
- package/lib/rest-express/endpoints/password/reset.js +27 -48
- package/lib/rest-express/endpoints/password/reset.js.map +1 -1
- package/lib/rest-express/endpoints/password/setSpaceUserPassword.js +112 -144
- package/lib/rest-express/endpoints/password/setSpaceUserPassword.js.map +1 -1
- package/lib/rest-express/endpoints/password/two-factor.js +39 -70
- package/lib/rest-express/endpoints/password/two-factor.js.map +1 -1
- package/lib/rest-express/endpoints/password/verify-email.js +34 -65
- package/lib/rest-express/endpoints/password/verify-email.js.map +1 -1
- package/lib/rest-express/endpoints/password/verify.js +45 -74
- package/lib/rest-express/endpoints/password/verify.js.map +1 -1
- package/lib/rest-express/endpoints/put-user-name.js +19 -32
- package/lib/rest-express/endpoints/put-user-name.js.map +1 -1
- package/lib/rest-express/endpoints/refresh-access-token.js +14 -27
- package/lib/rest-express/endpoints/refresh-access-token.js.map +1 -1
- package/lib/rest-express/endpoints/service-authenticate.js +55 -79
- package/lib/rest-express/endpoints/service-authenticate.js.map +1 -1
- package/lib/rest-express/endpoints/spaces.js +13 -30
- package/lib/rest-express/endpoints/spaces.js.map +1 -1
- package/lib/rest-express/endpoints/steedos/accept_invitation.js +28 -42
- package/lib/rest-express/endpoints/steedos/accept_invitation.js.map +1 -1
- package/lib/rest-express/endpoints/steedos/create-tenant.js +81 -102
- package/lib/rest-express/endpoints/steedos/create-tenant.js.map +1 -1
- package/lib/rest-express/endpoints/steedos/decline_invitation.js +28 -42
- package/lib/rest-express/endpoints/steedos/decline_invitation.js.map +1 -1
- package/lib/rest-express/endpoints/steedos/get-tenant.js +54 -67
- package/lib/rest-express/endpoints/steedos/get-tenant.js.map +1 -1
- package/lib/rest-express/endpoints/steedos/settings.js +110 -127
- package/lib/rest-express/endpoints/steedos/settings.js.map +1 -1
- package/lib/rest-express/endpoints/update-session.js +33 -48
- package/lib/rest-express/endpoints/update-session.js.map +1 -1
- package/lib/rest-express/express-middleware.js +57 -59
- package/lib/rest-express/express-middleware.js.map +1 -1
- package/lib/rest-express/index.js +1 -1
- package/lib/rest-express/index.js.map +1 -1
- package/lib/rest-express/user-loader.js +67 -86
- package/lib/rest-express/user-loader.js.map +1 -1
- package/lib/rest-express/utils/get-user-agent.js +2 -2
- package/lib/rest-express/utils/get-user-agent.js.map +1 -1
- package/lib/rest-express/utils/getClientIp.js +3 -3
- package/lib/rest-express/utils/getClientIp.js.map +1 -1
- package/lib/rest-express/utils/send-error.js +5 -7
- package/lib/rest-express/utils/send-error.js.map +1 -1
- package/lib/rest-express/utils/steedos-auth.js +21 -21
- package/lib/rest-express/utils/steedos-auth.js.map +1 -1
- package/lib/rest-express/utils/users.js +17 -30
- package/lib/rest-express/utils/users.js.map +1 -1
- package/lib/server/accounts-server.js +413 -574
- package/lib/server/accounts-server.js.map +1 -1
- package/lib/server/index.js +1 -1
- package/lib/server/index.js.map +1 -1
- package/lib/server/utils/email.js +19 -35
- package/lib/server/utils/email.js.map +1 -1
- package/lib/server/utils/get-first-user-email.js +3 -3
- package/lib/server/utils/get-first-user-email.js.map +1 -1
- package/lib/server/utils/tokens.js +14 -24
- package/lib/server/utils/tokens.js.map +1 -1
- package/lib/types/index.js +1 -1
- package/package.json +5 -5
- package/src/database-mongo/mongo.ts +6 -2
- package/src/password/accounts-password.ts +61 -0
- package/src/rest-express/endpoints/password/change-password.ts +1 -1
- package/src/rest-express/endpoints/password/setSpaceUserPassword.ts +5 -0
- package/src/rest-express/endpoints/service-authenticate.ts +2 -5
- package/src/types/types/user.ts +2 -0
|
@@ -1,20 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Mongo = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
var toMongoID = function (objectId) {
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
5
|
+
const mongodb_1 = require("mongodb");
|
|
6
|
+
const auth_1 = require("@steedos/auth");
|
|
7
|
+
const lodash_2 = require("lodash");
|
|
8
|
+
const objectql_1 = require("@steedos/objectql");
|
|
9
|
+
const moment = require("moment");
|
|
10
|
+
const toMongoID = (objectId) => {
|
|
12
11
|
if (typeof objectId === "string") {
|
|
13
12
|
return new mongodb_1.ObjectId(objectId);
|
|
14
13
|
}
|
|
15
14
|
return objectId;
|
|
16
15
|
};
|
|
17
|
-
|
|
16
|
+
const defaultOptions = {
|
|
18
17
|
collectionName: "users",
|
|
19
18
|
sessionCollectionName: "sessions",
|
|
20
19
|
codeCollectionName: "users_verify_code",
|
|
@@ -27,11 +26,18 @@ var defaultOptions = {
|
|
|
27
26
|
convertUserIdToMongoObjectId: false,
|
|
28
27
|
convertSessionIdToMongoObjectId: false,
|
|
29
28
|
caseSensitiveUserName: true,
|
|
30
|
-
dateProvider:
|
|
29
|
+
dateProvider: (date) => (date ? date.getTime() : Date.now()),
|
|
31
30
|
};
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
31
|
+
class Mongo {
|
|
32
|
+
options;
|
|
33
|
+
db;
|
|
34
|
+
collection;
|
|
35
|
+
sessionCollection;
|
|
36
|
+
codeCollection;
|
|
37
|
+
inviteCollection;
|
|
38
|
+
spaceUserCollection;
|
|
39
|
+
constructor(db, options) {
|
|
40
|
+
this.options = (0, lodash_1.merge)({ ...defaultOptions }, options);
|
|
35
41
|
if (!db) {
|
|
36
42
|
throw new Error("A database connection is required");
|
|
37
43
|
}
|
|
@@ -42,725 +48,432 @@ var Mongo = (function () {
|
|
|
42
48
|
this.inviteCollection = this.db.collection(this.options.inviteCollectionName);
|
|
43
49
|
this.spaceUserCollection = this.db.collection(this.options.spaceUserCollectionName);
|
|
44
50
|
}
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
.project({ _id: 1 })
|
|
64
|
-
.toArray()];
|
|
65
|
-
case 1:
|
|
66
|
-
sessions = _a.sent();
|
|
67
|
-
if (sessions) {
|
|
68
|
-
sessions.forEach(function (session) {
|
|
69
|
-
session.id = session._id.toString();
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
return [2, sessions];
|
|
73
|
-
}
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
};
|
|
77
|
-
Mongo.prototype.setupIndexes = function () {
|
|
78
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
79
|
-
return tslib_1.__generator(this, function (_a) {
|
|
80
|
-
switch (_a.label) {
|
|
81
|
-
case 0: return [4, this.sessionCollection.createIndex("token", {
|
|
82
|
-
unique: true,
|
|
83
|
-
sparse: true,
|
|
84
|
-
})];
|
|
85
|
-
case 1:
|
|
86
|
-
_a.sent();
|
|
87
|
-
return [4, this.collection.createIndex("username", {
|
|
88
|
-
unique: true,
|
|
89
|
-
sparse: true,
|
|
90
|
-
})];
|
|
91
|
-
case 2:
|
|
92
|
-
_a.sent();
|
|
93
|
-
return [4, this.collection.createIndex("emails.address", {
|
|
94
|
-
unique: true,
|
|
95
|
-
sparse: true,
|
|
96
|
-
})];
|
|
97
|
-
case 3:
|
|
98
|
-
_a.sent();
|
|
99
|
-
return [2];
|
|
100
|
-
}
|
|
101
|
-
});
|
|
102
|
-
});
|
|
103
|
-
};
|
|
104
|
-
Mongo.prototype.createUser = function (_a) {
|
|
105
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
106
|
-
var user, encryptedMobile, encryptedName, ret;
|
|
107
|
-
var _b;
|
|
108
|
-
var password = _a.password, username = _a.username, email = _a.email, email_verified = _a.email_verified, mobile = _a.mobile, mobile_verified = _a.mobile_verified, cleanUser = tslib_1.__rest(_a, ["password", "username", "email", "email_verified", "mobile", "mobile_verified"]);
|
|
109
|
-
return tslib_1.__generator(this, function (_c) {
|
|
110
|
-
switch (_c.label) {
|
|
111
|
-
case 0:
|
|
112
|
-
user = tslib_1.__assign(tslib_1.__assign({}, cleanUser), (_b = { services: {} }, _b[this.options.timestamps.createdAt] = this.options.dateProvider(), _b[this.options.timestamps.updatedAt] = this.options.dateProvider(), _b));
|
|
113
|
-
if (password) {
|
|
114
|
-
user.services.password = { bcrypt: password };
|
|
115
|
-
}
|
|
116
|
-
if (username) {
|
|
117
|
-
user.username = username;
|
|
118
|
-
}
|
|
119
|
-
if (email) {
|
|
120
|
-
user.email = email.toLowerCase();
|
|
121
|
-
user.email_verified = email_verified;
|
|
122
|
-
user.emails = [
|
|
123
|
-
{ address: email.toLowerCase(), verified: email_verified },
|
|
124
|
-
];
|
|
125
|
-
}
|
|
126
|
-
if (!mobile) return [3, 2];
|
|
127
|
-
user.mobile = mobile;
|
|
128
|
-
return [4, this.getEncryptedSpaceUserFieldValue(mobile, "mobile")];
|
|
129
|
-
case 1:
|
|
130
|
-
encryptedMobile = _c.sent();
|
|
131
|
-
if (encryptedMobile) {
|
|
132
|
-
user.mobile = encryptedMobile;
|
|
133
|
-
}
|
|
134
|
-
user.mobile_verified = mobile_verified;
|
|
135
|
-
_c.label = 2;
|
|
136
|
-
case 2:
|
|
137
|
-
if (!user.name) return [3, 4];
|
|
138
|
-
return [4, this.getEncryptedSpaceUserFieldValue(user.name, "name")];
|
|
139
|
-
case 3:
|
|
140
|
-
encryptedName = _c.sent();
|
|
141
|
-
if (encryptedName) {
|
|
142
|
-
user.name = encryptedName;
|
|
143
|
-
}
|
|
144
|
-
_c.label = 4;
|
|
145
|
-
case 4:
|
|
146
|
-
if (this.options.idProvider) {
|
|
147
|
-
user._id = this.options.idProvider();
|
|
148
|
-
}
|
|
149
|
-
user.steedos_id = user._id;
|
|
150
|
-
return [4, this.collection.insertOne(user)];
|
|
151
|
-
case 5:
|
|
152
|
-
ret = _c.sent();
|
|
153
|
-
return [2, user._id];
|
|
154
|
-
}
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
};
|
|
158
|
-
Mongo.prototype.findUserById = function (userId) {
|
|
159
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
160
|
-
var id, user;
|
|
161
|
-
return tslib_1.__generator(this, function (_a) {
|
|
162
|
-
switch (_a.label) {
|
|
163
|
-
case 0:
|
|
164
|
-
id = this.options.convertUserIdToMongoObjectId
|
|
165
|
-
? toMongoID(userId)
|
|
166
|
-
: userId;
|
|
167
|
-
return [4, this.collection.findOne({ _id: id })];
|
|
168
|
-
case 1:
|
|
169
|
-
user = _a.sent();
|
|
170
|
-
if (user) {
|
|
171
|
-
user.id = user._id.toString();
|
|
172
|
-
}
|
|
173
|
-
return [2, user];
|
|
174
|
-
}
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
|
-
};
|
|
178
|
-
Mongo.prototype.findUserByEmail = function (email) {
|
|
179
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
180
|
-
var user;
|
|
181
|
-
return tslib_1.__generator(this, function (_a) {
|
|
182
|
-
switch (_a.label) {
|
|
183
|
-
case 0:
|
|
184
|
-
if (!(0, lodash_2.isString)(email)) {
|
|
185
|
-
return [2, null];
|
|
186
|
-
}
|
|
187
|
-
return [4, this.collection.findOne({
|
|
188
|
-
email: email.toLowerCase(),
|
|
189
|
-
})];
|
|
190
|
-
case 1:
|
|
191
|
-
user = _a.sent();
|
|
192
|
-
if (user) {
|
|
193
|
-
user.id = user._id.toString();
|
|
194
|
-
}
|
|
195
|
-
return [2, user];
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
});
|
|
199
|
-
};
|
|
200
|
-
Mongo.prototype.findUserByMobile = function (mobile) {
|
|
201
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
202
|
-
var selector, encryptedMobile, user;
|
|
203
|
-
return tslib_1.__generator(this, function (_a) {
|
|
204
|
-
switch (_a.label) {
|
|
205
|
-
case 0:
|
|
206
|
-
if (!(0, lodash_2.isString)(mobile)) {
|
|
207
|
-
return [2, null];
|
|
208
|
-
}
|
|
209
|
-
selector = {
|
|
210
|
-
mobile: mobile,
|
|
211
|
-
};
|
|
212
|
-
return [4, this.getEncryptedSpaceUserFieldValue(mobile, "mobile")];
|
|
213
|
-
case 1:
|
|
214
|
-
encryptedMobile = _a.sent();
|
|
215
|
-
if (encryptedMobile) {
|
|
216
|
-
selector.mobile = encryptedMobile;
|
|
217
|
-
}
|
|
218
|
-
return [4, this.collection.findOne(selector)];
|
|
219
|
-
case 2:
|
|
220
|
-
user = _a.sent();
|
|
221
|
-
if (user) {
|
|
222
|
-
user.id = user._id.toString();
|
|
223
|
-
}
|
|
224
|
-
return [2, user];
|
|
225
|
-
}
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
};
|
|
229
|
-
Mongo.prototype.findUserByUsername = function (username) {
|
|
230
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
231
|
-
var filter, user;
|
|
232
|
-
return tslib_1.__generator(this, function (_a) {
|
|
233
|
-
switch (_a.label) {
|
|
234
|
-
case 0:
|
|
235
|
-
if (!(0, lodash_2.isString)(username)) {
|
|
236
|
-
return [2, null];
|
|
237
|
-
}
|
|
238
|
-
filter = this.options.caseSensitiveUserName
|
|
239
|
-
? { username: username }
|
|
240
|
-
: {
|
|
241
|
-
$where: "obj.username && (obj.username.toLowerCase() === \"".concat(username.toLowerCase(), "\")"),
|
|
242
|
-
};
|
|
243
|
-
return [4, this.collection.findOne(filter)];
|
|
244
|
-
case 1:
|
|
245
|
-
user = _a.sent();
|
|
246
|
-
if (user) {
|
|
247
|
-
user.id = user._id.toString();
|
|
248
|
-
}
|
|
249
|
-
return [2, user];
|
|
250
|
-
}
|
|
251
|
-
});
|
|
252
|
-
});
|
|
253
|
-
};
|
|
254
|
-
Mongo.prototype.findPasswordHash = function (userId) {
|
|
255
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
256
|
-
var user;
|
|
257
|
-
return tslib_1.__generator(this, function (_a) {
|
|
258
|
-
switch (_a.label) {
|
|
259
|
-
case 0: return [4, this.findUserById(userId)];
|
|
260
|
-
case 1:
|
|
261
|
-
user = _a.sent();
|
|
262
|
-
if (user) {
|
|
263
|
-
return [2, (0, lodash_1.get)(user, "services.password.bcrypt")];
|
|
264
|
-
}
|
|
265
|
-
return [2, null];
|
|
266
|
-
}
|
|
267
|
-
});
|
|
268
|
-
});
|
|
269
|
-
};
|
|
270
|
-
Mongo.prototype.findUserByEmailVerificationToken = function (token) {
|
|
271
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
272
|
-
var user;
|
|
273
|
-
return tslib_1.__generator(this, function (_a) {
|
|
274
|
-
switch (_a.label) {
|
|
275
|
-
case 0:
|
|
276
|
-
if (!(0, lodash_2.isString)(token)) {
|
|
277
|
-
return [2, null];
|
|
278
|
-
}
|
|
279
|
-
return [4, this.collection.findOne({
|
|
280
|
-
"services.email.verificationTokens.token": token,
|
|
281
|
-
})];
|
|
282
|
-
case 1:
|
|
283
|
-
user = _a.sent();
|
|
284
|
-
if (user) {
|
|
285
|
-
user.id = user._id.toString();
|
|
286
|
-
}
|
|
287
|
-
return [2, user];
|
|
288
|
-
}
|
|
289
|
-
});
|
|
290
|
-
});
|
|
291
|
-
};
|
|
292
|
-
Mongo.prototype.findUserByResetPasswordToken = function (token) {
|
|
293
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
294
|
-
var user;
|
|
295
|
-
return tslib_1.__generator(this, function (_a) {
|
|
296
|
-
switch (_a.label) {
|
|
297
|
-
case 0:
|
|
298
|
-
if (!(0, lodash_2.isString)(token)) {
|
|
299
|
-
return [2, null];
|
|
300
|
-
}
|
|
301
|
-
return [4, this.collection.findOne({
|
|
302
|
-
"services.password.reset.token": token,
|
|
303
|
-
})];
|
|
304
|
-
case 1:
|
|
305
|
-
user = _a.sent();
|
|
306
|
-
if (user) {
|
|
307
|
-
user.id = user._id.toString();
|
|
308
|
-
}
|
|
309
|
-
return [2, user];
|
|
310
|
-
}
|
|
311
|
-
});
|
|
312
|
-
});
|
|
313
|
-
};
|
|
314
|
-
Mongo.prototype.findUserByServiceId = function (serviceName, serviceId) {
|
|
315
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
316
|
-
var user;
|
|
317
|
-
var _a;
|
|
318
|
-
return tslib_1.__generator(this, function (_b) {
|
|
319
|
-
switch (_b.label) {
|
|
320
|
-
case 0:
|
|
321
|
-
if (!(0, lodash_2.isString)(serviceId)) {
|
|
322
|
-
return [2, null];
|
|
323
|
-
}
|
|
324
|
-
return [4, this.collection.findOne((_a = {},
|
|
325
|
-
_a["services.".concat(serviceName, ".id")] = serviceId,
|
|
326
|
-
_a))];
|
|
327
|
-
case 1:
|
|
328
|
-
user = _b.sent();
|
|
329
|
-
if (user) {
|
|
330
|
-
user.id = user._id.toString();
|
|
331
|
-
}
|
|
332
|
-
return [2, user];
|
|
333
|
-
}
|
|
334
|
-
});
|
|
335
|
-
});
|
|
336
|
-
};
|
|
337
|
-
Mongo.prototype.addEmail = function (userId, newEmail, verified) {
|
|
338
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
339
|
-
var id, ret;
|
|
340
|
-
var _a;
|
|
341
|
-
return tslib_1.__generator(this, function (_b) {
|
|
342
|
-
switch (_b.label) {
|
|
343
|
-
case 0:
|
|
344
|
-
id = this.options.convertUserIdToMongoObjectId
|
|
345
|
-
? toMongoID(userId)
|
|
346
|
-
: userId;
|
|
347
|
-
return [4, this.collection.updateOne({ _id: id }, {
|
|
348
|
-
$addToSet: {
|
|
349
|
-
emails: {
|
|
350
|
-
address: newEmail.toLowerCase(),
|
|
351
|
-
verified: verified,
|
|
352
|
-
},
|
|
353
|
-
},
|
|
354
|
-
$set: (_a = {},
|
|
355
|
-
_a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
|
|
356
|
-
_a),
|
|
357
|
-
})];
|
|
358
|
-
case 1:
|
|
359
|
-
ret = _b.sent();
|
|
360
|
-
if (ret.matchedCount === 0) {
|
|
361
|
-
throw new Error("User not found");
|
|
362
|
-
}
|
|
363
|
-
return [2];
|
|
364
|
-
}
|
|
365
|
-
});
|
|
366
|
-
});
|
|
367
|
-
};
|
|
368
|
-
Mongo.prototype.removeEmail = function (userId, email) {
|
|
369
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
370
|
-
var id, ret;
|
|
371
|
-
var _a;
|
|
372
|
-
return tslib_1.__generator(this, function (_b) {
|
|
373
|
-
switch (_b.label) {
|
|
374
|
-
case 0:
|
|
375
|
-
id = this.options.convertUserIdToMongoObjectId
|
|
376
|
-
? toMongoID(userId)
|
|
377
|
-
: userId;
|
|
378
|
-
return [4, this.collection.updateOne({ _id: id }, {
|
|
379
|
-
$pull: { emails: { address: email.toLowerCase() } },
|
|
380
|
-
$set: (_a = {},
|
|
381
|
-
_a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
|
|
382
|
-
_a),
|
|
383
|
-
})];
|
|
384
|
-
case 1:
|
|
385
|
-
ret = _b.sent();
|
|
386
|
-
if (ret.matchedCount === 0) {
|
|
387
|
-
throw new Error("User not found");
|
|
388
|
-
}
|
|
389
|
-
return [2];
|
|
390
|
-
}
|
|
51
|
+
async findValidSessionsByUserId(userId, is_phone) {
|
|
52
|
+
let query = {
|
|
53
|
+
userId,
|
|
54
|
+
valid: true,
|
|
55
|
+
};
|
|
56
|
+
if (is_phone) {
|
|
57
|
+
query.is_phone = true;
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
query.is_phone = { $ne: true };
|
|
61
|
+
}
|
|
62
|
+
const sessions = await this.sessionCollection
|
|
63
|
+
.find(query)
|
|
64
|
+
.project({ _id: 1 })
|
|
65
|
+
.toArray();
|
|
66
|
+
if (sessions) {
|
|
67
|
+
sessions.forEach(function (session) {
|
|
68
|
+
session.id = session._id.toString();
|
|
391
69
|
});
|
|
70
|
+
}
|
|
71
|
+
return sessions;
|
|
72
|
+
}
|
|
73
|
+
async setupIndexes() {
|
|
74
|
+
await this.sessionCollection.createIndex("token", {
|
|
75
|
+
unique: true,
|
|
76
|
+
sparse: true,
|
|
392
77
|
});
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
var id, ret;
|
|
397
|
-
var _a;
|
|
398
|
-
return tslib_1.__generator(this, function (_b) {
|
|
399
|
-
switch (_b.label) {
|
|
400
|
-
case 0:
|
|
401
|
-
id = this.options.convertUserIdToMongoObjectId
|
|
402
|
-
? toMongoID(userId)
|
|
403
|
-
: userId;
|
|
404
|
-
return [4, this.collection.updateOne({ _id: id, email: email }, {
|
|
405
|
-
$set: (_a = {
|
|
406
|
-
email_verified: true
|
|
407
|
-
},
|
|
408
|
-
_a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
|
|
409
|
-
_a),
|
|
410
|
-
$pull: {
|
|
411
|
-
"services.email.verificationTokens": { address: email },
|
|
412
|
-
},
|
|
413
|
-
})];
|
|
414
|
-
case 1:
|
|
415
|
-
ret = _b.sent();
|
|
416
|
-
return [4, this.spaceUserCollection.updateMany({ user: id }, {
|
|
417
|
-
$set: {
|
|
418
|
-
email_verified: true,
|
|
419
|
-
modified: this.options.dateProvider(),
|
|
420
|
-
modified_by: id,
|
|
421
|
-
},
|
|
422
|
-
})];
|
|
423
|
-
case 2:
|
|
424
|
-
_b.sent();
|
|
425
|
-
if (ret.matchedCount === 0) {
|
|
426
|
-
throw new Error("User not found");
|
|
427
|
-
}
|
|
428
|
-
return [2];
|
|
429
|
-
}
|
|
430
|
-
});
|
|
78
|
+
await this.collection.createIndex("username", {
|
|
79
|
+
unique: true,
|
|
80
|
+
sparse: true,
|
|
431
81
|
});
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
var objectql, objFields, datasource, encryptedValue;
|
|
436
|
-
return tslib_1.__generator(this, function (_a) {
|
|
437
|
-
switch (_a.label) {
|
|
438
|
-
case 0:
|
|
439
|
-
objectql = require("@steedos/objectql");
|
|
440
|
-
return [4, objectql.getObject("space_users").getFields()];
|
|
441
|
-
case 1:
|
|
442
|
-
objFields = _a.sent();
|
|
443
|
-
if (!objFields[fieldName].enable_encryption) return [3, 3];
|
|
444
|
-
datasource = (0, objectql_1.getDataSource)("default");
|
|
445
|
-
return [4, datasource.adapter.encryptValue(value)];
|
|
446
|
-
case 2:
|
|
447
|
-
encryptedValue = _a.sent();
|
|
448
|
-
if (encryptedValue) {
|
|
449
|
-
return [2, encryptedValue];
|
|
450
|
-
}
|
|
451
|
-
_a.label = 3;
|
|
452
|
-
case 3: return [2];
|
|
453
|
-
}
|
|
454
|
-
});
|
|
82
|
+
await this.collection.createIndex("emails.address", {
|
|
83
|
+
unique: true,
|
|
84
|
+
sparse: true,
|
|
455
85
|
});
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
86
|
+
}
|
|
87
|
+
async createUser({ password, username, email, email_verified, mobile, mobile_verified, ...cleanUser }) {
|
|
88
|
+
const user = {
|
|
89
|
+
...cleanUser,
|
|
90
|
+
services: {},
|
|
91
|
+
[this.options.timestamps.createdAt]: this.options.dateProvider(),
|
|
92
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
93
|
+
};
|
|
94
|
+
if (password) {
|
|
95
|
+
user.services.password = { bcrypt: password };
|
|
96
|
+
}
|
|
97
|
+
if (username) {
|
|
98
|
+
user.username = username;
|
|
99
|
+
}
|
|
100
|
+
if (email) {
|
|
101
|
+
user.email = email.toLowerCase();
|
|
102
|
+
user.email_verified = email_verified;
|
|
103
|
+
user.emails = [
|
|
104
|
+
{ address: email.toLowerCase(), verified: email_verified },
|
|
105
|
+
];
|
|
106
|
+
}
|
|
107
|
+
if (mobile) {
|
|
108
|
+
user.mobile = mobile;
|
|
109
|
+
const encryptedMobile = await this.getEncryptedSpaceUserFieldValue(mobile, "mobile");
|
|
110
|
+
if (encryptedMobile) {
|
|
111
|
+
user.mobile = encryptedMobile;
|
|
112
|
+
}
|
|
113
|
+
user.mobile_verified = mobile_verified;
|
|
114
|
+
}
|
|
115
|
+
if (user.name) {
|
|
116
|
+
const encryptedName = await this.getEncryptedSpaceUserFieldValue(user.name, "name");
|
|
117
|
+
if (encryptedName) {
|
|
118
|
+
user.name = encryptedName;
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (this.options.idProvider) {
|
|
122
|
+
user._id = this.options.idProvider();
|
|
123
|
+
}
|
|
124
|
+
user.steedos_id = user._id;
|
|
125
|
+
const ret = await this.collection.insertOne(user);
|
|
126
|
+
return user._id;
|
|
127
|
+
}
|
|
128
|
+
async findUserById(userId) {
|
|
129
|
+
const id = this.options.convertUserIdToMongoObjectId
|
|
130
|
+
? toMongoID(userId)
|
|
131
|
+
: userId;
|
|
132
|
+
const user = await this.collection.findOne({ _id: id });
|
|
133
|
+
if (user) {
|
|
134
|
+
user.id = user._id.toString();
|
|
135
|
+
}
|
|
136
|
+
return user;
|
|
137
|
+
}
|
|
138
|
+
async findUserByEmail(email) {
|
|
139
|
+
if (!(0, lodash_2.isString)(email)) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
const user = await this.collection.findOne({
|
|
143
|
+
email: email.toLowerCase(),
|
|
502
144
|
});
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
_b.sent();
|
|
554
|
-
_b.label = 6;
|
|
555
|
-
case 6:
|
|
556
|
-
if (!user) {
|
|
557
|
-
throw new Error("User not found");
|
|
558
|
-
}
|
|
559
|
-
return [2];
|
|
560
|
-
}
|
|
561
|
-
});
|
|
145
|
+
if (user) {
|
|
146
|
+
user.id = user._id.toString();
|
|
147
|
+
}
|
|
148
|
+
return user;
|
|
149
|
+
}
|
|
150
|
+
async findUserByMobile(mobile) {
|
|
151
|
+
if (!(0, lodash_2.isString)(mobile)) {
|
|
152
|
+
return null;
|
|
153
|
+
}
|
|
154
|
+
const selector = {
|
|
155
|
+
mobile,
|
|
156
|
+
};
|
|
157
|
+
const encryptedMobile = await this.getEncryptedSpaceUserFieldValue(mobile, "mobile");
|
|
158
|
+
if (encryptedMobile) {
|
|
159
|
+
selector.mobile = encryptedMobile;
|
|
160
|
+
}
|
|
161
|
+
const user = await this.collection.findOne(selector);
|
|
162
|
+
if (user) {
|
|
163
|
+
user.id = user._id.toString();
|
|
164
|
+
}
|
|
165
|
+
return user;
|
|
166
|
+
}
|
|
167
|
+
async findUserByUsername(username) {
|
|
168
|
+
if (!(0, lodash_2.isString)(username)) {
|
|
169
|
+
return null;
|
|
170
|
+
}
|
|
171
|
+
const filter = this.options.caseSensitiveUserName
|
|
172
|
+
? { username }
|
|
173
|
+
: {
|
|
174
|
+
$where: `obj.username && (obj.username.toLowerCase() === "${username.toLowerCase()}")`,
|
|
175
|
+
};
|
|
176
|
+
const user = await this.collection.findOne(filter);
|
|
177
|
+
if (user) {
|
|
178
|
+
user.id = user._id.toString();
|
|
179
|
+
}
|
|
180
|
+
return user;
|
|
181
|
+
}
|
|
182
|
+
async findPasswordHash(userId) {
|
|
183
|
+
const user = await this.findUserById(userId);
|
|
184
|
+
if (user) {
|
|
185
|
+
return (0, lodash_1.get)(user, "services.password.bcrypt");
|
|
186
|
+
}
|
|
187
|
+
return null;
|
|
188
|
+
}
|
|
189
|
+
async findUserByEmailVerificationToken(token) {
|
|
190
|
+
if (!(0, lodash_2.isString)(token)) {
|
|
191
|
+
return null;
|
|
192
|
+
}
|
|
193
|
+
const user = await this.collection.findOne({
|
|
194
|
+
"services.email.verificationTokens.token": token,
|
|
562
195
|
});
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
id = this.options.convertUserIdToMongoObjectId
|
|
575
|
-
? toMongoID(userId)
|
|
576
|
-
: userId;
|
|
577
|
-
return [4, this.collection
|
|
578
|
-
.find({ _id: { $ne: id }, email: newEmail })
|
|
579
|
-
.count()];
|
|
580
|
-
case 1:
|
|
581
|
-
existed = _b.sent();
|
|
582
|
-
if (existed > 0) {
|
|
583
|
-
throw new Error("该邮箱已被其他用户注册");
|
|
584
|
-
}
|
|
585
|
-
return [4, this.collection.findOne({ _id: id }, { projection: { email: 1 } })];
|
|
586
|
-
case 2:
|
|
587
|
-
user = _b.sent();
|
|
588
|
-
if (!(user && user.email != newEmail)) return [3, 5];
|
|
589
|
-
return [4, this.collection.updateOne({ _id: id }, {
|
|
590
|
-
$set: (_a = {
|
|
591
|
-
email: newEmail
|
|
592
|
-
},
|
|
593
|
-
_a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
|
|
594
|
-
_a),
|
|
595
|
-
$pull: {
|
|
596
|
-
"services.email.verificationTokens": { address: newEmail },
|
|
597
|
-
},
|
|
598
|
-
})];
|
|
599
|
-
case 3:
|
|
600
|
-
ret = _b.sent();
|
|
601
|
-
return [4, this.spaceUserCollection.updateMany({ user: id }, {
|
|
602
|
-
$set: {
|
|
603
|
-
email: newEmail,
|
|
604
|
-
modified: this.options.dateProvider(),
|
|
605
|
-
modified_by: id,
|
|
606
|
-
},
|
|
607
|
-
})];
|
|
608
|
-
case 4:
|
|
609
|
-
_b.sent();
|
|
610
|
-
_b.label = 5;
|
|
611
|
-
case 5:
|
|
612
|
-
if (!user) {
|
|
613
|
-
throw new Error("User not found");
|
|
614
|
-
}
|
|
615
|
-
return [2];
|
|
616
|
-
}
|
|
617
|
-
});
|
|
196
|
+
if (user) {
|
|
197
|
+
user.id = user._id.toString();
|
|
198
|
+
}
|
|
199
|
+
return user;
|
|
200
|
+
}
|
|
201
|
+
async findUserByResetPasswordToken(token) {
|
|
202
|
+
if (!(0, lodash_2.isString)(token)) {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
const user = await this.collection.findOne({
|
|
206
|
+
"services.password.reset.token": token,
|
|
618
207
|
});
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
return [4, this.collection.updateOne({ _id: id }, {
|
|
631
|
-
$set: (_a = {
|
|
632
|
-
username: newUsername
|
|
633
|
-
},
|
|
634
|
-
_a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
|
|
635
|
-
_a),
|
|
636
|
-
})];
|
|
637
|
-
case 1:
|
|
638
|
-
ret = _b.sent();
|
|
639
|
-
if (ret.matchedCount === 0) {
|
|
640
|
-
throw new Error("User not found");
|
|
641
|
-
}
|
|
642
|
-
return [2];
|
|
643
|
-
}
|
|
644
|
-
});
|
|
208
|
+
if (user) {
|
|
209
|
+
user.id = user._id.toString();
|
|
210
|
+
}
|
|
211
|
+
return user;
|
|
212
|
+
}
|
|
213
|
+
async findUserByServiceId(serviceName, serviceId) {
|
|
214
|
+
if (!(0, lodash_2.isString)(serviceId)) {
|
|
215
|
+
return null;
|
|
216
|
+
}
|
|
217
|
+
const user = await this.collection.findOne({
|
|
218
|
+
[`services.${serviceName}.id`]: serviceId,
|
|
645
219
|
});
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
220
|
+
if (user) {
|
|
221
|
+
user.id = user._id.toString();
|
|
222
|
+
}
|
|
223
|
+
return user;
|
|
224
|
+
}
|
|
225
|
+
async addEmail(userId, newEmail, verified) {
|
|
226
|
+
const id = this.options.convertUserIdToMongoObjectId
|
|
227
|
+
? toMongoID(userId)
|
|
228
|
+
: userId;
|
|
229
|
+
const ret = await this.collection.updateOne({ _id: id }, {
|
|
230
|
+
$addToSet: {
|
|
231
|
+
emails: {
|
|
232
|
+
address: newEmail.toLowerCase(),
|
|
233
|
+
verified,
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
$set: {
|
|
237
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
238
|
+
},
|
|
239
|
+
});
|
|
240
|
+
if (ret.matchedCount === 0) {
|
|
241
|
+
throw new Error("User not found");
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
async removeEmail(userId, email) {
|
|
245
|
+
const id = this.options.convertUserIdToMongoObjectId
|
|
246
|
+
? toMongoID(userId)
|
|
247
|
+
: userId;
|
|
248
|
+
const ret = await this.collection.updateOne({ _id: id }, {
|
|
249
|
+
$pull: { emails: { address: email.toLowerCase() } },
|
|
250
|
+
$set: {
|
|
251
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
252
|
+
},
|
|
253
|
+
});
|
|
254
|
+
if (ret.matchedCount === 0) {
|
|
255
|
+
throw new Error("User not found");
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
async verifyEmail(userId, email) {
|
|
259
|
+
const id = this.options.convertUserIdToMongoObjectId
|
|
260
|
+
? toMongoID(userId)
|
|
261
|
+
: userId;
|
|
262
|
+
const ret = await this.collection.updateOne({ _id: id, email: email }, {
|
|
263
|
+
$set: {
|
|
264
|
+
email_verified: true,
|
|
265
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
266
|
+
},
|
|
267
|
+
$pull: {
|
|
268
|
+
"services.email.verificationTokens": { address: email },
|
|
269
|
+
},
|
|
270
|
+
});
|
|
271
|
+
await this.spaceUserCollection.updateMany({ user: id }, {
|
|
272
|
+
$set: {
|
|
273
|
+
email_verified: true,
|
|
274
|
+
modified: this.options.dateProvider(),
|
|
275
|
+
modified_by: id,
|
|
276
|
+
},
|
|
277
|
+
});
|
|
278
|
+
if (ret.matchedCount === 0) {
|
|
279
|
+
throw new Error("User not found");
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
async getEncryptedSpaceUserFieldValue(value, fieldName) {
|
|
283
|
+
const objectql = require("@steedos/objectql");
|
|
284
|
+
const objFields = await objectql.getObject("space_users").getFields();
|
|
285
|
+
if (objFields[fieldName].enable_encryption) {
|
|
286
|
+
const datasource = (0, objectql_1.getDataSource)("default");
|
|
287
|
+
const encryptedValue = await datasource.adapter.encryptValue(value);
|
|
288
|
+
if (encryptedValue) {
|
|
289
|
+
return encryptedValue;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
async verifyMobile(userId, mobile) {
|
|
294
|
+
if (!(0, lodash_2.isString)(mobile)) {
|
|
295
|
+
return null;
|
|
296
|
+
}
|
|
297
|
+
const id = this.options.convertUserIdToMongoObjectId
|
|
298
|
+
? toMongoID(userId)
|
|
299
|
+
: userId;
|
|
300
|
+
const encryptedMobile = await this.getEncryptedSpaceUserFieldValue(mobile, "mobile");
|
|
301
|
+
const ret = await this.collection.updateOne({ _id: id, mobile: encryptedMobile || mobile }, {
|
|
302
|
+
$set: {
|
|
303
|
+
mobile_verified: true,
|
|
304
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
305
|
+
},
|
|
306
|
+
$pull: {
|
|
307
|
+
"services.mobile.verificationTokens": {
|
|
308
|
+
mobile: encryptedMobile || mobile,
|
|
309
|
+
},
|
|
310
|
+
},
|
|
311
|
+
});
|
|
312
|
+
await this.spaceUserCollection.updateMany({ user: id }, {
|
|
313
|
+
$set: {
|
|
314
|
+
mobile_verified: true,
|
|
315
|
+
modified: this.options.dateProvider(),
|
|
316
|
+
modified_by: id,
|
|
317
|
+
},
|
|
318
|
+
});
|
|
319
|
+
if (ret.matchedCount === 0) {
|
|
320
|
+
throw new Error("User not found");
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
async setMobile(userId, newMobile) {
|
|
324
|
+
if (!(0, lodash_2.isString)(newMobile)) {
|
|
325
|
+
return null;
|
|
326
|
+
}
|
|
327
|
+
const id = this.options.convertUserIdToMongoObjectId
|
|
328
|
+
? toMongoID(userId)
|
|
329
|
+
: userId;
|
|
330
|
+
const encryptedMobile = (await this.getEncryptedSpaceUserFieldValue(newMobile, "mobile")) ||
|
|
331
|
+
newMobile;
|
|
332
|
+
let existed = await this.collection
|
|
333
|
+
.find({ _id: { $ne: id }, mobile: encryptedMobile })
|
|
334
|
+
.count();
|
|
335
|
+
if (existed > 0) {
|
|
336
|
+
throw new Error("该手机号已被其他用户注册");
|
|
337
|
+
}
|
|
338
|
+
let user = await this.collection.findOne({ _id: id }, { projection: { mobile: 1 } });
|
|
339
|
+
if (user && user.mobile != newMobile) {
|
|
340
|
+
const ret = await this.collection.updateOne({ _id: id }, {
|
|
341
|
+
$set: {
|
|
342
|
+
mobile: encryptedMobile,
|
|
343
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
344
|
+
},
|
|
345
|
+
$pull: {
|
|
346
|
+
"services.mobile.verificationTokens": { mobile: encryptedMobile },
|
|
347
|
+
},
|
|
348
|
+
});
|
|
349
|
+
await this.spaceUserCollection.updateMany({ user: id }, {
|
|
350
|
+
$set: {
|
|
351
|
+
mobile: encryptedMobile,
|
|
352
|
+
modified: this.options.dateProvider(),
|
|
353
|
+
modified_by: id,
|
|
354
|
+
},
|
|
677
355
|
});
|
|
678
|
-
}
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
356
|
+
}
|
|
357
|
+
if (!user) {
|
|
358
|
+
throw new Error("User not found");
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
async setEmail(userId, newEmail) {
|
|
362
|
+
if (!(0, lodash_2.isString)(newEmail)) {
|
|
363
|
+
return null;
|
|
364
|
+
}
|
|
365
|
+
const id = this.options.convertUserIdToMongoObjectId
|
|
366
|
+
? toMongoID(userId)
|
|
367
|
+
: userId;
|
|
368
|
+
let existed = await this.collection
|
|
369
|
+
.find({ _id: { $ne: id }, email: newEmail })
|
|
370
|
+
.count();
|
|
371
|
+
if (existed > 0) {
|
|
372
|
+
throw new Error("该邮箱已被其他用户注册");
|
|
373
|
+
}
|
|
374
|
+
let user = await this.collection.findOne({ _id: id }, { projection: { email: 1 } });
|
|
375
|
+
if (user && user.email != newEmail) {
|
|
376
|
+
const ret = await this.collection.updateOne({ _id: id }, {
|
|
377
|
+
$set: {
|
|
378
|
+
email: newEmail,
|
|
379
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
380
|
+
},
|
|
381
|
+
$pull: {
|
|
382
|
+
"services.email.verificationTokens": { address: newEmail },
|
|
383
|
+
},
|
|
384
|
+
});
|
|
385
|
+
await this.spaceUserCollection.updateMany({ user: id }, {
|
|
386
|
+
$set: {
|
|
387
|
+
email: newEmail,
|
|
388
|
+
modified: this.options.dateProvider(),
|
|
389
|
+
modified_by: id,
|
|
390
|
+
},
|
|
700
391
|
});
|
|
392
|
+
}
|
|
393
|
+
if (!user) {
|
|
394
|
+
throw new Error("User not found");
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
async setUsername(userId, newUsername) {
|
|
398
|
+
const id = this.options.convertUserIdToMongoObjectId
|
|
399
|
+
? toMongoID(userId)
|
|
400
|
+
: userId;
|
|
401
|
+
const ret = await this.collection.updateOne({ _id: id }, {
|
|
402
|
+
$set: {
|
|
403
|
+
username: newUsername,
|
|
404
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
405
|
+
},
|
|
406
|
+
});
|
|
407
|
+
if (ret.matchedCount === 0) {
|
|
408
|
+
throw new Error("User not found");
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
async setPassword(userId, newPassword) {
|
|
412
|
+
const id = this.options.convertUserIdToMongoObjectId
|
|
413
|
+
? toMongoID(userId)
|
|
414
|
+
: userId;
|
|
415
|
+
const ret = await this.collection.updateOne({ _id: id }, {
|
|
416
|
+
$set: {
|
|
417
|
+
"services.password.bcrypt": newPassword,
|
|
418
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
419
|
+
},
|
|
420
|
+
$push: {
|
|
421
|
+
"services.password_history": newPassword,
|
|
422
|
+
},
|
|
423
|
+
$unset: {
|
|
424
|
+
"services.password.reset": "",
|
|
425
|
+
},
|
|
426
|
+
});
|
|
427
|
+
if (ret.matchedCount === 0) {
|
|
428
|
+
throw new Error("User not found");
|
|
429
|
+
}
|
|
430
|
+
}
|
|
431
|
+
async setService(userId, serviceName, service) {
|
|
432
|
+
const id = this.options.convertUserIdToMongoObjectId
|
|
433
|
+
? toMongoID(userId)
|
|
434
|
+
: userId;
|
|
435
|
+
await this.collection.updateOne({ _id: id }, {
|
|
436
|
+
$set: {
|
|
437
|
+
[`services.${serviceName}`]: service,
|
|
438
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
439
|
+
},
|
|
701
440
|
});
|
|
702
|
-
}
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
$set: (_a = {},
|
|
715
|
-
_a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
|
|
716
|
-
_a),
|
|
717
|
-
$unset: (_b = {},
|
|
718
|
-
_b["services.".concat(serviceName)] = "",
|
|
719
|
-
_b),
|
|
720
|
-
})];
|
|
721
|
-
case 1:
|
|
722
|
-
_c.sent();
|
|
723
|
-
return [2];
|
|
724
|
-
}
|
|
725
|
-
});
|
|
441
|
+
}
|
|
442
|
+
async unsetService(userId, serviceName) {
|
|
443
|
+
const id = this.options.convertUserIdToMongoObjectId
|
|
444
|
+
? toMongoID(userId)
|
|
445
|
+
: userId;
|
|
446
|
+
await this.collection.updateOne({ _id: id }, {
|
|
447
|
+
$set: {
|
|
448
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
449
|
+
},
|
|
450
|
+
$unset: {
|
|
451
|
+
[`services.${serviceName}`]: "",
|
|
452
|
+
},
|
|
726
453
|
});
|
|
727
|
-
}
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
: userId;
|
|
738
|
-
return [4, this.collection.updateOne({ _id: id }, {
|
|
739
|
-
$set: (_a = {
|
|
740
|
-
deactivated: deactivated
|
|
741
|
-
},
|
|
742
|
-
_a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
|
|
743
|
-
_a),
|
|
744
|
-
})];
|
|
745
|
-
case 1:
|
|
746
|
-
_b.sent();
|
|
747
|
-
return [2];
|
|
748
|
-
}
|
|
749
|
-
});
|
|
454
|
+
}
|
|
455
|
+
async setUserDeactivated(userId, deactivated) {
|
|
456
|
+
const id = this.options.convertUserIdToMongoObjectId
|
|
457
|
+
? toMongoID(userId)
|
|
458
|
+
: userId;
|
|
459
|
+
await this.collection.updateOne({ _id: id }, {
|
|
460
|
+
$set: {
|
|
461
|
+
deactivated,
|
|
462
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
463
|
+
},
|
|
750
464
|
});
|
|
751
|
-
}
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
var provider = connection.provider;
|
|
465
|
+
}
|
|
466
|
+
resolveInfo(connection = {}) {
|
|
467
|
+
let ip = connection.ip;
|
|
468
|
+
let userAgent = connection.userAgent;
|
|
469
|
+
let login_expiration_in_days = connection.login_expiration_in_days;
|
|
470
|
+
let phone_login_expiration_in_days = connection.phone_login_expiration_in_days;
|
|
471
|
+
let is_phone = connection.is_phone;
|
|
472
|
+
let is_tablet = connection.is_tablet;
|
|
473
|
+
let space = connection.space;
|
|
474
|
+
let provider = connection.provider;
|
|
762
475
|
if (userAgent) {
|
|
763
|
-
|
|
476
|
+
const foo = userAgent.split(" Space/");
|
|
764
477
|
if (foo.length > 1) {
|
|
765
478
|
userAgent = foo[0];
|
|
766
479
|
space = foo[1];
|
|
@@ -771,577 +484,346 @@ var Mongo = (function () {
|
|
|
771
484
|
}
|
|
772
485
|
if (space) {
|
|
773
486
|
return {
|
|
774
|
-
ip
|
|
775
|
-
userAgent
|
|
776
|
-
space
|
|
777
|
-
is_phone
|
|
778
|
-
is_tablet
|
|
779
|
-
login_expiration_in_days
|
|
487
|
+
ip,
|
|
488
|
+
userAgent,
|
|
489
|
+
space,
|
|
490
|
+
is_phone,
|
|
491
|
+
is_tablet,
|
|
492
|
+
login_expiration_in_days,
|
|
780
493
|
user_provider: provider,
|
|
781
494
|
};
|
|
782
495
|
}
|
|
783
496
|
return {
|
|
784
|
-
ip
|
|
785
|
-
userAgent
|
|
786
|
-
is_phone
|
|
787
|
-
is_tablet
|
|
788
|
-
login_expiration_in_days
|
|
497
|
+
ip,
|
|
498
|
+
userAgent,
|
|
499
|
+
is_phone,
|
|
500
|
+
is_tablet,
|
|
501
|
+
login_expiration_in_days,
|
|
789
502
|
user_provider: provider,
|
|
790
503
|
};
|
|
791
|
-
}
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
: sessionId;
|
|
826
|
-
infos = this.resolveInfo(connection);
|
|
827
|
-
_set = (_a = {
|
|
828
|
-
userAgent: infos.userAgent,
|
|
829
|
-
ip: infos.ip
|
|
830
|
-
},
|
|
831
|
-
_a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
|
|
832
|
-
_a);
|
|
833
|
-
if (infos.space) {
|
|
834
|
-
_set.space = infos.space;
|
|
835
|
-
}
|
|
836
|
-
return [4, this.sessionCollection.updateOne({ _id: _id }, {
|
|
837
|
-
$set: _set,
|
|
838
|
-
})];
|
|
839
|
-
case 1:
|
|
840
|
-
_b.sent();
|
|
841
|
-
return [2];
|
|
842
|
-
}
|
|
843
|
-
});
|
|
844
|
-
});
|
|
845
|
-
};
|
|
846
|
-
Mongo.prototype.invalidateSession = function (sessionId) {
|
|
847
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
848
|
-
var _id, session;
|
|
849
|
-
var _a;
|
|
850
|
-
return tslib_1.__generator(this, function (_b) {
|
|
851
|
-
switch (_b.label) {
|
|
852
|
-
case 0:
|
|
853
|
-
_id = this.options.convertSessionIdToMongoObjectId
|
|
854
|
-
? toMongoID(sessionId)
|
|
855
|
-
: sessionId;
|
|
856
|
-
return [4, this.sessionCollection.updateOne({ _id: _id }, {
|
|
857
|
-
$set: (_a = {
|
|
858
|
-
valid: false
|
|
859
|
-
},
|
|
860
|
-
_a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
|
|
861
|
-
_a),
|
|
862
|
-
})];
|
|
863
|
-
case 1:
|
|
864
|
-
_b.sent();
|
|
865
|
-
return [4, this.sessionCollection.findOne({
|
|
866
|
-
_id: _id,
|
|
867
|
-
})];
|
|
868
|
-
case 2:
|
|
869
|
-
session = _b.sent();
|
|
870
|
-
return [4, this.destroyMeteorToken(session.userId, session.token)];
|
|
871
|
-
case 3:
|
|
872
|
-
_b.sent();
|
|
873
|
-
return [2];
|
|
874
|
-
}
|
|
875
|
-
});
|
|
876
|
-
});
|
|
877
|
-
};
|
|
878
|
-
Mongo.prototype.invalidateAllSessions = function (userId) {
|
|
879
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
880
|
-
var _a;
|
|
881
|
-
return tslib_1.__generator(this, function (_b) {
|
|
882
|
-
switch (_b.label) {
|
|
883
|
-
case 0: return [4, this.sessionCollection.updateMany({ userId: userId }, {
|
|
884
|
-
$set: (_a = {
|
|
885
|
-
valid: false
|
|
886
|
-
},
|
|
887
|
-
_a[this.options.timestamps.updatedAt] = this.options.dateProvider(),
|
|
888
|
-
_a),
|
|
889
|
-
})];
|
|
890
|
-
case 1:
|
|
891
|
-
_b.sent();
|
|
892
|
-
return [2];
|
|
893
|
-
}
|
|
894
|
-
});
|
|
895
|
-
});
|
|
896
|
-
};
|
|
897
|
-
Mongo.prototype.findSessionByToken = function (token) {
|
|
898
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
899
|
-
var session;
|
|
900
|
-
return tslib_1.__generator(this, function (_a) {
|
|
901
|
-
switch (_a.label) {
|
|
902
|
-
case 0: return [4, this.sessionCollection.findOne({ token: token })];
|
|
903
|
-
case 1:
|
|
904
|
-
session = _a.sent();
|
|
905
|
-
if (session) {
|
|
906
|
-
session.id = session._id.toString();
|
|
907
|
-
}
|
|
908
|
-
return [2, session];
|
|
909
|
-
}
|
|
910
|
-
});
|
|
911
|
-
});
|
|
912
|
-
};
|
|
913
|
-
Mongo.prototype.findSessionById = function (sessionId) {
|
|
914
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
915
|
-
var _id, session;
|
|
916
|
-
return tslib_1.__generator(this, function (_a) {
|
|
917
|
-
switch (_a.label) {
|
|
918
|
-
case 0:
|
|
919
|
-
_id = this.options.convertSessionIdToMongoObjectId
|
|
920
|
-
? toMongoID(sessionId)
|
|
921
|
-
: sessionId;
|
|
922
|
-
return [4, this.sessionCollection.findOne({ _id: _id })];
|
|
923
|
-
case 1:
|
|
924
|
-
session = _a.sent();
|
|
925
|
-
if (session) {
|
|
926
|
-
session.id = session._id.toString();
|
|
927
|
-
}
|
|
928
|
-
return [2, session];
|
|
929
|
-
}
|
|
930
|
-
});
|
|
931
|
-
});
|
|
932
|
-
};
|
|
933
|
-
Mongo.prototype.addEmailVerificationToken = function (userId, email, token) {
|
|
934
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
935
|
-
var _id;
|
|
936
|
-
return tslib_1.__generator(this, function (_a) {
|
|
937
|
-
switch (_a.label) {
|
|
938
|
-
case 0:
|
|
939
|
-
_id = this.options.convertUserIdToMongoObjectId
|
|
940
|
-
? toMongoID(userId)
|
|
941
|
-
: userId;
|
|
942
|
-
return [4, this.collection.updateOne({ _id: _id }, {
|
|
943
|
-
$push: {
|
|
944
|
-
"services.email.verificationTokens": {
|
|
945
|
-
token: token,
|
|
946
|
-
address: email.toLowerCase(),
|
|
947
|
-
when: this.options.dateProvider(),
|
|
948
|
-
},
|
|
949
|
-
},
|
|
950
|
-
})];
|
|
951
|
-
case 1:
|
|
952
|
-
_a.sent();
|
|
953
|
-
return [2];
|
|
954
|
-
}
|
|
955
|
-
});
|
|
956
|
-
});
|
|
957
|
-
};
|
|
958
|
-
Mongo.prototype.addResetPasswordToken = function (userId, email, token, reason) {
|
|
959
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
960
|
-
var _id;
|
|
961
|
-
return tslib_1.__generator(this, function (_a) {
|
|
962
|
-
switch (_a.label) {
|
|
963
|
-
case 0:
|
|
964
|
-
_id = this.options.convertUserIdToMongoObjectId
|
|
965
|
-
? toMongoID(userId)
|
|
966
|
-
: userId;
|
|
967
|
-
return [4, this.collection.updateOne({ _id: _id }, {
|
|
968
|
-
$push: {
|
|
969
|
-
"services.password.reset": {
|
|
970
|
-
token: token,
|
|
971
|
-
address: email.toLowerCase(),
|
|
972
|
-
when: this.options.dateProvider(),
|
|
973
|
-
reason: reason,
|
|
974
|
-
},
|
|
975
|
-
},
|
|
976
|
-
})];
|
|
977
|
-
case 1:
|
|
978
|
-
_a.sent();
|
|
979
|
-
return [2];
|
|
980
|
-
}
|
|
981
|
-
});
|
|
982
|
-
});
|
|
983
|
-
};
|
|
984
|
-
Mongo.prototype.setResetPassword = function (userId, email, newPassword) {
|
|
985
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
986
|
-
return tslib_1.__generator(this, function (_a) {
|
|
987
|
-
switch (_a.label) {
|
|
988
|
-
case 0: return [4, this.setPassword(userId, newPassword)];
|
|
989
|
-
case 1:
|
|
990
|
-
_a.sent();
|
|
991
|
-
return [2];
|
|
992
|
-
}
|
|
993
|
-
});
|
|
994
|
-
});
|
|
995
|
-
};
|
|
996
|
-
Mongo.prototype.applyCode = function (name, owner, nextCode, MAX_FAILURE_COUNT, EFFECTIVE_TIME) {
|
|
997
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
998
|
-
var now, query, record, doc, result;
|
|
999
|
-
var _a;
|
|
1000
|
-
return tslib_1.__generator(this, function (_b) {
|
|
1001
|
-
switch (_b.label) {
|
|
1002
|
-
case 0:
|
|
1003
|
-
now = new Date();
|
|
1004
|
-
if (!(0, lodash_2.isString)(name)) {
|
|
1005
|
-
throw new Error("accounts.invalidCode");
|
|
1006
|
-
}
|
|
1007
|
-
query = {
|
|
1008
|
-
name: name,
|
|
1009
|
-
verifiedAt: null,
|
|
1010
|
-
expiredAt: { $gt: now },
|
|
1011
|
-
};
|
|
1012
|
-
if (owner) {
|
|
1013
|
-
query.owner = owner;
|
|
1014
|
-
}
|
|
1015
|
-
return [4, this.codeCollection.findOne(query)];
|
|
1016
|
-
case 1:
|
|
1017
|
-
record = _b.sent();
|
|
1018
|
-
if (!record) return [3, 2];
|
|
1019
|
-
return [3, 4];
|
|
1020
|
-
case 2:
|
|
1021
|
-
doc = (_a = {
|
|
1022
|
-
name: name,
|
|
1023
|
-
owner: owner,
|
|
1024
|
-
code: nextCode,
|
|
1025
|
-
expiredAt: new Date(moment().add(EFFECTIVE_TIME, "m"))
|
|
1026
|
-
},
|
|
1027
|
-
_a[this.options.timestamps.createdAt] = this.options.dateProvider(),
|
|
1028
|
-
_a);
|
|
1029
|
-
if (this.options.idProvider) {
|
|
1030
|
-
doc._id = this.options.idProvider();
|
|
1031
|
-
}
|
|
1032
|
-
return [4, this.codeCollection.insertOne(doc)];
|
|
1033
|
-
case 3:
|
|
1034
|
-
result = _b.sent();
|
|
1035
|
-
record = result.ops[0];
|
|
1036
|
-
_b.label = 4;
|
|
1037
|
-
case 4: return [2, record];
|
|
1038
|
-
}
|
|
1039
|
-
});
|
|
1040
|
-
});
|
|
1041
|
-
};
|
|
1042
|
-
Mongo.prototype.addVerificationCode = function (user, code, options) {
|
|
1043
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1044
|
-
var foundedUser, owner, ret;
|
|
1045
|
-
return tslib_1.__generator(this, function (_a) {
|
|
1046
|
-
switch (_a.label) {
|
|
1047
|
-
case 0:
|
|
1048
|
-
foundedUser = null;
|
|
1049
|
-
if (!user.email) return [3, 2];
|
|
1050
|
-
return [4, this.findUserByEmail(user.email)];
|
|
1051
|
-
case 1:
|
|
1052
|
-
foundedUser = _a.sent();
|
|
1053
|
-
return [3, 4];
|
|
1054
|
-
case 2:
|
|
1055
|
-
if (!user.mobile) return [3, 4];
|
|
1056
|
-
return [4, this.findUserByMobile(user.mobile)];
|
|
1057
|
-
case 3:
|
|
1058
|
-
foundedUser = _a.sent();
|
|
1059
|
-
_a.label = 4;
|
|
1060
|
-
case 4:
|
|
1061
|
-
owner = foundedUser ? foundedUser.id : null;
|
|
1062
|
-
return [4, this.applyCode(user.email ? user.email : user.mobile, owner, code, options.MAX_FAILURE_COUNT, options.EFFECTIVE_TIME)];
|
|
1063
|
-
case 5:
|
|
1064
|
-
ret = _a.sent();
|
|
1065
|
-
return [2, ret];
|
|
1066
|
-
}
|
|
1067
|
-
});
|
|
1068
|
-
});
|
|
1069
|
-
};
|
|
1070
|
-
Mongo.prototype.verifyCodeByName = function (name, code) {
|
|
1071
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1072
|
-
var now, query, result;
|
|
1073
|
-
return tslib_1.__generator(this, function (_a) {
|
|
1074
|
-
switch (_a.label) {
|
|
1075
|
-
case 0:
|
|
1076
|
-
if (!(0, lodash_2.isString)(code)) {
|
|
1077
|
-
throw new Error("accounts.invalidCode");
|
|
1078
|
-
}
|
|
1079
|
-
if (!(0, lodash_2.isString)(name)) {
|
|
1080
|
-
throw new Error("accounts.invalidCode");
|
|
1081
|
-
}
|
|
1082
|
-
now = new Date();
|
|
1083
|
-
query = {
|
|
1084
|
-
name: name,
|
|
1085
|
-
code: code,
|
|
1086
|
-
verifiedAt: null,
|
|
1087
|
-
expiredAt: { $gt: now },
|
|
1088
|
-
};
|
|
1089
|
-
return [4, this.codeCollection.findOne(query)];
|
|
1090
|
-
case 1:
|
|
1091
|
-
result = _a.sent();
|
|
1092
|
-
if (!result) return [3, 3];
|
|
1093
|
-
return [4, this.codeCollection.updateOne({ _id: result._id }, { $set: { verifiedAt: now } })];
|
|
1094
|
-
case 2:
|
|
1095
|
-
_a.sent();
|
|
1096
|
-
return [2, result];
|
|
1097
|
-
case 3: throw new Error("accounts.invalidCode");
|
|
1098
|
-
}
|
|
1099
|
-
});
|
|
1100
|
-
});
|
|
1101
|
-
};
|
|
1102
|
-
Mongo.prototype.verifyCodeByOwner = function (owner, code) {
|
|
1103
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1104
|
-
var now, query, result;
|
|
1105
|
-
return tslib_1.__generator(this, function (_a) {
|
|
1106
|
-
switch (_a.label) {
|
|
1107
|
-
case 0:
|
|
1108
|
-
if (!(0, lodash_2.isString)(code)) {
|
|
1109
|
-
throw new Error("accounts.invalidCode");
|
|
1110
|
-
}
|
|
1111
|
-
if (!(0, lodash_2.isString)(owner)) {
|
|
1112
|
-
throw new Error("accounts.invalidCode");
|
|
1113
|
-
}
|
|
1114
|
-
now = new Date();
|
|
1115
|
-
query = {
|
|
1116
|
-
owner: owner,
|
|
1117
|
-
code: code,
|
|
1118
|
-
verifiedAt: null,
|
|
1119
|
-
expiredAt: { $gt: now },
|
|
1120
|
-
};
|
|
1121
|
-
return [4, this.codeCollection.findOne(query)];
|
|
1122
|
-
case 1:
|
|
1123
|
-
result = _a.sent();
|
|
1124
|
-
if (!result) return [3, 3];
|
|
1125
|
-
return [4, this.codeCollection.updateOne({ _id: result._id }, { $set: { verifiedAt: now } })];
|
|
1126
|
-
case 2:
|
|
1127
|
-
_a.sent();
|
|
1128
|
-
return [2, result];
|
|
1129
|
-
case 3:
|
|
1130
|
-
console.log("verifyCodeByOwner throw new Error accounts.invalidCode");
|
|
1131
|
-
throw new Error("accounts.invalidCode");
|
|
1132
|
-
}
|
|
1133
|
-
});
|
|
1134
|
-
});
|
|
1135
|
-
};
|
|
1136
|
-
Mongo.prototype.checkVerificationCode = function (user, code) {
|
|
1137
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1138
|
-
var name, record;
|
|
1139
|
-
return tslib_1.__generator(this, function (_a) {
|
|
1140
|
-
switch (_a.label) {
|
|
1141
|
-
case 0:
|
|
1142
|
-
name = null;
|
|
1143
|
-
if (user.email)
|
|
1144
|
-
name = user.email;
|
|
1145
|
-
else if (user.mobile)
|
|
1146
|
-
name = user.mobile;
|
|
1147
|
-
if (!name)
|
|
1148
|
-
return [2, false];
|
|
1149
|
-
return [4, this.verifyCodeByName(name, code)];
|
|
1150
|
-
case 1:
|
|
1151
|
-
record = _a.sent();
|
|
1152
|
-
if (!record)
|
|
1153
|
-
return [2, false];
|
|
1154
|
-
return [2, true];
|
|
1155
|
-
}
|
|
1156
|
-
});
|
|
1157
|
-
});
|
|
1158
|
-
};
|
|
1159
|
-
Mongo.prototype.findUserByVerificationCode = function (user, code) {
|
|
1160
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1161
|
-
var foundedUser, owner, record;
|
|
1162
|
-
return tslib_1.__generator(this, function (_a) {
|
|
1163
|
-
switch (_a.label) {
|
|
1164
|
-
case 0:
|
|
1165
|
-
foundedUser = null;
|
|
1166
|
-
if (!user.email) return [3, 2];
|
|
1167
|
-
return [4, this.findUserByEmail(user.email)];
|
|
1168
|
-
case 1:
|
|
1169
|
-
foundedUser = _a.sent();
|
|
1170
|
-
return [3, 4];
|
|
1171
|
-
case 2:
|
|
1172
|
-
if (!user.mobile) return [3, 4];
|
|
1173
|
-
return [4, this.findUserByMobile(user.mobile)];
|
|
1174
|
-
case 3:
|
|
1175
|
-
foundedUser = _a.sent();
|
|
1176
|
-
_a.label = 4;
|
|
1177
|
-
case 4:
|
|
1178
|
-
if (!foundedUser)
|
|
1179
|
-
return [2, null];
|
|
1180
|
-
owner = foundedUser.id;
|
|
1181
|
-
return [4, this.verifyCodeByOwner(owner, code)];
|
|
1182
|
-
case 5:
|
|
1183
|
-
record = _a.sent();
|
|
1184
|
-
if (!record)
|
|
1185
|
-
return [2, null];
|
|
1186
|
-
if (!(user.email && foundedUser.email_verified != true)) return [3, 8];
|
|
1187
|
-
return [4, this.verifyEmail(owner, user.email)];
|
|
1188
|
-
case 6:
|
|
1189
|
-
_a.sent();
|
|
1190
|
-
return [4, this.findUserById(owner)];
|
|
1191
|
-
case 7:
|
|
1192
|
-
foundedUser = _a.sent();
|
|
1193
|
-
return [3, 11];
|
|
1194
|
-
case 8:
|
|
1195
|
-
if (!(user.mobile && foundedUser.mobile_verified != true)) return [3, 11];
|
|
1196
|
-
return [4, this.verifyMobile(owner, user.mobile)];
|
|
1197
|
-
case 9:
|
|
1198
|
-
_a.sent();
|
|
1199
|
-
return [4, this.findUserById(owner)];
|
|
1200
|
-
case 10:
|
|
1201
|
-
foundedUser = _a.sent();
|
|
1202
|
-
_a.label = 11;
|
|
1203
|
-
case 11: return [2, foundedUser];
|
|
1204
|
-
}
|
|
1205
|
-
});
|
|
1206
|
-
});
|
|
1207
|
-
};
|
|
1208
|
-
Mongo.prototype.getMySpaces = function (userId) {
|
|
1209
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1210
|
-
var userSpaces, spaceIds, spaces;
|
|
1211
|
-
return tslib_1.__generator(this, function (_a) {
|
|
1212
|
-
switch (_a.label) {
|
|
1213
|
-
case 0: return [4, this.db
|
|
1214
|
-
.collection("space_users")
|
|
1215
|
-
.find({ user: userId, invite_state: { $ne: "refused" } })
|
|
1216
|
-
.project({ space: 1, user_accepted: 1, invite_state: 1 })
|
|
1217
|
-
.toArray()];
|
|
1218
|
-
case 1:
|
|
1219
|
-
userSpaces = _a.sent();
|
|
1220
|
-
spaceIds = (0, lodash_1.map)(userSpaces, "space");
|
|
1221
|
-
return [4, this.db
|
|
1222
|
-
.collection("spaces")
|
|
1223
|
-
.find({ _id: { $in: spaceIds } })
|
|
1224
|
-
.project({
|
|
1225
|
-
name: 1,
|
|
1226
|
-
favicon: 1,
|
|
1227
|
-
account_logo: 1,
|
|
1228
|
-
avatar_square: 1,
|
|
1229
|
-
avatar: 1,
|
|
1230
|
-
background: 1,
|
|
1231
|
-
enable_register: 1,
|
|
1232
|
-
})
|
|
1233
|
-
.toArray()];
|
|
1234
|
-
case 2:
|
|
1235
|
-
spaces = _a.sent();
|
|
1236
|
-
return [2, (0, lodash_1.map)(spaces, function (space) {
|
|
1237
|
-
var spaceUser = (0, lodash_1.find)(userSpaces, function (item) {
|
|
1238
|
-
return item.space == space._id;
|
|
1239
|
-
});
|
|
1240
|
-
return tslib_1.__assign(tslib_1.__assign({}, space), { user_accepted: spaceUser.user_accepted, invite_state: spaceUser.invite_state });
|
|
1241
|
-
})];
|
|
1242
|
-
}
|
|
1243
|
-
});
|
|
1244
|
-
});
|
|
1245
|
-
};
|
|
1246
|
-
Mongo.prototype.getFirstSpace = function () {
|
|
1247
|
-
return tslib_1.__awaiter(this, void 0, void 0, function () {
|
|
1248
|
-
var space;
|
|
1249
|
-
return tslib_1.__generator(this, function (_a) {
|
|
1250
|
-
switch (_a.label) {
|
|
1251
|
-
case 0: return [4, this.db.collection("spaces").findOne()];
|
|
1252
|
-
case 1:
|
|
1253
|
-
space = _a.sent();
|
|
1254
|
-
return [2, space];
|
|
1255
|
-
}
|
|
1256
|
-
});
|
|
504
|
+
}
|
|
505
|
+
async createSession(userId, token, connection = {}, extraData) {
|
|
506
|
+
const infos = this.resolveInfo(connection);
|
|
507
|
+
const session = {
|
|
508
|
+
userId,
|
|
509
|
+
token,
|
|
510
|
+
...infos,
|
|
511
|
+
extraData,
|
|
512
|
+
valid: true,
|
|
513
|
+
[this.options.timestamps.createdAt]: this.options.dateProvider(),
|
|
514
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
515
|
+
};
|
|
516
|
+
if (this.options.idProvider) {
|
|
517
|
+
session._id = this.options.idProvider();
|
|
518
|
+
}
|
|
519
|
+
const ret = await this.sessionCollection.insertOne(session);
|
|
520
|
+
await this.updateMeteorSession(userId, token, infos);
|
|
521
|
+
return ret.insertedId.toString();
|
|
522
|
+
}
|
|
523
|
+
async updateSession(sessionId, connection) {
|
|
524
|
+
const _id = this.options.convertSessionIdToMongoObjectId
|
|
525
|
+
? toMongoID(sessionId)
|
|
526
|
+
: sessionId;
|
|
527
|
+
const infos = this.resolveInfo(connection);
|
|
528
|
+
let _set = {
|
|
529
|
+
userAgent: infos.userAgent,
|
|
530
|
+
ip: infos.ip,
|
|
531
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
532
|
+
};
|
|
533
|
+
if (infos.space) {
|
|
534
|
+
_set.space = infos.space;
|
|
535
|
+
}
|
|
536
|
+
await this.sessionCollection.updateOne({ _id }, {
|
|
537
|
+
$set: _set,
|
|
1257
538
|
});
|
|
1258
|
-
}
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
hashedToken.is_phone = is_phone;
|
|
1281
|
-
hashedToken.is_tablet = is_tablet;
|
|
1282
|
-
return [4, this.collection.updateOne({ _id: userId }, {
|
|
1283
|
-
$push: {
|
|
1284
|
-
"services.resume.loginTokens": hashedToken,
|
|
1285
|
-
},
|
|
1286
|
-
})];
|
|
1287
|
-
case 1:
|
|
1288
|
-
_a.sent();
|
|
1289
|
-
return [4, this.spaceUserCollection.updateMany({ user: userId }, {
|
|
1290
|
-
$set: {
|
|
1291
|
-
last_logon: new Date(),
|
|
1292
|
-
},
|
|
1293
|
-
})];
|
|
1294
|
-
case 2:
|
|
1295
|
-
_a.sent();
|
|
1296
|
-
return [2, true];
|
|
1297
|
-
}
|
|
1298
|
-
});
|
|
539
|
+
}
|
|
540
|
+
async invalidateSession(sessionId) {
|
|
541
|
+
const _id = this.options.convertSessionIdToMongoObjectId
|
|
542
|
+
? toMongoID(sessionId)
|
|
543
|
+
: sessionId;
|
|
544
|
+
await this.sessionCollection.updateOne({ _id }, {
|
|
545
|
+
$set: {
|
|
546
|
+
valid: false,
|
|
547
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
548
|
+
},
|
|
549
|
+
});
|
|
550
|
+
const session = await this.sessionCollection.findOne({
|
|
551
|
+
_id: _id,
|
|
552
|
+
});
|
|
553
|
+
await this.destroyMeteorToken(session.userId, session.token);
|
|
554
|
+
}
|
|
555
|
+
async invalidateAllSessions(userId) {
|
|
556
|
+
await this.sessionCollection.updateMany({ userId }, {
|
|
557
|
+
$set: {
|
|
558
|
+
valid: false,
|
|
559
|
+
[this.options.timestamps.updatedAt]: this.options.dateProvider(),
|
|
560
|
+
},
|
|
1299
561
|
});
|
|
1300
|
-
}
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
562
|
+
}
|
|
563
|
+
async findSessionByToken(token) {
|
|
564
|
+
const session = await this.sessionCollection.findOne({ token });
|
|
565
|
+
if (session) {
|
|
566
|
+
session.id = session._id.toString();
|
|
567
|
+
}
|
|
568
|
+
return session;
|
|
569
|
+
}
|
|
570
|
+
async findSessionById(sessionId) {
|
|
571
|
+
const _id = this.options.convertSessionIdToMongoObjectId
|
|
572
|
+
? toMongoID(sessionId)
|
|
573
|
+
: sessionId;
|
|
574
|
+
const session = await this.sessionCollection.findOne({ _id });
|
|
575
|
+
if (session) {
|
|
576
|
+
session.id = session._id.toString();
|
|
577
|
+
}
|
|
578
|
+
return session;
|
|
579
|
+
}
|
|
580
|
+
async addEmailVerificationToken(userId, email, token) {
|
|
581
|
+
const _id = this.options.convertUserIdToMongoObjectId
|
|
582
|
+
? toMongoID(userId)
|
|
583
|
+
: userId;
|
|
584
|
+
await this.collection.updateOne({ _id }, {
|
|
585
|
+
$push: {
|
|
586
|
+
"services.email.verificationTokens": {
|
|
587
|
+
token,
|
|
588
|
+
address: email.toLowerCase(),
|
|
589
|
+
when: this.options.dateProvider(),
|
|
590
|
+
},
|
|
591
|
+
},
|
|
1325
592
|
});
|
|
1326
|
-
}
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
593
|
+
}
|
|
594
|
+
async addResetPasswordToken(userId, email, token, reason) {
|
|
595
|
+
const _id = this.options.convertUserIdToMongoObjectId
|
|
596
|
+
? toMongoID(userId)
|
|
597
|
+
: userId;
|
|
598
|
+
await this.collection.updateOne({ _id }, {
|
|
599
|
+
$push: {
|
|
600
|
+
"services.password.reset": {
|
|
601
|
+
token,
|
|
602
|
+
address: email.toLowerCase(),
|
|
603
|
+
when: this.options.dateProvider(),
|
|
604
|
+
reason,
|
|
605
|
+
},
|
|
606
|
+
},
|
|
1335
607
|
});
|
|
1336
|
-
}
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
608
|
+
}
|
|
609
|
+
async setResetPassword(userId, email, newPassword) {
|
|
610
|
+
await this.setPassword(userId, newPassword);
|
|
611
|
+
}
|
|
612
|
+
async applyCode(name, owner, nextCode, MAX_FAILURE_COUNT, EFFECTIVE_TIME) {
|
|
613
|
+
const now = new Date();
|
|
614
|
+
if (!(0, lodash_2.isString)(name)) {
|
|
615
|
+
throw new Error("accounts.invalidCode");
|
|
616
|
+
}
|
|
617
|
+
const query = {
|
|
618
|
+
name: name,
|
|
619
|
+
verifiedAt: null,
|
|
620
|
+
expiredAt: { $gt: now },
|
|
621
|
+
};
|
|
622
|
+
if (owner) {
|
|
623
|
+
query.owner = owner;
|
|
624
|
+
}
|
|
625
|
+
let record = await this.codeCollection.findOne(query);
|
|
626
|
+
if (record) {
|
|
627
|
+
}
|
|
628
|
+
else {
|
|
629
|
+
let doc = {
|
|
630
|
+
name,
|
|
631
|
+
owner,
|
|
632
|
+
code: nextCode,
|
|
633
|
+
expiredAt: new Date(moment().add(EFFECTIVE_TIME, "m")),
|
|
634
|
+
[this.options.timestamps.createdAt]: this.options.dateProvider(),
|
|
635
|
+
};
|
|
636
|
+
if (this.options.idProvider) {
|
|
637
|
+
doc._id = this.options.idProvider();
|
|
638
|
+
}
|
|
639
|
+
let result = await this.codeCollection.insertOne(doc);
|
|
640
|
+
record = result.ops[0];
|
|
641
|
+
}
|
|
642
|
+
return record;
|
|
643
|
+
}
|
|
644
|
+
async addVerificationCode(user, code, options) {
|
|
645
|
+
let foundedUser = null;
|
|
646
|
+
if (user.email)
|
|
647
|
+
foundedUser = await this.findUserByEmail(user.email);
|
|
648
|
+
else if (user.mobile)
|
|
649
|
+
foundedUser = await this.findUserByMobile(user.mobile);
|
|
650
|
+
const owner = foundedUser ? foundedUser.id : null;
|
|
651
|
+
const ret = await this.applyCode(user.email ? user.email : user.mobile, owner, code, options.MAX_FAILURE_COUNT, options.EFFECTIVE_TIME);
|
|
652
|
+
return ret;
|
|
653
|
+
}
|
|
654
|
+
async verifyCodeByName(name, code) {
|
|
655
|
+
if (!(0, lodash_2.isString)(code)) {
|
|
656
|
+
throw new Error("accounts.invalidCode");
|
|
657
|
+
}
|
|
658
|
+
if (!(0, lodash_2.isString)(name)) {
|
|
659
|
+
throw new Error("accounts.invalidCode");
|
|
660
|
+
}
|
|
661
|
+
const now = new Date();
|
|
662
|
+
let query = {
|
|
663
|
+
name: name,
|
|
664
|
+
code: code,
|
|
665
|
+
verifiedAt: null,
|
|
666
|
+
expiredAt: { $gt: now },
|
|
667
|
+
};
|
|
668
|
+
let result = await this.codeCollection.findOne(query);
|
|
669
|
+
if (result) {
|
|
670
|
+
await this.codeCollection.updateOne({ _id: result._id }, { $set: { verifiedAt: now } });
|
|
671
|
+
return result;
|
|
672
|
+
}
|
|
673
|
+
else {
|
|
674
|
+
throw new Error("accounts.invalidCode");
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
async verifyCodeByOwner(owner, code) {
|
|
678
|
+
if (!(0, lodash_2.isString)(code)) {
|
|
679
|
+
throw new Error("accounts.invalidCode");
|
|
680
|
+
}
|
|
681
|
+
if (!(0, lodash_2.isString)(owner)) {
|
|
682
|
+
throw new Error("accounts.invalidCode");
|
|
683
|
+
}
|
|
684
|
+
const now = new Date();
|
|
685
|
+
let query = {
|
|
686
|
+
owner: owner,
|
|
687
|
+
code: code,
|
|
688
|
+
verifiedAt: null,
|
|
689
|
+
expiredAt: { $gt: now },
|
|
690
|
+
};
|
|
691
|
+
let result = await this.codeCollection.findOne(query);
|
|
692
|
+
if (result) {
|
|
693
|
+
await this.codeCollection.updateOne({ _id: result._id }, { $set: { verifiedAt: now } });
|
|
694
|
+
return result;
|
|
695
|
+
}
|
|
696
|
+
else {
|
|
697
|
+
console.log("verifyCodeByOwner throw new Error accounts.invalidCode");
|
|
698
|
+
throw new Error("accounts.invalidCode");
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
async checkVerificationCode(user, code) {
|
|
702
|
+
let name = null;
|
|
703
|
+
if (user.email)
|
|
704
|
+
name = user.email;
|
|
705
|
+
else if (user.mobile)
|
|
706
|
+
name = user.mobile;
|
|
707
|
+
if (!name)
|
|
708
|
+
return false;
|
|
709
|
+
const record = await this.verifyCodeByName(name, code);
|
|
710
|
+
if (!record)
|
|
711
|
+
return false;
|
|
712
|
+
return true;
|
|
713
|
+
}
|
|
714
|
+
async findUserByVerificationCode(user, code) {
|
|
715
|
+
let foundedUser = null;
|
|
716
|
+
if (user.email)
|
|
717
|
+
foundedUser = await this.findUserByEmail(user.email);
|
|
718
|
+
else if (user.mobile)
|
|
719
|
+
foundedUser = await this.findUserByMobile(user.mobile);
|
|
720
|
+
if (!foundedUser)
|
|
721
|
+
return null;
|
|
722
|
+
const owner = foundedUser.id;
|
|
723
|
+
const record = await this.verifyCodeByOwner(owner, code);
|
|
724
|
+
if (!record)
|
|
725
|
+
return null;
|
|
726
|
+
if (user.email && foundedUser.email_verified != true) {
|
|
727
|
+
await this.verifyEmail(owner, user.email);
|
|
728
|
+
foundedUser = await this.findUserById(owner);
|
|
729
|
+
}
|
|
730
|
+
else if (user.mobile && foundedUser.mobile_verified != true) {
|
|
731
|
+
await this.verifyMobile(owner, user.mobile);
|
|
732
|
+
foundedUser = await this.findUserById(owner);
|
|
733
|
+
}
|
|
734
|
+
return foundedUser;
|
|
735
|
+
}
|
|
736
|
+
async getMySpaces(userId) {
|
|
737
|
+
const userSpaces = await this.db
|
|
738
|
+
.collection("space_users")
|
|
739
|
+
.find({ user: userId, invite_state: { $ne: "refused" } })
|
|
740
|
+
.project({ space: 1, user_accepted: 1, invite_state: 1 })
|
|
741
|
+
.toArray();
|
|
742
|
+
const spaceIds = (0, lodash_1.map)(userSpaces, "space");
|
|
743
|
+
const spaces = await this.db
|
|
744
|
+
.collection("spaces")
|
|
745
|
+
.find({ _id: { $in: spaceIds } })
|
|
746
|
+
.project({
|
|
747
|
+
name: 1,
|
|
748
|
+
favicon: 1,
|
|
749
|
+
account_logo: 1,
|
|
750
|
+
avatar_square: 1,
|
|
751
|
+
avatar: 1,
|
|
752
|
+
background: 1,
|
|
753
|
+
enable_register: 1,
|
|
754
|
+
})
|
|
755
|
+
.toArray();
|
|
756
|
+
return (0, lodash_1.map)(spaces, function (space) {
|
|
757
|
+
const spaceUser = (0, lodash_1.find)(userSpaces, (item) => {
|
|
758
|
+
return item.space == space._id;
|
|
1341
759
|
});
|
|
760
|
+
return {
|
|
761
|
+
...space,
|
|
762
|
+
user_accepted: spaceUser.user_accepted,
|
|
763
|
+
invite_state: spaceUser.invite_state,
|
|
764
|
+
};
|
|
1342
765
|
});
|
|
1343
|
-
}
|
|
1344
|
-
|
|
1345
|
-
|
|
766
|
+
}
|
|
767
|
+
async getFirstSpace() {
|
|
768
|
+
const space = await this.db.collection("spaces").findOne();
|
|
769
|
+
return space;
|
|
770
|
+
}
|
|
771
|
+
async updateMeteorSession(userId, token, infos) {
|
|
772
|
+
let when = new Date();
|
|
773
|
+
const { login_expiration_in_days, is_phone, is_tablet } = infos;
|
|
774
|
+
if (login_expiration_in_days &&
|
|
775
|
+
(0, lodash_2.isNumber)(login_expiration_in_days) &&
|
|
776
|
+
login_expiration_in_days > 0) {
|
|
777
|
+
when = moment()
|
|
778
|
+
.subtract((90 - login_expiration_in_days) * 24 * 60, "minute")
|
|
779
|
+
.toDate();
|
|
780
|
+
}
|
|
781
|
+
let stampedAuthToken = {
|
|
782
|
+
token: token,
|
|
783
|
+
when: when,
|
|
784
|
+
};
|
|
785
|
+
const now = new Date();
|
|
786
|
+
let hashedToken = (0, auth_1.hashStampedToken)(stampedAuthToken);
|
|
787
|
+
hashedToken.created = now;
|
|
788
|
+
hashedToken.is_phone = is_phone;
|
|
789
|
+
hashedToken.is_tablet = is_tablet;
|
|
790
|
+
await this.collection.updateOne({ _id: userId }, {
|
|
791
|
+
$push: {
|
|
792
|
+
"services.resume.loginTokens": hashedToken,
|
|
793
|
+
},
|
|
794
|
+
$set: {
|
|
795
|
+
last_logon: now,
|
|
796
|
+
},
|
|
797
|
+
});
|
|
798
|
+
await this.spaceUserCollection.updateMany({ user: userId }, {
|
|
799
|
+
$set: {
|
|
800
|
+
last_logon: now,
|
|
801
|
+
},
|
|
802
|
+
});
|
|
803
|
+
return true;
|
|
804
|
+
}
|
|
805
|
+
async destroyMeteorToken(userId, token) {
|
|
806
|
+
let stampedAuthToken = {
|
|
807
|
+
token: token,
|
|
808
|
+
when: new Date(),
|
|
809
|
+
};
|
|
810
|
+
let hashedTokenDoc = (0, auth_1.hashStampedToken)(stampedAuthToken);
|
|
811
|
+
let loginToken = hashedTokenDoc.hashedToken;
|
|
812
|
+
await this.collection.updateOne({ _id: userId }, {
|
|
813
|
+
$pull: {
|
|
814
|
+
"services.resume.loginTokens": {
|
|
815
|
+
$or: [{ hashedToken: loginToken }, { token: loginToken }],
|
|
816
|
+
},
|
|
817
|
+
},
|
|
818
|
+
});
|
|
819
|
+
return true;
|
|
820
|
+
}
|
|
821
|
+
async getInviteInfo(id) {
|
|
822
|
+
return await this.inviteCollection.findOne({ _id: id });
|
|
823
|
+
}
|
|
824
|
+
async updateUser(userId, options) {
|
|
825
|
+
return this.collection.updateOne({ _id: userId }, options);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
1346
828
|
exports.Mongo = Mongo;
|
|
1347
829
|
//# sourceMappingURL=mongo.js.map
|