| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | var async = require('async'), | 
					
						
							| 
									
										
										
										
											2014-04-15 00:17:35 -04:00
										 |  |  | 	db = require('../database'), | 
					
						
							|  |  |  | 	utils = require('../../public/src/utils'), | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 	validator = require('validator'), | 
					
						
							| 
									
										
										
										
											2014-04-15 00:17:35 -04:00
										 |  |  | 	plugins = require('../plugins'), | 
					
						
							|  |  |  | 	groups = require('../groups'), | 
					
						
							| 
									
										
										
										
											2014-05-22 10:21:01 -04:00
										 |  |  | 	meta = require('../meta'), | 
					
						
							| 
									
										
										
										
											2014-04-15 00:17:35 -04:00
										 |  |  | 	notifications = require('../notifications'), | 
					
						
							|  |  |  | 	translator = require('../../public/src/translator'); | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = function(User) { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	User.create = function(userData, callback) { | 
					
						
							| 
									
										
										
										
											2014-08-11 16:26:20 -04:00
										 |  |  | 		var gravatar = User.createGravatarURLFromEmail(userData.email); | 
					
						
							|  |  |  | 		var timestamp = Date.now(); | 
					
						
							|  |  |  | 		var password = userData.password; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		userData = { | 
					
						
							|  |  |  | 			'username': userData.username.trim(), | 
					
						
							|  |  |  | 			'email': userData.email, | 
					
						
							|  |  |  | 			'joindate': timestamp, | 
					
						
							|  |  |  | 			'picture': gravatar, | 
					
						
							|  |  |  | 			'gravatarpicture': gravatar, | 
					
						
							|  |  |  | 			'fullname': '', | 
					
						
							|  |  |  | 			'location': '', | 
					
						
							|  |  |  | 			'birthday': '', | 
					
						
							|  |  |  | 			'website': '', | 
					
						
							|  |  |  | 			'signature': '', | 
					
						
							|  |  |  | 			'uploadedpicture': '', | 
					
						
							|  |  |  | 			'profileviews': 0, | 
					
						
							|  |  |  | 			'reputation': 0, | 
					
						
							|  |  |  | 			'postcount': 0, | 
					
						
							|  |  |  | 			'lastposttime': 0, | 
					
						
							|  |  |  | 			'banned': 0, | 
					
						
							|  |  |  | 			'status': 'online' | 
					
						
							|  |  |  | 		}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 		userData.userslug = utils.slugify(userData.username); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (userData.email !== undefined) { | 
					
						
							|  |  |  | 			userData.email = userData.email.trim(); | 
					
						
							|  |  |  | 			userData.email = validator.escape(userData.email); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 		async.parallel({ | 
					
						
							|  |  |  | 			emailValid: function(next) { | 
					
						
							| 
									
										
										
										
											2014-04-07 20:19:40 -04:00
										 |  |  | 				if (userData.email) { | 
					
						
							| 
									
										
										
										
											2014-04-09 22:26:23 -04:00
										 |  |  | 					next(!utils.isEmailValid(userData.email) ? new Error('[[error:invalid-email]]') : null); | 
					
						
							| 
									
										
										
										
											2014-04-07 20:19:40 -04:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					next(); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 			userNameValid: function(next) { | 
					
						
							| 
									
										
										
										
											2014-04-09 22:26:23 -04:00
										 |  |  | 				next((!utils.isUserNameValid(userData.username) || !userData.userslug) ? new Error('[[error:invalid-username]]') : null); | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 			passwordValid: function(next) { | 
					
						
							| 
									
										
										
										
											2014-06-21 19:54:16 -04:00
										 |  |  | 				if (password) { | 
					
						
							|  |  |  | 					next(!utils.isPasswordValid(password) ? new Error('[[error:invalid-password]]') : null); | 
					
						
							| 
									
										
										
										
											2014-03-18 20:21:24 -04:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					next(); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 			renamedUsername: function(next) { | 
					
						
							| 
									
										
										
										
											2014-05-22 10:21:01 -04:00
										 |  |  | 				meta.userOrGroupExists(userData.userslug, function(err, exists) { | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 					if (err) { | 
					
						
							|  |  |  | 						return next(err); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-05-22 10:21:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-15 00:17:35 -04:00
										 |  |  | 					if (exists) { | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 						var	newUsername = ''; | 
					
						
							| 
									
										
										
										
											2014-04-15 00:17:35 -04:00
										 |  |  | 						async.forever(function(next) { | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 							newUsername = userData.username + (Math.floor(Math.random() * 255) + 1); | 
					
						
							| 
									
										
										
										
											2014-04-15 00:17:35 -04:00
										 |  |  | 							User.exists(newUsername, function(err, exists) { | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 								if (err) { | 
					
						
							|  |  |  | 									return callback(err); | 
					
						
							|  |  |  | 								} | 
					
						
							| 
									
										
										
										
											2014-04-15 00:17:35 -04:00
										 |  |  | 								if (!exists) { | 
					
						
							|  |  |  | 									next(newUsername); | 
					
						
							|  |  |  | 								} else { | 
					
						
							|  |  |  | 									next(); | 
					
						
							|  |  |  | 								} | 
					
						
							|  |  |  | 							}); | 
					
						
							|  |  |  | 						}, function(username) { | 
					
						
							|  |  |  | 							next(null, username); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						next(); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 				}); | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 			emailAvailable: function(next) { | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 				if (userData.email) { | 
					
						
							|  |  |  | 					User.email.available(userData.email, function(err, available) { | 
					
						
							|  |  |  | 						if (err) { | 
					
						
							|  |  |  | 							return next(err); | 
					
						
							|  |  |  | 						} | 
					
						
							| 
									
										
										
										
											2014-04-09 22:26:23 -04:00
										 |  |  | 						next(!available ? new Error('[[error:email-taken]]') : null); | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 					}); | 
					
						
							|  |  |  | 				} else { | 
					
						
							|  |  |  | 					next(); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 			customFields: function(next) { | 
					
						
							| 
									
										
										
										
											2014-07-24 16:44:42 -04:00
										 |  |  | 				plugins.fireHook('filter:user.custom_fields', userData, next); | 
					
						
							| 
									
										
										
										
											2014-06-04 18:55:25 -04:00
										 |  |  | 			}, | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 			userData: function(next) { | 
					
						
							| 
									
										
										
										
											2014-07-24 16:44:42 -04:00
										 |  |  | 				plugins.fireHook('filter:user.create', userData, next); | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 		}, function(err, results) { | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 			if (err) { | 
					
						
							|  |  |  | 				return callback(err); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-24 16:44:42 -04:00
										 |  |  | 			userData = utils.merge(results.userData, results.customFields); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 			var userNameChanged = !!results.renamedUsername; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-16 15:31:47 -04:00
										 |  |  | 			if (userNameChanged) { | 
					
						
							| 
									
										
										
										
											2014-07-23 16:49:32 -04:00
										 |  |  | 				userData.username = results.renamedUsername; | 
					
						
							|  |  |  | 				userData.userslug = utils.slugify(results.renamedUsername); | 
					
						
							| 
									
										
										
										
											2014-04-15 00:17:35 -04:00
										 |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 			db.incrObjectField('global', 'nextUid', function(err, uid) { | 
					
						
							| 
									
										
										
										
											2014-08-11 16:26:20 -04:00
										 |  |  | 				if (err) { | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 					return callback(err); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-11 16:26:20 -04:00
										 |  |  | 				userData.uid = uid; | 
					
						
							| 
									
										
										
										
											2014-06-04 18:55:25 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 				db.setObject('user:' + uid, userData, function(err) { | 
					
						
							| 
									
										
										
										
											2014-08-11 16:26:20 -04:00
										 |  |  | 					if (err) { | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 						return callback(err); | 
					
						
							|  |  |  | 					} | 
					
						
							| 
									
										
										
										
											2014-05-04 14:33:12 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 					db.setObjectField('username:uid', userData.username, uid); | 
					
						
							|  |  |  | 					db.setObjectField('userslug:uid', userData.userslug, uid); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					if (userData.email !== undefined) { | 
					
						
							| 
									
										
										
										
											2014-07-14 13:28:04 -04:00
										 |  |  | 						db.setObjectField('email:uid', userData.email.toLowerCase(), uid); | 
					
						
							| 
									
										
										
										
											2014-06-03 17:23:36 -04:00
										 |  |  | 						if (parseInt(uid, 10) !== 1 && parseInt(meta.config.requireEmailConfirmation, 10) === 1) { | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 							User.email.verify(uid, userData.email); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					plugins.fireHook('action:user.create', userData); | 
					
						
							|  |  |  | 					db.incrObjectField('global', 'userCount'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					db.sortedSetAdd('users:joindate', timestamp, uid); | 
					
						
							|  |  |  | 					db.sortedSetAdd('users:postcount', 0, uid); | 
					
						
							|  |  |  | 					db.sortedSetAdd('users:reputation', 0, uid); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-20 09:12:49 -04:00
										 |  |  | 					groups.join('registered-users', uid); | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-16 15:31:47 -04:00
										 |  |  | 					if (userNameChanged) { | 
					
						
							|  |  |  | 						notifications.create({ | 
					
						
							| 
									
										
										
										
											2014-06-20 17:53:40 -04:00
										 |  |  | 							bodyShort: '[[user:username_taken_workaround, ' + userData.username + ']]', | 
					
						
							|  |  |  | 							bodyLong: '', | 
					
						
							| 
									
										
										
										
											2014-05-22 21:31:48 -04:00
										 |  |  | 							image: 'brand:logo', | 
					
						
							| 
									
										
										
										
											2014-04-16 15:31:47 -04:00
										 |  |  | 							datetime: Date.now() | 
					
						
							| 
									
										
										
										
											2014-07-28 15:52:33 -04:00
										 |  |  | 						}, function(err, nid) { | 
					
						
							|  |  |  | 							if (!err) { | 
					
						
							|  |  |  | 								notifications.push(nid, uid); | 
					
						
							|  |  |  | 							} | 
					
						
							| 
									
										
										
										
											2014-04-15 00:17:35 -04:00
										 |  |  | 						}); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-17 21:47:37 -04:00
										 |  |  | 					if (password) { | 
					
						
							|  |  |  | 						User.hashPassword(password, function(err, hash) { | 
					
						
							|  |  |  | 							if(err) { | 
					
						
							|  |  |  | 								return callback(err); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 							User.setUserField(uid, 'password', hash); | 
					
						
							|  |  |  | 							callback(null, uid); | 
					
						
							|  |  |  | 						}); | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						callback(null, uid); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 				}); | 
					
						
							|  |  |  | 			}); | 
					
						
							|  |  |  | 		}); | 
					
						
							|  |  |  | 	}; | 
					
						
							| 
									
										
										
										
											2014-04-10 20:31:57 +01:00
										 |  |  | }; |