<commit_before>"""
Decorators to protects routes with permissions
"""
import json
from functools import wraps

from flask import redirect, request, Response, current_app, g, Response
from werkzeug.exceptions import Unauthorized, Forbidden

from geonature.core.gn_permissions.tools import (
    get_user_permissions,
    get_user_from_token_and_raise,
    UserCruved,
)


def login_required(view_func):
    @wraps(view_func)
    def decorated_view(*args, **kwargs):
        if g.current_user is None:
            raise Unauthorized
        return view_func(*args, **kwargs)
    return decorated_view


def check_cruved_scope(
    action,
    get_role=False,
    module_code=None,
    object_code=None,
    redirect_on_expiration=None,
    redirect_on_invalid_token=None,
    get_scope=True,
):
    """
    Decorator to protect routes with SCOPE CRUVED
    The decorator first check if the user is conected and have a corect token (get_user_from_token_and_raise)
    and then return the max user SCOPE permission for the action in parameter
    The decorator manage herited CRUVED from user's group and parent module (GeoNature)
    Return a VUsersPermissions as kwargs of the decorated function as 'info_role' parameter

    Parameters:
        action(string): the requested action of the route <'C', 'R', 'U', 'V', 'E', 'D'>
        get_role(boolean): is the decorator should retour the VUsersPermissions object as kwargs
        module_code(string): the code of the module (gn_commons.t_modules) ('OCCTAX') for the requested permission
        object_code(string): the code of the object (gn_permissions.t_object) for the requested permission ('PERMISSIONS')
    """

    def _check_cruved_scope(fn):
        @wraps(fn)
        def __check_cruved_scope(*args, **kwargs):
            user = get_user_from_token_and_raise(
                request,
                action,<commit_msg>check_cruved_scope: default to no scope<commit_after>"""
Decorators to protects routes with permissions
"""
import json
from functools import wraps

from flask import redirect, request, Response, current_app, g, Response
from werkzeug.exceptions import Unauthorized, Forbidden

from geonature.core.gn_permissions.tools import (
    get_user_permissions,
    get_user_from_token_and_raise,
    UserCruved,
)


def login_required(view_func):
    @wraps(view_func)
    def decorated_view(*args, **kwargs):
        if g.current_user is None:
            raise Unauthorized
        return view_func(*args, **kwargs)
    return decorated_view


def check_cruved_scope(
    action,
    get_role=False,
    module_code=None,
    object_code=None,
    redirect_on_expiration=None,
    redirect_on_invalid_token=None,
    get_scope=False,
):
    """
    Decorator to protect routes with SCOPE CRUVED
    The decorator first check if the user is conected and have a corect token (get_user_from_token_and_raise)
    and then return the max user SCOPE permission for the action in parameter
    The decorator manage herited CRUVED from user's group and parent module (GeoNature)
    Return a VUsersPermissions as kwargs of the decorated function as 'info_role' parameter

    Parameters:
        action(string): the requested action of the route <'C', 'R', 'U', 'V', 'E', 'D'>
        get_role(boolean): is the decorator should retour the VUsersPermissions object as kwargs
        module_code(string): the code of the module (gn_commons.t_modules) ('OCCTAX') for the requested permission
        object_code(string): the code of the object (gn_permissions.t_object) for the requested permission ('PERMISSIONS')
    """

    def _check_cruved_scope(fn):
        @wraps(fn)
        def __check_cruved_scope(*args, **kwargs):
            user = get_user_from_token_and_raise(
                request,
                action,
{'fraction_non_alphanumeric': 0.07254022685307307, 'fraction_numerical': 0.0, 'mean_word_length': 3.6413708690330475, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6954864', 'n_tokens_mistral': 1128, 'n_tokens_neox': 1110, 'n_words': 357}