Browse Source

Got all the user routes to work.

pull/6/head
jrtechs 5 years ago
parent
commit
1658127b68
16 changed files with 126 additions and 71 deletions
  1. +22
    -0
      conf.json
  2. +12
    -5
      configManager.js
  3. +1
    -1
      html/header.html
  4. +2
    -2
      routes/index.js
  5. +1
    -1
      routes/user/addUser.js
  6. +2
    -1
      routes/user/edituser.js
  7. +46
    -0
      routes/user/index.js
  8. +0
    -0
      routes/user/login.js
  9. +0
    -0
      routes/user/logout.js
  10. +2
    -2
      routes/user/removeuser.js
  11. +3
    -3
      routes/user/revokeAPI.js
  12. +1
    -1
      routes/user/updateUser.js
  13. +0
    -34
      routes/users/index.js
  14. +21
    -15
      server.js
  15. +7
    -4
      user.js
  16. +6
    -2
      utils.js

+ 22
- 0
conf.json View File

@ -0,0 +1,22 @@
{
"port": 5000,
"sessionSecret": "super_duper_secret_secret",
"users": [
{
"username": "jeff",
"password": "bb75cb47ec1c1132d25bf2afbc3e10ea0181748c9bdef8e0909f2cbabd4cad57",
"id": 1,
"salt": "4a9cbd94309329e9591b60a3def16d10724cb89444deee357a3eb0dd1e95d683",
"admin": true,
"api": "39c75cfe9b367ccf2bf1fe5451e1f7a966f653abb827db6bb96a2f532776df49"
},
{
"username": "josh",
"api": "5fb6cc61e5589b302b7383d98e7412cde8d04491a6e5d765445d334331a752e7",
"id": 2,
"salt": "4d7159f4d6aa4ee3467a13ef7a7a3ec17ae4b560756673c21eeee8bc887ab3c9",
"password": "320142ed202563b462b744f221cbc236cb042efda47b550395a32920f0dd937c",
"admin": false
}
]
}

+ 12
- 5
configManager.js View File

@ -1,13 +1,20 @@
module.exports =
{
const fileIO = require('./fileIO');
const CONFIG_FILE_NAME = "conf.json";
const config = fileIO.getFileAsJSON(CONFIG_FILE_NAME);
module.exports=
{
getConfiguration: function() getConfiguration: function()
{ {
return config;
}, },
syncToDisk: function() syncToDisk: function()
{ {
fileIO.writeJSONToFile(CONFIG_FILE_NAME, config);
// console.log(config);
// console.log("Config Updated");
} }
};
};

+ 1
- 1
html/header.html View File

@ -38,7 +38,7 @@
<div class="collapse navbar-collapse" id="navbarSupportedContent"> <div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav mr-auto"> <ul class="navbar-nav mr-auto">
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/users">Users</a>
<a class="nav-link" href="/user">Users</a>
</li> </li>
<li class="nav-item"> <li class="nav-item">
<a class="nav-link" href="/videos">Videos</a> <a class="nav-link" href="/videos">Videos</a>

+ 2
- 2
routes/index.js View File

@ -12,8 +12,8 @@ routes.get('/videos', videos);
const watch = require('./watch'); const watch = require('./watch');
routes.get('/watch', watch); routes.get('/watch', watch);
const users = require('./users');
routes.get('/users', users);
const user = require('./user');
routes.use('/user', user);
const utils = require("../utils"); const utils = require("../utils");

routes/users/addUser.js → routes/user/addUser.js View File

@ -13,7 +13,7 @@ routes.post('/', (request, result) =>
if(request.body.admin === 'on') if(request.body.admin === 'on')
admin = true; admin = true;
userUtils.addUser(request.body.username, request.body.password,admin); userUtils.addUser(request.body.username, request.body.password,admin);
result.redirect('/users');
result.redirect('/user');
} }
else else
{ {

routes/users/edituser.js → routes/user/edituser.js View File

@ -8,11 +8,12 @@ routes.post('/', (request, result) =>
{ {
if(utils.checkPrivilege(request) === utils.PRIVILEGE.ADMIN) if(utils.checkPrivilege(request) === utils.PRIVILEGE.ADMIN)
{ {
console.log("zat");
var admin = false; var admin = false;
if(request.body.admin === 'on') if(request.body.admin === 'on')
admin = true; admin = true;
userUtils.editUser(request.body.id, request.body.username, request.body.password,admin); userUtils.editUser(request.body.id, request.body.username, request.body.password,admin);
result.redirect('/users');
result.redirect('/user');
} }
else else
{ {

+ 46
- 0
routes/user/index.js View File

@ -0,0 +1,46 @@
const routes = require('express').Router();
const utils = require("../../utils");
const addUser = require('./addUser');
routes.use('/addUser', addUser);
const editUser = require('./edituser');
routes.use('/edituser', editUser);
const login = require('./login');
routes.use('/login', login);
const logout = require('./logout');
routes.use('/logout', logout);
const removeuser = require('./removeuser');
routes.use('/removeuser', removeuser);
const revokeAPI = require('./revokeAPI');
routes.use('/revokeAPI', revokeAPI);
const updateUser = require('./updateUser');
routes.use('/updateUser', updateUser);
const configLoader = require("../../configManager");
function getUserInformation(templateContext, request)
{
templateContext.users = configLoader.getConfiguration().users;
templateContext.apiKey = request.session.API;
templateContext.id = request.session.userID;
templateContext.username = request.session.username;
}
routes.get('/', (request, result) =>
{
utils.renderHTML(request, result, "users.html", getUserInformation);
});
module.exports = routes;

routes/users/login.js → routes/user/login.js View File


routes/users/logout.js → routes/user/logout.js View File


routes/users/removeuser.js → routes/user/removeuser.js View File

@ -4,12 +4,12 @@ const utils = require("../../utils");
const userUtils = require("../../user"); const userUtils = require("../../user");
routes.get('/', (request, result) =>
routes.post('/', (request, result) =>
{ {
if(utils.checkPrivilege(request) === utils.PRIVILEGE.ADMIN) if(utils.checkPrivilege(request) === utils.PRIVILEGE.ADMIN)
{ {
userUtils.removeUser(request.body.id); userUtils.removeUser(request.body.id);
result.redirect('/users');
result.redirect('/user');
} }
else else
{ {

routes/users/revokeAPI.js → routes/user/revokeAPI.js View File

@ -8,15 +8,15 @@ routes.post('/', (request, result) =>
{ {
if(utils.checkPrivilege(request) === utils.PRIVILEGE.ADMIN) if(utils.checkPrivilege(request) === utils.PRIVILEGE.ADMIN)
{ {
userUtils.revokeAPI(request.body.username, config);
userUtils.revokeAPI(request.body.username);
request.session.API = userUtils.getAPIKEY(request.session.username); request.session.API = userUtils.getAPIKEY(request.session.username);
} }
else if (checkPrivilege(request) === PRIVILEGE.MEMBER)
else if (utils.checkPrivilege(request) === PRIVILEGE.MEMBER)
{ {
userUtils.revokeAPI(request.session.username); userUtils.revokeAPI(request.session.username);
request.session.API = userUtils.getAPIKEY(request.session.username); request.session.API = userUtils.getAPIKEY(request.session.username);
} }
result.redirect('/users');
result.redirect('/user');
}); });
module.exports = routes; module.exports = routes;

routes/users/updateUser.js → routes/user/updateUser.js View File

@ -10,7 +10,7 @@ routes.post('/', (request, result) =>
{ {
var admin = false; var admin = false;
userUtils.editUser(request.session.userID, request.body.username, request.body.password,admin); userUtils.editUser(request.session.userID, request.body.username, request.body.password,admin);
result.redirect('/users');
result.redirect('/user');
} }
else else
{ {

+ 0
- 34
routes/users/index.js View File

@ -1,34 +0,0 @@
const routes = require('express').Router();
const utils = require("../../utils");
const addUser = require('./addUser');
routes.get('/addUser', addUser);
const editUser = require('./edituser');
routes.get('/edituser', editUser);
const login = require('./login');
routes.get('/login', login);
const logout = require('./logout');
routes.get('/logout', logout);
const removeuser = require('./removeuser');
routes.get('/removeuser', removeuser);
const revokeAPI = require('./revokeAPI');
routes.get('/revokeAPI', revokeAPI);
const updateUser = require('./updateUser');
routes.get('/updateUser', updateUser);
routes.get('/', (request, result) =>
{
utils.renderHTML(request, result, "users.html", getUserInformation);
});
module.exports = routes;

+ 21
- 15
server.js View File

@ -8,30 +8,36 @@ const fileIO = require('./fileIO');
const userUtils = require('./user.js'); const userUtils = require('./user.js');
const configLoader = require('./configManager.js');
const recursive = require('./recursiveTraversal'); const recursive = require('./recursiveTraversal');
const filepreview = require('filepreview'); const filepreview = require('filepreview');
const fs = require('fs'); const fs = require('fs');
const routes = require('./routes');
const app = express();
const app = express();
app.use('/', routes);
/**Initializes sessions for login */
app.use(session({ secret: configLoader.getConfiguration().sessionSecret, cookie: { maxAge: 6000000 }}));
app.use(express.urlencoded()); app.use(express.urlencoded());
app.use(express.json()); // if needed app.use(express.json()); // if needed
const routes = require('./routes');
app.use('/', routes);
// const CONFIG_FILE_NAME = "conf.json";
// const config = fileIO.getFileAsJSON(CONFIG_FILE_NAME);
const CONFIG_FILE_NAME = "conf.json";
const config = fileIO.getFileAsJSON(CONFIG_FILE_NAME);
/**Initializes sessions for login */
app.use(session({ secret: config.sessionSecret, cookie: { maxAge: 6000000 }}));
// /** Template engine */ // /** Template engine */
// const whiskers = require('whiskers'); // const whiskers = require('whiskers');
@ -74,13 +80,13 @@ var serverURL = "http://localhost:5000";
// }); // });
// } // }
function getUserInformation(templateContext, request)
{
templateContext.users = config.users;
templateContext.apiKey = request.session.API;
templateContext.id = request.session.userID;
templateContext.username = request.session.username;
}
// function getUserInformation(templateContext, request)
// {
// templateContext.users = configLoader.getConfiguration().users;
// templateContext.apiKey = request.session.API;
// templateContext.id = request.session.userID;
// templateContext.username = request.session.username;
// }
// function getHomePageInformation(templateContext, request) // function getHomePageInformation(templateContext, request)
// { // {
@ -369,4 +375,4 @@ app.get('/video/', function(request, result)
// }); // });
app.listen(config.port, () => console.log(`App listening on port ${config.port}!`));
app.listen(configLoader.getConfiguration().port, () => console.log(`App listening on port ${configLoader.getConfiguration().port}!`));

+ 7
- 4
user.js View File

@ -116,6 +116,8 @@ module.exports =
{ {
users[index].api = generateRandomAPIKey(); users[index].api = generateRandomAPIKey();
} }
console.log(users);
configManager.getConfiguration().users = users;
configManager.syncToDisk(); configManager.syncToDisk();
}, },
@ -172,8 +174,9 @@ module.exports =
newUser.password = passObject.pass; newUser.password = passObject.pass;
newUser.admin = admin; newUser.admin = admin;
users.push(newUser); users.push(newUser);
configManager.syncToDisk();
configManager.getConfiguration().users = users;
configManager.syncToDisk();
return true; return true;
}, },
@ -189,7 +192,7 @@ module.exports =
{ {
for(var i = 0; i < users.length; i++) for(var i = 0; i < users.length; i++)
{ {
if (users[i].id=== id)
if (users[i].id + "" === id)
{ {
console.log("User account updated."); console.log("User account updated.");
users[i].username = userName; users[i].username = userName;
@ -200,7 +203,7 @@ module.exports =
users[i].password = passObj.pass; users[i].password = passObj.pass;
} }
} }
configManager.getConfiguration().users = users;
configManager.syncToDisk(); configManager.syncToDisk();
}, },
@ -215,7 +218,7 @@ module.exports =
{ {
return value.id + "" !== id return value.id + "" !== id
}); });
configManager.getConfiguration().users = users;
configManager.syncToDisk(); configManager.syncToDisk();
} }
}; };

+ 6
- 2
utils.js View File

@ -1,9 +1,13 @@
const fileIO = require('./fileIO');
function fetchInTemplate(templateContext, templateKey, filename) function fetchInTemplate(templateContext, templateKey, filename)
{ {
templateContext[templateKey] = fileIO.getFile(filename); templateContext[templateKey] = fileIO.getFile(filename);
} }
const PRIVILEGE = {NOBODY: 0, MEMBER: 1, ADMIN: 2}; const PRIVILEGE = {NOBODY: 0, MEMBER: 1, ADMIN: 2};
/** Template engine */ /** Template engine */
@ -19,10 +23,10 @@ module.exports =
prom.push(fileIO.getFile("./html/mainTemplate.html")); prom.push(fileIO.getFile("./html/mainTemplate.html"));
prom.push(fetchInTemplate(templateContext, "header", "./html/header.html")); prom.push(fetchInTemplate(templateContext, "header", "./html/header.html"));
prom.push(fetchInTemplate(templateContext, "footer", "./html/footer.html")); prom.push(fetchInTemplate(templateContext, "footer", "./html/footer.html"));
if(checkPrivilege(request) >= PRIVILEGE.MEMBER)
if(module.exports.checkPrivilege(request) >= PRIVILEGE.MEMBER)
{ {
templateContext.loggedIn = true; templateContext.loggedIn = true;
if(checkPrivilege(request) === PRIVILEGE.ADMIN)
if(module.exports.checkPrivilege(request) === PRIVILEGE.ADMIN)
templateContext.admin = true; templateContext.admin = true;
if(templateDependencyFunction !== null) if(templateDependencyFunction !== null)
prom.push(templateDependencyFunction(templateContext, request)); prom.push(templateDependencyFunction(templateContext, request));

Loading…
Cancel
Save