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

Loading…
Cancel
Save