Merge pull request #2270 from stevelacy/master

Add user.updateProfile - hook user profile updates
v1.18.x
Barış Soner Uşaklı 10 years ago
commit 591a8b6b12

@ -9,113 +9,122 @@ var async = require('async'),
meta = require('../meta'), meta = require('../meta'),
events = require('../events'), events = require('../events'),
db = require('../database'), db = require('../database'),
Password = require('../password'); Password = require('../password'),
plugins = require('./../plugins');
module.exports = function(User) { module.exports = function(User) {
User.updateProfile = function(uid, data, callback) { User.updateProfile = function(uid, data, callback) {
var fields = ['username', 'email', 'fullname', 'website', 'location', 'birthday', 'signature'];
function isSignatureValid(next) { plugins.fireHook('filter:user.updateProfile', {uid: uid, settings: data}, function(err, data) {
if (data.signature !== undefined && data.signature.length > meta.config.maximumSignatureLength) { if(err) {
next(new Error('[[error:signature-too-long, ' + meta.config.maximumSignatureLength + ']]')); return callback(err);
} else {
next();
} }
}
function isEmailAvailable(next) { data = data.settings;
if (!data.email) { var fields = ['username', 'email', 'fullname', 'website', 'location', 'birthday', 'signature'];
return next();
}
if (!utils.isEmailValid(data.email)) { function isSignatureValid(next) {
return next(new Error('[[error:invalid-email]]')); if (data.signature !== undefined && data.signature.length > meta.config.maximumSignatureLength) {
next(new Error('[[error:signature-too-long, ' + meta.config.maximumSignatureLength + ']]'));
} else {
next();
}
} }
User.getUserField(uid, 'email', function(err, email) { function isEmailAvailable(next) {
if(email === data.email) { if (!data.email) {
return next(); return next();
} }
User.email.available(data.email, function(err, available) { if (!utils.isEmailValid(data.email)) {
if (err) { return next(new Error('[[error:invalid-email]]'));
return next(err); }
User.getUserField(uid, 'email', function(err, email) {
if(email === data.email) {
return next();
} }
next(!available ? new Error('[[error:email-taken]]') : null); User.email.available(data.email, function(err, available) {
}); if (err) {
}); return next(err);
} }
function isUsernameAvailable(next) { next(!available ? new Error('[[error:email-taken]]') : null);
User.getUserFields(uid, ['username', 'userslug'], function(err, userData) { });
});
}
var userslug = utils.slugify(data.username); function isUsernameAvailable(next) {
User.getUserFields(uid, ['username', 'userslug'], function(err, userData) {
if(userslug === userData.userslug) { var userslug = utils.slugify(data.username);
return next();
}
if (data.username.length < meta.config.minimumUsernameLength) { if(userslug === userData.userslug) {
return next(new Error('[[error:username-too-short]]')); return next();
} }
if (data.username.length > meta.config.maximumUsernameLength) { if (data.username.length < meta.config.minimumUsernameLength) {
return next(new Error('[[error:username-too-long]]')); return next(new Error('[[error:username-too-short]]'));
} }
if(!utils.isUserNameValid(data.username) || !userslug) { if (data.username.length > meta.config.maximumUsernameLength) {
return next(new Error('[[error:invalid-username]]')); return next(new Error('[[error:username-too-long]]'));
} }
User.exists(userslug, function(err, exists) { if(!utils.isUserNameValid(data.username) || !userslug) {
if(err) { return next(new Error('[[error:invalid-username]]'));
return next(err);
} }
next(exists ? new Error('[[error:username-taken]]') : null); User.exists(userslug, function(err, exists) {
}); if(err) {
}); return next(err);
} }
async.series([isSignatureValid, isEmailAvailable, isUsernameAvailable], function(err, results) { next(exists ? new Error('[[error:username-taken]]') : null);
if (err) { });
return callback(err); });
} }
async.each(fields, updateField, function(err) { async.series([isSignatureValid, isEmailAvailable, isUsernameAvailable], function(err, results) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
User.getUserFields(uid, ['email', 'userslug', 'picture', 'gravatarpicture'], callback); async.each(fields, updateField, function(err) {
if (err) {
return callback(err);
}
User.getUserFields(uid, ['email', 'userslug', 'picture', 'gravatarpicture'], callback);
});
}); });
});
function updateField(field, next) { function updateField(field, next) {
if (!(data[field] !== undefined && typeof data[field] === 'string')) { if (!(data[field] !== undefined && typeof data[field] === 'string')) {
return next(); return next();
} }
data[field] = data[field].trim(); data[field] = data[field].trim();
data[field] = validator.escape(data[field]); data[field] = validator.escape(data[field]);
if (field === 'email') { if (field === 'email') {
return updateEmail(uid, data.email, next); return updateEmail(uid, data.email, next);
} else if (field === 'username') { } else if (field === 'username') {
return updateUsername(uid, data.username, next); return updateUsername(uid, data.username, next);
} else if (field === 'signature') { } else if (field === 'signature') {
data[field] = S(data[field]).stripTags().s; data[field] = S(data[field]).stripTags().s;
} else if (field === 'website') { } else if (field === 'website') {
if(data[field].substr(0, 7) !== 'http://' && data[field].substr(0, 8) !== 'https://') { if(data[field].substr(0, 7) !== 'http://' && data[field].substr(0, 8) !== 'https://') {
data[field] = 'http://' + data[field]; data[field] = 'http://' + data[field];
}
} }
}
User.setUserField(uid, field, data[field], next); User.setUserField(uid, field, data[field], next);
} }
}; });
};
function updateEmail(uid, newEmail, callback) { function updateEmail(uid, newEmail, callback) {
User.getUserFields(uid, ['email', 'picture', 'uploadedpicture'], function(err, userData) { User.getUserFields(uid, ['email', 'picture', 'uploadedpicture'], function(err, userData) {

Loading…
Cancel
Save