-
Notifications
You must be signed in to change notification settings - Fork 8
/
users.php
285 lines (267 loc) · 12.8 KB
/
users.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
<?php
require_once 'conn/conn.php';
require_once 'auth.php';
// Función para obtener todos los usuarios de la base de datos
function getUsers($db) {
$query = "SELECT * FROM users";
$stmt = $db->query($query);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Función para insertar un nuevo usuario en la base de datos
function insertUser($db, $username, $usermail, $password, $permissions) {
$query = "INSERT INTO users (username, usermail, password, permissions) VALUES (:username, :usermail, :password, :permissions)";
$stmt = $db->prepare($query);
$stmt->bindValue(':username', $username, PDO::PARAM_STR);
$stmt->bindValue(':usermail', $usermail, PDO::PARAM_STR);
$stmt->bindValue(':password', $password, PDO::PARAM_STR);
$stmt->bindValue(':permissions', $permissions, PDO::PARAM_STR);
return $stmt->execute();
}
// Función para eliminar un usuario de la base de datos
function deleteUser($db, $userId) {
$query = "DELETE FROM users WHERE id = :userId";
$stmt = $db->prepare($query);
$stmt->bindValue(':userId', $userId, PDO::PARAM_INT);
return $stmt->execute();
}
// Obtener todos los usuarios de la base de datos
$users = getUsers($db);
// Procesar el formulario de agregar usuarios
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['submit'])) {
$username = $_POST['username'];
$usermail = $_POST['usermail'];
$password = password_hash($_POST['password'], PASSWORD_DEFAULT);
$permissions = $_POST['permissions'];
// Insertar el nuevo usuario en la base de datos
insertUser($db, $username, $usermail, $password, $permissions);
// Recargar la página para mostrar la tabla actualizada
header("Location: {$_SERVER['PHP_SELF']}");
exit;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>DRMUY</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/tailwind.min.css">
<link href="https://vjs.zencdn.net/7.15.4/video-js.min.css" rel="stylesheet">
<link href="https://cdnjs.cloudflare.com/ajax/libs/flowbite/1.7.0/flowbite.min.css" rel="stylesheet" />
</head>
<body class="bg-gray-900">
<?php include "menu.php"; ?>
<div class="p-4 sm:ml-64">
<div class="container mx-auto p-4">
<h1 class="text-2xl font-bold mb-4 text-gray-500">Users</h1>
<div class="bg-gray-800 p-4 rounded-lg shadow-md mb-4">
<h2 class="text-xl font-bold mb-2 text-gray-500">Add User</h2>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" class="grid grid-cols-2 gap-4">
<div>
<label class="block text-gray-500 font-bold">Username</label>
<input type="text" name="username" class="w-full border rounded-lg py-2 px-3 bg-gray-700 text-white" required>
</div>
<div>
<label class="block text-gray-500 font-bold">Email</label>
<input type="email" name="usermail" class="w-full border rounded-lg py-2 px-3 bg-gray-700 text-white" required>
</div>
<div>
<label class="block text-gray-500 font-bold">Password</label>
<input type="password" name="password" class="w-full border rounded-lg py-2 px-3 bg-gray-700 text-white" required>
</div>
<div>
<label class="block text-gray-500 font-bold">Permissions</label>
<select name="permissions" class="w-full border rounded-lg py-2 px-3 bg-gray-700 text-white">
<option value="admin">Admin</option>
<option value="user">User</option>
</select>
</div>
<div class="col-span-2">
<button type="submit" name="submit" class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 px-4 rounded">
Add User
</button>
</div>
</form>
</div>
<div class="relative overflow-x-auto shadow-md sm:rounded-lg">
<table class="w-full text-lg text-center text-white">
<thead class="text-sm text-gray-700 uppercase bg-gray-700 text-gray-400">
<tr>
<th scope="col" class="px-6 py-3 text-white">
ID
</th>
<th scope="col" class="px-6 py-3 text-white">
Username
</th>
<th scope="col" class="px-6 py-3 text-white">
Email
</th>
<th scope="col" class="px-6 py-3 text-white">
Permissions
</th>
<th scope="col" class="px-6 py-3 text-white">
Actions
</th>
</tr>
</thead>
<tbody>
<?php foreach ($users as $user) { ?>
<tr class="border-b bg-gray-800 border-gray-700 hover:bg-gray-600" data-user-id="<?php echo $user['id']; ?>">
<td class="px-6 py-4 whitespace-nowrap">
<?php echo $user['id']; ?>
</td>
<td class="px-6 py-4">
<?php echo $user['username']; ?>
</td>
<td class="px-6 py-4">
<?php echo $user['usermail']; ?>
</td>
<td class="px-6 py-4">
<?php echo $user['permissions']; ?>
</td>
<td class="px-6 py-4 text-center"> <!-- Ajustamos el estilo aquí -->
<button class="text-blue-500 hover:underline mx-2" onclick="editUser(<?php echo $user['id']; ?>)"> <!-- Ajustamos el estilo aquí -->
Edit
</button>
<button class="text-red-500 hover:underline mx-2" onclick="deleteUser(<?php echo $user['id']; ?>)"> <!-- Ajustamos el estilo aquí -->
Delete
</button>
</td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<!-- Modal for editing user -->
<div id="editUserModal" class="hidden fixed inset-0 z-50 overflow-auto bg-gray-900 bg-opacity-50 flex items-center justify-center">
<div class="relative p-8 mx-auto my-4 max-w-lg max-h-full w-full">
<div class="bg-gray-800 rounded-lg text-left overflow-auto shadow-xl transform transition-all sm:my-8 sm:align-middle sm:max-w-2xl">
<form id="editUserForm" class="p-4">
<h3 class="text-lg leading-6 font-medium text-gray-500">Edit User</h3>
<div class="mt-4 space-y-4">
<input type="hidden" id="editUserId" value="">
<div class="flex items-center space-x-4">
<label class="text-gray-500 font-bold w-24">Username:</label>
<input type="text" id="editUsername" class="flex-1 border rounded-lg py-2 px-3 bg-gray-700 text-white" required>
</div>
<div class="flex items-center space-x-4">
<label class="text-gray-500 font-bold w-24">Email:</label>
<input type="email" id="editUsermail" class="flex-1 border rounded-lg py-2 px-3 bg-gray-700 text-white" required>
</div>
<div class="flex items-center space-x-4">
<label class="text-gray-500 font-bold w-24">Password:</label>
<input type="password" id="editPassword" class="flex-1 border rounded-lg py-2 px-3 bg-gray-700 text-white" required>
</div>
<div class="flex items-center space-x-4">
<label class="text-gray-500 font-bold w-24">Permissions:</label>
<select id="editPermissions" class="flex-1 border rounded-lg py-2 px-3 bg-gray-700 text-white">
<option value="admin">Admin</option>
<option value="user">User</option>
</select>
</div>
</div>
<div class="mt-6 flex justify-end">
<button type="submit" class="bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 px-4 rounded">
Save Changes
</button>
<button type="button" class="ml-4 bg-red-500 hover:bg-red-600 text-white font-bold py-2 px-4 rounded" onclick="closeEditUserModal()">
Cancel
</button>
</div>
</form>
</div>
</div>
</div>
<script>
function deleteUser(userId) {
Swal.fire({
title: 'Are you sure?',
text: 'You will not be able to recover this user!',
icon: 'warning',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes, delete it!'
}).then((result) => {
if (result.isConfirmed) {
// Send a request to delete the user
$.ajax({
url: 'delete_user.php', // Replace with the PHP file to handle user deletion
type: 'POST',
data: { userId: userId },
success: function (response) {
// Reload the page after successful deletion
window.location.reload();
},
error: function () {
// Show an error message if deletion fails
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Something went wrong. Unable to delete the user.',
});
}
});
}
});
}
function editUser(userId) {
// Get user data from the table
const username = document.querySelector(`tr[data-user-id="${userId}"] td:nth-child(2)`).innerText;
const usermail = document.querySelector(`tr[data-user-id="${userId}"] td:nth-child(3)`).innerText;
const permissions = document.querySelector(`tr[data-user-id="${userId}"] td:nth-child(4)`).innerText;
// Populate the edit user form with the data
document.getElementById('editUserId').value = userId;
document.getElementById('editUsername').value = username;
document.getElementById('editUsermail').value = usermail;
document.getElementById('editPermissions').value = permissions;
// Clear the password field before showing the modal
document.getElementById('editPassword').value = '';
// Show the modal
document.getElementById('editUserModal').classList.remove('hidden');
}
function closeEditUserModal() {
// Close the modal
document.getElementById('editUserModal').classList.add('hidden');
// Clear the password field when closing the modal
document.getElementById('editPassword').value = '';
}
// Submit the edit user form
document.getElementById('editUserForm').addEventListener('submit', function (e) {
e.preventDefault();
const userId = document.getElementById('editUserId').value;
const username = document.getElementById('editUsername').value;
const usermail = document.getElementById('editUsermail').value;
const permissions = document.getElementById('editPermissions').value;
// Send a request to update the user data
$.ajax({
url: 'update_user.php', // Replace with the PHP file to handle user data update
type: 'POST',
data: {
userId: userId,
username: username,
usermail: usermail,
permissions: permissions
},
success: function (response) {
// Reload the page after successful update
window.location.reload();
},
error: function () {
// Show an error message if update fails
Swal.fire({
icon: 'error',
title: 'Oops...',
text: 'Something went wrong. Unable to update the user.',
});
}
});
});
</script>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/tippy-bundle.umd.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="https://vjs.zencdn.net/7.15.4/video.min.js"></script>
</body>
</html>