From 3ff7719cd44f6e24f1050c579746e78829b27650 Mon Sep 17 00:00:00 2001 From: Steve Lacy Date: Wed, 15 Oct 2014 16:26:40 -0700 Subject: [PATCH 1/2] add hook: filter: user.updateProfile --- src/user/profile.js | 151 +++++++++++++++++++++++--------------------- 1 file changed, 80 insertions(+), 71 deletions(-) diff --git a/src/user/profile.js b/src/user/profile.js index 8b46f53994..1c872fb268 100644 --- a/src/user/profile.js +++ b/src/user/profile.js @@ -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.settings}, 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) { From 2d238e61acd7401e65b3bced3a038d0c277bce16 Mon Sep 17 00:00:00 2001 From: Steve Lacy Date: Fri, 17 Oct 2014 23:15:13 -0700 Subject: [PATCH 2/2] correct object --- src/user/profile.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/user/profile.js b/src/user/profile.js index 1c872fb268..e8251b73e7 100644 --- a/src/user/profile.js +++ b/src/user/profile.js @@ -16,7 +16,7 @@ module.exports = function(User) { User.updateProfile = function(uid, data, callback) { - plugins.fireHook('filter:user.updateProfile', {uid: uid, settings: data.settings}, function(err, data) { + plugins.fireHook('filter:user.updateProfile', {uid: uid, settings: data}, function(err, data) { if(err) { return callback(err); }