- Add user status toggle (activate/deactivate) functionality - Add user deletion with post/comment transfer to admin - Implement safety checks for admin protection - Add interactive JavaScript for user management actions - Update admin users interface with action buttons - Add confirmation dialogs for destructive operations - Update README with new admin features and capabilities - Add database migration utility for future updates Features: - Toggle user active/inactive status - Delete users with content preservation - Transfer all posts/comments to admin on user deletion - Prevent admin self-modification and deletion - AJAX-powered interface with real-time feedback - Comprehensive error handling and user notifications
231 lines
9.3 KiB
HTML
231 lines
9.3 KiB
HTML
{% extends "admin/base.html" %}
|
|
|
|
{% block title %}User Management - Admin{% endblock %}
|
|
|
|
{% block admin_content %}
|
|
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pb-2 mb-3 border-bottom">
|
|
<h1 class="h2">Users</h1>
|
|
<div class="btn-toolbar mb-2 mb-md-0">
|
|
<button type="button" class="btn btn-sm btn-outline-secondary" onclick="location.reload()">
|
|
<i class="fas fa-sync-alt"></i> Refresh
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
{% if users.items %}
|
|
<div class="card">
|
|
<div class="card-header py-3">
|
|
<h6 class="m-0 fw-bold text-primary">Users ({{ users.total }})</h6>
|
|
</div>
|
|
<div class="card-body p-0">
|
|
<div class="table-responsive">
|
|
<table class="table table-hover mb-0">
|
|
<thead>
|
|
<tr>
|
|
<th style="width: 20%;">User</th>
|
|
<th style="width: 25%;">Email</th>
|
|
<th style="width: 10%;">Role</th>
|
|
<th style="width: 15%;">Posts</th>
|
|
<th style="width: 10%;">Status</th>
|
|
<th style="width: 10%;">Joined</th>
|
|
<th style="width: 10%;">Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{% for user in users.items %}
|
|
<tr>
|
|
<td>
|
|
<div>
|
|
<a href="{{ url_for('admin.user_detail', user_id=user.id) }}" class="text-decoration-none fw-bold">
|
|
{{ user.nickname }}
|
|
</a>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<small>{{ user.email }}</small>
|
|
</td>
|
|
<td>
|
|
{% if user.is_admin %}
|
|
<span class="badge bg-danger">Admin</span>
|
|
{% else %}
|
|
<span class="badge bg-primary">User</span>
|
|
{% endif %}
|
|
</td>
|
|
<td>
|
|
<span class="badge bg-secondary">{{ user.posts.count() }}</span>
|
|
{% if user.posts.filter_by(published=True).count() > 0 %}
|
|
<br><small class="text-success">({{ user.posts.filter_by(published=True).count() }} published)</small>
|
|
{% endif %}
|
|
</td>
|
|
<td>
|
|
{% if user.is_active %}
|
|
<span class="badge bg-success">Active</span>
|
|
{% else %}
|
|
<span class="badge bg-secondary">Inactive</span>
|
|
{% endif %}
|
|
</td>
|
|
<td>
|
|
<small>{{ user.created_at.strftime('%Y-%m-%d') }}</small>
|
|
</td>
|
|
<td>
|
|
<div class="btn-group" role="group">
|
|
<a href="{{ url_for('admin.user_detail', user_id=user.id) }}"
|
|
class="btn btn-sm btn-outline-info" title="View Details">
|
|
<i class="fas fa-eye"></i>
|
|
</a>
|
|
{% if not user.is_admin or current_user.id != user.id %}
|
|
<button class="btn btn-sm btn-outline-warning"
|
|
title="{{ 'Deactivate User' if user.is_active else 'Activate User' }}"
|
|
onclick="toggleUserStatus({{ user.id }}, '{{ user.nickname }}', {{ user.is_active|lower }})">
|
|
<i class="fas fa-{{ 'toggle-on' if user.is_active else 'toggle-off' }}"></i>
|
|
</button>
|
|
{% endif %}
|
|
{% if not user.is_admin and current_user.id != user.id %}
|
|
<button class="btn btn-sm btn-outline-danger"
|
|
title="Delete User"
|
|
onclick="confirmDeleteUser({{ user.id }}, '{{ user.nickname }}', {{ user.posts.count() }})">
|
|
<i class="fas fa-trash"></i>
|
|
</button>
|
|
{% endif %}
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
{% endfor %}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Pagination -->
|
|
{% if users.pages > 1 %}
|
|
<nav aria-label="Users pagination" class="mt-4">
|
|
<ul class="pagination justify-content-center">
|
|
{% if users.has_prev %}
|
|
<li class="page-item">
|
|
<a class="page-link" href="{{ url_for('admin.users', page=users.prev_num) }}">Previous</a>
|
|
</li>
|
|
{% endif %}
|
|
|
|
{% for page_num in users.iter_pages() %}
|
|
{% if page_num %}
|
|
{% if page_num != users.page %}
|
|
<li class="page-item">
|
|
<a class="page-link" href="{{ url_for('admin.users', page=page_num) }}">{{ page_num }}</a>
|
|
</li>
|
|
{% else %}
|
|
<li class="page-item active">
|
|
<span class="page-link">{{ page_num }}</span>
|
|
</li>
|
|
{% endif %}
|
|
{% else %}
|
|
<li class="page-item disabled">
|
|
<span class="page-link">…</span>
|
|
</li>
|
|
{% endif %}
|
|
{% endfor %}
|
|
|
|
{% if users.has_next %}
|
|
<li class="page-item">
|
|
<a class="page-link" href="{{ url_for('admin.users', page=users.next_num) }}">Next</a>
|
|
</li>
|
|
{% endif %}
|
|
</ul>
|
|
</nav>
|
|
{% endif %}
|
|
|
|
{% else %}
|
|
<div class="card">
|
|
<div class="card-body text-center py-5">
|
|
<i class="fas fa-users fa-3x text-muted mb-3"></i>
|
|
<h5 class="text-muted">No users found</h5>
|
|
<p class="text-muted">No users have registered yet.</p>
|
|
</div>
|
|
</div>
|
|
{% endif %}
|
|
|
|
<script>
|
|
function toggleUserStatus(userId, nickname, currentStatus) {
|
|
const action = currentStatus ? 'deactivate' : 'activate';
|
|
const message = `Are you sure you want to ${action} user "${nickname}"?`;
|
|
|
|
if (confirm(message)) {
|
|
fetch(`/admin/users/${userId}/toggle-status`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
}
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
showAlert(data.message, 'success');
|
|
setTimeout(() => {
|
|
location.reload();
|
|
}, 1000);
|
|
} else {
|
|
showAlert(data.message || 'An error occurred', 'danger');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
showAlert('An error occurred while updating user status', 'danger');
|
|
});
|
|
}
|
|
}
|
|
|
|
function confirmDeleteUser(userId, nickname, postsCount) {
|
|
let message = `Are you sure you want to delete user "${nickname}"?`;
|
|
if (postsCount > 0) {
|
|
message += `\n\nThis user has ${postsCount} post(s) that will be transferred to you as the admin.`;
|
|
}
|
|
message += '\n\nThis action cannot be undone.';
|
|
|
|
if (confirm(message)) {
|
|
fetch(`/admin/users/${userId}/delete`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
}
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
showAlert(data.message, 'success');
|
|
setTimeout(() => {
|
|
location.reload();
|
|
}, 1000);
|
|
} else {
|
|
showAlert(data.message || 'An error occurred', 'danger');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
console.error('Error:', error);
|
|
showAlert('An error occurred while deleting user', 'danger');
|
|
});
|
|
}
|
|
}
|
|
|
|
function showAlert(message, type) {
|
|
// Create alert container if it doesn't exist
|
|
let alertContainer = document.querySelector('.alert-container');
|
|
if (!alertContainer) {
|
|
alertContainer = document.createElement('div');
|
|
alertContainer.className = 'alert-container mt-3';
|
|
document.querySelector('.container-fluid').insertBefore(
|
|
alertContainer,
|
|
document.querySelector('.container-fluid').firstChild
|
|
);
|
|
}
|
|
|
|
alertContainer.innerHTML = `
|
|
<div class="alert alert-${type} alert-dismissible fade show" role="alert">
|
|
${message}
|
|
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
|
</div>
|
|
`;
|
|
}
|
|
</script>
|
|
|
|
{% endblock %}
|