2014-03-17 21:47:37 -04:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-03-21 16:07:59 +02:00
|
|
|
var async = require('async');
|
|
|
|
|
var db = require('../database');
|
|
|
|
|
var utils = require('../../public/src/utils');
|
|
|
|
|
var validator = require('validator');
|
|
|
|
|
var plugins = require('../plugins');
|
|
|
|
|
var groups = require('../groups');
|
|
|
|
|
var meta = require('../meta');
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
module.exports = function (User) {
|
|
|
|
|
User.create = function (data, callback) {
|
2014-10-28 18:28:26 -04:00
|
|
|
data.username = data.username.trim();
|
|
|
|
|
data.userslug = utils.slugify(data.username);
|
|
|
|
|
if (data.email !== undefined) {
|
2016-08-27 15:45:15 +03:00
|
|
|
data.email = validator.escape(String(data.email).trim());
|
2014-03-17 21:47:37 -04:00
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
User.isDataValid(data, function (err) {
|
2017-02-18 01:52:56 -07:00
|
|
|
if (err) {
|
2014-10-28 18:28:26 -04:00
|
|
|
return callback(err);
|
|
|
|
|
}
|
2015-05-15 09:47:39 -04:00
|
|
|
var timestamp = data.timestamp || Date.now();
|
2014-10-28 18:28:26 -04:00
|
|
|
|
|
|
|
|
var userData = {
|
2017-02-18 01:19:20 -07:00
|
|
|
username: data.username,
|
|
|
|
|
userslug: data.userslug,
|
|
|
|
|
email: data.email || '',
|
|
|
|
|
joindate: timestamp,
|
|
|
|
|
lastonline: timestamp,
|
2017-03-27 11:05:06 -07:00
|
|
|
picture: data.picture || '',
|
2017-02-18 01:19:20 -07:00
|
|
|
fullname: data.fullname || '',
|
2017-03-27 11:05:06 -07:00
|
|
|
location: data.location || '',
|
|
|
|
|
birthday: data.birthday || '',
|
2017-02-18 01:19:20 -07:00
|
|
|
website: '',
|
|
|
|
|
signature: '',
|
|
|
|
|
uploadedpicture: '',
|
|
|
|
|
profileviews: 0,
|
|
|
|
|
reputation: 0,
|
|
|
|
|
postcount: 0,
|
|
|
|
|
topiccount: 0,
|
|
|
|
|
lastposttime: 0,
|
|
|
|
|
banned: 0,
|
|
|
|
|
status: 'online',
|
2014-10-28 18:28:26 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async.parallel({
|
2016-10-13 11:43:39 +02:00
|
|
|
renamedUsername: function (next) {
|
2017-03-24 10:33:38 -06:00
|
|
|
User.uniqueUsername(userData, next);
|
2014-10-28 18:28:26 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
userData: function (next) {
|
2017-02-18 12:30:49 -07:00
|
|
|
plugins.fireHook('filter:user.create', { user: userData, data: data }, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
}, function (err, results) {
|
2014-10-28 18:28:26 -04:00
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var userNameChanged = !!results.renamedUsername;
|
|
|
|
|
|
|
|
|
|
if (userNameChanged) {
|
|
|
|
|
userData.username = results.renamedUsername;
|
|
|
|
|
userData.userslug = utils.slugify(results.renamedUsername);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.waterfall([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2014-10-28 18:28:26 -04:00
|
|
|
db.incrObjectField('global', 'nextUid', next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (uid, next) {
|
2014-10-28 18:28:26 -04:00
|
|
|
userData.uid = uid;
|
|
|
|
|
db.setObject('user:' + uid, userData, next);
|
2015-05-04 18:01:45 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-05-04 18:01:45 -04:00
|
|
|
async.parallel([
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-05-21 14:52:39 -04:00
|
|
|
db.incrObjectField('global', 'userCount', next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-05-07 13:43:06 -04:00
|
|
|
db.sortedSetAdd('username:uid', userData.uid, userData.username, next);
|
2015-05-04 18:01:45 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-05-19 23:04:28 -04:00
|
|
|
db.sortedSetAdd('username:sorted', 0, userData.username.toLowerCase() + ':' + userData.uid, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-05-07 13:43:06 -04:00
|
|
|
db.sortedSetAdd('userslug:uid', userData.uid, userData.userslug, next);
|
2015-05-04 18:01:45 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2016-03-21 16:07:59 +02:00
|
|
|
var sets = ['users:joindate', 'users:online'];
|
2016-12-14 16:49:03 +03:00
|
|
|
if (parseInt(userData.uid, 10) !== 1) {
|
2016-03-21 16:07:59 +02:00
|
|
|
sets.push('users:notvalidated');
|
|
|
|
|
}
|
|
|
|
|
db.sortedSetsAdd(sets, timestamp, userData.uid, next);
|
2015-05-04 18:01:45 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-05-04 18:01:45 -04:00
|
|
|
db.sortedSetsAdd(['users:postcount', 'users:reputation'], 0, userData.uid, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-05-04 18:01:45 -04:00
|
|
|
groups.join('registered-users', userData.uid, next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2016-02-01 20:43:51 +02:00
|
|
|
User.notifications.sendWelcomeNotification(userData.uid, next);
|
2016-02-01 20:34:21 +02:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-05-04 18:01:45 -04:00
|
|
|
if (userData.email) {
|
2015-05-19 23:04:28 -04:00
|
|
|
async.parallel([
|
|
|
|
|
async.apply(db.sortedSetAdd, 'email:uid', userData.uid, userData.email.toLowerCase()),
|
2017-02-17 19:31:21 -07:00
|
|
|
async.apply(db.sortedSetAdd, 'email:sorted', 0, userData.email.toLowerCase() + ':' + userData.uid),
|
2015-05-19 23:04:28 -04:00
|
|
|
], next);
|
|
|
|
|
|
2015-05-04 18:01:45 -04:00
|
|
|
if (parseInt(userData.uid, 10) !== 1 && parseInt(meta.config.requireEmailConfirmation, 10) === 1) {
|
|
|
|
|
User.email.sendValidationEmail(userData.uid, userData.email);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
2014-10-28 18:28:26 -04:00
|
|
|
}
|
2015-05-04 18:01:45 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2015-05-04 18:01:45 -04:00
|
|
|
if (!data.password) {
|
|
|
|
|
return next();
|
2014-10-28 18:28:26 -04:00
|
|
|
}
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
User.hashPassword(data.password, function (err, hash) {
|
2015-05-04 18:01:45 -04:00
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.parallel([
|
|
|
|
|
async.apply(User.setUserField, userData.uid, 'password', hash),
|
2017-02-17 19:31:21 -07:00
|
|
|
async.apply(User.reset.updateExpiry, userData.uid),
|
2015-05-04 18:01:45 -04:00
|
|
|
], next);
|
|
|
|
|
});
|
2016-09-20 17:46:32 +03:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (next) {
|
2016-10-12 10:09:49 -04:00
|
|
|
User.updateDigestSetting(userData.uid, meta.config.dailyDigestFreq, next);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-05-04 18:01:45 -04:00
|
|
|
], next);
|
|
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
function (results, next) {
|
2014-10-28 18:28:26 -04:00
|
|
|
if (userNameChanged) {
|
|
|
|
|
User.notifications.sendNameChangeNotification(userData.uid, userData.username);
|
|
|
|
|
}
|
2017-02-24 12:47:46 -05:00
|
|
|
plugins.fireHook('action:user.create', { user: userData });
|
2015-05-04 18:01:45 -04:00
|
|
|
next(null, userData.uid);
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2015-05-04 18:01:45 -04:00
|
|
|
], callback);
|
2014-10-28 18:28:26 -04:00
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
User.isDataValid = function (userData, callback) {
|
2014-07-23 16:49:32 -04:00
|
|
|
async.parallel({
|
2016-10-13 11:43:39 +02:00
|
|
|
emailValid: function (next) {
|
2014-04-07 20:19:40 -04:00
|
|
|
if (userData.email) {
|
2014-04-09 22:26:23 -04:00
|
|
|
next(!utils.isEmailValid(userData.email) ? new Error('[[error:invalid-email]]') : null);
|
2014-04-07 20:19:40 -04:00
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
2014-03-17 21:47:37 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
userNameValid: function (next) {
|
2015-11-10 13:13:16 -05:00
|
|
|
next((!utils.isUserNameValid(userData.username) || !userData.userslug) ? new Error('[[error:invalid-username, ' + userData.username + ']]') : null);
|
2014-03-17 21:47:37 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
passwordValid: function (next) {
|
2014-10-28 18:28:26 -04:00
|
|
|
if (userData.password) {
|
2015-09-14 15:50:02 -04:00
|
|
|
User.isPasswordValid(userData.password, next);
|
2014-03-18 20:21:24 -04:00
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
2014-03-17 21:47:37 -04:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
emailAvailable: function (next) {
|
2014-03-17 21:47:37 -04:00
|
|
|
if (userData.email) {
|
2016-10-13 11:43:39 +02:00
|
|
|
User.email.available(userData.email, function (err, available) {
|
2014-03-17 21:47:37 -04:00
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
2014-04-09 22:26:23 -04:00
|
|
|
next(!available ? new Error('[[error:email-taken]]') : null);
|
2014-03-17 21:47:37 -04:00
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
2017-02-17 19:31:21 -07:00
|
|
|
},
|
2016-10-13 11:43:39 +02:00
|
|
|
}, function (err) {
|
2015-06-27 21:26:19 -04:00
|
|
|
callback(err);
|
|
|
|
|
});
|
|
|
|
|
};
|
2014-03-17 21:47:37 -04:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
User.isPasswordValid = function (password, callback) {
|
2015-09-14 15:50:02 -04:00
|
|
|
if (!password || !utils.isPasswordValid(password)) {
|
|
|
|
|
return callback(new Error('[[error:invalid-password]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (password.length < meta.config.minimumPasswordLength) {
|
|
|
|
|
return callback(new Error('[[user:change_password_error_length]]'));
|
|
|
|
|
}
|
2016-01-10 10:26:47 +02:00
|
|
|
|
|
|
|
|
if (password.length > 4096) {
|
|
|
|
|
return callback(new Error('[[error:password-too-long]]'));
|
|
|
|
|
}
|
|
|
|
|
|
2015-09-14 15:50:02 -04:00
|
|
|
callback();
|
|
|
|
|
};
|
|
|
|
|
|
2017-03-24 10:33:38 -06:00
|
|
|
User.uniqueUsername = function (userData, callback) {
|
2016-10-13 11:43:39 +02:00
|
|
|
meta.userOrGroupExists(userData.userslug, function (err, exists) {
|
2014-10-28 18:28:26 -04:00
|
|
|
if (err || !exists) {
|
|
|
|
|
return callback(err);
|
2014-04-15 00:17:35 -04:00
|
|
|
}
|
|
|
|
|
|
2017-03-23 16:00:22 -06:00
|
|
|
var num = 0;
|
|
|
|
|
|
|
|
|
|
function go() {
|
|
|
|
|
var username = userData.username + ' ' + num.toString(32);
|
2017-03-24 14:43:58 -06:00
|
|
|
meta.userOrGroupExists(username, function (err, exists) {
|
2017-03-23 16:00:22 -06:00
|
|
|
if (err || !exists) {
|
|
|
|
|
return callback(err, username);
|
2014-03-17 21:47:37 -04:00
|
|
|
}
|
2017-03-23 16:00:22 -06:00
|
|
|
|
|
|
|
|
num += 1;
|
|
|
|
|
go();
|
2014-03-17 21:47:37 -04:00
|
|
|
});
|
2017-03-23 16:00:22 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
go();
|
2014-03-17 21:47:37 -04:00
|
|
|
});
|
2017-03-23 16:00:22 -06:00
|
|
|
};
|
2014-04-10 20:31:57 +01:00
|
|
|
};
|