|
|
|
@ -13,147 +13,138 @@ var plugins = require('../plugins');
|
|
|
|
|
module.exports = function (User) {
|
|
|
|
|
|
|
|
|
|
User.updateProfile = function (uid, data, callback) {
|
|
|
|
|
var fields = ['username', 'email', 'fullname', 'website', 'location', 'groupTitle', 'birthday', 'signature', 'aboutme', 'picture', 'uploadedpicture'];
|
|
|
|
|
var fields = ['username', 'email', 'fullname', 'website', 'location',
|
|
|
|
|
'groupTitle', 'birthday', 'signature', 'aboutme', 'picture', 'uploadedpicture'];
|
|
|
|
|
|
|
|
|
|
plugins.fireHook('filter:user.updateProfile', {uid: uid, data: data, fields: fields}, function (err, data) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fields = data.fields;
|
|
|
|
|
data = data.data;
|
|
|
|
|
|
|
|
|
|
function isAboutMeValid(next) {
|
|
|
|
|
if (data.aboutme !== undefined && data.aboutme.length > meta.config.maximumAboutMeLength) {
|
|
|
|
|
next(new Error('[[error:about-me-too-long, ' + meta.config.maximumAboutMeLength + ']]'));
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isEmailAvailable(next) {
|
|
|
|
|
if (!data.email) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!utils.isEmailValid(data.email)) {
|
|
|
|
|
return next(new Error('[[error:invalid-email]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
User.getUserField(uid, 'email', function (err, email) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(email === data.email) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
User.email.available(data.email, function (err, available) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next(!available ? new Error('[[error:email-taken]]') : null);
|
|
|
|
|
});
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
plugins.fireHook('filter:user.updateProfile', {uid: uid, data: data, fields: fields}, next);
|
|
|
|
|
},
|
|
|
|
|
function (data, next) {
|
|
|
|
|
fields = data.fields;
|
|
|
|
|
data = data.data;
|
|
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
|
async.apply(isAboutMeValid, data),
|
|
|
|
|
async.apply(isSignatureValid, data),
|
|
|
|
|
async.apply(isEmailAvailable, data, uid),
|
|
|
|
|
async.apply(isUsernameAvailable, data, uid),
|
|
|
|
|
async.apply(isGroupTitleValid, data)
|
|
|
|
|
], function (err) {
|
|
|
|
|
next(err);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isUsernameAvailable(next) {
|
|
|
|
|
if (!data.username) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
data.username = data.username.trim();
|
|
|
|
|
User.getUserFields(uid, ['username', 'userslug'], function (err, userData) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
async.each(fields, function (field, next) {
|
|
|
|
|
if (!(data[field] !== undefined && typeof data[field] === 'string')) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var userslug = utils.slugify(data.username);
|
|
|
|
|
data[field] = data[field].trim();
|
|
|
|
|
|
|
|
|
|
if (data.username.length < meta.config.minimumUsernameLength) {
|
|
|
|
|
return next(new Error('[[error:username-too-short]]'));
|
|
|
|
|
if (field === 'email') {
|
|
|
|
|
return updateEmail(uid, data.email, next);
|
|
|
|
|
} else if (field === 'username') {
|
|
|
|
|
return updateUsername(uid, data.username, next);
|
|
|
|
|
} else if (field === 'fullname') {
|
|
|
|
|
return updateFullname(uid, data.fullname, next);
|
|
|
|
|
} else if (field === 'signature') {
|
|
|
|
|
data[field] = S(data[field]).stripTags().s;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (data.username.length > meta.config.maximumUsernameLength) {
|
|
|
|
|
return next(new Error('[[error:username-too-long]]'));
|
|
|
|
|
}
|
|
|
|
|
User.setUserField(uid, field, data[field], next);
|
|
|
|
|
}, next);
|
|
|
|
|
},
|
|
|
|
|
function (next) {
|
|
|
|
|
plugins.fireHook('action:user.updateProfile', {data: data, uid: uid});
|
|
|
|
|
User.getUserFields(uid, ['email', 'username', 'userslug', 'picture', 'icon:text', 'icon:bgColor'], next);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!utils.isUserNameValid(data.username) || !userslug) {
|
|
|
|
|
return next(new Error('[[error:invalid-username]]'));
|
|
|
|
|
}
|
|
|
|
|
function isAboutMeValid(data, callback) {
|
|
|
|
|
if (data.aboutme !== undefined && data.aboutme.length > meta.config.maximumAboutMeLength) {
|
|
|
|
|
callback(new Error('[[error:about-me-too-long, ' + meta.config.maximumAboutMeLength + ']]'));
|
|
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (userslug === userData.userslug) {
|
|
|
|
|
return next();
|
|
|
|
|
}
|
|
|
|
|
function isSignatureValid(data, callback) {
|
|
|
|
|
if (data.signature !== undefined && data.signature.length > meta.config.maximumSignatureLength) {
|
|
|
|
|
callback(new Error('[[error:signature-too-long, ' + meta.config.maximumSignatureLength + ']]'));
|
|
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
User.existsBySlug(userslug, function (err, exists) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
function isEmailAvailable(data, uid, callback) {
|
|
|
|
|
if (!data.email) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
next(exists ? new Error('[[error:username-taken]]') : null);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
if (!utils.isEmailValid(data.email)) {
|
|
|
|
|
return callback(new Error('[[error:invalid-email]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isGroupTitleValid(next) {
|
|
|
|
|
if (data.groupTitle === 'registered-users' || groups.isPrivilegeGroup(data.groupTitle)) {
|
|
|
|
|
next(new Error('[[error:invalid-group-title]]'));
|
|
|
|
|
} else {
|
|
|
|
|
next();
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
User.getUserField(uid, 'email', next);
|
|
|
|
|
},
|
|
|
|
|
function (email, next) {
|
|
|
|
|
if (email === data.email) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
User.email.available(data.email, next);
|
|
|
|
|
},
|
|
|
|
|
function (available, next) {
|
|
|
|
|
next(!available ? new Error('[[error:email-taken]]') : null);
|
|
|
|
|
}
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.series([
|
|
|
|
|
isAboutMeValid,
|
|
|
|
|
isSignatureValid,
|
|
|
|
|
isEmailAvailable,
|
|
|
|
|
isUsernameAvailable,
|
|
|
|
|
isGroupTitleValid
|
|
|
|
|
], function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async.each(fields, updateField, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
plugins.fireHook('action:user.updateProfile', {data: data, uid: uid});
|
|
|
|
|
User.getUserFields(uid, ['email', 'username', 'userslug', 'picture', 'icon:text', 'icon:bgColor'], callback);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
function isUsernameAvailable(data, uid, callback) {
|
|
|
|
|
if (!data.username) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
data.username = data.username.trim();
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
User.getUserFields(uid, ['username', 'userslug'], next);
|
|
|
|
|
},
|
|
|
|
|
function (userData, next) {
|
|
|
|
|
var userslug = utils.slugify(data.username);
|
|
|
|
|
|
|
|
|
|
function updateField(field, next) {
|
|
|
|
|
if (!(data[field] !== undefined && typeof data[field] === 'string')) {
|
|
|
|
|
return next();
|
|
|
|
|
if (data.username.length < meta.config.minimumUsernameLength) {
|
|
|
|
|
return next(new Error('[[error:username-too-short]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data[field] = data[field].trim();
|
|
|
|
|
if (data.username.length > meta.config.maximumUsernameLength) {
|
|
|
|
|
return next(new Error('[[error:username-too-long]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (field === 'email') {
|
|
|
|
|
return updateEmail(uid, data.email, next);
|
|
|
|
|
} else if (field === 'username') {
|
|
|
|
|
return updateUsername(uid, data.username, next);
|
|
|
|
|
} else if (field === 'fullname') {
|
|
|
|
|
return updateFullname(uid, data.fullname, next);
|
|
|
|
|
} else if (field === 'signature') {
|
|
|
|
|
data[field] = S(data[field]).stripTags().s;
|
|
|
|
|
if (!utils.isUserNameValid(data.username) || !userslug) {
|
|
|
|
|
return next(new Error('[[error:invalid-username]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
User.setUserField(uid, field, data[field], next);
|
|
|
|
|
if (userslug === userData.userslug) {
|
|
|
|
|
return callback();
|
|
|
|
|
}
|
|
|
|
|
User.existsBySlug(userslug, next);
|
|
|
|
|
},
|
|
|
|
|
function (exists, next) {
|
|
|
|
|
next(exists ? new Error('[[error:username-taken]]') : null);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
], callback);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function isGroupTitleValid(data, callback) {
|
|
|
|
|
if (data.groupTitle === 'registered-users' || groups.isPrivilegeGroup(data.groupTitle)) {
|
|
|
|
|
callback(new Error('[[error:invalid-group-title]]'));
|
|
|
|
|
} else {
|
|
|
|
|
callback();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateEmail(uid, newEmail, callback) {
|
|
|
|
|
User.getUserFields(uid, ['email', 'picture', 'uploadedpicture'], function (err, userData) {
|
|
|
|
|