From 383b68e7fc53777dad680ddd3a16e2fb5ba25ed1 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Wed, 12 Mar 2014 21:57:25 -0400 Subject: [PATCH] user js refactor --- src/user.js | 71 --------------------------------------------- src/user/admin.js | 8 +++++ src/user/profile.js | 62 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 72 deletions(-) diff --git a/src/user.js b/src/user.js index 360266e887..78184bdbdd 100644 --- a/src/user.js +++ b/src/user.js @@ -159,14 +159,6 @@ var bcrypt = require('bcryptjs'), }); }; - User.ban = function(uid, callback) { - User.setUserField(uid, 'banned', 1, callback); - }; - - User.unban = function(uid, callback) { - User.setUserField(uid, 'banned', 0, callback); - }; - User.getUserField = function(uid, field, callback) { db.getObjectField('user:' + uid, field, callback); }; @@ -229,8 +221,6 @@ var bcrypt = require('bcryptjs'), }); }; - - User.updateLastOnlineTime = function(uid, callback) { User.getUserField(uid, 'status', function(err, status) { function cb(err) { @@ -264,67 +254,6 @@ var bcrypt = require('bcryptjs'), }); }; - User.changePassword = function(uid, data, callback) { - if(!data || !data.uid) { - return callback(new Error('invalid-uid')); - } - - function hashAndSetPassword(callback) { - User.hashPassword(data.newPassword, function(err, hash) { - if(err) { - return callback(err); - } - - User.setUserField(data.uid, 'password', hash, function(err) { - if(err) { - return callback(err); - } - - if(parseInt(uid, 10) === parseInt(data.uid, 10)) { - events.logPasswordChange(data.uid); - } else { - events.logAdminChangeUserPassword(uid, data.uid); - } - - callback(); - }); - }); - } - - if (!utils.isPasswordValid(data.newPassword)) { - return callback(new Error('Invalid password!')); - } - - if(parseInt(uid, 10) !== parseInt(data.uid, 10)) { - User.isAdministrator(uid, function(err, isAdmin) { - if(err || !isAdmin) { - return callback(err || new Error('not-allowed')); - } - - hashAndSetPassword(callback); - }); - } else { - User.getUserField(uid, 'password', function(err, currentPassword) { - if(err) { - return callback(err); - } - - if (currentPassword !== null) { - bcrypt.compare(data.currentPassword, currentPassword, function(err, res) { - if (err || !res) { - return callback(err || new Error('Your current password is not correct!')); - } - - hashAndSetPassword(callback); - }); - } else { - // No password in account (probably SSO login) - hashAndSetPassword(callback); - } - }); - } - }; - User.setUserField = function(uid, field, value, callback) { db.setObjectField('user:' + uid, field, value, callback); }; diff --git a/src/user/admin.js b/src/user/admin.js index 6a94c1715b..ece5b56cf3 100644 --- a/src/user/admin.js +++ b/src/user/admin.js @@ -43,4 +43,12 @@ module.exports = function(User) { } ], callback); }; + + User.ban = function(uid, callback) { + User.setUserField(uid, 'banned', 1, callback); + }; + + User.unban = function(uid, callback) { + User.setUserField(uid, 'banned', 0, callback); + }; }; \ No newline at end of file diff --git a/src/user/profile.js b/src/user/profile.js index 8273cf72de..e1fc6284d4 100644 --- a/src/user/profile.js +++ b/src/user/profile.js @@ -1,7 +1,8 @@ 'use strict'; -var async = require('async'), +var bcrypt = require('bcryptjs'), + async = require('async'), validator = require('validator'), S = require('string'), @@ -193,4 +194,63 @@ module.exports = function(User) { }); } + User.changePassword = function(uid, data, callback) { + if(!data || !data.uid) { + return callback(new Error('invalid-uid')); + } + + function hashAndSetPassword(callback) { + User.hashPassword(data.newPassword, function(err, hash) { + if(err) { + return callback(err); + } + + User.setUserField(data.uid, 'password', hash, function(err) { + if(err) { + return callback(err); + } + + if(parseInt(uid, 10) === parseInt(data.uid, 10)) { + events.logPasswordChange(data.uid); + } else { + events.logAdminChangeUserPassword(uid, data.uid); + } + + callback(); + }); + }); + } + + if (!utils.isPasswordValid(data.newPassword)) { + return callback(new Error('Invalid password!')); + } + + if(parseInt(uid, 10) !== parseInt(data.uid, 10)) { + User.isAdministrator(uid, function(err, isAdmin) { + if(err || !isAdmin) { + return callback(err || new Error('not-allowed')); + } + + hashAndSetPassword(callback); + }); + } else { + User.getUserField(uid, 'password', function(err, currentPassword) { + if(err) { + return callback(err); + } + + if (!currentPassword) { + return hashAndSetPassword(callback); + } + + bcrypt.compare(data.currentPassword, currentPassword, function(err, res) { + if (err || !res) { + return callback(err || new Error('Your current password is not correct!')); + } + hashAndSetPassword(callback); + }); + }); + } + }; + }; \ No newline at end of file