# Path: shared_schema_tenants/models.py
# class TenantRelationship(TimeStampedModel, SingleTenantModelMixin):
#     tenant = models.ForeignKey('Tenant', related_name="relationships")
#     user = models.ForeignKey(django_settings.AUTH_USER_MODEL, related_name="relationships")
#     groups = models.ManyToManyField('auth.Group',
#                                     related_name="user_tenant_groups",
#                                     blank=True)
#     permissions = models.ManyToManyField('auth.Permission',
#                                          related_name="user_tenant_permissions",
#                                          blank=True)
# 
#     def __str__(self):
#         groups_str = ', '.join([g.name for g in self.groups.all()])
#         return '%s - %s (%s)' % (str(self.user), str(self.tenant), groups_str)
# 
#     class Meta:
#         unique_together = [('user', 'tenant')]
# 
# Path: shared_schema_tenants/helpers/tenants.py
# def get_current_tenant():
#     from shared_schema_tenants.middleware import TenantMiddleware
#     return TenantMiddleware.get_current_tenant()


import logging
from django.contrib.auth.models import Permission
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth import get_user_model
from django.db.models import Q
from shared_schema_tenants.models import TenantRelationship
from shared_schema_tenants.helpers.tenants import get_current_tenant
        return getattr(user_obj, perm_cache_name)

    def _get_permissions(self, user_obj, obj, from_name):
        return self._get_global_permissions(user_obj, obj, from_name).union(
            self._get_tenant_permissions(user_obj, obj, from_name))

    def get_user_global_permissions(self, user_obj, obj=None):
        return self._get_global_permissions(user_obj, obj, 'user')

    def get_user_tenant_permissions(self, user_obj, obj=None):
        return self._get_tenant_permissions(user_obj, obj, 'user')

    def get_group_global_permissions(self, user_obj, obj=None):
        return self._get_global_permissions(user_obj, obj, 'group')

    def get_group_tenant_permissions(self, user_obj, obj=None):
        return self._get_tenant_permissions(user_obj, obj, 'group')

    def get_all_global_permissions(self, user_obj, obj=None):
        if not user_obj.is_active or user_obj.is_anonymous() or obj is not None:
            return set()
        if not hasattr(user_obj, '_perm_cache'):
            user_obj._perm_cache = self.get_user_global_permissions(user_obj, obj)
            user_obj._perm_cache.update(self.get_group_global_permissions(user_obj, obj))
        return user_obj._perm_cache

    def get_all_tenant_permissions(self, user_obj, obj=None):
        if not user_obj.is_active or user_obj.is_anonymous() or obj is not None:
            return set()


# Path: shared_schema_tenants/models.py
# class TenantRelationship(TimeStampedModel, SingleTenantModelMixin):
#     tenant = models.ForeignKey('Tenant', related_name="relationships")
#     user = models.ForeignKey(django_settings.AUTH_USER_MODEL, related_name="relationships")
#     groups = models.ManyToManyField('auth.Group',
#                                     related_name="user_tenant_groups",
#                                     blank=True)
#     permissions = models.ManyToManyField('auth.Permission',
#                                          related_name="user_tenant_permissions",
#                                          blank=True)
# 
#     def __str__(self):
#         groups_str = ', '.join([g.name for g in self.groups.all()])
#         return '%s - %s (%s)' % (str(self.user), str(self.tenant), groups_str)
# 
#     class Meta:
#         unique_together = [('user', 'tenant')]
# 
# Path: shared_schema_tenants/helpers/tenants.py
# def get_current_tenant():
#     from shared_schema_tenants.middleware import TenantMiddleware
#     return TenantMiddleware.get_current_tenant()


# Path: shared_schema_tenants/auth_backends.py
import logging
from django.contrib.auth.models import Permission
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth import get_user_model
from django.db.models import Q
from shared_schema_tenants.models import TenantRelationship
from shared_schema_tenants.helpers.tenants import get_current_tenant

        return getattr(user_obj, perm_cache_name)

    def _get_permissions(self, user_obj, obj, from_name):
        return self._get_global_permissions(user_obj, obj, from_name).union(
            self._get_tenant_permissions(user_obj, obj, from_name))

    def get_user_global_permissions(self, user_obj, obj=None):
        return self._get_global_permissions(user_obj, obj, 'user')

    def get_user_tenant_permissions(self, user_obj, obj=None):
        return self._get_tenant_permissions(user_obj, obj, 'user')

    def get_group_global_permissions(self, user_obj, obj=None):
        return self._get_global_permissions(user_obj, obj, 'group')

    def get_group_tenant_permissions(self, user_obj, obj=None):
        return self._get_tenant_permissions(user_obj, obj, 'group')

    def get_all_global_permissions(self, user_obj, obj=None):
        if not user_obj.is_active or user_obj.is_anonymous() or obj is not None:
            return set()
        if not hasattr(user_obj, '_perm_cache'):
            user_obj._perm_cache = self.get_user_global_permissions(user_obj, obj)
            user_obj._perm_cache.update(self.get_group_global_permissions(user_obj, obj))
        return user_obj._perm_cache

    def get_all_tenant_permissions(self, user_obj, obj=None):
        if not user_obj.is_active or user_obj.is_anonymous() or obj is not None:
            return set()


        tenant = get_current_tenant()