Closed Melendeze13 closed 3 months ago
Hello @Melendeze13 please check #485 I think it solves your issue
This does solve my issue but makes me wonder how I should go about adding users to new tenant schemas.
Currently this middleware is prevent all users from logging into non-public tenants. This is the case even if the user is the owner of that tenant. I used provision_tenant when creating the new tenant.
Currently I have two cases:
Looking at the code it seems that add_user is only done for a public tenant. That makes sense. But I'm wondering, in order to solve my issues, would I have to just call add_user in my specific cases?
https://github.com/Corvia/django-tenant-users/blob/master/tenant_users/tenants/models.py#L99
Thanks again!
To add a specific user to a tenant you can use tenant.add_user(user, is_superuser=True, is_staff=True)
.
This method creates the UserTenantPermissions
and links the user to the tenant.
if you check the UserProfile
class (from which your User Model should inherit) you will see a tenants ManyToManyField
where all tenants the user is allowed to access are stored
For example below is the code snippets from my UserCreateView
def form_valid(self, form: BaseForm) -> HttpResponse:
user: User = form.instance
>>>>>>
##Adding the user in the current tenant
tenant: Client = self.request.tenant
tenant.add_user(user, is_superuser=False, is_staff=False)
>>>>>>>>>>>>>>
##Adding the user to a group
group = Group.objects.get(name="attendant")
user.tenant_perms.groups.add(group)
if,for example,I would want to also add the user to the public schema I would have queried for the public tenant and added the user as follows
from django_tenants.utils import get_public_schema_name
public_tenant = Client.objects.get(schema_name=get_public_schema_name())
public_tenant.add_user(user, is_superuser=False, is_staff=False)
@Wizely99 Thank you!
I really appreciate you providing the context it has definitely moved me along on my project!
Hello!
For my project, when accounts are made they're part of the public schema. From the docs, that's completely expected. My issue is when a tenant is created I need the accounts made under that tenant to only have access to that tenant. I understand that those accounts would still be in the public schema.
I'm pretty new to Django so I don't know if I need to do something with permissions.
Example of what's going on: Public Schema: public Tenant Schema: bigco account made in bigco.localhost : manager@bigco.com
A new schema is created: Tenant Schema: smallco
manager@bigco.com can log into smallco.localhost
Is there a way to handle this? I understand that part of the project is allowing users access to multiple schemas but I need a way to limit access.
Thanks!