From b17f9d655c9a0c6258acbf95eb1f355f67892b0a Mon Sep 17 00:00:00 2001 From: jrtechs Date: Sat, 19 Jan 2019 11:12:58 -0500 Subject: [PATCH 1/3] Added users section of admin page to modify and add users. --- admin/admin.js | 7 +- admin/users.js | 175 ++++++++++++++++++++++++++++++++ includes/html/adminHeader.html | 5 +- templates/admin/adminUsers.html | 95 +++++++++++++++++ utils/sql.js | 143 ++++++++++++++++++++------ utils/utils.js | 24 ----- 6 files changed, 393 insertions(+), 56 deletions(-) create mode 100644 admin/users.js create mode 100644 templates/admin/adminUsers.html diff --git a/admin/admin.js b/admin/admin.js index 5e4b7c7..4a95c8b 100644 --- a/admin/admin.js +++ b/admin/admin.js @@ -48,6 +48,10 @@ module.exports= { page = "./posts.js"; } + else if(filename.includes("/users")) + { + page = "./users.js"; + } require(page).main(postData, templateContext).then(function(template) { @@ -61,7 +65,8 @@ module.exports= } else { - require("./login/login.js").main(request, clientAddress, templateContext).then(function() + require("./login/login.js").main(request, clientAddress, templateContext) + .then(function() { resolve(); }).catch(function(err) diff --git a/admin/users.js b/admin/users.js new file mode 100644 index 0000000..b0d1c4c --- /dev/null +++ b/admin/users.js @@ -0,0 +1,175 @@ +/** + * File which deals with adding and removing downloads from + * the admin section of the website. + * + * @author Jeffery Russell 6-30-18 + */ + +/** Whiskers template file */ +const TEMPLATE_FILE = "admin/adminUsers.html"; + + +const includes = require('../includes/includes.js'); + +//updates db +const sql = require('../utils/sql'); + +//parses post data +const qs = require('querystring'); + + +/** + * Processes post requests from the addDownload form + * + * @param postData + * @returns {*|Promise} + */ +const addUserPostData = function(postData) +{ + return new Promise(function(resolve, reject) + { + const post = qs.parse(postData); + if(post.add_user) + { + sql.addUser(post.add_user_name, post.add_user_password) + .then(function() + { + resolve(); + }).catch(function(error) + { + reject(error); + }) + } + else + { + resolve(); + } + }); +}; + + +/** + * Removes a download if requested by the + * post data from an admin. + */ +const removeUserPost = function(postData) +{ + return new Promise(function(resolve, reject) + { + const post = qs.parse(postData); + console.log(post); + if(post.delete_user) + { + console.log("Removing user: " + post.delete_user); + sql.removeUser(post.delete_user).then(function() + { + resolve(); + }).catch(function(err) + { + reject(err); + }); + } + else + { + resolve(); + } + }); +}; + + +/** + * Processes post data to determine if the user requested that + * a user be updated in the database. + */ +const editUserPost = function(postData, templateContext) +{ + return new Promise(function(resolve, reject) + { + const post = qs.parse(postData); + if(post.edit_user) + { + sql.getUserByID(post.edit_user).then(function(user) + { + if(user.length == 1) + { + templateContext.edit_user = post.edit_user; + templateContext.user_name = user[0].user_name; + resolve(); + } + else + { + resolve(); + } + }).catch(function(err) + { + reject(err); + }); + } + else if(post.edit_user_2) + { + sql.updateUser(post.edit_user_2, post.edit_user_name, post.edit_user_password) + .then(function() + { + resolve(); + }) + } + else + { + resolve(); + } + }); +}; + + +/** + * Fetches the download items in the database so that + * the template engine can use it to display them in + * a table. + * + * @param templateContext-- context item used by whiskers + * @returns {Promise} + */ +const getUserInformation = function(templateContext) +{ + return new Promise(function(resolve, reject) + { + sql.getAllUsers().then(function(users) + { + templateContext.users = users; + resolve(); + }).catch(function(error) + { + reject(error); + }); + }); +}; + + +module.exports= + { + /** Fetches context information for the template and handles + * post data for the downloads. + * + * @param postData posted by user + * @param templateContext json object used as the template context + * @returns {Promise} renders the template used for this page + */ + main: function(postData, templateContext) + { + return new Promise(function(resolve, reject) + { + Promise.all([includes.fetchTemplate(TEMPLATE_FILE), + addUserPostData(postData), + removeUserPost(postData), + editUserPost(postData, templateContext), + getUserInformation(templateContext)]).then(function(template) + { + resolve(template[0]); + }).catch(function(error) + { + console.log("error in add downloads.js"); + reject(error); + }); + }); + } + }; \ No newline at end of file diff --git a/includes/html/adminHeader.html b/includes/html/adminHeader.html index a47e45b..6076443 100644 --- a/includes/html/adminHeader.html +++ b/includes/html/adminHeader.html @@ -58,7 +58,7 @@