Add debug routes and improve dashboard authentication handling
This commit is contained in:
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
# Dashboard Routes (Admin)
|
# Dashboard Routes (Admin)
|
||||||
|
|
||||||
from quart import Blueprint, request, render_template, redirect, url_for, jsonify
|
from quart import Blueprint, request, render_template, redirect, url_for, jsonify, session
|
||||||
from models.user import User
|
from models.user import User
|
||||||
from models.project import Project
|
from models.project import Project
|
||||||
from models.category import Category
|
from models.category import Category
|
||||||
@@ -13,27 +13,115 @@ from utils.validators import validate_project_data
|
|||||||
|
|
||||||
dashboard_bp = Blueprint('dashboard', __name__, url_prefix='/dashboard')
|
dashboard_bp = Blueprint('dashboard', __name__, url_prefix='/dashboard')
|
||||||
|
|
||||||
|
# Debug route to check authentication status
|
||||||
|
@dashboard_bp.route('/debug/auth')
|
||||||
|
async def debug_auth():
|
||||||
|
"""Debug route to check authentication status"""
|
||||||
|
current_user = await get_current_user()
|
||||||
|
session_data = dict(session)
|
||||||
|
|
||||||
|
debug_info = {
|
||||||
|
'session_exists': 'user_id' in session,
|
||||||
|
'user_id_in_session': session.get('user_id'),
|
||||||
|
'username_in_session': session.get('username'),
|
||||||
|
'is_admin_in_session': session.get('is_admin'),
|
||||||
|
'current_user_found': current_user is not None,
|
||||||
|
'current_user_is_admin': current_user.is_admin if current_user else None,
|
||||||
|
'current_user_details': {
|
||||||
|
'id': current_user.id,
|
||||||
|
'username': current_user.username,
|
||||||
|
'email': current_user.email,
|
||||||
|
'role': current_user.role,
|
||||||
|
'is_admin': current_user.is_admin
|
||||||
|
} if current_user else None,
|
||||||
|
'session_data': session_data
|
||||||
|
}
|
||||||
|
|
||||||
|
return jsonify(debug_info)
|
||||||
|
|
||||||
|
# Public route to check dashboard access without admin_required decorator
|
||||||
|
@dashboard_bp.route('/debug/access')
|
||||||
|
async def debug_access():
|
||||||
|
"""Debug route to check dashboard access requirements"""
|
||||||
|
try:
|
||||||
|
current_user = await get_current_user()
|
||||||
|
|
||||||
|
if not current_user:
|
||||||
|
return jsonify({
|
||||||
|
'status': 'error',
|
||||||
|
'message': 'Nessun utente loggato',
|
||||||
|
'redirect': url_for('auth.login')
|
||||||
|
}), 401
|
||||||
|
|
||||||
|
if not current_user.is_admin:
|
||||||
|
return jsonify({
|
||||||
|
'status': 'error',
|
||||||
|
'message': f'Utente {current_user.username} non ha privilegi di amministratore',
|
||||||
|
'user_role': current_user.role,
|
||||||
|
'is_admin': current_user.is_admin,
|
||||||
|
'redirect': url_for('home.index')
|
||||||
|
}), 403
|
||||||
|
|
||||||
|
return jsonify({
|
||||||
|
'status': 'success',
|
||||||
|
'message': f'Accesso consentito per {current_user.username}',
|
||||||
|
'user': {
|
||||||
|
'id': current_user.id,
|
||||||
|
'username': current_user.username,
|
||||||
|
'role': current_user.role,
|
||||||
|
'is_admin': current_user.is_admin
|
||||||
|
},
|
||||||
|
'dashboard_url': url_for('dashboard.index')
|
||||||
|
})
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({
|
||||||
|
'status': 'error',
|
||||||
|
'message': f'Errore durante il controllo accesso: {str(e)}'
|
||||||
|
}), 500
|
||||||
|
|
||||||
@dashboard_bp.route('/')
|
@dashboard_bp.route('/')
|
||||||
@admin_required
|
@admin_required
|
||||||
async def index():
|
async def index():
|
||||||
"""Dashboard home"""
|
"""Dashboard home"""
|
||||||
|
try:
|
||||||
current_user = await get_current_user()
|
current_user = await get_current_user()
|
||||||
|
|
||||||
# Get statistics
|
# Get statistics with error handling
|
||||||
stats = {
|
stats = {
|
||||||
'total_users': await User.count(),
|
'total_users': 0,
|
||||||
'total_projects': await Project.count(published_only=False),
|
'total_projects': 0,
|
||||||
'published_projects': await Project.count(published_only=True),
|
'published_projects': 0,
|
||||||
'featured_projects': len(await Project.get_featured())
|
'featured_projects': 0
|
||||||
}
|
}
|
||||||
|
|
||||||
# Get recent projects
|
try:
|
||||||
|
stats['total_users'] = await User.count()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error getting user count: {e}")
|
||||||
|
|
||||||
|
try:
|
||||||
|
stats['total_projects'] = await Project.count(published_only=False)
|
||||||
|
stats['published_projects'] = await Project.count(published_only=True)
|
||||||
|
featured_projects = await Project.get_featured()
|
||||||
|
stats['featured_projects'] = len(featured_projects) if featured_projects else 0
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error getting project stats: {e}")
|
||||||
|
|
||||||
|
# Get recent projects with error handling
|
||||||
|
recent_projects = []
|
||||||
|
try:
|
||||||
recent_projects = await Project.get_all(published_only=False, limit=5)
|
recent_projects = await Project.get_all(published_only=False, limit=5)
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error getting recent projects: {e}")
|
||||||
|
|
||||||
return await render_template('dashboard/index.html',
|
return await render_template('dashboard/index.html',
|
||||||
user=current_user,
|
user=current_user,
|
||||||
stats=stats,
|
stats=stats,
|
||||||
recent_projects=recent_projects)
|
recent_projects=recent_projects)
|
||||||
|
except Exception as e:
|
||||||
|
flash_message(f'Errore nel caricamento della dashboard: {str(e)}', 'error')
|
||||||
|
return redirect(url_for('home.index'))
|
||||||
|
|
||||||
@dashboard_bp.route('/projects')
|
@dashboard_bp.route('/projects')
|
||||||
@admin_required
|
@admin_required
|
||||||
@@ -42,6 +130,7 @@ async def projects():
|
|||||||
page = int(request.args.get('page', 1))
|
page = int(request.args.get('page', 1))
|
||||||
per_page = 10
|
per_page = 10
|
||||||
|
|
||||||
|
try:
|
||||||
# Get projects with pagination
|
# Get projects with pagination
|
||||||
projects = await Project.get_all(published_only=False, limit=per_page, offset=(page-1)*per_page)
|
projects = await Project.get_all(published_only=False, limit=per_page, offset=(page-1)*per_page)
|
||||||
total_projects = await Project.count(published_only=False)
|
total_projects = await Project.count(published_only=False)
|
||||||
@@ -51,17 +140,24 @@ async def projects():
|
|||||||
return await render_template('dashboard/projects.html',
|
return await render_template('dashboard/projects.html',
|
||||||
projects=projects,
|
projects=projects,
|
||||||
pagination=pagination)
|
pagination=pagination)
|
||||||
|
except Exception as e:
|
||||||
|
flash_message(f'Errore nel caricamento dei progetti: {str(e)}', 'error')
|
||||||
|
return redirect(url_for('dashboard.index'))
|
||||||
|
|
||||||
@dashboard_bp.route('/projects/new', methods=['GET', 'POST'])
|
@dashboard_bp.route('/projects/new', methods=['GET', 'POST'])
|
||||||
@admin_required
|
@admin_required
|
||||||
async def new_project():
|
async def new_project():
|
||||||
"""Create new project"""
|
"""Create new project"""
|
||||||
if request.method == 'GET':
|
if request.method == 'GET':
|
||||||
|
try:
|
||||||
categories = await Category.get_all()
|
categories = await Category.get_all()
|
||||||
return await render_template('dashboard/project_form.html',
|
return await render_template('dashboard/project_form.html',
|
||||||
project=None,
|
project=None,
|
||||||
categories=categories,
|
categories=categories,
|
||||||
action='create')
|
action='create')
|
||||||
|
except Exception as e:
|
||||||
|
flash_message(f'Errore nel caricamento delle categorie: {str(e)}', 'error')
|
||||||
|
return redirect(url_for('dashboard.projects'))
|
||||||
|
|
||||||
form_data = await request.form
|
form_data = await request.form
|
||||||
data = {
|
data = {
|
||||||
@@ -208,6 +304,7 @@ async def users():
|
|||||||
page = int(request.args.get('page', 1))
|
page = int(request.args.get('page', 1))
|
||||||
per_page = 10
|
per_page = 10
|
||||||
|
|
||||||
|
try:
|
||||||
users = await User.get_all(limit=per_page, offset=(page-1)*per_page)
|
users = await User.get_all(limit=per_page, offset=(page-1)*per_page)
|
||||||
total_users = await User.count()
|
total_users = await User.count()
|
||||||
|
|
||||||
@@ -216,3 +313,6 @@ async def users():
|
|||||||
return await render_template('dashboard/users.html',
|
return await render_template('dashboard/users.html',
|
||||||
users=users,
|
users=users,
|
||||||
pagination=pagination)
|
pagination=pagination)
|
||||||
|
except Exception as e:
|
||||||
|
flash_message(f'Errore nel caricamento degli utenti: {str(e)}', 'error')
|
||||||
|
return redirect(url_for('dashboard.index'))
|
||||||
Reference in New Issue
Block a user