Skip to content
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

How to prevent tenants superusers from seeing other tenants in admin? #554

Closed
hemulin opened this issue Jul 13, 2018 · 3 comments
Closed

Comments

@hemulin
Copy link

hemulin commented Jul 13, 2018

Hi Y'all,

Just started integrating dts into an existing project.

I wish to have a public scheme superuser that can see all the tenants models while other tenants superusers would not be able to access other tenants data.

I have an organization app that holds a Client model which is also the tenant model.

# organization.models.py

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models
from django.utils.encoding import python_2_unicode_compatible
from tenant_schemas.models import TenantMixin

@python_2_unicode_compatible
class Client(TenantMixin):
    name = models.CharField(max_length=100)
    paid_until =  models.DateField()
    on_trial = models.BooleanField()
    created_on = models.DateField(auto_now_add=True)
    logo = models.ImageField(upload_to='logos/', null=True)
    auto_create_schema = True

    def __str__(self):
        return self.name

The admin.py currently is just

# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.contrib import admin
from organization.models import Client

admin.site.register(Client)

When I put 'organization' in the SHARED_APPS section, every tenant can see the other tenants
When I put 'organization' in the TENANT_APPS section, I get the following error running migrate_schemas:

TypeError: argument of type 'TenantQueryset' is not iterable

Is there a standard way to achieve this super-superuser access to tenants without letting others tenant superusers to see others?

Cheers

@kingbuzzman
Copy link

kingbuzzman commented Jul 19, 2018

@hemulin https://docs.djangoproject.com/en/2.0/ref/contrib/admin/#django.contrib.admin.ModelAdmin.get_queryset

something like:

@admin.register(Client)
class ClientAdmin(admin.ModelAdmin):
    def get_queryset(self):
        return super(ClientAdmin, self).get_queryset().filter(id__in=[connection.get_tenant()])

PS. i did not test this.

@hemulin
Copy link
Author

hemulin commented Jul 23, 2018

Cool, I get the idea.
Thanks for the link and the snippet 👍 .

@hemulin hemulin closed this as completed Jul 23, 2018
@saileshkush95
Copy link

@hemulin can you share how you solve this, this is not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants