57 lines
1.7 KiB
Python
57 lines
1.7 KiB
Python
"""
|
|
Authentifizierungs-Decoratoren
|
|
"""
|
|
from functools import wraps
|
|
from flask import session, redirect, url_for, request, g, abort, flash
|
|
|
|
|
|
def _get_tenant_for_redirect():
|
|
"""Hilfsfunktion um Tenant für Redirect zu ermitteln"""
|
|
if hasattr(g, 'tenant_code') and g.tenant_code:
|
|
return g.tenant_code
|
|
if 'tenant_code' in session:
|
|
return session['tenant_code']
|
|
# Versuche aus URL
|
|
tenant = request.view_args.get('tenant') if request.view_args else None
|
|
if tenant:
|
|
return tenant
|
|
return 'RV'
|
|
|
|
|
|
def login_required(f):
|
|
"""Decorator für routes die einen eingeloggten Account erfordern"""
|
|
@wraps(f)
|
|
def decorated_function(*args, **kwargs):
|
|
if 'account_id' not in session:
|
|
return redirect(url_for('auth.login', tenant=_get_tenant_for_redirect()))
|
|
return f(*args, **kwargs)
|
|
return decorated_function
|
|
|
|
|
|
def role_required(roles):
|
|
"""Decorator für routes die bestimmte Rollen erfordern"""
|
|
if isinstance(roles, str):
|
|
roles = [roles]
|
|
|
|
def decorator(f):
|
|
@wraps(f)
|
|
def decorated_function(*args, **kwargs):
|
|
if 'account_id' not in session:
|
|
return redirect(url_for('auth.login', tenant=_get_tenant_for_redirect()))
|
|
|
|
account_role = session.get('role')
|
|
if account_role not in roles:
|
|
abort(403)
|
|
return f(*args, **kwargs)
|
|
return decorated_function
|
|
return decorator
|
|
|
|
|
|
def tenant_required(f):
|
|
"""Decorator für tenant-spezifische Routes"""
|
|
@wraps(f)
|
|
def decorated_function(*args, **kwargs):
|
|
# Tenant wird bereits in der Route über URL extrahiert
|
|
return f(*args, **kwargs)
|
|
return decorated_function
|