-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TenantAccessMiddleware & Authentification #666
Comments
The There are reasons it won't do that (see the try/except earlier in the method), so I would suggest adding a debug point in the |
Hey @killer24vrccp, the error is caused by the ordering in the middleware (weird, I know :P). Just ensure the tenant_user middleware comes after the django auth middleware like so:
|
Good catch @thubamamba! Yes, the tenant_users middleware requires the AuthenticationMiddle since it's checking for authentication status. We should probably document this in the docs and middleware docstring. I did originally write this as a starting point since a few people were asking, but by no means is it meant to fit every use case. |
Hey @thubamamba I have try but I have the same error. I don't knwo why. Environment:
Request Method: GET
Request URL: http://localhost:8000/en/
Django Version: 4.2.15
Python Version: 3.10.11
Installed Applications:
['jazzmin',
'django_tenants',
'tenant_users.permissions',
'tenant_users.tenants',
'app.generic.business',
'app.generic.account',
'django.contrib.contenttypes',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.humanize',
'django.contrib.admin',
'django.contrib.staticfiles',
'django.contrib.admindocs',
'django.contrib.sitemaps',
'rosetta',
'django_user_agents',
'rest_framework',
'rest_framework.authtoken',
'app.public.user_sessions',
'app.public.core',
'app.public.changelog',
'app.generic.address',
'app.generic.module',
'bootstrap5',
'app.api',
'django_htmx',
'crispy_forms',
'crispy_bootstrap5',
'django_countries',
'parler']
Installed Middleware:
['django_tenants.middleware.main.TenantMainMiddleware',
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'tenant_users.tenants.middleware.TenantAccessMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_user_agents.middleware.UserAgentMiddleware']
Traceback (most recent call last):
File "D:\Projet\CVS_tenant\.venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "D:\Projet\CVS_tenant\.venv\lib\site-packages\tenant_users\tenants\middleware.py", line 44, in __call__
if not self.has_tenant_access(request):
File "D:\Projet\CVS_tenant\.venv\lib\site-packages\tenant_users\tenants\middleware.py", line 61, in has_tenant_access
return request.user.tenants.filter(id=request.tenant.id).exists()
Exception Type: AttributeError at /en/
Exception Value: 'WSGIRequest' object has no attribute 'tenant' |
Did you check out my prior comment? That I would bet something is not setup right with from django_tenants.utils import get_tenant_domain_model
hostname = "whatever-you're-using"
domain_model = get_tenant_domain_model()
domain_model.objects.select_related('tenant').get(domain=hostname) If that doesn't come up with anything, you'll get the error you're seeing. |
I don't know if that's can help me but I use this command for approvisionning public tenant : |
I have setup few print : localhost Debug: localhost
Internal Server Error: /favicon.ico
Traceback (most recent call last):
File "D:\Projet\CVS_tenant\.venv\lib\site-packages\django\core\handlers\exception.py", line 55, in inner
response = get_response(request)
File "D:\Projet\CVS_tenant\.venv\lib\site-packages\tenant_users\tenants\middleware.py", line 44, in __call__
if not self.has_tenant_access(request):
File "D:\Projet\CVS_tenant\.venv\lib\site-packages\tenant_users\tenants\middleware.py", line 61, in has_tenant_access
return request.user.tenants.filter(id=request.tenant.id).exists()
AttributeError: 'WSGIRequest' object has no attribute 'tenant' def process_request(self, request):
# Connection needs first to be at the public schema, as this is where
# the tenant metadata is stored.
connection.set_schema_to_public()
try:
hostname = self.hostname_from_request(request)
print(hostname)
except DisallowedHost:
from django.http import HttpResponseNotFound
return HttpResponseNotFound()
domain_model = get_tenant_domain_model()
try:
tenant = self.get_tenant(domain_model, hostname)
print(tenant)
except domain_model.DoesNotExist:
self.no_tenant_found(request, hostname)
return
tenant.domain_url = hostname
request.tenant = tenant
connection.set_tenant(request.tenant)
self.setup_url_routing(request) |
I'm confused - did you not try the commands I suggested?
You're creating a "domain" with the value
The domain you're accessing is |
I managed to get this working, the issue was indeed that there was no tenant being passed to the request. So, the first thing you have to ensure to do is create a public tenant.. check out the django-tenant docs or the django-tenant-users. |
@Dresdn Yes I have try. I have retry with >>> hostname = "localhost"
>>> domain_model = get_tenant_domain_model()
>>> domain_model.objects.select_related('tenant').get(domain=hostname)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "D:\Projet\CVS_tenant\.venv\lib\site-packages\django\db\models\query.py", line 637, in get
raise self.model.DoesNotExist(
app.generic.business.models.Domain.DoesNotExist: Domain matching query does not exist.
>>> |
Let's ensure we're on the same page. Can you confirm the following?
To your last note, if there is no I would ensure you have everything setup as called out in the Installation Docs. |
I have resolve the problem. I have juste use localhost then public.localhost. But raise a new problem. My tenant URL not work in my public tenant. Thats said I use |
I have a problem with
TenantAccessMiddleware
. When user authentificated in public after in my public I receive this error: 'WSGIRequest' object has no attribute 'tenant'Account model
Tenant/Domain Model
When I'm on my tenant and I try to login i'm stay not authentificated. I have add this settings
SESSION_COOKIE_DOMAIN = ".localhost"
. I'm in localhost for dev.The text was updated successfully, but these errors were encountered: