<filename>server/apps/tenants/mixins.py
from django.contrib.auth.mixins import AccessMixin
from django.core.exceptions import PermissionDenied


class TenantAccessRequiredMixin(AccessMixin):
    """CBV mixin which verifies that the current user is authenticated."""

    def dispatch(self, request, *args, **kwargs):

        if request.user.is_anonymous():
            return self.handle_no_permission()

        # TODO: Only out so that Lorenz can work on the Comments API

        if request.tenant is None or not request.user.tenants.filter(
                pk=request.tenant.id):
            return self.handle_no_permission()

        print("SiteAccessRequiredMixin: User {} on tenant {} okay!".format(
            request.user, request.tenant))

        return super(TenantAccessRequiredMixin, self).dispatch(
            request, *args, **kwargs)

    def handle_no_permission(self):
        raise PermissionDenied(self.get_permission_denied_message())
