@modular-rest/server 1.6.2 → 1.6.4
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/package.json
CHANGED
|
@@ -1,21 +1,31 @@
|
|
|
1
|
-
const DataProvider = require(
|
|
1
|
+
const DataProvider = require("../services/data_provider/service");
|
|
2
2
|
|
|
3
3
|
function createPermissions() {
|
|
4
|
-
let model = DataProvider.getCollection(
|
|
5
|
-
|
|
6
|
-
return new Promise(async (done, reject) => {
|
|
4
|
+
let model = DataProvider.getCollection("cms", "permission");
|
|
7
5
|
|
|
6
|
+
return new Promise(async (done, reject) => {
|
|
8
7
|
// create customer permission
|
|
9
|
-
let isAnonymousExisted = await model
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
let isAnonymousExisted = await model
|
|
9
|
+
.countDocuments({ title: "anonymous" })
|
|
10
|
+
.exec()
|
|
11
|
+
.catch(reject);
|
|
12
|
+
let isCoustomerExisted = await model
|
|
13
|
+
.countDocuments({ title: "customer" })
|
|
14
|
+
.exec()
|
|
15
|
+
.catch(reject);
|
|
16
|
+
let isAdministratorExisted = await model
|
|
17
|
+
.countDocuments({ title: "administrator" })
|
|
18
|
+
.exec()
|
|
19
|
+
.catch(reject);
|
|
12
20
|
|
|
13
21
|
if (!isAnonymousExisted) {
|
|
14
22
|
await new model({
|
|
15
23
|
anonymous_access: true,
|
|
16
24
|
isAnonymous: true,
|
|
17
|
-
title:
|
|
18
|
-
})
|
|
25
|
+
title: "anonymous",
|
|
26
|
+
})
|
|
27
|
+
.save()
|
|
28
|
+
.catch(reject);
|
|
19
29
|
}
|
|
20
30
|
|
|
21
31
|
if (!isCoustomerExisted) {
|
|
@@ -25,8 +35,10 @@ function createPermissions() {
|
|
|
25
35
|
upload_file_access: true,
|
|
26
36
|
remove_file_access: true,
|
|
27
37
|
isDefault: true,
|
|
28
|
-
title:
|
|
29
|
-
})
|
|
38
|
+
title: "customer",
|
|
39
|
+
})
|
|
40
|
+
.save()
|
|
41
|
+
.catch(reject);
|
|
30
42
|
}
|
|
31
43
|
|
|
32
44
|
if (!isAdministratorExisted) {
|
|
@@ -36,50 +48,61 @@ function createPermissions() {
|
|
|
36
48
|
anonymous_access: true,
|
|
37
49
|
upload_file_access: true,
|
|
38
50
|
remove_file_access: true,
|
|
39
|
-
title:
|
|
40
|
-
})
|
|
51
|
+
title: "administrator",
|
|
52
|
+
})
|
|
53
|
+
.save()
|
|
54
|
+
.catch(reject);
|
|
41
55
|
}
|
|
42
56
|
|
|
43
57
|
done();
|
|
44
58
|
});
|
|
45
|
-
|
|
46
59
|
}
|
|
47
60
|
|
|
48
|
-
function createAdminUser({ email, password }) {
|
|
49
|
-
let permissionModel = DataProvider.getCollection(
|
|
50
|
-
let authModel = DataProvider.getCollection(
|
|
61
|
+
async function createAdminUser({ email, password }) {
|
|
62
|
+
let permissionModel = DataProvider.getCollection("cms", "permission");
|
|
63
|
+
let authModel = DataProvider.getCollection("cms", "auth");
|
|
51
64
|
|
|
52
|
-
|
|
53
|
-
let isAnonymousExisted = await authModel
|
|
54
|
-
|
|
65
|
+
try {
|
|
66
|
+
let isAnonymousExisted = await authModel
|
|
67
|
+
.countDocuments({ type: "anonymous" })
|
|
68
|
+
.exec();
|
|
55
69
|
|
|
56
|
-
let
|
|
57
|
-
|
|
70
|
+
let isAdministratorExisted = await authModel
|
|
71
|
+
.countDocuments({ type: "user", email: email })
|
|
72
|
+
.exec();
|
|
58
73
|
|
|
59
|
-
|
|
74
|
+
let anonymousPermission = await permissionModel
|
|
75
|
+
.findOne({ title: "anonymous" })
|
|
76
|
+
.exec();
|
|
77
|
+
|
|
78
|
+
let administratorPermission = await permissionModel
|
|
79
|
+
.findOne({ title: "administrator" })
|
|
80
|
+
.exec();
|
|
81
|
+
|
|
82
|
+
if (isAnonymousExisted == 0) {
|
|
60
83
|
await new authModel({
|
|
61
84
|
permission: anonymousPermission._id,
|
|
62
|
-
email:
|
|
63
|
-
phone:
|
|
64
|
-
password:
|
|
65
|
-
type:
|
|
66
|
-
}).save()
|
|
85
|
+
email: "",
|
|
86
|
+
phone: "",
|
|
87
|
+
password: "",
|
|
88
|
+
type: "anonymous",
|
|
89
|
+
}).save();
|
|
67
90
|
}
|
|
68
91
|
|
|
69
|
-
if (
|
|
92
|
+
if (isAdministratorExisted == 0) {
|
|
70
93
|
await new authModel({
|
|
71
94
|
permission: administratorPermission._id,
|
|
72
95
|
email: email,
|
|
73
96
|
password: password,
|
|
74
|
-
type:
|
|
75
|
-
}).save()
|
|
97
|
+
type: "user",
|
|
98
|
+
}).save();
|
|
76
99
|
}
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
}
|
|
100
|
+
} catch (e) {
|
|
101
|
+
return Promise.reject(e);
|
|
102
|
+
}
|
|
80
103
|
}
|
|
81
104
|
|
|
82
105
|
module.exports = {
|
|
83
106
|
createPermissions,
|
|
84
107
|
createAdminUser,
|
|
85
|
-
};
|
|
108
|
+
};
|
|
@@ -35,38 +35,14 @@ let authSchema = new Schema({
|
|
|
35
35
|
type: { type: String, default: "user", enum: ["user", "anonymous"] },
|
|
36
36
|
});
|
|
37
37
|
authSchema.index({ email: 1 }, { unique: true });
|
|
38
|
-
|
|
39
|
-
authSchema
|
|
40
|
-
.virtual("base64Password")
|
|
41
|
-
.get(function () {
|
|
42
|
-
return Buffer.from(this.password, "base64").toString("utf-8");
|
|
43
|
-
})
|
|
44
|
-
.set(function (value) {
|
|
45
|
-
this.password = Buffer.from(value).toString("base64");
|
|
46
|
-
});
|
|
47
|
-
|
|
48
|
-
// Middleware for encoding password on save and init
|
|
49
|
-
authSchema.pre("save", function (next) {
|
|
38
|
+
authSchema.pre(["save", "updateOne"], function (next) {
|
|
50
39
|
// Encode the password before saving
|
|
51
40
|
if (this.isModified("password")) {
|
|
52
|
-
this.
|
|
41
|
+
this.password = Buffer.from(this.password).toString("base64");
|
|
53
42
|
}
|
|
54
43
|
next();
|
|
55
44
|
});
|
|
56
45
|
|
|
57
|
-
authSchema.pre("init", function (next) {
|
|
58
|
-
// Decode the password on init
|
|
59
|
-
this.password = this.base64Password;
|
|
60
|
-
next();
|
|
61
|
-
});
|
|
62
|
-
|
|
63
|
-
// Middleware for decoding password on findOne
|
|
64
|
-
authSchema.post("findOne", function (doc) {
|
|
65
|
-
if (this._conditions.password) {
|
|
66
|
-
this._conditions.password = Buffer.from(value).toString("base64");
|
|
67
|
-
}
|
|
68
|
-
});
|
|
69
|
-
|
|
70
46
|
module.exports = [
|
|
71
47
|
new CollectionDefinition({
|
|
72
48
|
db: "cms",
|
|
@@ -1,271 +1,275 @@
|
|
|
1
|
-
let User = require(
|
|
2
|
-
const DataProvider = require(
|
|
3
|
-
const JWT = require(
|
|
1
|
+
let User = require("../../class/user");
|
|
2
|
+
const DataProvider = require("../data_provider/service");
|
|
3
|
+
const JWT = require("../jwt/service");
|
|
4
4
|
|
|
5
5
|
class UserManager {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
6
|
+
constructor() {
|
|
7
|
+
this.tempIds = {};
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
*
|
|
12
|
+
* @param {function} method a method that return random verification code
|
|
13
|
+
*/
|
|
14
|
+
setCustomVerificationCodeGeneratorMethod(method) {
|
|
15
|
+
this.verificationCodeGeneratorMethod = method;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
generateVerificationCode(id, idType) {
|
|
19
|
+
if (this.verificationCodeGeneratorMethod)
|
|
20
|
+
return this.verificationCodeGeneratorMethod(id, idType);
|
|
21
|
+
|
|
22
|
+
// this is default code
|
|
23
|
+
return "123";
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Get a user by its Id.
|
|
28
|
+
*
|
|
29
|
+
* @param {string} id userid
|
|
30
|
+
*/
|
|
31
|
+
getUserById(id) {
|
|
32
|
+
return new Promise(async (done, reject) => {
|
|
33
|
+
let userModel = DataProvider.getCollection("cms", "auth");
|
|
34
|
+
|
|
35
|
+
let userDoc = await userModel
|
|
36
|
+
.findOne({ _id: id })
|
|
37
|
+
.select({ password: 0 })
|
|
38
|
+
.populate("permission")
|
|
39
|
+
.exec()
|
|
40
|
+
.catch(reject);
|
|
41
|
+
|
|
42
|
+
if (!userDoc) reject("user not found");
|
|
43
|
+
|
|
44
|
+
let user = User.loadFromModel(userDoc);
|
|
45
|
+
done(user);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Get user by token.
|
|
51
|
+
*
|
|
52
|
+
* @param {string} token auth token
|
|
53
|
+
*/
|
|
54
|
+
getUserByToken(token) {
|
|
55
|
+
return JWT.main.verify(token).then(async (payload) => {
|
|
56
|
+
let user = payload;
|
|
57
|
+
let permission = await DataProvider.getCollection("cms", "permission")
|
|
58
|
+
.findOne({ _id: user.permission })
|
|
59
|
+
.exec()
|
|
60
|
+
.then();
|
|
61
|
+
|
|
62
|
+
if (!permission) throw "user has a wrong permission";
|
|
63
|
+
|
|
64
|
+
user.permission = permission;
|
|
65
|
+
return user;
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Define whether or not verification code is valid.
|
|
71
|
+
*
|
|
72
|
+
* @param {string} id auth id phone|email
|
|
73
|
+
* @param {string} code verification code
|
|
74
|
+
*/
|
|
75
|
+
isCodeValid(id, code) {
|
|
76
|
+
let key = false;
|
|
77
|
+
|
|
78
|
+
if (
|
|
79
|
+
this.tempIds.hasOwnProperty(id) &&
|
|
80
|
+
this.tempIds[id].code.toString() === code.toString()
|
|
81
|
+
)
|
|
82
|
+
key = true;
|
|
83
|
+
|
|
84
|
+
return key;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Login and return user token.
|
|
89
|
+
*
|
|
90
|
+
* @param {string} id auth id phone|email
|
|
91
|
+
* @param {string} idType auth type phone|email
|
|
92
|
+
* @param {string} password
|
|
93
|
+
*/
|
|
94
|
+
loginUser(id = "", idType = "", password = "") {
|
|
95
|
+
let token;
|
|
96
|
+
|
|
97
|
+
return new Promise(async (done, reject) => {
|
|
98
|
+
// Get user model
|
|
99
|
+
let userModel = DataProvider.getCollection("cms", "auth");
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Setup query to find by phone or email
|
|
103
|
+
*/
|
|
104
|
+
let query = {
|
|
105
|
+
password: Buffer.from(password).toString("base64"),
|
|
106
|
+
type: "user",
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
if (idType == "phone") query["phone"] = id;
|
|
110
|
+
else if (idType == "email") query["email"] = id;
|
|
111
|
+
|
|
112
|
+
// Get from database
|
|
113
|
+
let gottenFromDB = await userModel
|
|
114
|
+
.findOne(query)
|
|
115
|
+
.populate("permission")
|
|
116
|
+
.exec()
|
|
117
|
+
.catch(reject);
|
|
118
|
+
|
|
119
|
+
if (!gottenFromDB) reject("user not found");
|
|
120
|
+
// Token
|
|
121
|
+
else {
|
|
122
|
+
// Load user
|
|
123
|
+
let user = await User.loadFromModel(gottenFromDB).then().catch(reject);
|
|
124
|
+
|
|
125
|
+
// Get token payload
|
|
126
|
+
// This is some information about the user.
|
|
127
|
+
let payload = user.getBrief();
|
|
128
|
+
|
|
129
|
+
// Generate json web token
|
|
130
|
+
token = await JWT.main.sign(payload).then().catch(reject);
|
|
131
|
+
|
|
132
|
+
done(token);
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Login as anonymous user
|
|
139
|
+
*/
|
|
140
|
+
loginAnonymous() {
|
|
141
|
+
let token;
|
|
142
|
+
|
|
143
|
+
return new Promise(async (done, reject) => {
|
|
144
|
+
// Get user model
|
|
145
|
+
let userModel = DataProvider.getCollection("cms", "auth");
|
|
146
|
+
|
|
147
|
+
// Setup query
|
|
148
|
+
let query = { type: "anonymous" };
|
|
149
|
+
|
|
150
|
+
// Get from database
|
|
151
|
+
let gottenFromDB = await userModel
|
|
152
|
+
.findOne(query)
|
|
153
|
+
.populate("permission")
|
|
154
|
+
.exec()
|
|
155
|
+
.then()
|
|
156
|
+
.catch(reject);
|
|
157
|
+
|
|
158
|
+
// Create a new anonymous user if it doesn't exist.
|
|
159
|
+
// There are only one anonymous user in the database
|
|
160
|
+
// and every guest token being generated from it.
|
|
161
|
+
if (!gottenFromDB) {
|
|
162
|
+
let newUserId = await this.registerUser({ type: "anonymous" }).catch(
|
|
163
|
+
reject
|
|
164
|
+
);
|
|
165
|
+
gottenFromDB = await this.getUserById(newUserId).catch(reject);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// load User
|
|
169
|
+
let user = await User.loadFromModel(gottenFromDB).then().catch(reject);
|
|
170
|
+
|
|
171
|
+
// Get token payload
|
|
172
|
+
// This is some information about the user.
|
|
173
|
+
let payload = user.getBrief();
|
|
174
|
+
|
|
175
|
+
// Generate json web token
|
|
176
|
+
token = await JWT.main.sign(payload).then().catch(reject);
|
|
177
|
+
|
|
178
|
+
done(token);
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Store user email|phone temporarily in memory.
|
|
184
|
+
*
|
|
185
|
+
* @param {string} id id is username or phone number
|
|
186
|
+
* @param {string} type type is the type of id
|
|
187
|
+
* @param {string} code code is a string being sent to user and he/she must return it back.
|
|
188
|
+
*/
|
|
189
|
+
registerTemporaryID(id, type, code) {
|
|
190
|
+
this.tempIds[id] = { id: id, type: type, code: code };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async submitPasswordForTemporaryID(id, password, code) {
|
|
194
|
+
let key = false;
|
|
195
|
+
|
|
196
|
+
// If user email|phone has already stored
|
|
197
|
+
// a new user being created
|
|
198
|
+
if (
|
|
199
|
+
this.tempIds.hasOwnProperty(id) &&
|
|
200
|
+
this.tempIds[id].code.toString() == code.toString()
|
|
201
|
+
) {
|
|
202
|
+
let authDetail = { password: password };
|
|
203
|
+
|
|
204
|
+
if (this.tempIds[id].type == "phone") authDetail["phone"] = id;
|
|
205
|
+
else if (this.tempIds[id].type == "email") authDetail["email"] = id;
|
|
206
|
+
|
|
207
|
+
await this.registerUser(authDetail)
|
|
208
|
+
.then(() => (key = true))
|
|
209
|
+
.catch((e) => console.log(e));
|
|
210
210
|
}
|
|
211
211
|
|
|
212
|
-
|
|
213
|
-
|
|
212
|
+
delete this.tempIds[id];
|
|
213
|
+
return key;
|
|
214
|
+
}
|
|
214
215
|
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
let query = {};
|
|
216
|
+
async changePasswordForTemporaryID(id, password, code) {
|
|
217
|
+
let key = false;
|
|
218
218
|
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
else if (this.tempIds[id].type == 'email')
|
|
222
|
-
query['email'] = id;
|
|
219
|
+
if (this.tempIds.hasOwnProperty(id) && this.tempIds[id].code == code) {
|
|
220
|
+
let query = {};
|
|
223
221
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
.catch((e) => console.log(e));
|
|
227
|
-
}
|
|
222
|
+
if (this.tempIds[id].type == "phone") query["phone"] = id;
|
|
223
|
+
else if (this.tempIds[id].type == "email") query["email"] = id;
|
|
228
224
|
|
|
229
|
-
|
|
230
|
-
|
|
225
|
+
await this.changePassword(query, password)
|
|
226
|
+
.then(() => (key = true))
|
|
227
|
+
.catch((e) => console.log(e));
|
|
231
228
|
}
|
|
232
229
|
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
let permissionId;
|
|
237
|
-
let perM = DataProvider.getCollection('cms', 'permission');
|
|
230
|
+
delete this.tempIds[id];
|
|
231
|
+
return key;
|
|
232
|
+
}
|
|
238
233
|
|
|
239
|
-
|
|
234
|
+
registerUser(detail) {
|
|
235
|
+
return new Promise(async (done, reject) => {
|
|
236
|
+
// get default permission
|
|
237
|
+
let permissionId;
|
|
238
|
+
let perM = DataProvider.getCollection("cms", "permission");
|
|
240
239
|
|
|
241
|
-
|
|
242
|
-
pQuery = { isAnonymous: true };
|
|
240
|
+
let pQuery = { isDefault: true };
|
|
243
241
|
|
|
244
|
-
|
|
245
|
-
.then((doc) => permissionId = doc._id)
|
|
246
|
-
.catch(reject);
|
|
242
|
+
if (detail.type == "anonymous") pQuery = { isAnonymous: true };
|
|
247
243
|
|
|
248
|
-
|
|
244
|
+
await perM
|
|
245
|
+
.findOne(pQuery, "_id")
|
|
246
|
+
.exec()
|
|
247
|
+
.then((doc) => (permissionId = doc._id))
|
|
248
|
+
.catch(reject);
|
|
249
249
|
|
|
250
|
-
|
|
251
|
-
return User.createFromModel(authM, detail)
|
|
252
|
-
.then(newUser => {
|
|
253
|
-
DataProvider.triggers
|
|
254
|
-
.call('insertOne', 'cms', 'auth', { 'input': detail, 'output': newUser.dbModel });
|
|
250
|
+
detail.permission = permissionId;
|
|
255
251
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
252
|
+
let authM = DataProvider.getCollection("cms", "auth");
|
|
253
|
+
return User.createFromModel(authM, detail)
|
|
254
|
+
.then((newUser) => {
|
|
255
|
+
DataProvider.triggers.call("insertOne", "cms", "auth", {
|
|
256
|
+
input: detail,
|
|
257
|
+
output: newUser.dbModel,
|
|
258
|
+
});
|
|
261
259
|
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
260
|
+
done(newUser.id);
|
|
261
|
+
})
|
|
262
|
+
.catch(reject);
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
changePassword(query, newPass) {
|
|
267
|
+
let update = { $set: { password: newPass } };
|
|
268
|
+
let authM = DataProvider.getCollection("cms", "auth");
|
|
269
|
+
return authM.updateOne(query, update).exec().then();
|
|
270
|
+
}
|
|
267
271
|
}
|
|
268
272
|
|
|
269
|
-
UserManager.instance = new UserManager()
|
|
270
|
-
module.exports.name =
|
|
271
|
-
module.exports.main = UserManager.instance;
|
|
273
|
+
UserManager.instance = new UserManager();
|
|
274
|
+
module.exports.name = "userManager";
|
|
275
|
+
module.exports.main = UserManager.instance;
|