185 lines
6.3 KiB
JavaScript
185 lines
6.3 KiB
JavaScript
var connection = require("./connection");
|
|
var hash = require("../authentication/hash");
|
|
var dbRights = require("./user-rights");
|
|
var async = require("async");
|
|
var dbUsers = this;
|
|
|
|
exports.getUser = function(userIdOrUserName, callback){
|
|
console.log(userIdOrUserName + " -- " + typeof userIdOrUserName);
|
|
var queryString = "SELECT * FROM Users WHERE UserName = ? AND DeletedDate IS NULL;";
|
|
if (typeof userIdOrUserName == "number"){
|
|
queryString = "SELECT * FROM Users WHERE Id = ? AND DeletedDate IS NULL;";
|
|
}
|
|
connection.query(queryString,[userIdOrUserName],function(error,rows,fields){
|
|
if (error){
|
|
console.log(error);
|
|
callback(error);
|
|
return;
|
|
}
|
|
|
|
var user = null;
|
|
if (rows.length > 0){
|
|
user = {};
|
|
user.id = rows[0].Id;
|
|
user.userName = rows[0].UserName;
|
|
user.email = rows[0].Email;
|
|
user.password = rows[0].Password;
|
|
user.deletedDate = rows[0].deletedDate;
|
|
}
|
|
console.log(user);
|
|
//console.log(fields);
|
|
callback(null,user);
|
|
});
|
|
}
|
|
|
|
exports.insertUser = function(userNameOrUserObject, passwordOrCallbackFunction, email, topCallback){
|
|
if (typeof passwordOrCallbackFunction === 'function'){
|
|
insertUserFromObject(userNameOrUserObject,passwordOrCallbackFunction);
|
|
return;
|
|
}
|
|
|
|
async.waterfall([
|
|
//First step is to check if the user name already exists
|
|
function(callback){
|
|
dbUsers.getUser(userNameOrUserObject,function(error,user){
|
|
if (error){
|
|
callback(error);
|
|
return;
|
|
}
|
|
if (user){
|
|
callback("user-exists"); //The user exists
|
|
return;
|
|
} else {
|
|
callback(null,false); //The user does not exist
|
|
return;
|
|
}
|
|
});
|
|
},
|
|
//If the user does not exist - get the hashed version of the password
|
|
function(userExists,callback){
|
|
//Get the password hash
|
|
hash.hashPassword(passwordOrCallbackFunction,function(error,hashedPW){
|
|
if (error){
|
|
callback(error);
|
|
return;
|
|
}
|
|
callback(null,hashedPW);
|
|
});
|
|
},
|
|
//Insert the user into the database with the hashed password
|
|
function(hashedPW,callback){
|
|
//User does not exist. Lets add it
|
|
connection.query("INSERT INTO Users (CreatedDate,ModifiedDate,Id,UserName,Password,DeletedDate,Email) VALUES(CURRENT_TIMESTAMP,CURRENT_TIMESTAMP,NULL,?,?,NULL,?)",[userNameOrUserObject,hashedPW,email],function(error,result){
|
|
if (error){
|
|
callback(error);
|
|
return;
|
|
}
|
|
callback(null,result);
|
|
});
|
|
},
|
|
//Get the new user from the database and return it to the final function
|
|
function(insertResult,callback){
|
|
//Get the inserted user
|
|
dbUsers.getUser(insertResult.insertId,function(error,user){
|
|
if(error){
|
|
callback(error);
|
|
return;
|
|
}
|
|
if (!user){
|
|
callback("error retrieving new user");
|
|
return;
|
|
}
|
|
callback(null,user);
|
|
});
|
|
},
|
|
//add the rights for this user
|
|
function(user, callback){
|
|
var rightIdArray = [1,2];
|
|
dbRights.addRights(user.id,rightIdArray,function(error){
|
|
if (error){
|
|
callback(error);
|
|
} else {
|
|
callback(null,user);
|
|
}
|
|
});
|
|
}
|
|
],function(err,result){
|
|
//Complete
|
|
if (err == "user-exists"){
|
|
topCallback("User already Exists");
|
|
return;
|
|
}
|
|
if (err){
|
|
topCallback(err);
|
|
return;
|
|
}
|
|
result.password = "*";
|
|
topCallback(null,result);
|
|
});
|
|
}
|
|
|
|
function insertUserFromObject(sermon, callback){
|
|
dbSermons.insertUser(user.userName,user.password,user.email,callback);
|
|
}
|
|
|
|
exports.updateUser = function(userIdOrUserObject, userNameOrCallbackFunction, password, email, deletedDate, topCallback){
|
|
if (typeof userNameOrCallbackFunction == 'function'){
|
|
updateUserFromObject(userIdOrUserObject,userNameOrCallbackFunction);
|
|
return;
|
|
}
|
|
|
|
async.waterfall([
|
|
//First Step: == GET Hashed Password
|
|
function(callback){
|
|
hash.hashPassword(password,function(error, hashedPW){
|
|
if (error){
|
|
callback(error);
|
|
return;
|
|
}
|
|
callback(null,hashedPW);
|
|
});
|
|
},
|
|
function(hashedPW, callback){
|
|
connection.query("UPDATE Users SET UserName = ?, Password = ?, Email = ?, DeletedDate = ? WHERE Id = ?;",[userNameOrCallbackFunction,hashedPW,email,deletedDate,userIdOrUserObject],function(error,result){
|
|
if (error){
|
|
callback(error);
|
|
return;
|
|
}
|
|
callback(null, result);
|
|
});
|
|
},
|
|
function(updateResult, callback){
|
|
dbUsers.getUser(userIdOrUserObject,function(error,user){
|
|
if (error){
|
|
callback(error);
|
|
return;
|
|
}
|
|
callback(null, user);
|
|
});
|
|
}
|
|
], function(error, updatedUser){
|
|
if (error){
|
|
topCallback(error);
|
|
return;
|
|
}
|
|
topCallback(null,updatedUser);
|
|
});
|
|
}
|
|
|
|
function updateUserFromObject(user,callback){
|
|
dbUsers.updateUser(user.id,user.userName,user.password,user.email,user.deletedDate,callback);
|
|
}
|
|
|
|
exports.deleteUser = function(userIdOrUserObject, callback){
|
|
if (typeof userIdOrUserObject !== "number"){
|
|
userIdOrUserObject = userIdOrUserObject.id;
|
|
}
|
|
connection.query("UPDATE Users SET DeletedDate = CURRENT_TIMESTAMP WHERE Id = ?;",[userIdOrUserObject],function(error,result){
|
|
if (error){
|
|
callback(error);
|
|
return;
|
|
}
|
|
callback(null,result);
|
|
});
|
|
}
|