# Path: protector/helpers.py
# def get_all_user_permissions(user, obj=None):
#     perm_query = _get_permissions_query(obj)
#     perm_query.fields.append("op.permission_id as perm_id")
#     perm_query.fields.append("gl.permission_id AS gl_perm_id")
#     perm_query.conditions.append("gug.user_id = {user_id!s}".format(user_id=user.id))
#     query = Query(tables=[
#         """
#             {permission_table!s} perm_table LEFT JOIN {content_type_table!s} ctype_table ON
#             perm_table.content_type_id=ctype_table.id INNER JOIN (
#                 {permission_owners_query!s}
#             ) perm_query ON perm_query.perm_id = perm_table.id
#                 OR perm_query.gl_perm_id = perm_table.id
#         """.format(
#             permission_table=Permission._meta.db_table,
#             content_type_table=ContentType._meta.db_table,
#             permission_owners_query=perm_query.get_raw_query(),
#         )
#     ])
#     query.fields.append("perm_table.id as id")
#     query.fields.append("perm_table.codename as codename")
#     query.fields.append("ctype_table.app_label as app_label")
#     perms = Permission.objects.raw(query.get_raw_query())
#     return set("{app}.{codename}".format(app=p.app_label, codename=p.codename) for p in perms)
# 
# def check_single_permission(user, permission, obj=None):
#     if user.is_superuser:
#         return True
#     if obj is not None and obj.id is not None:
#         ctype_id = ContentType.objects.get_for_model(obj).id
#         obj_id = obj.id
#     else:
#         ctype_id = None
#         obj_id = None
#     perm_id = get_permission_id_by_name(permission)
#     if perm_id is None:
#         return False
#     query = "SELECT op.id FROM {permission_owners} WHERE {filter_condition} LIMIT 1"
#     query = query.format(
#         permission_owners=get_permission_owners_query(),
#         filter_condition=_generate_filter_condition(
#             user.id, perm_id, ctype_id, obj_id
#         )
#     )
#     cursor = connection.cursor()
#     cursor.execute(query)
#     return len(cursor.fetchall()) > 0


from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from protector.helpers import get_all_user_permissions, check_single_permission
    return set("{0}.{1}".format(ct, name) for ct, name in perm_qset)


def get_cache_field_name(obj=None):
    if obj is None:
        cache_field_name = ALL_PERMS_CACHE_FIELD
    else:
        cache_field_name = '{field}_{pk}_{ctype_id}'.format(
            field=ALL_PERMS_CACHE_FIELD, pk=obj.pk,
            ctype_id=ContentType.objects.get_for_model(obj).id
        )
    return cache_field_name


class BaseGenericPermissionBackend(object):

    def get_all_permissions(self, user_obj, obj=None):
        if not user_obj.is_active or user_obj.is_anonymous:
            return set()
        disable_cache = getattr(settings, 'DISABLE_GENERIC_PERMISSION_CACHE', False)
        if user_obj.is_superuser:
            perms = getattr(self, '_all_permissions', None)
            if perms is None:
                perms = generate_perm_list(Permission.objects.all())
                if not disable_cache:
                    setattr(self, '_all_permissions', perms)
            return perms
        cache_field_name = get_cache_field_name(obj)
        # need this for testing purposes, do not enable this in production
        if not hasattr(user_obj, cache_field_name) or disable_cache:

# Path: protector/helpers.py
# def get_all_user_permissions(user, obj=None):
#     perm_query = _get_permissions_query(obj)
#     perm_query.fields.append("op.permission_id as perm_id")
#     perm_query.fields.append("gl.permission_id AS gl_perm_id")
#     perm_query.conditions.append("gug.user_id = {user_id!s}".format(user_id=user.id))
#     query = Query(tables=[
#         """
#             {permission_table!s} perm_table LEFT JOIN {content_type_table!s} ctype_table ON
#             perm_table.content_type_id=ctype_table.id INNER JOIN (
#                 {permission_owners_query!s}
#             ) perm_query ON perm_query.perm_id = perm_table.id
#                 OR perm_query.gl_perm_id = perm_table.id
#         """.format(
#             permission_table=Permission._meta.db_table,
#             content_type_table=ContentType._meta.db_table,
#             permission_owners_query=perm_query.get_raw_query(),
#         )
#     ])
#     query.fields.append("perm_table.id as id")
#     query.fields.append("perm_table.codename as codename")
#     query.fields.append("ctype_table.app_label as app_label")
#     perms = Permission.objects.raw(query.get_raw_query())
#     return set("{app}.{codename}".format(app=p.app_label, codename=p.codename) for p in perms)
# 
# def check_single_permission(user, permission, obj=None):
#     if user.is_superuser:
#         return True
#     if obj is not None and obj.id is not None:
#         ctype_id = ContentType.objects.get_for_model(obj).id
#         obj_id = obj.id
#     else:
#         ctype_id = None
#         obj_id = None
#     perm_id = get_permission_id_by_name(permission)
#     if perm_id is None:
#         return False
#     query = "SELECT op.id FROM {permission_owners} WHERE {filter_condition} LIMIT 1"
#     query = query.format(
#         permission_owners=get_permission_owners_query(),
#         filter_condition=_generate_filter_condition(
#             user.id, perm_id, ctype_id, obj_id
#         )
#     )
#     cursor = connection.cursor()
#     cursor.execute(query)
#     return len(cursor.fetchall()) > 0


# Path: protector/backends.py
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from protector.helpers import get_all_user_permissions, check_single_permission

    return set("{0}.{1}".format(ct, name) for ct, name in perm_qset)


def get_cache_field_name(obj=None):
    if obj is None:
        cache_field_name = ALL_PERMS_CACHE_FIELD
    else:
        cache_field_name = '{field}_{pk}_{ctype_id}'.format(
            field=ALL_PERMS_CACHE_FIELD, pk=obj.pk,
            ctype_id=ContentType.objects.get_for_model(obj).id
        )
    return cache_field_name


class BaseGenericPermissionBackend(object):

    def get_all_permissions(self, user_obj, obj=None):
        if not user_obj.is_active or user_obj.is_anonymous:
            return set()
        disable_cache = getattr(settings, 'DISABLE_GENERIC_PERMISSION_CACHE', False)
        if user_obj.is_superuser:
            perms = getattr(self, '_all_permissions', None)
            if perms is None:
                perms = generate_perm_list(Permission.objects.all())
                if not disable_cache:
                    setattr(self, '_all_permissions', perms)
            return perms
        cache_field_name = get_cache_field_name(obj)
        # need this for testing purposes, do not enable this in production
        if not hasattr(user_obj, cache_field_name) or disable_cache:

            perms = get_all_user_permissions(user_obj, obj)