Skip to content

Commit

Permalink
/admin: update ui instead of reload on user management
Browse files Browse the repository at this point in the history
  • Loading branch information
cowpod committed Nov 10, 2024
1 parent 343c61f commit 9f43f28
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 45 deletions.
14 changes: 7 additions & 7 deletions functions/edit_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
$config = require("./config.php");

if (empty($_POST['name'])) {
die("Email not specified.");
die('{"status":"error","message":"Email not specified."}');
}
if (empty($_POST['display_name'])) {
die("Name not specified.");
die('{"status":"error","message":"Name not specified."}');
}
if (empty($_POST['perms'])) {
die("perms not specified.");
die('{"status":"error","message":"Perms not specified."}');
}
if (!$_SESSION['user']||$_SESSION['user']=="") {
die("Unauthorized request or login session has expired.");
die('{"status":"error","message":"Unauthorized request or login session has expired."}');
}
if ($_SESSION['user']!==$config['mail']) {
die("insufficient permission!");
die('{"status":"error","message":"Insufficient permission!"}');
}

global $db;
Expand All @@ -31,8 +31,8 @@
);

if ($sql) {
echo '<span class="text-success">Saved.</span>';
echo '{"status":"succ","message":"Saved."}';
} else {
echo '<span class="text-danger">An error has occurred</span>';
echo '{"status":"error","message":"An error has occurred"}';
}
exit();
25 changes: 13 additions & 12 deletions functions/new_user.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
$config = require("./config.php");

if (empty($_POST['name'])) {
die("Email not specified.");
die('{"status":"error","message":"Email not specified."}');
}
if (empty($_POST['display_name'])) {
die("Name not specified.");
die('{"status":"error","message":"Name not specified."}');
}
if (empty($_POST['pass'])) {
die("password not specified.");
die('{"status":"error","message":"password not specified."}');
}
if (!$_SESSION['user']||$_SESSION['user']=="") {
die("Unauthorized request or login session has expired.");
die('{"status":"error","message":"Unauthorized request or login session has expired."}');
}
if ($_SESSION['user']!==$config['mail']) {
die("insufficient permission!");
die('{"status":"error","message":"Insufficient permission!"}');
}
if (!isset($config['encrypted'])||$config['encrypted']==false) {
$pass = $_POST['pass'];
Expand All @@ -29,17 +29,17 @@

// sanitize name (email)
if (!preg_match('/^[\w\.\-\+]+@[a-zA-Z\d\.-]+\.[a-zA-Z]{2,}$/', $_POST['name'])) {
die('<span class="text-danger">Invalid email</span>');
die('{"status":"error","message":"Invalid email"}');
}

// sanitize username
if (!ctype_alnum($_POST['display_name'])) {
die('<span class="text-danger">Invalid username</span>');
die('{"status":"error","message":"Invalid username"}');

}

if ($_POST['name']==$config['mail']) {
die('<span class="text-danger">User with that email already exists</span>');
die('{"status":"error","message":"User with that email already exists"}"');
}

require_once("db.php");
Expand All @@ -49,7 +49,7 @@
// email is stored in name field...
$user_existsq = $db->query("SELECT 1 FROM users WHERE name='".$_POST['name']."'");
if ($user_existsq) {
die('<span class="text-danger">User with that email already exists</span>');
die('{"status":"error","message":"User with that email already exists"}"');
}

$icon = "iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAB9ElEQVR4Xu2bSytEcRiHZyJRaDYWRhJilFlYKjakNOWS7OxEGCRGpAg1KykRSlHSKLkO0YyFhSiRIQmbIcVEsnCXW/EJPB/g9Jvt0/8s3t73+b3nnDnmpZWaXxP8dssRm6yL+XTc9OO1Ib+9GWCe60BuyUpEvvDYiNysAqgDNAJygCSoFPi/AoaPwbCvXnRAKKoZc/T7rA/5kasEeV1wEvlJnBf5lM+KfD16mPcAFUAdoBGQA8gSkqBSwOAxmBZ8QQdsOTIwRzsPOae7Iy/w/Op3DvLwZd4zgrYnPJ83Xcp7gAqgDtAIyAFkCUlQKWDwGKzdPeUH//ftmKPz9ePIQ6m1yANufq+QPteK58s6tpHvRZTxHqACqAM0AnIAWkISVAoYOwaf13bQAZn2WSzAQ1EB38/3FyP/9R0jz/K/I/cMxSM3VSTzHqACqAM0AnIAWUISVAoYPAbfe6/RAV07b5ijH/uFyD8Dd8jnejy8R+TwnuG8GsTzpXdJvAeoAOoAjYAcQJaQBJUCBo9B+6sDHfDSUoM5Wm1uQ34Z60YeMzOB3DJygNy5yU+sHGNNvAeoAOoAjYAcQJaQBJUCBo/B7Cr+aMrvnMEctVbx9wCVXbxINboS8Pqu0DnyFDf//2B0o4H3ABVAHaARwD1ADpAElQKGjsE/aSRgFj7BEuwAAAAASUVORK5CYII=";
Expand All @@ -61,11 +61,12 @@
'".$pass."',
'".$icon."')");

$db->disconnect();

if ($sql) {
echo '<span class="text-success">New user created</span>';
echo '{"status":"succ","message":"New user created","id":'.$db->insert_id().'}';
} else {
echo '<span class="text-danger">An error has occured</span>';
echo '{"status":"error","message":"An error has occured"}';
}

$db->disconnect();
exit();
19 changes: 12 additions & 7 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -2444,18 +2444,20 @@ class="btn btn-primary">Edit</button>
<th style="width:30%" scope="col"></th>
</tr>
</thead>
<tbody>
<tbody id="users">
<?php
$users = $db->query("SELECT * FROM `users`");
foreach ($users as $user) {
// if editing remember to change in page_admin.js
?>
<tr>
<tr id="user-<?php echo $user['id'] ?>">
<td scope="row"><?php echo $user['display_name'] ?></td>
<td><?php echo $user['name'] ?></td>
<td><div class="btn-group btn-group-sm" role="group" aria-label="Actions">
<button onclick="edit('<?php echo $user['name'] ?>','<?php echo $user['display_name'] ?>','<?php echo $user['perms'] ?>')" class="btn btn-primary" data-toggle="modal" data-target="#editUser" >Edit</button>
<button onclick="remove_box(<?php echo $user['id'] ?>,'<?php echo $user['name'] ?>')" data-toggle="modal" data-target="#removeUser" class="btn btn-danger">Remove</button>
</div></td>
<font style="display:hidden" id="user-perms-<?php echo $user['id'] ?>" perms="<?php echo $user['perms'] ?>"></font>
<button id="user-edit-<?php echo $user['id'] ?>" onclick="edit(<?php echo $user['id'] ?>,'<?php echo $user['name'] ?>','<?php echo $user['display_name'] ?>')" class="btn btn-primary" data-toggle="modal" data-target="#editUser" >Edit</button>
<button onclick="remove_box(<?php echo $user['id'] ?>,'<?php echo $user['name'] ?>')" data-toggle="modal" data-target="#removeUser" class="btn btn-danger">Remove</button>
</div></td>
</tr>
<?php
}
Expand Down Expand Up @@ -2528,8 +2530,9 @@ class="btn btn-primary">Edit</button>
</form>
</div>
<div class="modal-footer">
<font id="newUser-message"></font>
<button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
<button id="save-button" type="button" class="btn btn-success" disabled="disabled" onclick='new_user($("#email").val(),$("#name").val(),$("#pass1").val())' data-dismiss="modal">Save</button>
<button id="save-button" type="button" class="btn btn-success" disabled="disabled" onclick='new_user($("#email").val(),$("#name").val(),$("#pass1").val())' >Save</button>
</div>
</div>
</div>
Expand All @@ -2546,6 +2549,7 @@ class="btn btn-primary">Edit</button>
</div>
<div class="modal-body">
<form>
<input type="hidden" id="edit-user-id">
<input readonly id="mail2" placeholder="Email" class="form-control" type="text"><br />
<input id="name2" placeholder="Username" class="form-control" type="text"><br />
<h4>Permissions</h4>
Expand Down Expand Up @@ -2586,8 +2590,9 @@ class="btn btn-primary">Edit</button>
</form>
</div>
<div class="modal-footer">
<font id="editUser-message"></font>
<button type="button" class="btn btn-primary" data-dismiss="modal">Cancel</button>
<button id="save-button-2" type="button" class="btn btn-success" disabled="disabled" onclick='edit_user($("#mail2").val(),$("#name2").val(),$("#perms").val())' data-dismiss="modal">Save</button>
<button id="save-button-2" type="button" class="btn btn-success" disabled="disabled" onclick='edit_user($("#mail2").val(),$("#name2").val(),$("#perms").val())'>Save</button>
</div>
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions resources/css/global.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
}
.custom-table-error {
background-color:rgba(255,100,100,0.5); /* transparent to accomodate dark mode */
}
#newUser-message{
display:hidden; width:99%;text-align:left; font-weight:bold;
}
#editUser-message{
display:hidden; width:99%;text-align:left; font-weight:bold;
}
76 changes: 57 additions & 19 deletions resources/js/page_admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ function remove(id) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
console.log(request.responseText);
$("#info").html(request.responseText + "<br />");
// $("#info").html(request.responseText + "<br />");
// setTimeout(function(){ window.location.reload(); }, 500);
$("#user-"+id).remove();
}

}
Expand All @@ -17,10 +18,12 @@ function remove_box(id,name) {
$("#user-name-title").text(name);
$("#remove-button").attr("onclick","remove("+id+")");
}
function edit(mail,name, perms) {
function edit(id,mail,name) {
let perms = $('#user-perms-'+id).attr('perms');
$("#save-button-2").attr("disabled", true);
$("#mail2").val(mail);
$("#name2").val(name);
$("#edit-user-id").val(id);
if (perms.match("^[01]+$")) {
$("#perms").val(perms);
} else {
Expand Down Expand Up @@ -76,13 +79,63 @@ function edit_user(mail,name,perms) {
request.onreadystatechange = function() {
if (request.readyState == 4) {
console.log(request.responseText);
$("#info").html(request.responseText + "<br />");
// setTimeout(function(){ window.location.reload(); }, 500);

json = JSON.parse(request.responseText);
if (json['status']=='succ') {
// $("#editUser-message").hide();
// $("#editUser-message").html('<span class="text-success"'+json['message'] + "</span><br />");
let id=$("#edit-user-id").val()
$('#user-perms-'+id).attr('perms',perms);
$('#editUser').modal('hide');
} else {
$("#editUser-message").show();
$("#editUser-message").html('<span class="text-danger"'+json['message'] + "</span><br />");
}
}

}
request.send("name="+mail+"&display_name="+name+"&perms="+perms);
}

function new_user(email,name,pass) {
var request = new XMLHttpRequest();
request.open('POST', './functions/new_user.php');
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
request.onreadystatechange = function() {
if (request.readyState == 4) {
console.log(request.responseText);

json=JSON.parse(request.responseText);
if (json['status']=='succ') {
// $("#info").html('<span class="text-success">'+json['message']+"</span><br />");
// setTimeout(function(){ window.location.reload(); }, 500);

// if editing remember to change in index.php
let newuserrow = $("<tr>", {
id: "user-"+json['id'],
html: `<td scope="row">${name}</td>
<td>${email}</td>
<td>
<font style="display:hidden" id="user-perms-${json['id']}" perms="0000000"></font>
<div class="btn-group btn-group-sm" role="group" aria-label="Actions">
<button id="user-edit-${json['id']}" onclick="edit(${json['id']},'${email}','${email}')" class="btn btn-primary" data-toggle="modal" data-target="#editUser" >Edit</button>
<button onclick="remove_box(${json['id']},'${email}')" data-toggle="modal" data-target="#removeUser" class="btn btn-danger">Remove</button>
</div>
</td>
`
});
$('#users').append(newuserrow);
$('#newUser').modal('hide');
} else {
$("#newUser-message").html('<span class="text-danger">'+json['message']+"</span><br />");
$("#newUser-message").show();
}
}

}
request.send("name="+email+"&display_name="+name+"&pass="+pass);
}

// https://gist.github.com/endel/321925f6cafa25bbfbde
Number.prototype.pad = function(size) {
var s = String(this);
Expand Down Expand Up @@ -159,21 +212,6 @@ $("#perm7").change(function(){
$("#save-button-2").attr("disabled", false);
}
});

function new_user(email,name,pass) {
var request = new XMLHttpRequest();
request.open('POST', './functions/new_user.php');
request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
request.onreadystatechange = function() {
if (request.readyState == 4) {
console.log(request.responseText);
$("#info").html(request.responseText + "<br />");
// setTimeout(function(){ window.location.reload(); }, 500);
}

}
request.send("name="+email+"&display_name="+name+"&pass="+pass);
}
$("#name2").on("keyup", function() {
if ($("#name2").val()!=="") {
$("#name2").addClass("is-valid");
Expand Down

0 comments on commit 9f43f28

Please sign in to comment.