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

Improve data consistency and prevent orphaned records in user/tenant management #732

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion tenant_users/tenants/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.conf import settings
from django.contrib.auth import get_user_model
from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.db import connection, models
from django.db import connection, models, transaction
from django.dispatch import Signal
from django.utils.translation import gettext_lazy as _
from django_tenants.models import TenantMixin
Expand Down Expand Up @@ -98,6 +98,7 @@ def delete(self, *args, force_drop: bool = False, **kwargs):
raise DeleteError(TENANT_DELETE_ERROR_MESSAGE)

@schema_required
@transaction.atomic
def add_user(self, user_obj, *, is_superuser: bool = False, is_staff: bool = False):
"""Add user to tenant.

Expand Down Expand Up @@ -129,6 +130,7 @@ def add_user(self, user_obj, *, is_superuser: bool = False, is_staff: bool = Fal
)

@schema_required
@transaction.atomic
def remove_user(self, user_obj):
"""Remove user from tenant."""
# Test that user is already in the tenant
Expand Down Expand Up @@ -161,6 +163,7 @@ def remove_user(self, user_obj):
tenant=self,
)

@transaction.atomic
def delete_tenant(self):
"""Mark tenant for deletion.

Expand Down Expand Up @@ -204,6 +207,7 @@ def delete_tenant(self):
self.remove_user(old_owner)

@schema_required
@transaction.atomic
def transfer_ownership(self, new_owner):
old_owner = self.owner

Expand Down Expand Up @@ -236,6 +240,7 @@ class Meta:


class UserProfileManager(BaseUserManager):
@transaction.atomic
def _create_user(
self,
email,
Expand Down Expand Up @@ -332,6 +337,7 @@ def create_superuser(self, password, email=None, **extra_fields):
**extra_fields,
)

@transaction.atomic
def delete_user(self, user_obj):
# Check to make sure we don't try to delete the public tenant owner
# that would be bad....
Expand Down
2 changes: 1 addition & 1 deletion tenant_users/tenants/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
DomainModel = get_tenant_domain_model()


@transaction.atomic()
@transaction.atomic
def provision_tenant( # noqa: PLR0913
tenant_name: str,
tenant_slug: str,
Expand Down
3 changes: 2 additions & 1 deletion tenant_users/tenants/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from typing import Optional

from django.contrib.auth import get_user_model
from django.db import connection
from django.db import connection, transaction
from django_tenants.utils import (
get_multi_type_database_field_name,
get_public_schema_name,
Expand Down Expand Up @@ -32,6 +32,7 @@ def get_current_tenant():
return tenant


@transaction.atomic
def create_public_tenant(
domain_url,
owner_email,
Expand Down
Loading