Revize 15493e3d
Přidáno uživatelem Ondřej Anděl před téměř 4 roky(ů)
application/controller/LoginController.php | ||
---|---|---|
2 | 2 |
require "../model/DB.php"; |
3 | 3 |
session_start(); |
4 | 4 |
$DB = new DB(); |
5 |
var_dump($_POST); |
|
6 | 5 |
$result = $DB->getUser($_POST['username']); |
7 | 6 |
|
8 | 7 |
if (count($result) == 1) { |
application/controller/UpdateUserRoleController.php | ||
---|---|---|
1 | 1 |
<?php |
2 | 2 |
require "../model/DB.php"; |
3 |
|
|
4 |
if ($_SESSION['role'] == "ADMIN") {
|
|
3 |
session_start(); |
|
4 |
if ($_SESSION['role'] == "admin") {
|
|
5 | 5 |
$DB = new DB(); |
6 | 6 |
|
7 | 7 |
$results = $DB->updateUserRole($_POST["userId"], $_POST["role"]); |
application/controller/UsersListController.php | ||
---|---|---|
1 | 1 |
<?php |
2 | 2 |
require "../model/DB.php"; |
3 |
session_start(); |
|
3 | 4 |
|
4 |
if ($_SESSION['role'] == "ADMIN") {
|
|
5 |
if ($_SESSION['role'] == "admin") {
|
|
5 | 6 |
$DB = new DB(); |
6 | 7 |
|
7 | 8 |
$results = $DB->select("SELECT id, username, role from USERS", array()); |
application/users.php | ||
---|---|---|
1 |
<!doctype html> |
|
2 |
|
|
3 |
<html > |
|
4 |
<head> |
|
5 |
<meta charset="utf-8"> |
|
6 |
<title>Dalimilova kronika</title> |
|
7 |
|
|
8 |
<!-- css --> |
|
9 |
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css"> |
|
10 |
<link rel="stylesheet" href="view/style.css?<?php echo date('l jS \of F Y h:i:s A'); ?>"> |
|
11 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"> |
|
12 |
|
|
13 |
<!-- jQuery library --> |
|
14 |
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js"></script> |
|
15 |
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js"></script> |
|
16 |
|
|
17 |
<!-- JavaScript --> |
|
18 |
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js"></script> |
|
19 |
</head> |
|
20 |
<body> |
|
21 |
<div class="container-fluid"> |
|
22 |
<?php include "./view/header.html" ?> |
|
23 |
<div class="row app-content"> |
|
24 |
<?php include "./view/menu.php" ?> |
|
25 |
|
|
26 |
<div class="col-xs-12 col-sm-10 p-4 content d-flex"> |
|
27 |
<?php include "./view/userContent.php" ?> |
|
28 |
</div> |
|
29 |
</div> |
|
30 |
<?php include "./view/footer.html" ?> |
|
31 |
</div> |
|
32 |
<?php include "./view/modals/settingsModal.html" ?>?> |
|
33 |
<?php include "./view/modals/editUserModal.html" ?> |
|
34 |
<?php include "./view/modals/removeUserModal.html" ?> |
|
35 |
</body> |
|
36 |
</html> |
application/view/menu.php | ||
---|---|---|
6 | 6 |
<a class="px-4 py-1 <?php echo (strstr($_SERVER['PHP_SELF'], '/index.php') ? 'active' : '');?>" href="index.php">Úvodní info</a> |
7 | 7 |
<a class="px-4 py-1 <?php echo (strstr($_SERVER['PHP_SELF'],'/search.php') ? 'active' : '');?>" href="search.php">Vyhledávání</a> |
8 | 8 |
<a class="px-4 py-1 <?php echo (strstr($_SERVER['PHP_SELF'],'/login.php') ? 'active' : '');?>" href="#" data-toggle="modal" data-target="#login-modal">Přihlášení</a> |
9 |
<?php |
|
10 |
if (isset($_SESSION['role']) && $_SESSION['role'] == "admin") { |
|
11 |
echo "<a class=\"px-4 py-1"; |
|
12 |
echo (strstr($_SERVER['PHP_SELF'],'/users.php') ? 'active' : ''); |
|
13 |
echo "href=\"users.php\">Správa uživatelů</a>"; |
|
14 |
} |
|
15 |
?> |
|
9 | 16 |
<a class="px-4 py-1 <?php echo (strstr($_SERVER['PHP_SELF'],'/settings.php') ? 'active' : '');?>" href="#" data-toggle="modal" data-target="#settings-modal" id="modalSettingsHref">Nastavení</a> |
10 | 17 |
</div> |
11 | 18 |
</div> |
application/view/modals/editUserModal.html | ||
---|---|---|
1 |
<div id="edit-modal" class="modal fade" role="dialog"> |
|
2 |
<div class="modal-dialog modal-dialog-centered modal-lg" role="document"> |
|
3 |
<div class="modal-content"> |
|
4 |
<div class="modal-header"> |
|
5 |
<h4 class="modal-title"></h4> |
|
6 |
<button type="button" class="close" data-dismiss="modal">×</button> |
|
7 |
</div> |
|
8 |
<div class="modal-body"> |
|
9 |
<div class="container"> |
|
10 |
<div class="row my-1" id="username-wrapper"> |
|
11 |
<div class="col-xs-12 col-sm-4"> |
|
12 |
<label for="username">Uživatelské jméno:</label> |
|
13 |
</div> |
|
14 |
<div class="col-xs-12 col-sm-8"> |
|
15 |
<input class="min-wdth" id="username"/> |
|
16 |
</div> |
|
17 |
</div> |
|
18 |
<div class="row my-1" id="password-wrapper"> |
|
19 |
<div class="col-xs-12 col-sm-4"> |
|
20 |
<label for="password">Heslo:</label> |
|
21 |
</div> |
|
22 |
<div class="col-xs-12 col-sm-8"> |
|
23 |
<input type="password" class="min-wdth" id="password"/> |
|
24 |
</div> |
|
25 |
</div> |
|
26 |
<div class="row my-1"> |
|
27 |
<div class="col-xs-12 col-sm-4"> |
|
28 |
<label for="email">Email:</label> |
|
29 |
</div> |
|
30 |
<div class="col-xs-12 col-sm-8"> |
|
31 |
<input class="min-wdth" id="email"/> |
|
32 |
</div> |
|
33 |
</div> |
|
34 |
<div class="row my-1"> |
|
35 |
<div class="col-xs-12 col-sm-4"> |
|
36 |
<label for="rights">Práva:</label> |
|
37 |
</div> |
|
38 |
<div class="col-xs-12 col-sm-8"> |
|
39 |
<select class="min-wdth" id="rights"> |
|
40 |
<option value = "editor">Editor</option> |
|
41 |
<option value = "admin">Admin</option> |
|
42 |
</select> |
|
43 |
</div> |
|
44 |
</div> |
|
45 |
|
|
46 |
</div> |
|
47 |
</div> |
|
48 |
<div class="modal-footer"> |
|
49 |
<button type="button" id="submit-but" class="btn btn-default" data-dismiss="modal"></button> |
|
50 |
<button type="button" class="btn btn-default" data-dismiss="modal">Zavřít</button> |
|
51 |
</div> |
|
52 |
</div> |
|
53 |
</div> |
|
54 |
</div> |
|
55 |
|
|
56 |
<script> |
|
57 |
$('#edit-modal').on('show.bs.modal', function (event) { |
|
58 |
const button = $(event.relatedTarget); |
|
59 |
const pseudo_id = button.data('pseudo-id');// Extract info from data-* attributes |
|
60 |
const title = button.data('title');// Extract info from data-* attributes |
|
61 |
|
|
62 |
let modal = $(this); |
|
63 |
modal.find('.modal-title').text(title); |
|
64 |
|
|
65 |
if(title === "Nový uživatel"){ |
|
66 |
prefill(modal); |
|
67 |
modal.find('#submit-but').text("Založit"); |
|
68 |
modal.find("#username-wrapper").show(); |
|
69 |
modal.find("#password-wrapper").show(); |
|
70 |
modal.find('#submit-but').click(() => createData(modal)); |
|
71 |
} else { |
|
72 |
const data_obj = users[pseudo_id]; |
|
73 |
modal.find('#submit-but').text("Upravit"); |
|
74 |
modal.find("#username-wrapper").hide(); |
|
75 |
modal.find("#password-wrapper").hide(); |
|
76 |
modal.find('#submit-but').click(() => editData(data_obj, modal)); |
|
77 |
prefill(modal, data_obj); |
|
78 |
} |
|
79 |
}); |
|
80 |
|
|
81 |
|
|
82 |
function prefill(modal, data = null){ |
|
83 |
modal.find("#username").val(""); |
|
84 |
modal.find("#password").val(""); |
|
85 |
modal.find("#email").val(""); |
|
86 |
modal.find("#rights").val(""); |
|
87 |
|
|
88 |
if(data !== null){ |
|
89 |
modal.find("#email").val(data.email); |
|
90 |
modal.find("#rights").val(data.role); |
|
91 |
} |
|
92 |
} |
|
93 |
|
|
94 |
function editData(data, modal){ |
|
95 |
const formData = prepareFormData(modal); |
|
96 |
formData.append("userId", data.id); |
|
97 |
|
|
98 |
var xhr = new XMLHttpRequest(); |
|
99 |
xhr.open("POST", "./controller/UpdateUserRoleController.php"); |
|
100 |
xhr.send(formData); |
|
101 |
setTimeout(function(){ |
|
102 |
fetchUsers(); //reload data after item delete |
|
103 |
}, 500); |
|
104 |
} |
|
105 |
|
|
106 |
function prepareFormData(modal) { |
|
107 |
const formData = new FormData(); |
|
108 |
formData.append("role", modal.find("#rights")[0].value); |
|
109 |
formData.append("email", modal.find("#email")[0].value); |
|
110 |
|
|
111 |
return formData; |
|
112 |
} |
|
113 |
|
|
114 |
function createData(modal){ |
|
115 |
const formData = prepareFormData(modal); |
|
116 |
const username = modal.find("#username")[0].value; |
|
117 |
if(username !== undefined && username !== null){ |
|
118 |
formData.append("username", username); |
|
119 |
formData.append("password", modal.find("#password")[0].value); |
|
120 |
|
|
121 |
var xhr = new XMLHttpRequest(); |
|
122 |
xhr.open("POST", "./controller/RegistrationController.php"); |
|
123 |
xhr.send(formData); |
|
124 |
setTimeout(function(){ |
|
125 |
fetchUsers(); //reload data after item delete |
|
126 |
}, 500); |
|
127 |
} |
|
128 |
} |
|
129 |
</script> |
application/view/modals/removeUserModal.html | ||
---|---|---|
1 |
<div id="remove-modal" class="modal fade" role="dialog"> |
|
2 |
<div class="modal-dialog modal-dialog-centered" role="document"> |
|
3 |
<div class="modal-content"> |
|
4 |
<div class="modal-header"> |
|
5 |
<h4 class="modal-title">Potvrdit odstranění</h4> |
|
6 |
<button type="button" class="close" data-dismiss="modal">×</button> |
|
7 |
</div> |
|
8 |
<div class="modal-body"> |
|
9 |
<div class="container"> |
|
10 |
<div class="row"> |
|
11 |
<p id="mesBody"></p> |
|
12 |
</div> |
|
13 |
</div> |
|
14 |
</div> |
|
15 |
<div class="modal-footer"> |
|
16 |
<button type="button" class="btn btn-default" data-dismiss="modal" onclick="removeUser(document.getElementById('idToRemoveInput').value)">Ano</button> |
|
17 |
<button type="button" class="btn btn-default" data-dismiss="modal">Ne</button> |
|
18 |
<input type="hidden" id="idToRemoveInput" /> |
|
19 |
</div> |
|
20 |
</div> |
|
21 |
</div> |
|
22 |
</div> |
|
23 |
|
|
24 |
<script> |
|
25 |
$('#remove-modal').on('show.bs.modal', function (event){ //wait for modal to show |
|
26 |
const button = $(event.relatedTarget); |
|
27 |
const pseudo_id = button.data('pseudo-id');//Extract info from data-* attributes |
|
28 |
|
|
29 |
const data_obj = users[pseudo_id]; |
|
30 |
let modal = $(this); |
|
31 |
|
|
32 |
modal.find('#mesBody').text("Opravdu chcete odstranit uživatele \"" + data_obj.username + "\"?"); |
|
33 |
modal.find('#idToRemoveInput').val(data_obj.id); //assign id to delete to hidden input |
|
34 |
}); |
|
35 |
|
|
36 |
function removeUser(id){ |
|
37 |
const data = new FormData(); //to pass ids, which we want to delete from DB |
|
38 |
data.append("userId", ""+id); //id to delete from table dd_wordform |
|
39 |
|
|
40 |
let xhr = new XMLHttpRequest(); |
|
41 |
xhr.open("POST", "./controller/DeleteUserController.php"); |
|
42 |
xhr.send(data); |
|
43 |
setTimeout(function(){ |
|
44 |
fetchUsers(); //reload data after item delete |
|
45 |
}, 500); |
|
46 |
} |
|
47 |
</script> |
application/view/userContent.php | ||
---|---|---|
1 |
<?php |
|
2 |
|
|
3 |
session_start(); |
|
4 |
if (isset($_SESSION['role']) && $_SESSION['role'] == "admin") { |
|
5 |
echo "<script> |
|
6 |
//what to do when page renders |
|
7 |
window.onload = function() { |
|
8 |
fetchUsers(); |
|
9 |
}; |
|
10 |
</script> |
|
11 |
|
|
12 |
<div class=\"search-results my-4\"> |
|
13 |
<table class=\"table table-striped\"> |
|
14 |
<thead> |
|
15 |
<tr> |
|
16 |
<th scope=\"col\">Uživatel</th> |
|
17 |
<th scope=\"col\">Email</th> |
|
18 |
<th scope=\"col\">Práva</th> |
|
19 |
<th scope=\"col\"><button class=\"btn ml-1\" title=\"Přidat\" data-toggle=\"modal\" data-target=\"#edit-modal\" data-title=\"Nový uživatel\"><i class=\"fa fa-plus\"></i></button></th> |
|
20 |
</tr> |
|
21 |
</thead> |
|
22 |
<tbody id=\"search-table\"> |
|
23 |
<script> |
|
24 |
let users; |
|
25 |
function fetchUsers () { |
|
26 |
// (A1) GET SEARCH TERM |
|
27 |
const formData = new FormData(); |
|
28 |
|
|
29 |
// (A2) AJAX - USE HTTP:// NOT FILE:/ |
|
30 |
let xhr = new XMLHttpRequest(); |
|
31 |
xhr.open(\"POST\", \"./controller/UsersListController.php\"); |
|
32 |
xhr.onload = function(){ |
|
33 |
let search = this.response; |
|
34 |
let parsedJSON = JSON.parse(search); |
|
35 |
users = parsedJSON; |
|
36 |
renderUsers(parsedJSON); |
|
37 |
}; |
|
38 |
xhr.send(formData); |
|
39 |
} |
|
40 |
|
|
41 |
|
|
42 |
function renderUsers(users) { |
|
43 |
let result = \"\"; |
|
44 |
users.forEach((item,id) => { |
|
45 |
result += \"<tr>\"; |
|
46 |
result += \"<td>\" + item.username + \"</td>\"; |
|
47 |
result += \"<td>\" + ((item.email !== undefined && item.email !== null) ? item.email : \"-\") + \"</td>\"; |
|
48 |
result += \"<td>\" + item.role + \"</td>\"; |
|
49 |
(item.role !== \"admin\") ? (result += \"<td class='action-td'>\" + |
|
50 |
\"<button class='btn ml-1' title='Upravit' data-toggle='modal' data-target='#edit-modal' data-pseudo-id='\" + id + \"' data-title='Upravit uživatele'><i class='fa fa-pencil'></i></button>\" + |
|
51 |
\"<button class='btn ml-1' title='Odstranit' data-toggle='modal' data-target='#remove-modal' data-pseudo-id='\" + id + \"'><i class='fa fa-trash'></i></button>\" + |
|
52 |
\"</td>\") : result += \"<td class='action-td'/>\"; |
|
53 |
result += \"</tr>\"; |
|
54 |
}); |
|
55 |
document.getElementById(\"search-table\").innerHTML = result; |
|
56 |
} |
|
57 |
</script> |
|
58 |
</tbody> |
|
59 |
</table> |
|
60 |
</div>"; |
|
61 |
} else { |
|
62 |
echo "<h3 class=\"mx-auto text-center\">Pro navštívení stránky nemáte dostatečná oprávnění</h3>"; |
|
63 |
} |
|
64 |
|
|
65 |
|
Také k dispozici: Unified diff
Feature #8646: Správa uživatelů