Browse Source

Implemented basic user manipulation functionality.

pull/6/head
jrtechs 5 years ago
parent
commit
edeb000c02
4 changed files with 177 additions and 2 deletions
  1. +6
    -0
      html/header.html
  2. +110
    -0
      html/users.html
  3. +60
    -1
      server.js
  4. +1
    -1
      user.js

+ 6
- 0
html/header.html View File

@ -17,6 +17,12 @@
p{font-size:18px;}
</style>
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous">
</script>
</head>
<body>

+ 110
- 0
html/users.html View File

@ -0,0 +1,110 @@
<script>
function editHostForm(id, username)
{
$("#editUserBox").css("display", "block");
$("#useridLabel").val(id);
$("#usernameLabel").val(username);
}
</script>
<div id="editUserBox" class="row" style="display:none">
<!-- Edit User -->
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h3 class="text-center">Edit User</h3>
</div>
<div class="card-body">
<form action="/edituser" method ="post" class="p-2">
<div class="form-group">
<input class="form-control" id="useridLabel" type="text" name="id" required disabled>
<label>User ID</label>
</div>
<div class="form-group">
<input class="form-control" id="usernameLabel" type="text" name="username" required>
<label>User Name</label>
</div>
<div class="form-group">
<input class="form-control" type="password" name="password" required>
<label>Password</label>
</div>
<div class="text-center">
<input type="submit" name="add_user" value="Update User"
class="btn btn-lg btn-secondary"/>
</div>
</form>
</div>
</div>
<br>
</div>
</div>
<div class="row">
<!-- Current Users -->
<div class="col-md-6">
<div class='card'>
<div class="card-header">
<h3 class="text-center">Users</h3>
</div>
<div class="card-body">
<table class="table table-striped">
<thead class="thead-dark">
<tr>
<td>User Name</td>
<td>User ID</td>
<td>Edit</td>
<td>Terminate</td>
</tr>
</thead>
<tbody>
{for user in users}
<tr>
<td>{user.username}</td>
<td>{user.id}</td>
<td>
<button onclick="editHostForm({user.id}, '{user.username}')" class="btn btn-secondary">Edit User</button>
</td>
<td>
<form action="/removeuser" method ="post" >
<input type="submit" name="submit" value="Delete" class="btn btn-secondary"/>
<input type='hidden' name='id' value='{user.id}' />
</form>
</td>
</tr>
{/for}
</tbody>
</table>
</div>
</div>
</div>
<!-- Add User -->
<div class="col-md-6">
<div class="card">
<div class="card-header">
<h3 class="text-center">Add New User</h3>
</div>
<div class="card-body">
<form action="/addUser" method ="post" class="p-2">
<div class="form-group">
<label> User Name
<input class="form-control w-100" type="text" name="username" required>
</label>
</div>
<div class="form-group">
<label>Password
<input class="form-control w-100" type="password" name="password" required>
</label>
</div>
<div class="text-center">
<input type="submit" name="add_user" value="Create User"
class="btn btn-lg btn-secondary"/>
</div>
</form>
</div>
</div>
<br>
</div>
</div>

+ 60
- 1
server.js View File

@ -48,6 +48,9 @@ function renderHTML(request, result, templateFile, templateDependencyFunction)
if(request.session.login === true)
{
templateContext.loggedIn = true;
if(templateDependencyFunction !== null)
prom.push(templateDependencyFunction(templateContext));
prom.push(fetchInTemplate(templateContext, "main","./html/" + templateFile));
}
else
@ -62,7 +65,13 @@ function renderHTML(request, result, templateFile, templateDependencyFunction)
});
}
function getUserInformation(templateContext)
{
templateContext.users = config.users;
}
app.get('/', (req, res) => renderHTML(req, res, "home.html", null));
app.get('/users', (req, res) => renderHTML(req, res, "users.html", getUserInformation));
app.use(express.static('css'));
app.use(express.static('js'));
@ -109,7 +118,6 @@ app.get('/video', function(request, result)
'Content-Length': chunksize,
'Content-Type': 'video/mp4',
};
result.writeHead(206, head);
file.pipe(result);
}
@ -132,4 +140,55 @@ app.get('/video', function(request, result)
}
});
app.post('/addUser', function(request, result)
{
if(request.session.login === true)
{
userUtils.addUser(request.body.username, request.body.password, config);
fileIO.writeJSONToFile(CONFIG_FILE_NAME, config);
result.redirect('/users');
}
else
{
result.status(401);
result.send('None shall pass');
}
});
app.post('/edituser', function(request, result)
{
if(request.session.login === true)
{
userUtils.editUser(request.body.id, request.body.username, request.body.password, config);
fileIO.writeJSONToFile(CONFIG_FILE_NAME, config);
result.redirect('/users');
}
else
{
result.status(401);
result.send('None shall pass');
}
});
app.post('/removeuser', function(request, result)
{
if(request.session.login === true)
{
userUtils.removeUser(request.body.id, config);
fileIO.writeJSONToFile(CONFIG_FILE_NAME, config);
result.redirect('/users');
}
else
{
result.status(401);
result.send('None shall pass');
}
});
app.listen(config.port, () => console.log(`App listening on port ${config.port}!`));

+ 1
- 1
user.js View File

@ -107,7 +107,7 @@ module.exports =
newUser.salt = passObject.salt;
newUser.password = passObject.pass;
configuration.push(newUser);
configuration.users.push(newUser);
return true;
},

Loading…
Cancel
Save