Added function of users.php
This commit is contained in:
parent
e8b00e3e02
commit
54c00cbe8b
113
api/users.php
Normal file
113
api/users.php
Normal file
|
@ -0,0 +1,113 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2016 Lukas Metzger <developer@lukas-metzger.com>.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
require_once '../config/config-default.php';
|
||||
require_once '../lib/database.php';
|
||||
require_once '../lib/session.php';
|
||||
|
||||
$input = json_decode(file_get_contents('php://input'));
|
||||
|
||||
if(!isset($_SESSION['type']) || $_SESSION['type'] != "admin") {
|
||||
echo "Permission denied!";
|
||||
exit();
|
||||
}
|
||||
|
||||
if(isset($input->action) && $input->action == "getUsers") {
|
||||
|
||||
$sql = "
|
||||
SELECT id,name,type
|
||||
FROM user
|
||||
WHERE
|
||||
(name LIKE ? OR ?) AND
|
||||
(type=? OR ?)
|
||||
";
|
||||
|
||||
if(isset($input->sort->field) && $input->sort->field != "") {
|
||||
if($input->sort->field == "id") {
|
||||
$sql .= "ORDER BY id";
|
||||
} else if($input->sort->field == "name") {
|
||||
$sql .= "ORDER BY name";
|
||||
} else if($input->sort->field == "type") {
|
||||
$sql .= "ORDER BY type";
|
||||
}
|
||||
|
||||
if(isset($input->sort->order)) {
|
||||
if($input->sort->order == 0) {
|
||||
$sql .= " DESC";
|
||||
} else if($input->sort->order == 1) {
|
||||
$sql .= " ASC";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$stmt = $db->prepare($sql);
|
||||
|
||||
if(isset($input->name)) {
|
||||
$name_filter = "%" . $input->name . "%";
|
||||
$name_filter_used = 0;
|
||||
} else {
|
||||
$name_filter = "";
|
||||
$name_filter_used = 1;
|
||||
}
|
||||
|
||||
if(isset($input->type)) {
|
||||
$type_filter = $input->type;
|
||||
$type_filter_used = 0;
|
||||
} else {
|
||||
$type_filter = "";
|
||||
$type_filter_used = 1;
|
||||
}
|
||||
|
||||
$stmt->bind_param("sisi",
|
||||
$name_filter, $name_filter_used,
|
||||
$type_filter, $type_filter_used
|
||||
);
|
||||
$stmt->execute();
|
||||
|
||||
$result = $stmt->get_result();
|
||||
|
||||
$retval = Array();
|
||||
|
||||
while($obj = $result->fetch_object()) {
|
||||
$retval[] = $obj;
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($input->action) && $input->action == "deleteUser") {
|
||||
$userId = $input->id;
|
||||
|
||||
$db->autocommit(false);
|
||||
|
||||
$stmt = $db->prepare("DELETE FROM permissions WHERE user=?");
|
||||
$stmt->bind_param("i", $userId);
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
|
||||
$stmt = $db->prepare("DELETE FROM user WHERE id=?");
|
||||
$stmt->bind_param("i", $userId);
|
||||
$stmt->execute();
|
||||
$stmt->close();
|
||||
|
||||
$db->commit();
|
||||
}
|
||||
|
||||
if(isset($retval)) {
|
||||
echo json_encode($retval);
|
||||
} else {
|
||||
echo "{}";
|
||||
}
|
137
js/users.js
Normal file
137
js/users.js
Normal file
|
@ -0,0 +1,137 @@
|
|||
/*
|
||||
* Copyright 2016 Lukas Metzger <developer@lukas-metzger.com>.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
var sort = {
|
||||
field: "",
|
||||
order: 1
|
||||
}
|
||||
|
||||
$(document).ready(function() {
|
||||
requestData();
|
||||
|
||||
$('#table-users>thead>tr>td span').click(function() {
|
||||
var field = $(this).siblings('strong').text().toLowerCase();
|
||||
if(sort.field == field) {
|
||||
if(sort.order == 1) sort.order = 0;
|
||||
else sort.field = "";
|
||||
} else {
|
||||
sort.field = field;
|
||||
sort.order = 1;
|
||||
}
|
||||
$('#table-users>thead>tr>td span').removeClass("glyphicon-sort-by-attributes glyphicon-sort-by-attributes-alt");
|
||||
|
||||
if(sort.field == field) {
|
||||
if(sort.order == 1) $(this).addClass("glyphicon-sort-by-attributes");
|
||||
else $(this).addClass("glyphicon-sort-by-attributes-alt");
|
||||
}
|
||||
requestData();
|
||||
});
|
||||
|
||||
$('#searchName').bind("paste keyup", function() {
|
||||
requestData();
|
||||
});
|
||||
|
||||
$('#searchType').change(function() {
|
||||
requestData();
|
||||
});
|
||||
|
||||
$('#searchType').select2({
|
||||
minimumResultsForSearch: Infinity
|
||||
});
|
||||
});
|
||||
|
||||
function requestData() {
|
||||
var restrictions = {};
|
||||
|
||||
restrictions.sort = sort;
|
||||
|
||||
var searchName = $('#searchName').val();
|
||||
if(searchName.length > 0) {
|
||||
restrictions.name = searchName;
|
||||
}
|
||||
|
||||
var searchType = $('#searchType').val();
|
||||
if(searchType != "none") {
|
||||
restrictions.type = searchType;
|
||||
}
|
||||
|
||||
restrictions.action = "getUsers";
|
||||
|
||||
$.post(
|
||||
"api/users.php",
|
||||
JSON.stringify(restrictions),
|
||||
function(data) {
|
||||
recreateTable(data);
|
||||
},
|
||||
"json"
|
||||
);
|
||||
}
|
||||
|
||||
function recreateTable(data) {
|
||||
$('#table-users>tbody').empty();
|
||||
|
||||
$.each(data, function(index,item) {
|
||||
$('<tr></tr>').appendTo('#table-users>tbody')
|
||||
.append('<td>' + item.id + '</td>')
|
||||
.append('<td>' + item.name + '</td>')
|
||||
.append('<td>' + item.type + '</td>')
|
||||
.append('<td><span class="glyphicon glyphicon-trash cursor-pointer"></span></td>');
|
||||
|
||||
});
|
||||
|
||||
$('#table-users>tbody>tr').click(function() {
|
||||
var id = $(this).children('td').first().text();
|
||||
location.assign('edit-user.php#' + id);
|
||||
});
|
||||
|
||||
$('#table-users>tbody>tr>td>span.glyphicon-trash').click(function() {
|
||||
$(this).parent().parent().unbind();
|
||||
deleteDomain.call(this);
|
||||
});
|
||||
}
|
||||
|
||||
function deleteDomain() {
|
||||
var deleteId = $(this).parent().parent().children('td').eq(0).text();
|
||||
var deleteName = $(this).parent().parent().children('td').eq(1).text();
|
||||
var rowToRemove = $(this).parent().parent();
|
||||
|
||||
$('#userToDelete').text(deleteName);
|
||||
|
||||
$('#deleteConfirm #buttonDelete').click(function() {
|
||||
deleteUserWithId(deleteId, function() {
|
||||
$('#deleteConfirm').modal("hide");
|
||||
$(rowToRemove).remove();
|
||||
});
|
||||
});
|
||||
|
||||
$('#deleteConfirm').modal();
|
||||
}
|
||||
|
||||
function deleteUserWithId(id, callback) {
|
||||
var data = {
|
||||
action: "deleteUser",
|
||||
id: id
|
||||
};
|
||||
|
||||
$.post(
|
||||
"api/users.php",
|
||||
JSON.stringify(data),
|
||||
function() {
|
||||
callback();
|
||||
},
|
||||
"json"
|
||||
);
|
||||
}
|
|
@ -50,7 +50,7 @@ limitations under the License.
|
|||
</nav>
|
||||
|
||||
<div class="container">
|
||||
<table class="table table-hover" id="table-domains">
|
||||
<table class="table table-hover" id="table-users">
|
||||
<thead>
|
||||
<tr>
|
||||
<td class="cell-vertical-middle"><strong>ID</strong> <span class="glyphicon glyphicon-sort cursor-pointer"></span></td>
|
||||
|
|
Loading…
Reference in a new issue