Node.js based forum software built for the modern web
closes #3458
barisusaklie6755d0adcfb
diff --git a/public/language/en_GB/user.json b/public/language/en_GB/user.jsonindex b11db877cb..d7f1576548 100644--- a/public/language/en_GB/user.json+++ b/public/language/en_GB/user.json@@ -56,6 +56,7 @@"upload_picture": "Upload picture","upload_a_picture": "Upload a picture",+ "remove_uploaded_picture" : "Remove Uploaded Picture","image_spec": "You may only upload PNG, JPG, or GIF files","settings": "Settings",diff --git a/public/src/client/account/edit.js b/public/src/client/account/edit.jsindex aba7058b94..886069a40c 100644--- a/public/src/client/account/edit.js+++ b/public/src/client/account/edit.js@@ -186,8 +186,16 @@ define('forum/account/edit', ['forum/account/header', 'uploader', 'translator'],$('#user-uploaded-picture').attr('src', urlOnServer);updateHeader(urlOnServer);uploadedPicture = urlOnServer;+ $('#removeUploadedPictureBtn').removeClass('hide');}+ function onRemoveComplete(urlOnServer) {+ $('#user-current-picture').attr('src', urlOnServer);+ $('#user-uploaded-picture').attr('src', '');+ updateHeader(urlOnServer);+ uploadedPicture = '';+ $('#removeUploadedPictureBtn').addClass('hide');+ }$('#upload-picture-modal').on('hide', function() {$('#userPhotoInput').val('');@@ -226,6 +234,16 @@ define('forum/account/edit', ['forum/account/header', 'uploader', 'translator'],});return false;});++ $('#removeUploadedPictureBtn').on('click', function() {+ socket.emit('user.removeUploadedPicture', {uid: ajaxify.data.theirid}, function(err, imageUrlOnServer) {+ if (err) {+ return app.alertError(err.message);+ }+ onRemoveComplete(imageUrlOnServer);+ $('#change-picture-modal').modal('hide');+ });+ })}function handleEmailConfirm() {diff --git a/public/src/client/account/settings.js b/public/src/client/account/settings.jsindex 23afc2f5c0..bf535d8ec0 100644--- a/public/src/client/account/settings.js+++ b/public/src/client/account/settings.js@@ -45,7 +45,7 @@ define('forum/account/settings', ['forum/account/header'], function(header) {config[key] = newSettings[key];}}- app.exposeConfigToTemplates();+if (requireReload && parseInt(app.user.uid, 10) === parseInt(ajaxify.data.theirid, 10)) {app.alert({id: 'setting-change',diff --git a/src/controllers/accounts.js b/src/controllers/accounts.jsindex 6a1d4f100e..d997c4b79a 100644--- a/src/controllers/accounts.js+++ b/src/controllers/accounts.js@@ -493,7 +493,7 @@ accountsController.uploadPicture = function (req, res, next) {}], function(err, image) {fs.unlink(userPhoto.path, function(err) {- winston.error('unable to delete picture', err);+ winston.error('unable to delete picture ' + userPhoto.path, err);});if (err) {return next(err);diff --git a/src/socket.io/user.js b/src/socket.io/user.jsindex cb8d80ba87..8946b3e03d 100644--- a/src/socket.io/user.js+++ b/src/socket.io/user.js@@ -2,6 +2,7 @@var async = require('async'),nconf = require('nconf'),+ winston = require('winston'),user = require('../user'),groups = require('../groups'),topics = require('../topics'),@@ -166,6 +167,18 @@ SocketUser.changePassword = function(socket, data, callback) {});};+function isAdminOrSelf(socket, uid, callback) {+ if (socket.uid === parseInt(uid, 10)) {+ return callback();+ }+ user.isAdministrator(socket.uid, function(err, isAdmin) {+ if (err || !isAdmin) {+ return callback(err || new Error('[[error:no-privileges]]'));+ }+ callback();+ });+}+SocketUser.updateProfile = function(socket, data, callback) {function update(oldUserData) {function done(err, userData) {@@ -197,15 +210,10 @@ SocketUser.updateProfile = function(socket, data, callback) {callback(null, userData);}- if (socket.uid === parseInt(data.uid, 10)) {- return user.updateProfile(socket.uid, data, done);- }-- user.isAdministrator(socket.uid, function(err, isAdmin) {- if (err || !isAdmin) {- return callback(err || new Error('[[error:no-privileges]]'));+ isAdminOrSelf(socket, data.uid, function(err) {+ if (err) {+ return callback(err);}-user.updateProfile(data.uid, data, done);});}@@ -238,16 +246,6 @@ SocketUser.changePicture = function(socket, data, callback) {var type = data.type;- function changePicture(uid, callback) {- user.getUserField(uid, type, function(err, picture) {- if (err) {- return callback(err);- }-- user.setUserField(uid, 'picture', picture, callback);- });- }-if (type === 'gravatar') {type = 'gravatarpicture';} else if (type === 'uploaded') {@@ -256,41 +254,60 @@ SocketUser.changePicture = function(socket, data, callback) {return callback(new Error('[[error:invalid-image-type, ' + ['gravatar', 'uploadedpicture'].join(', ') + ']]'));}- if (socket.uid === parseInt(data.uid, 10)) {- return changePicture(socket.uid, callback);- }-- user.isAdministrator(socket.uid, function(err, isAdmin) {- if (err || !isAdmin) {- return callback(err || new Error('[[error:no-privileges]]'));+ async.waterfall([+ function (next) {+ isAdminOrSelf(socket, data.uid, next);+ },+ function (next) {+ user.getUserField(data.uid, type, next);+ },+ function (picture, next) {+ user.setUserField(data.uid, 'picture', picture, next);}-- changePicture(data.uid, callback);- });+ ], callback);};SocketUser.uploadProfileImageFromUrl = function(socket, data, callback) {- function upload() {+ if (!socket.uid || !data.url || !data.uid) {+ return;+ }++ isAdminOrSelf(socket, data.uid, function(err) {+ if (err) {+ return callback(err);+ }user.uploadFromUrl(data.uid, data.url, function(err, uploadedImage) {callback(err, uploadedImage ? uploadedImage.url : null);});- }+ });+};- if (!socket.uid || !data.url || !data.uid) {+SocketUser.removeUploadedPicture = function(socket, data, callback) {+ if (!socket.uid || !data.uid) {return;}- if (parseInt(socket.uid, 10) === parseInt(data.uid, 10)) {- return upload();- }-- user.isAdministrator(socket.uid, function(err, isAdmin) {- if (err || !isAdmin) {- return callback(err || new Error('[[error:not-allowed]]'));+ async.waterfall([+ function (next) {+ isAdminOrSelf(socket, data.uid, next);+ },+ function (next) {+ user.getUserField(data.uid, 'uploadedpicture', next);+ },+ function(uploadedPicture, next) {+ if (!uploadedPicture.startsWith('http')) {+ require('fs').unlink(uploadedPicture, function(err) {+ if (err) {+ winston.error(err);+ }+ });+ }+ user.setUserField(data.uid, 'uploadedpicture', '', next);+ },+ function(next) {+ user.getUserField(data.uid, 'picture', next);}-- upload();- });+ ], callback);};SocketUser.follow = function(socket, data, callback) {@@ -346,19 +363,10 @@ SocketUser.saveSettings = function(socket, data, callback) {return callback(new Error('[[error:invalid-data]]'));}- if (socket.uid === parseInt(data.uid, 10)) {- return user.saveSettings(socket.uid, data.settings, callback);- }-- user.isAdministrator(socket.uid, function(err, isAdmin) {+ isAdminOrSelf(socket, data.uid, function(err) {if (err) {return callback(err);}-- if (!isAdmin) {- return callback(new Error('[[error:no-privileges]]'));- }-user.saveSettings(data.uid, data.settings, callback);});};