from django.core.exceptions import PermissionDenied


def raise_error_if_email_not_validated(jwt_decoded, user, new):
    """
    If JWT does not contain `email_verified` with a value of True
    PermissionDenied is raised to cancel the request
    """
    if "email_verified" not in jwt_decoded or not \
            jwt_decoded["email_verified"]:
        raise PermissionDenied()


def raise_error_if_user_not_active(jwt_decoded, user, new):
    """
    If user is not active PermissionDenied is raised to cancel the request
    """
    if not hasattr(user, 'is_active') or not user.is_active:
        raise PermissionDenied()


def set_is_active_false_on_new_users(jwt_decoded, user, new):
    """
    If user is new, set is_active to False
    """
    if new and hasattr(user, 'is_active'):
        setattr(user, 'is_active', False)
        user.save()
