[dyad] wrote 4 file(s)

This commit is contained in:
Michael committed 2025-11-06 16:01:23 +01:00
1 parent 0bcffed331
commit 11dca3824c
4 files changed
+121 -52

No files matched your search

+34 -11
View File
@@ -1,21 +1,30 @@
class AdminService {
constructor() {
if (!window.auth.user?.is_admin) return;
// Check if auth exists and user is admin
if (!window.auth || !window.auth.user || !window.auth.user.is_admin) return;
this.adminPanel = document.getElementById('admin-panel');
this.closeAdminBtn = document.getElementById('close-admin-btn');
this.addUserBtn = document.getElementById('add-user-btn');
this.usersList = document.getElementById('users-list');
this.setupEventListeners();
if (this.adminPanel && this.closeAdminBtn && this.addUserBtn && this.usersList) {
this.setupEventListeners();
}
}
setupEventListeners() {
this.closeAdminBtn.addEventListener('click', () => this.closePanel());
this.addUserBtn.addEventListener('click', () => this.showAddUserForm());
if (this.closeAdminBtn) {
this.closeAdminBtn.addEventListener('click', () => this.closePanel());
}
if (this.addUserBtn) {
this.addUserBtn.addEventListener('click', () => this.showAddUserForm());
}
}
async loadUsers() {
if (!window.auth || !window.auth.getHeaders) return;
try {
const response = await fetch('/api/users', {
headers: window.auth.getHeaders()
@@ -28,24 +37,28 @@ class AdminService {
}
renderUsersList(users) {
if (!this.usersList || !Array.isArray(users)) return;
this.usersList.innerHTML = '';
users.forEach(user => {
if (!user || typeof user !== 'object') return;
const userElement = document.createElement('div');
userElement.className = 'user-item';
userElement.innerHTML = `
<div>
<span class="font-medium">${user.username}</span>
<span class="font-medium">${user.username || 'Utilisateur'}</span>
${user.is_admin ? '<span class="ml-2 text-sm text-blue-600">Admin</span>' : ''}
</div>
<div class="actions">
${user.id !== 1 ? `
<button class="text-gray-600 hover:text-blue-600" onclick="window.admin.changePassword(${user.id})">
<button class="text-gray-600 hover:text-blue-600" onclick="if(window.admin && typeof window.admin.changePassword === 'function') window.admin.changePassword(${user.id})">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M21 2l-2 2m-7.61 7.61a5.5 5.5 0 1 1-7.778 7.778 5.5 5.5 0 0 1 7.777-7.777zm0 0L15.5 7.5m0 0l3 3L22 7l-3-3m-3.5 3.5L19 4"></path></svg>
</button>
<button class="text-gray-600 hover:text-blue-600" onclick="window.admin.toggleAdmin(${user.id}, ${user.is_admin})">
<button class="text-gray-600 hover:text-blue-600" onclick="if(window.admin && typeof window.admin.toggleAdmin === 'function') window.admin.toggleAdmin(${user.id}, ${user.is_admin ? 'true' : 'false'})">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z"></path></svg>
</button>
<button class="text-gray-600 hover:text-red-600" onclick="window.admin.deleteUser(${user.id})">
<button class="text-gray-600 hover:text-red-600" onclick="if(window.admin && typeof window.admin.deleteUser === 'function') window.admin.deleteUser(${user.id})">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><polyline points="3 6 5 6 21 6"></polyline><path d="M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6m3 0V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2"></path><line x1="10" y1="11" x2="10" y2="17"></line><line x1="14" y1="11" x2="14" y2="17"></line></svg>
</button>
` : ''}
@@ -68,6 +81,8 @@ class AdminService {
}
async createUser(username, password, isAdmin) {
if (!window.auth || !window.auth.getHeaders) return;
try {
const response = await fetch('/api/users', {
method: 'POST',
@@ -77,16 +92,18 @@ class AdminService {
if (!response.ok) {
const error = await response.json();
throw new Error(error.message);
throw new Error(error.message || 'Erreur lors de la création de l\'utilisateur');
}
this.loadUsers();
} catch (error) {
alert(error.message);
alert(error.message || 'Erreur lors de la création de l\'utilisateur');
}
}
async changePassword(userId) {
if (!window.auth || !window.auth.getHeaders) return;
const password = prompt('Nouveau mot de passe:');
if (!password) return;
@@ -103,6 +120,8 @@ class AdminService {
}
async toggleAdmin(userId, currentStatus) {
if (!window.auth || !window.auth.getHeaders) return;
try {
await fetch(`/api/users/${userId}`, {
method: 'PUT',
@@ -116,6 +135,8 @@ class AdminService {
}
async deleteUser(userId) {
if (!window.auth || !window.auth.getHeaders) return;
if (!confirm('Êtes-vous sûr de vouloir supprimer cet utilisateur ?')) return;
try {
@@ -130,7 +151,9 @@ class AdminService {
}
closePanel() {
this.adminPanel.classList.add('hidden');
if (this.adminPanel) {
this.adminPanel.classList.add('hidden');
}
}
}