all files / src/controllers/accounts/ edit.js

24.42% Statements 21/86
0% Branches 0/40
0% Functions 0/22
24.42% Lines 21/86
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                                                                                                                                                                                                                                                                                           
'use strict';
 
var async = require('async');
var fs = require('fs');
var nconf = require('nconf');
var winston = require('winston');
 
var db = require('../../database');
var user = require('../../user');
var meta = require('../../meta');
var plugins = require('../../plugins');
var helpers = require('../helpers');
var groups = require('../../groups');
var accountHelpers = require('./helpers');
 
var editController = {};
 
editController.get = function(req, res, callback) {
	accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, function(err, userData) {
		if (err || !userData) {
			return callback(err);
		}
 
		userData.maximumSignatureLength = parseInt(meta.config.maximumSignatureLength, 10) || 255;
		userData.maximumAboutMeLength = parseInt(meta.config.maximumAboutMeLength, 10) || 1000;
		userData.maximumProfileImageSize = parseInt(meta.config.maximumProfileImageSize, 10);
		userData.allowProfileImageUploads = parseInt(meta.config.allowProfileImageUploads) === 1;
		userData.allowAccountDelete = parseInt(meta.config.allowAccountDelete, 10) === 1;
 
		userData.groups = userData.groups.filter(function(group) {
			return group && group.userTitleEnabled && !groups.isPrivilegeGroup(group.name) && group.name !== 'registered-users';
		});
		userData.groups.forEach(function(group) {
			group.selected = group.name === userData.groupTitle;
		});
 
		userData.title = '[[pages:account/edit, ' + userData.username + ']]';
		userData.breadcrumbs = helpers.buildBreadcrumbs([{text: userData.username, url: '/user/' + userData.userslug}, {text: '[[user:edit]]'}]);
		userData.editButtons = [];
 
		plugins.fireHook('filter:user.account.edit', userData, function(err, userData) {
			if (err) {
				return callback(err);
			}
 
			res.render('account/edit', userData);
		});
	});
};
 
editController.password = function(req, res, next) {
	renderRoute('password', req, res, next);
};
 
editController.username = function(req, res, next) {
	renderRoute('username', req, res, next);
};
 
editController.email = function(req, res, next) {
	renderRoute('email', req, res, next);
};
 
function renderRoute(name, req, res, next) {
	getUserData(req, next, function(err, userData) {
		if (err) {
			return next(err);
		}
		if ((name === 'username' && userData['username:disableEdit']) || (name === 'email' && userData['email:disableEdit'])) {
			return next();
		}
 
		if (name === 'password') {
			userData.minimumPasswordLength = parseInt(meta.config.minimumPasswordLength, 10);
		}
 
		userData.title = '[[pages:account/edit/' + name + ', ' + userData.username + ']]';
		userData.breadcrumbs = helpers.buildBreadcrumbs([
			{text: userData.username, url: '/user/' + userData.userslug},
			{text: '[[user:edit]]', url: '/user/' + userData.userslug + '/edit'},
			{text: '[[user:' + name + ']]'}
		]);
 
		res.render('account/edit/' + name, userData);
	});
}
 
function getUserData(req, next, callback) {
	var userData;
	async.waterfall([
		function(next) {
			accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, next);
		},
		function(data, next) {
			userData = data;
			if (!userData) {
				return next();
			}
			db.getObjectField('user:' + userData.uid, 'password', next);
		}
	], function(err, password) {
		if (err) {
			return callback(err);
		}
 
		userData.hasPassword = !!password;
		callback(null, userData);
	});
}
 
editController.uploadPicture = function (req, res, next) {
	var userPhoto = req.files.files[0];
 
	var updateUid;
 
	async.waterfall([
		function(next) {
			user.getUidByUserslug(req.params.userslug, next);
		},
		function(uid, next) {
			updateUid = uid;
			if (parseInt(req.uid, 10) === parseInt(uid, 10)) {
				return next(null, true);
			}
 
			user.isAdminOrGlobalMod(req.uid, next);
		},
		function(isAllowed, next) {
			if (!isAllowed) {
				return helpers.notAllowed(req, res);
			}
 
			user.uploadPicture(updateUid, userPhoto, next);
		}
	], function(err, image) {
		fs.unlink(userPhoto.path, function(err) {
			if (err) {
				winston.warn('[user/picture] Unable to delete picture ' + userPhoto.path, err);
			}
		});
		if (err) {
			return next(err);
		}
 
		res.json([{name: userPhoto.name, url: image.url.startsWith('http') ? image.url : nconf.get('relative_path') + image.url}]);
	});
};
 
editController.uploadCoverPicture = function(req, res, next) {
	var params = JSON.parse(req.body.params);
 
	user.updateCoverPicture({
		file: req.files.files[0],
		uid: params.uid
	}, function(err, image) {
		if (err) {
			return next(err);
		}
 
		res.json([{ url: image.url }]);
	});
};
 
module.exports = editController;