from functools import wraps
from flask import flash, redirect, session, url_for


def login_required(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        if not session.get("user_id"):
            flash("Bitte zuerst anmelden.", "warning")
            return redirect(url_for("login"))
        return func(*args, **kwargs)
    return wrapper


def admin_required(func):
    @wraps(func)
    def wrapper(*args, **kwargs):
        groups = session.get("groups", [])
        if "Admins" not in groups:
            flash("Keine Berechtigung.", "danger")
            return redirect(url_for("dashboard"))
        return func(*args, **kwargs)
    return wrapper