-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
create.js
189 lines (161 loc) · 5.42 KB
/
create.js
1
2
3
4
5
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
'use strict';
const zxcvbn = require('zxcvbn');
const db = require('../database');
const utils = require('../utils');
const plugins = require('../plugins');
const groups = require('../groups');
const meta = require('../meta');
module.exports = function (User) {
User.create = async function (data) {
data.username = data.username.trim();
data.userslug = utils.slugify(data.username);
if (data.email !== undefined) {
data.email = String(data.email).trim();
}
try {
await lock(data.username, '[[error:username-taken]]');
if (data.email) {
await lock(data.email, '[[error:email-taken]]');
}
await User.isDataValid(data);
return await create(data);
} finally {
await db.deleteObjectFields('locks', [data.username, data.email]);
}
};
async function lock(value, error) {
const count = await db.incrObjectField('locks', value);
if (count > 1) {
throw new Error(error);
}
}
async function create(data) {
const timestamp = data.timestamp || Date.now();
let userData = {
username: data.username,
userslug: data.userslug,
email: data.email || '',
joindate: timestamp,
lastonline: timestamp,
status: 'online',
};
['picture', 'fullname', 'location', 'birthday'].forEach((field) => {
if (data[field]) {
userData[field] = data[field];
}
});
if (data.gdpr_consent === true) {
userData.gdpr_consent = 1;
}
if (data.acceptTos === true) {
userData.acceptTos = 1;
}
const renamedUsername = await User.uniqueUsername(userData);
const userNameChanged = !!renamedUsername;
if (userNameChanged) {
userData.username = renamedUsername;
userData.userslug = utils.slugify(renamedUsername);
}
const results = await plugins.fireHook('filter:user.create', { user: userData, data: data });
userData = results.user;
const uid = await db.incrObjectField('global', 'nextUid');
userData.uid = uid;
await db.setObject('user:' + uid, userData);
const bulkAdd = [
['username:uid', userData.uid, userData.username],
['user:' + userData.uid + ':usernames', timestamp, userData.username + ':' + timestamp],
['username:sorted', 0, userData.username.toLowerCase() + ':' + userData.uid],
['userslug:uid', userData.uid, userData.userslug],
['users:joindate', timestamp, userData.uid],
['users:online', timestamp, userData.uid],
['users:postcount', 0, userData.uid],
['users:reputation', 0, userData.uid],
];
if (parseInt(userData.uid, 10) !== 1) {
bulkAdd.push(['users:notvalidated', timestamp, userData.uid]);
}
if (userData.email) {
bulkAdd.push(['email:uid', userData.uid, userData.email.toLowerCase()]);
bulkAdd.push(['email:sorted', 0, userData.email.toLowerCase() + ':' + userData.uid]);
bulkAdd.push(['user:' + userData.uid + ':emails', timestamp, userData.email + ':' + timestamp]);
}
if (userData.fullname) {
bulkAdd.push(['fullname:sorted', 0, userData.fullname.toLowerCase() + ':' + userData.uid]);
}
await Promise.all([
db.incrObjectField('global', 'userCount'),
db.sortedSetAddBulk(bulkAdd),
groups.join('registered-users', userData.uid),
User.notifications.sendWelcomeNotification(userData.uid),
storePassword(userData.uid, data.password),
User.updateDigestSetting(userData.uid, meta.config.dailyDigestFreq),
]);
if (userData.email && userData.uid > 1 && meta.config.requireEmailConfirmation) {
User.email.sendValidationEmail(userData.uid, {
email: userData.email,
});
}
if (userNameChanged) {
await User.notifications.sendNameChangeNotification(userData.uid, userData.username);
}
plugins.fireHook('action:user.create', { user: userData, data: data });
return userData.uid;
}
async function storePassword(uid, password) {
if (!password) {
return;
}
const hash = await User.hashPassword(password);
await Promise.all([
User.setUserField(uid, 'password', hash),
User.reset.updateExpiry(uid),
]);
}
User.isDataValid = async function (userData) {
if (userData.email && !utils.isEmailValid(userData.email)) {
throw new Error('[[error:invalid-email]]');
}
if (!utils.isUserNameValid(userData.username) || !userData.userslug) {
throw new Error('[[error:invalid-username, ' + userData.username + ']]');
}
if (userData.password) {
User.isPasswordValid(userData.password);
}
if (userData.email) {
const available = await User.email.available(userData.email);
if (!available) {
throw new Error('[[error:email-taken]]');
}
}
};
User.isPasswordValid = function (password, minStrength) {
minStrength = (minStrength || minStrength === 0) ? minStrength : meta.config.minimumPasswordStrength;
// Sanity checks: Checks if defined and is string
if (!password || !utils.isPasswordValid(password)) {
throw new Error('[[error:invalid-password]]');
}
if (password.length < meta.config.minimumPasswordLength) {
throw new Error('[[reset_password:password_too_short]]');
}
if (password.length > 512) {
throw new Error('[[error:password-too-long]]');
}
const strength = zxcvbn(password);
if (strength.score < minStrength) {
throw new Error('[[user:weak_password]]');
}
};
User.uniqueUsername = async function (userData) {
let numTries = 0;
let username = userData.username;
while (true) {
/* eslint-disable no-await-in-loop */
const exists = await meta.userOrGroupExists(username);
if (!exists) {
return numTries ? username : null;
}
username = userData.username + ' ' + numTries.toString(32);
numTries += 1;
}
};
};