2022-07-27 23:33:49 +02:00
|
|
|
from email.headerregistry import Address
|
2021-10-06 17:13:57 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from django.conf import settings
|
|
|
|
from django.core.exceptions import ValidationError
|
|
|
|
from django.core.validators import validate_email
|
|
|
|
from django.db import migrations
|
2022-05-27 23:33:51 +02:00
|
|
|
from django.db.backends.postgresql.schema import BaseDatabaseSchemaEditor
|
2021-10-06 17:13:57 +02:00
|
|
|
from django.db.migrations.state import StateApps
|
|
|
|
|
|
|
|
|
|
|
|
def host_for_subdomain(subdomain: str) -> str:
|
|
|
|
if subdomain == "":
|
|
|
|
return settings.EXTERNAL_HOST
|
|
|
|
default_host = f"{subdomain}.{settings.EXTERNAL_HOST}"
|
|
|
|
return settings.REALM_HOSTS.get(subdomain, default_host)
|
|
|
|
|
|
|
|
|
|
|
|
def get_fake_email_domain(realm: Any) -> str:
|
|
|
|
"""
|
|
|
|
Taken from zerver.models. Adjusted to work in a migration without changing
|
|
|
|
behavior.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
# Check that realm.host can be used to form valid email addresses.
|
|
|
|
realm_host = host_for_subdomain(realm.string_id)
|
2022-07-27 23:33:49 +02:00
|
|
|
validate_email(Address(username="bot", domain=realm_host).addr_spec)
|
2021-10-06 17:13:57 +02:00
|
|
|
return realm_host
|
|
|
|
except ValidationError:
|
|
|
|
pass
|
|
|
|
|
|
|
|
try:
|
|
|
|
# Check that the fake email domain can be used to form valid email addresses.
|
2022-07-27 23:33:49 +02:00
|
|
|
validate_email(Address(username="bot", domain=settings.FAKE_EMAIL_DOMAIN).addr_spec)
|
2021-10-06 17:13:57 +02:00
|
|
|
except ValidationError:
|
|
|
|
raise Exception(
|
|
|
|
settings.FAKE_EMAIL_DOMAIN + " is not a valid domain. "
|
|
|
|
"Consider setting the FAKE_EMAIL_DOMAIN setting."
|
|
|
|
)
|
|
|
|
|
|
|
|
return settings.FAKE_EMAIL_DOMAIN
|
|
|
|
|
|
|
|
|
2022-05-27 23:33:51 +02:00
|
|
|
def fix_dummy_users(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
|
2021-10-06 17:13:57 +02:00
|
|
|
"""
|
|
|
|
do_delete_users had two bugs:
|
|
|
|
1. Creating the replacement dummy users with active=True
|
|
|
|
2. Creating the replacement dummy users with email domain set to realm.uri,
|
|
|
|
which may not be a valid email domain.
|
|
|
|
Prior commits fixed the bugs, and this migration fixes the pre-existing objects.
|
|
|
|
"""
|
|
|
|
|
|
|
|
UserProfile = apps.get_model("zerver", "UserProfile")
|
|
|
|
Subscription = apps.get_model("zerver", "Subscription")
|
|
|
|
users_to_fix = UserProfile.objects.filter(
|
|
|
|
is_mirror_dummy=True, is_active=True, delivery_email__regex=r"^deleteduser\d+@.+"
|
|
|
|
)
|
|
|
|
|
|
|
|
update_fields = ["is_active"]
|
|
|
|
for user_profile in users_to_fix:
|
|
|
|
user_profile.is_active = False
|
|
|
|
try:
|
|
|
|
validate_email(user_profile.delivery_email)
|
|
|
|
except ValidationError:
|
2022-07-27 23:33:49 +02:00
|
|
|
user_profile.delivery_email = Address(
|
|
|
|
username=f"deleteduser{user_profile.id}",
|
|
|
|
domain=get_fake_email_domain(user_profile.realm),
|
|
|
|
).addr_spec
|
2021-10-06 17:13:57 +02:00
|
|
|
|
|
|
|
update_fields.append("delivery_email")
|
|
|
|
|
|
|
|
UserProfile.objects.bulk_update(users_to_fix, update_fields)
|
|
|
|
# The denormalized is_user_active field needs to be updated too.
|
|
|
|
Subscription.objects.filter(user_profile__in=users_to_fix).update(is_user_active=False)
|
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
("zerver", "0372_realmemoji_unique_realm_emoji_when_false_deactivated"),
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.RunPython(
|
|
|
|
fix_dummy_users,
|
|
|
|
reverse_code=migrations.RunPython.noop,
|
|
|
|
)
|
|
|
|
]
|