[英]How to implement multi-tenancy with custom user model?
我正在嘗試創建一個多租戶應用程序,其中通過 django 默認用戶模型進行身份驗證。 我使用了我自己的自定義用戶模型。 我想對多個模式使用相同的數據庫。
用戶模型
class UsersManager(BaseUserManager):
def create_user(self, username, password=None, **kwargs):
if not username:
raise ValueError('Users must have a valid username.')
if not kwargs.get('email'):
raise ValueError('Users must have a valid email.')
if not kwargs.get('contact_number'):
raise ValueError('Users must have a valid contact number.')
account = self.model(
username=username, email = kwargs.get('email'), contact_number=kwargs.get('contact_number'),first_name=kwargs.get('first_name'),last_name=kwargs.get('last_name')
)
account.set_password(password)
account.save()
return account
def create_staffuser(self, username, password, **kwargs):
account = self.create_user(username, password, **kwargs)
account.is_admin = False
account.is_superuser = False
account.is_staff = True
account.is_agent = False
account.save()
return account
def create_superuser(self, username, password, **kwargs):
account = self.create_user(username, password, **kwargs)
account.is_admin = True
account.is_staff = True
account.is_superuser = True
account.is_agent = False
account.save()
return account
def create tenant_user(self, username, password, **kwargs):
"""Custom command to create a tenant user"""
... ?????????????????????
class User(AbstractBaseUser, PermissionsMixin):
first_name = models.CharField(max_length=50, default=None, null=True)
middle_name = models.CharField(max_length=50, default=None, null=True)
last_name = models.CharField(max_length=50, default = None, null=True)
username = models.CharField(unique=True, max_length=50)
email = models.EmailField(unique=True)
street_address = models.CharField(max_length=150)
city = models.CharField(max_length=50)
contact_number = models.CharField(max_length=40)
alternative_contact_number = models.CharField(max_length=40,default=None, null=True)
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(default=timezone.now)
objects = UsersManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email','first_name','last_name','city','contact_number']
is_staff = models.BooleanField(default=False)
is_admin = models.BooleanField(default=False)
is_agent = models.BooleanField(default=False)
is_customer = models.BooleanField(default=False)
is_tenant = models.BooleanField(default=False)
租戶模型
class Client(TenantMixin):
user = models.ManyToMany('User', related_name='hospital')
paid_until = models.DateField()
on_trial = models.BooleanField()
created_on = models.DateField(auto_now_add=True)
auto_create_schema = True
我正在使用 User 模型在 TenantModel 中創建 many2many 關系。 我正在努力實現的目標:
python manage.py create_tenant_user
創建租戶這一切都在文檔中。 您錯過了代碼中最重要的部分,即您的 config settings.py
。 如果您希望 auth 是全局的,那么您需要將 auth 放在共享模式中,如下所示:
設置.py
SHARED_APPS = [
'django_tenants',
'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'apps.client',
'apps.user_app_goes_here',
]
TENANT_APPS = [
'apps.other_tenant_specific_apps',
'more_3rd_party_apps',
]
身份驗證發生在您定義它的地方。 在上面的設置中,我有租戶特定的應用程序,這些應用程序只能由特定租戶訪問,但auth
、 user_model
和client
在共享架構上處理。
你可能會遇到的事情:
如果你這樣做,你將不得不編寫一個自定義中間件,如果你試圖讓所有用戶只能訪問他們自己的模式數據,那么你將不得不編寫一個自定義中間件來將用戶重定向到正確的域(對正確的模式進行操作)。 也就是說,您需要登錄正確的子域才能訪問租戶特定的數據。
您不需要通過 CLI 創建租戶。 這也可以通過加載要寫入的模式來完成。 這也都在文檔中。
對於這個包,您可能不會在 SO 上找到大量幫助 - 至少這是我使用它的歷史。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.