def setup_default_permissions(session, instance):
    """Setup default permissions on newly created entities according to.

    :attr:`Entity.__default_permissions__`.
    """
    if instance not in session.new or not isinstance(instance, Entity):
        return

    if not current_app:
        # working outside app_context. Raw object manipulation
        return

    _setup_default_permissions(instance)