<commit_before>from flask import request, jsonify, abort
from flask_restful import Resource, reqparse, current_app
from flask_principal import (
    Identity,
    AnonymousIdentity,
    RoleNeed,
    UserNeed,
    identity_changed,
    identity_loaded
)

from zou.app.utils import auth
from zou.app.services.exception import PersonNotFoundException
from zou.app.services import persons_service, auth_service
from zou.app import app
from zou.app.services.exception import (
    NoAuthStrategyConfigured,
    WrongPasswordException,
    WrongUserException,
    UnactiveUserException
)


from flask_jwt_extended import (
    jwt_required,
    jwt_refresh_token_required,
    create_access_token,
    create_refresh_token,
    get_jwt_identity,
    get_raw_jwt,
    set_access_cookies,
    set_refresh_cookies,
    unset_jwt_cookies
)


def is_from_browser(user_agent):
    return user_agent.browser in [
        "camino",
        "chrome",
        "firefox",
        "galeon",
        "kmeleon",
        "konqueror",
        "links",
        "lynx",
        "msie",
        "msn",
        "netscape",
        "opera",
        "safari",
        "seamonkey",
        "webkit"
    ]


@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
    if identity.id is not None:
        from zou.app.services import persons_service
        try:
            identity.user = persons_service.get_person(identity.id)

            if hasattr(identity.user, "id"):
                identity.provides.add(UserNeed(identity.user["id"]))

            if identity.user["role"] == "admin":
                identity.provides.add(RoleNeed("admin"))
                identity.provides.add(RoleNeed("manager"))

            if identity.user["role"] == "manager":
                identity.provides.add(RoleNeed("manager"))

            return identity
        except PersonNotFoundException:
            return None


class AuthenticatedResource(Resource):
    """
    Returns information if the user is authenticade else it returns a 401
    response.
    It can be used by third party tools, especially browser frontend, to know
    if current user is still logged in.
    """

    @jwt_required
    def get(self):
        try:
            person = persons_service.get_person_by_email(get_jwt_identity())
            return {
                "authenticated": True,
                "user": person<commit_msg>Handle all exception when identity fails to load<commit_after>from flask import request, jsonify, abort
from flask_restful import Resource, reqparse, current_app
from flask_principal import (
    Identity,
    AnonymousIdentity,
    RoleNeed,
    UserNeed,
    identity_changed,
    identity_loaded
)

from zou.app.utils import auth
from zou.app.services.exception import PersonNotFoundException
from zou.app.services import persons_service, auth_service
from zou.app import app
from zou.app.services.exception import (
    NoAuthStrategyConfigured,
    WrongPasswordException,
    WrongUserException,
    UnactiveUserException
)


from flask_jwt_extended import (
    jwt_required,
    jwt_refresh_token_required,
    create_access_token,
    create_refresh_token,
    get_jwt_identity,
    get_raw_jwt,
    set_access_cookies,
    set_refresh_cookies,
    unset_jwt_cookies
)


def is_from_browser(user_agent):
    return user_agent.browser in [
        "camino",
        "chrome",
        "firefox",
        "galeon",
        "kmeleon",
        "konqueror",
        "links",
        "lynx",
        "msie",
        "msn",
        "netscape",
        "opera",
        "safari",
        "seamonkey",
        "webkit"
    ]


@identity_loaded.connect_via(app)
def on_identity_loaded(sender, identity):
    if identity.id is not None:
        from zou.app.services import persons_service
        try:
            identity.user = persons_service.get_person(identity.id)

            if hasattr(identity.user, "id"):
                identity.provides.add(UserNeed(identity.user["id"]))

            if identity.user["role"] == "admin":
                identity.provides.add(RoleNeed("admin"))
                identity.provides.add(RoleNeed("manager"))

            if identity.user["role"] == "manager":
                identity.provides.add(RoleNeed("manager"))

            return identity
        except PersonNotFoundException:
            return None
        except Exception as exception:
            current_app.logger.error(exception.message)
            return None


class AuthenticatedResource(Resource):
    """
    Returns information if the user is authenticade else it returns a 401
    response.
    It can be used by third party tools, especially browser frontend, to know
    if current user is still logged in.
    """

    @jwt_required
    def get(self):
        try:
            person = persons_service.get_person_by_email(get_jwt_identity())
            return {
                "authenticated": True,
                "user": person
{'fraction_non_alphanumeric': 0.0840064620355412, 'fraction_numerical': 0.0012116316639741518, 'mean_word_length': 2.4929478138222847, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 3, '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': '1196438', 'n_tokens_mistral': 1447, 'n_tokens_neox': 1398, 'n_words': 329}