django-tenant-users: Unable to successfully login to django in any of public/other tenant

Read me or sample code never guided the exact format/order of instructions for tenant creations so I am trying mine according to given guidelines to create tenants with code in one of my shared app view

Created public tenant public_owner = "owner@local" create_public_tenant("localhost", public_owner) tenant_admin_email = "admin@local" TenantUser.objects.create_superuser('123', tenant_admin_email) Created my tenant tenant_admin_email = "admin@" + tenant_name TenantUser.objects.create_superuser('123', tenant_admin_email) provision_tenant(tenant_name, tenant_name, tenant_admin_email) It does all => creates tenant with schema and both the super users as well

But access to admin panel on public never succeeds (even login gets the user) with zero message And in tenants it always gives message as “Please enter the correct Email Address and password”

My settings are as ` MIDDLEWARE = ( ‘tenant_tutorial.middleware.TenantTutorialMiddleware’, ‘django.middleware.common.CommonMiddleware’, ‘django.contrib.sessions.middleware.SessionMiddleware’, ‘django.middleware.csrf.CsrfViewMiddleware’, ‘django.contrib.auth.middleware.AuthenticationMiddleware’, ‘django.contrib.messages.middleware.MessageMiddleware’, # Uncomment the next line for simple clickjacking protection: # ‘django.middleware.clickjacking.XFrameOptionsMiddleware’, )

TENANT_MODEL = “customers.Client” # app.Model TENANT_DOMAIN_MODEL = “customers.Domain” # app.Model TENANT_USERS_DOMAIN = “localhost” AUTH_USER_MODEL = ‘users.TenantUser’ AUTHENTICATION_BACKENDS = ( ‘tenant_users.permissions.backend.UserBackend’, ) SESSION_COOKIE_DOMAIN = ‘.’ + TENANT_USERS_DOMAIN

SHARED_APPS = ( ‘django_tenants’, ‘django.contrib.admin’, ‘django.contrib.admin’, ‘django.contrib.auth’, ‘django.contrib.contenttypes’, ‘django.contrib.sessions’, ‘django.contrib.messages’, ‘django.contrib.staticfiles’, ‘tenant_users.permissions’, ‘tenant_users.tenants’, ‘customers’, # you must list the app where your tenant model resides in ‘users’, )

TENANT_APPS = ( ‘django.contrib.admin’, ‘django.contrib.auth’, ‘django.contrib.contenttypes’, ‘tenant_users.permissions’, ‘django.contrib.admin’, ‘django.contrib.sessions’, ‘django.contrib.messages’, ‘tenant_only’, )

INSTALLED_APPS = list(set(TENANT_APPS + SHARED_APPS)) `

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Comments: 15

Most upvoted comments

For me, the issue was: SESSION_COOKIE_DOMAIN = ".localhost".

It’s not a django-tenant-users issue, or a django-tenant issue. It’s just a web issue - you can’t have .localhost as a valid session domain. https://code.djangoproject.com/ticket/10560

Removing this solved, but now I can’t stay logged in across domains. But that’s probably outside the scope here.

Also, @JupiterThreads - I wouldn’t call UserTenantPermissions.objects.create() directly as there’s an extra step to link the User to the Tenant. Instead, you can do:

new_user = TenantUser.objects.create_user(email="user@evilcorp.com", password="NotRandom!", is_active=True, is_staff=True, is_superuser=True)
tenant = TenantModel.objects.get(<whatever criteria>)
tenant.add_user(new_user)

After executing this command in manage.py shell

from tenant_users.tenants.utils import create_public_tenant create_public_tenant("localhost", "admin@evilcorp.com")

¿Do i have to do something else? ¿What password is it assigned? (I see set_unusable_password is called, ¿how can i change the password then? ¿Do i have to create first a superuser? (createsuperuser command doesn´t work for me. Error: User matching query does not exist)

I can not log in to the public tenant, it says i need an staff account

Sorry if iam asking a very basic question, i am learning django. Thanks

SETTINGS `ALLOWED_HOSTS = [‘localhost’, ‘www.localhost’, ‘.localhost’, ‘127.0.0.1’]

Application definition

SHARED_APPS = [ ‘tenant_schemas’, ‘rest_framework’,

'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',

'tenant_users.permissions',
'tenant_users.tenants',

'django.contrib.sites',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',

'client',
'users',

]

TENANT_APPS = [ ‘django.contrib.admin’, ‘django.contrib.auth’, ‘django.contrib.contenttypes’, ‘tenant_users.permissions’, ‘django.contrib.messages’, ‘django.contrib.staticfiles’,

'tenant',

]

INSTALLED_APPS = ( ‘tenant_schemas’,

'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'django.contrib.sites',

'tenant_users.permissions',
'tenant_users.tenants',

'rest_framework',

'client',
'tenant',
'users',

)

SITE_ID=1

TENANT_MODEL = ‘client.Client’ TENANT_USERS_DOMAIN = ‘localhost’ AUTH_USER_MODEL = ‘users.TenantUser’ AUTHENTICATION_BACKENDS = (‘tenant_users.permissions.backend.UserBackend’,)

if DEBUG: SESSION_COOKIE_DOMAIN = ‘None’ else: SESSION_COOKIE_DOMAIN = ‘.yourdomain.com’

MIDDLEWARE = [ ‘tenant_schemas.middleware.DefaultTenantMiddleware’,

'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',

]`

Yeah after you’ve created that user that is a system user you don’t really interact with again, it creates a random password for it. You need to create another user with super user permissions and use that user.

So after you’ve created your new user in the public tenant now create a permission for that user like so: UserTenantPermissions.objects.create(profile=new_user, is_staff=True, is_superuser=True) Then you should be able to login to admin.

I’m having a very similar problem. My second tenant is setup at test.localhost:5000 and I can view test.localhost:500/admin just fine, but when logging in, it keeps saying I need a staff or active account.

I then tried to login to the base localhost:5000/admin and was met with an error that says:

You are authenticated as admin@test.com, but are not authorized to access this page. Would you like to login to a different account?

This is after trying to login to test.localhost:5000 with that email - so I’m logging in, but somehow don’t have permissions to view the admin?