Files
noteflow/public/js/admin.js
T
2025-11-06 16:05:46 +01:00

169 lines
6.8 KiB
JavaScript

class AdminService {
constructor() {
// 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.adminBtn = document.getElementById('admin-btn');
if (this.adminPanel && this.closeAdminBtn && this.addUserBtn && this.usersList) {
this.setupEventListeners();
}
}
setupEventListeners() {
if (this.closeAdminBtn) {
this.closeAdminBtn.addEventListener('click', () => this.closePanel());
}
if (this.addUserBtn) {
this.addUserBtn.addEventListener('click', () => this.showAddUserForm());
}
if (this.adminBtn) {
this.adminBtn.addEventListener('click', () => {
if (this.adminPanel) {
this.adminPanel.classList.remove('hidden');
}
});
}
}
async loadUsers() {
if (!window.auth || !window.auth.getHeaders) return;
try {
const response = await fetch('/api/users', {
headers: window.auth.getHeaders()
});
const users = await response.json();
this.renderUsersList(users);
} catch (error) {
console.error('Error loading users:', error);
}
}
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 || '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="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="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="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>
` : ''}
</div>
`;
this.usersList.appendChild(userElement);
});
}
showAddUserForm() {
const username = prompt('Nom d\'utilisateur:');
if (!username) return;
const password = prompt('Mot de passe:');
if (!password) return;
const isAdmin = confirm('Donner les droits administrateur ?');
this.createUser(username, password, isAdmin);
}
async createUser(username, password, isAdmin) {
if (!window.auth || !window.auth.getHeaders) return;
try {
const response = await fetch('/api/users', {
method: 'POST',
headers: window.auth.getHeaders(),
body: JSON.stringify({ username, password, is_admin: isAdmin })
});
if (!response.ok) {
const error = await response.json();
throw new Error(error.message || 'Erreur lors de la création de l\'utilisateur');
}
this.loadUsers();
} catch (error) {
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;
try {
await fetch(`/api/users/${userId}`, {
method: 'PUT',
headers: window.auth.getHeaders(),
body: JSON.stringify({ password })
});
alert('Mot de passe modifié');
} catch (error) {
console.error('Error changing password:', error);
}
}
async toggleAdmin(userId, currentStatus) {
if (!window.auth || !window.auth.getHeaders) return;
try {
await fetch(`/api/users/${userId}`, {
method: 'PUT',
headers: window.auth.getHeaders(),
body: JSON.stringify({ is_admin: !currentStatus })
});
this.loadUsers();
} catch (error) {
console.error('Error toggling admin status:', error);
}
}
async deleteUser(userId) {
if (!window.auth || !window.auth.getHeaders) return;
if (!confirm('Êtes-vous sûr de vouloir supprimer cet utilisateur ?')) return;
try {
await fetch(`/api/users/${userId}`, {
method: 'DELETE',
headers: window.auth.getHeaders()
});
this.loadUsers();
} catch (error) {
console.error('Error deleting user:', error);
}
}
closePanel() {
if (this.adminPanel) {
this.adminPanel.classList.add('hidden');
}
}
}
// Initialize admin service
window.admin = new AdminService();