|
|
@@ -121,6 +121,7 @@ function renderUsers(users) {
|
|
|
<td>${createdDate}</td>
|
|
|
<td>${lastLogin}</td>
|
|
|
<td>
|
|
|
+ <button class="btn btn-small" onclick="showChangePasswordModal(${user.id}, '${user.username}')">Change Password</button>
|
|
|
${!user.is_admin ? `
|
|
|
<button class="btn btn-small btn-secondary" onclick="toggleAdmin(${user.id}, true)">Make Admin</button>
|
|
|
` : ''}
|
|
|
@@ -194,3 +195,93 @@ async function deleteUser(userId, username) {
|
|
|
showMessage('Error deleting user: ' + error.message, 'error');
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// ==================== Add User Modal ====================
|
|
|
+
|
|
|
+function showAddUserModal() {
|
|
|
+ document.getElementById('add-user-modal').classList.remove('hidden');
|
|
|
+ document.getElementById('add-user-form').reset();
|
|
|
+}
|
|
|
+
|
|
|
+function hideAddUserModal() {
|
|
|
+ document.getElementById('add-user-modal').classList.add('hidden');
|
|
|
+ document.getElementById('add-user-form').reset();
|
|
|
+}
|
|
|
+
|
|
|
+async function submitAddUser(event) {
|
|
|
+ event.preventDefault();
|
|
|
+
|
|
|
+ const form = event.target;
|
|
|
+ const formData = new FormData(form);
|
|
|
+
|
|
|
+ try {
|
|
|
+ const response = await fetch('/api/admin/users', {
|
|
|
+ method: 'POST',
|
|
|
+ body: formData
|
|
|
+ });
|
|
|
+
|
|
|
+ if (response.status === 401) {
|
|
|
+ window.location.href = '/login';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const data = await response.json();
|
|
|
+
|
|
|
+ if (data.status === 'success') {
|
|
|
+ showMessage(data.message || 'User created successfully', 'success');
|
|
|
+ hideAddUserModal();
|
|
|
+ loadUsers(); // Reload user list
|
|
|
+ } else {
|
|
|
+ showMessage(data.message || 'Failed to create user', 'error');
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ showMessage('Error creating user: ' + error.message, 'error');
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// ==================== Change Password Modal ====================
|
|
|
+
|
|
|
+function showChangePasswordModal(userId, username) {
|
|
|
+ document.getElementById('change-password-user-id').value = userId;
|
|
|
+ document.getElementById('change-password-username').textContent = username;
|
|
|
+ document.getElementById('change-password-modal').classList.remove('hidden');
|
|
|
+ document.getElementById('change-password-form').reset();
|
|
|
+ // Re-set the hidden user ID after reset
|
|
|
+ document.getElementById('change-password-user-id').value = userId;
|
|
|
+}
|
|
|
+
|
|
|
+function hideChangePasswordModal() {
|
|
|
+ document.getElementById('change-password-modal').classList.add('hidden');
|
|
|
+ document.getElementById('change-password-form').reset();
|
|
|
+}
|
|
|
+
|
|
|
+async function submitChangePassword(event) {
|
|
|
+ event.preventDefault();
|
|
|
+
|
|
|
+ const form = event.target;
|
|
|
+ const userId = document.getElementById('change-password-user-id').value;
|
|
|
+ const formData = new FormData(form);
|
|
|
+
|
|
|
+ try {
|
|
|
+ const response = await fetch(`/api/admin/users/${userId}/password`, {
|
|
|
+ method: 'PUT',
|
|
|
+ body: formData
|
|
|
+ });
|
|
|
+
|
|
|
+ if (response.status === 401) {
|
|
|
+ window.location.href = '/login';
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ const data = await response.json();
|
|
|
+
|
|
|
+ if (data.status === 'success') {
|
|
|
+ showMessage(data.message || 'Password changed successfully', 'success');
|
|
|
+ hideChangePasswordModal();
|
|
|
+ } else {
|
|
|
+ showMessage(data.message || 'Failed to change password', 'error');
|
|
|
+ }
|
|
|
+ } catch (error) {
|
|
|
+ showMessage('Error changing password: ' + error.message, 'error');
|
|
|
+ }
|
|
|
+}
|