| 
									
										
										
										
											2015-09-25 01:52:41 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | const user = require('../../user'); | 
					
						
							|  |  |  | const meta = require('../../meta'); | 
					
						
							|  |  |  | const helpers = require('../helpers'); | 
					
						
							|  |  |  | const groups = require('../../groups'); | 
					
						
							|  |  |  | const accountHelpers = require('./helpers'); | 
					
						
							|  |  |  | const privileges = require('../../privileges'); | 
					
						
							| 
									
										
										
										
											2020-06-24 23:04:24 -04:00
										 |  |  | const file = require('../../file'); | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | const editController = module.exports; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | editController.get = async function (req, res, next) { | 
					
						
							|  |  |  | 	const [userData, canUseSignature] = await Promise.all([ | 
					
						
							| 
									
										
										
										
											2021-05-17 10:26:32 -04:00
										 |  |  | 		accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query), | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 		privileges.global.can('signature', req.uid), | 
					
						
							|  |  |  | 	]); | 
					
						
							|  |  |  | 	if (!userData) { | 
					
						
							|  |  |  | 		return next(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	userData.maximumSignatureLength = meta.config.maximumSignatureLength; | 
					
						
							|  |  |  | 	userData.maximumAboutMeLength = meta.config.maximumAboutMeLength; | 
					
						
							|  |  |  | 	userData.maximumProfileImageSize = meta.config.maximumProfileImageSize; | 
					
						
							|  |  |  | 	userData.allowProfilePicture = !userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:profile-picture']; | 
					
						
							|  |  |  | 	userData.allowCoverPicture = !userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:cover-picture']; | 
					
						
							|  |  |  | 	userData.allowProfileImageUploads = meta.config.allowProfileImageUploads; | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	userData.allowedProfileImageExtensions = user.getAllowedProfileImageExtensions().map(ext => `.${ext}`).join(', '); | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 	userData.allowMultipleBadges = meta.config.allowMultipleBadges === 1; | 
					
						
							|  |  |  | 	userData.allowAccountDelete = meta.config.allowAccountDelete === 1; | 
					
						
							|  |  |  | 	userData.allowWebsite = !userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:website']; | 
					
						
							|  |  |  | 	userData.allowAboutMe = !userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:aboutme']; | 
					
						
							|  |  |  | 	userData.allowSignature = canUseSignature && (!userData.isSelf || !!meta.config['reputation:disabled'] || userData.reputation >= meta.config['min:rep:signature']); | 
					
						
							|  |  |  | 	userData.profileImageDimension = meta.config.profileImageDimension; | 
					
						
							|  |  |  | 	userData.defaultAvatar = user.getDefaultAvatar(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	userData.groups = userData.groups.filter(g => g && g.userTitleEnabled && !groups.isPrivilegeGroup(g.name) && g.name !== 'registered-users'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (!userData.allowMultipleBadges) { | 
					
						
							|  |  |  | 		userData.groupTitle = userData.groupTitleArray[0]; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2019-08-13 16:13:39 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	userData.groups.sort((a, b) => { | 
					
						
							|  |  |  | 		const i1 = userData.groupTitleArray.indexOf(a.name); | 
					
						
							|  |  |  | 		const i2 = userData.groupTitleArray.indexOf(b.name); | 
					
						
							|  |  |  | 		if (i1 === -1) { | 
					
						
							|  |  |  | 			return 1; | 
					
						
							|  |  |  | 		} else if (i2 === -1) { | 
					
						
							|  |  |  | 			return -1; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return i1 - i2; | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2021-02-04 00:01:39 -07:00
										 |  |  | 	userData.groups.forEach((group) => { | 
					
						
							| 
									
										
										
										
											2019-10-08 12:19:17 -04:00
										 |  |  | 		group.userTitle = group.userTitle || group.displayName; | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 		group.selected = userData.groupTitleArray.includes(group.name); | 
					
						
							|  |  |  | 	}); | 
					
						
							| 
									
										
										
										
											2019-08-13 16:13:39 -04:00
										 |  |  | 	userData.groupSelectSize = Math.min(10, Math.max(5, userData.groups.length + 1)); | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	userData.title = `[[pages:account/edit, ${userData.username}]]`; | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 	userData.breadcrumbs = helpers.buildBreadcrumbs([ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			text: userData.username, | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			url: `/user/${userData.userslug}`, | 
					
						
							| 
									
										
										
										
											2017-05-24 00:02:30 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			text: '[[user:edit]]', | 
					
						
							| 
									
										
										
										
											2017-05-24 00:02:30 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 	]); | 
					
						
							|  |  |  | 	userData.editButtons = []; | 
					
						
							| 
									
										
										
										
											2020-07-24 10:39:51 -04:00
										 |  |  | 	res.render('account/edit', userData); | 
					
						
							| 
									
										
										
										
											2015-10-09 17:52:55 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | editController.password = async function (req, res, next) { | 
					
						
							|  |  |  | 	await renderRoute('password', req, res, next); | 
					
						
							| 
									
										
										
										
											2015-10-09 17:52:55 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | editController.username = async function (req, res, next) { | 
					
						
							|  |  |  | 	await renderRoute('username', req, res, next); | 
					
						
							| 
									
										
										
										
											2015-10-09 17:52:55 -04:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-06-16 16:03:06 -04:00
										 |  |  | editController.email = async function (req, res) { | 
					
						
							|  |  |  | 	req.session.registration = req.session.registration || {}; | 
					
						
							|  |  |  | 	req.session.registration.updateEmail = true; | 
					
						
							|  |  |  | 	req.session.registration.uid = req.uid; | 
					
						
							|  |  |  | 	helpers.redirect(res, '/register/complete'); | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2015-10-09 17:52:55 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | async function renderRoute(name, req, res, next) { | 
					
						
							|  |  |  | 	const userData = await getUserData(req, next); | 
					
						
							|  |  |  | 	if (!userData) { | 
					
						
							|  |  |  | 		return next(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	if (meta.config[`${name}:disableEdit`] && !userData.isAdmin) { | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 		return helpers.notAllowed(req, res); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (name === 'password') { | 
					
						
							|  |  |  | 		userData.minimumPasswordLength = meta.config.minimumPasswordLength; | 
					
						
							|  |  |  | 		userData.minimumPasswordStrength = meta.config.minimumPasswordStrength; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	userData.title = `[[pages:account/edit/${name}, ${userData.username}]]`; | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 	userData.breadcrumbs = helpers.buildBreadcrumbs([ | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			text: userData.username, | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			url: `/user/${userData.userslug}`, | 
					
						
							| 
									
										
										
										
											2015-09-25 01:52:41 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 		{ | 
					
						
							|  |  |  | 			text: '[[user:edit]]', | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			url: `/user/${userData.userslug}/edit`, | 
					
						
							| 
									
										
										
										
											2017-02-17 19:31:21 -07:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 		{ | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 			text: `[[user:${name}]]`, | 
					
						
							| 
									
										
										
										
											2017-05-24 00:02:30 -04:00
										 |  |  | 		}, | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 	]); | 
					
						
							| 
									
										
										
										
											2015-09-25 13:11:11 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-03 23:59:08 -07:00
										 |  |  | 	res.render(`account/edit/${name}`, userData); | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2015-09-25 13:11:11 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | async function getUserData(req) { | 
					
						
							| 
									
										
										
										
											2021-05-17 10:26:32 -04:00
										 |  |  | 	const userData = await accountHelpers.getUserDataByUserSlug(req.params.userslug, req.uid, req.query); | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 	if (!userData) { | 
					
						
							|  |  |  | 		return null; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2015-09-25 13:11:11 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-12 20:58:29 -04:00
										 |  |  | 	userData.hasPassword = await user.hasPassword(userData.uid); | 
					
						
							|  |  |  | 	return userData; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-06-24 23:04:24 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | editController.uploadPicture = async function (req, res, next) { | 
					
						
							|  |  |  | 	const userPhoto = req.files.files[0]; | 
					
						
							|  |  |  | 	try { | 
					
						
							|  |  |  | 		const updateUid = await user.getUidByUserslug(req.params.userslug); | 
					
						
							|  |  |  | 		const isAllowed = await privileges.users.canEdit(req.uid, updateUid); | 
					
						
							|  |  |  | 		if (!isAllowed) { | 
					
						
							|  |  |  | 			return helpers.notAllowed(req, res); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		await user.checkMinReputation(req.uid, updateUid, 'min:rep:profile-picture'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		const image = await user.uploadCroppedPictureFile({ | 
					
						
							| 
									
										
										
										
											2021-03-03 17:11:13 -05:00
										 |  |  | 			callerUid: req.uid, | 
					
						
							| 
									
										
										
										
											2020-06-24 23:04:24 -04:00
										 |  |  | 			uid: updateUid, | 
					
						
							|  |  |  | 			file: userPhoto, | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		res.json([{ | 
					
						
							|  |  |  | 			name: userPhoto.name, | 
					
						
							|  |  |  | 			url: image.url, | 
					
						
							|  |  |  | 		}]); | 
					
						
							|  |  |  | 	} catch (err) { | 
					
						
							|  |  |  | 		next(err); | 
					
						
							|  |  |  | 	} finally { | 
					
						
							|  |  |  | 		await file.delete(userPhoto.path); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | }; |