user js refactor

v1.18.x
barisusakli 11 years ago
parent cc0ac29b36
commit 383b68e7fc

@ -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);
};

@ -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);
};
};

@ -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);
});
});
}
};
};
Loading…
Cancel
Save