diff --git a/CHANGELOG.md b/CHANGELOG.md index fa2ead1..e02d0ca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ # Version history We follow [Semantic Versions](https://semver.org/) starting at the `0.4.0` release. +## 2.0.1 (2024-11-19) + +### Features +* Added support for UserModels where the primary key field isnt `id` + ## 2.0.0 (2024-09-09) Breaking changes diff --git a/django_test_app/users/migrations/0001_initial.py b/django_test_app/users/migrations/0001_initial.py index e4094b0..fa24575 100644 --- a/django_test_app/users/migrations/0001_initial.py +++ b/django_test_app/users/migrations/0001_initial.py @@ -1,3 +1,5 @@ +import uuid + from django.db import migrations, models from tenant_users.permissions.models import PermissionsMixinFacade @@ -80,4 +82,66 @@ class Migration(migrations.Migration): PermissionsMixinFacade, ), ), + migrations.CreateModel( + name="GuidUser", + fields=[ + ( + "guid", + models.UUIDField(default=uuid.uuid4, primary_key=True, serialize=False) + ), + ( + "password", + models.CharField( + max_length=128, + verbose_name="password", + ), + ), + ( + "last_login", + models.DateTimeField( + blank=True, + null=True, + verbose_name="last login", + ), + ), + ( + "email", + models.EmailField( + db_index=True, + max_length=254, + unique=True, + verbose_name="Email Address", + ), + ), + ( + "is_active", + models.BooleanField( + default=True, + verbose_name="active", + ), + ), + ( + "is_verified", + models.BooleanField(default=False, verbose_name="verified"), + ), + ("name", models.CharField(blank=True, max_length=64)), + ( + "tenants", + models.ManyToManyField( + blank=True, + help_text="The tenants this user belongs to.", + related_name="guid_users_set", + to="companies.Company", + verbose_name="tenants", + ), + ), + ], + options={ + "abstract": False, + }, + bases=( + models.Model, + PermissionsMixinFacade, + ), + ), ] diff --git a/django_test_app/users/models.py b/django_test_app/users/models.py index be802fb..0fd815d 100644 --- a/django_test_app/users/models.py +++ b/django_test_app/users/models.py @@ -1,15 +1,23 @@ import uuid from django.db import models from tenant_users.tenants.models import UserProfile - +from django.conf import settings +from django.utils.translation import gettext_lazy as _ _NameFieldLength = 64 -class GuidUser(UserProfile): - guid = models.UUIDField(default=uuid.uuid4, primary_key=True) - - class TenantUser(UserProfile): """Simple user model definition for testing.""" name = models.CharField(max_length=_NameFieldLength, blank=True) + + +class GuidUser(UserProfile): + guid = models.UUIDField(default=uuid.uuid4, primary_key=True) + tenants = models.ManyToManyField( + settings.TENANT_MODEL, + verbose_name=_("tenants"), + blank=True, + help_text=_("The tenants this user belongs to."), + related_name="guid_users_set", + ) diff --git a/tenant_users/tenants/middleware.py b/tenant_users/tenants/middleware.py index ca471b8..35d8997 100644 --- a/tenant_users/tenants/middleware.py +++ b/tenant_users/tenants/middleware.py @@ -58,4 +58,4 @@ def has_tenant_access(self, request: HttpRequest) -> bool: if not request.user.is_authenticated: return True - return request.user.tenants.filter(id=request.tenant.id).exists() + return request.user.tenants.filter(pk=request.tenant.pk).exists() diff --git a/tests/test_tenants/test_tenants_models.py b/tests/test_tenants/test_tenants_models.py index c734047..3e99e25 100644 --- a/tests/test_tenants/test_tenants_models.py +++ b/tests/test_tenants/test_tenants_models.py @@ -20,19 +20,14 @@ UserModel = get_user_model() -def test_add_guid_based_user_to_tenant(guid_tenant_user) -> None: +def test_add_guid_based_user_to_tenant(test_tenants, guid_tenant_user) -> None: """Test tenant user management with guid based user""" - slug = "sample" - custom_schema_name = "guid_schema" - tenant, _ = provision_tenant( - tenant_name="GUID Tenant", - tenant_slug=slug, - owner=guid_tenant_user, - schema_name=custom_schema_name - ) - owner = tenant.owner - tenant.add_user(owner) + tenant = test_tenants.first() + # Just the owner assert tenant.user_set.count() == 1 + tenant.add_user(guid_tenant_user) + # Owner + My test user + assert tenant.user_set.count() == 2 def test_provision_with_schema_name(tenant_user) -> None: