# 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



UserModel = get_user_model()

logger = logging.getLogger(__name__)


class TenantModelBackend(ModelBackend):
    """
    Authenticates against settings.AUTH_USER_MODEL.
    """

    def _get_user_global_permissions(self, user_obj):
        return user_obj.user_permissions.all()

    def _get_group_global_permissions(self, user_obj):
        user_groups_field = get_user_model()._meta.get_field('groups')
        user_groups_query = 'group__%s' % user_groups_field.related_query_name()
        return Permission.objects.filter(**{user_groups_query: user_obj})

    def _get_user_tenant_permissions(self, relationship):
        return relationship.permissions.all()

    def _get_group_tenant_permissions(self, relationship):
        relationship_groups_field = TenantRelationship._meta.get_field('groups')
        relationship_groups_query = 'group__%s' % relationship_groups_field.related_query_name()
        return Permission.objects.filter(**{relationship_groups_query: relationship})

    def _get_user_permissions(self, relationship):

# 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




UserModel = get_user_model()

logger = logging.getLogger(__name__)


class TenantModelBackend(ModelBackend):
    """
    Authenticates against settings.AUTH_USER_MODEL.
    """

    def _get_user_global_permissions(self, user_obj):
        return user_obj.user_permissions.all()

    def _get_group_global_permissions(self, user_obj):
        user_groups_field = get_user_model()._meta.get_field('groups')
        user_groups_query = 'group__%s' % user_groups_field.related_query_name()
        return Permission.objects.filter(**{user_groups_query: user_obj})

    def _get_user_tenant_permissions(self, relationship):
        return relationship.permissions.all()

    def _get_group_tenant_permissions(self, relationship):
        relationship_groups_field = TenantRelationship._meta.get_field('groups')
        relationship_groups_query = 'group__%s' % relationship_groups_field.related_query_name()
        return Permission.objects.filter(**{relationship_groups_query: relationship})

    def _get_user_permissions(self, relationship):

        relationship_permissions_field = TenantRelationship._meta.get_field('permissions')