2022-04-14 23:57:26 +02:00
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import orjson
|
|
|
|
from django.db import transaction
|
|
|
|
from django.utils.timezone import now as timezone_now
|
|
|
|
|
|
|
|
from zerver.actions.realm_settings import do_set_realm_property
|
|
|
|
from zerver.models import (
|
|
|
|
Realm,
|
|
|
|
RealmAuditLog,
|
|
|
|
RealmDomain,
|
2022-06-23 20:04:16 +02:00
|
|
|
RealmDomainDict,
|
2022-04-14 23:57:26 +02:00
|
|
|
UserProfile,
|
|
|
|
active_user_ids,
|
|
|
|
get_realm_domains,
|
|
|
|
)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
from zerver.tornado.django_api import send_event_on_commit
|
2022-04-14 23:57:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic(durable=True)
|
|
|
|
def do_add_realm_domain(
|
|
|
|
realm: Realm, domain: str, allow_subdomains: bool, *, acting_user: Optional[UserProfile]
|
2023-02-02 04:35:24 +01:00
|
|
|
) -> RealmDomain:
|
2022-04-14 23:57:26 +02:00
|
|
|
realm_domain = RealmDomain.objects.create(
|
|
|
|
realm=realm, domain=domain, allow_subdomains=allow_subdomains
|
|
|
|
)
|
2022-06-23 20:04:16 +02:00
|
|
|
added_domain = RealmDomainDict(domain=domain, allow_subdomains=allow_subdomains)
|
2022-04-14 23:57:26 +02:00
|
|
|
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_type=RealmAuditLog.REALM_DOMAIN_ADDED,
|
|
|
|
event_time=timezone_now(),
|
|
|
|
extra_data=orjson.dumps(
|
|
|
|
{
|
|
|
|
"realm_domains": get_realm_domains(realm),
|
2022-06-23 20:04:16 +02:00
|
|
|
"added_domain": added_domain,
|
2022-04-14 23:57:26 +02:00
|
|
|
}
|
|
|
|
).decode(),
|
|
|
|
)
|
|
|
|
|
|
|
|
event = dict(
|
|
|
|
type="realm_domains",
|
|
|
|
op="add",
|
2022-06-23 20:04:16 +02:00
|
|
|
realm_domain=RealmDomainDict(
|
2022-04-14 23:57:26 +02:00
|
|
|
domain=realm_domain.domain, allow_subdomains=realm_domain.allow_subdomains
|
|
|
|
),
|
|
|
|
)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(realm, event, active_user_ids(realm.id))
|
2022-04-14 23:57:26 +02:00
|
|
|
|
|
|
|
return realm_domain
|
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic(durable=True)
|
|
|
|
def do_change_realm_domain(
|
|
|
|
realm_domain: RealmDomain, allow_subdomains: bool, *, acting_user: Optional[UserProfile]
|
|
|
|
) -> None:
|
|
|
|
realm_domain.allow_subdomains = allow_subdomains
|
|
|
|
realm_domain.save(update_fields=["allow_subdomains"])
|
2022-06-23 20:04:16 +02:00
|
|
|
changed_domain = RealmDomainDict(
|
|
|
|
domain=realm_domain.domain,
|
|
|
|
allow_subdomains=realm_domain.allow_subdomains,
|
|
|
|
)
|
2022-04-14 23:57:26 +02:00
|
|
|
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm_domain.realm,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_type=RealmAuditLog.REALM_DOMAIN_CHANGED,
|
|
|
|
event_time=timezone_now(),
|
|
|
|
extra_data=orjson.dumps(
|
|
|
|
{
|
|
|
|
"realm_domains": get_realm_domains(realm_domain.realm),
|
2022-06-23 20:04:16 +02:00
|
|
|
"changed_domain": changed_domain,
|
2022-04-14 23:57:26 +02:00
|
|
|
}
|
|
|
|
).decode(),
|
|
|
|
)
|
|
|
|
|
|
|
|
event = dict(
|
|
|
|
type="realm_domains",
|
|
|
|
op="change",
|
|
|
|
realm_domain=dict(
|
|
|
|
domain=realm_domain.domain, allow_subdomains=realm_domain.allow_subdomains
|
|
|
|
),
|
|
|
|
)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(realm_domain.realm, event, active_user_ids(realm_domain.realm_id))
|
2022-04-14 23:57:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
@transaction.atomic(durable=True)
|
|
|
|
def do_remove_realm_domain(
|
|
|
|
realm_domain: RealmDomain, *, acting_user: Optional[UserProfile]
|
|
|
|
) -> None:
|
|
|
|
realm = realm_domain.realm
|
|
|
|
domain = realm_domain.domain
|
|
|
|
realm_domain.delete()
|
2022-06-23 20:04:16 +02:00
|
|
|
removed_domain = RealmDomainDict(
|
|
|
|
domain=realm_domain.domain,
|
|
|
|
allow_subdomains=realm_domain.allow_subdomains,
|
|
|
|
)
|
2022-04-14 23:57:26 +02:00
|
|
|
|
|
|
|
RealmAuditLog.objects.create(
|
|
|
|
realm=realm,
|
|
|
|
acting_user=acting_user,
|
|
|
|
event_type=RealmAuditLog.REALM_DOMAIN_REMOVED,
|
|
|
|
event_time=timezone_now(),
|
|
|
|
extra_data=orjson.dumps(
|
|
|
|
{
|
|
|
|
"realm_domains": get_realm_domains(realm),
|
2022-06-23 20:04:16 +02:00
|
|
|
"removed_domain": removed_domain,
|
2022-04-14 23:57:26 +02:00
|
|
|
}
|
|
|
|
).decode(),
|
|
|
|
)
|
|
|
|
|
|
|
|
if RealmDomain.objects.filter(realm=realm).count() == 0 and realm.emails_restricted_to_domains:
|
|
|
|
# If this was the last realm domain, we mark the realm as no
|
|
|
|
# longer restricted to domain, because the feature doesn't do
|
|
|
|
# anything if there are no domains, and this is probably less
|
|
|
|
# confusing than the alternative.
|
|
|
|
do_set_realm_property(realm, "emails_restricted_to_domains", False, acting_user=acting_user)
|
|
|
|
event = dict(type="realm_domains", op="remove", domain=domain)
|
django_api: Extract send_event_on_commit helper.
django-stubs 4.2.1 gives transaction.on_commit a more accurate type
annotation, but this exposed that mypy can’t handle the lambda default
parameters that we use to recapture loop variables such as
for stream_id in public_stream_ids:
peer_user_ids = …
event = …
transaction.on_commit(
lambda event=event, peer_user_ids=peer_user_ids: send_event(
realm, event, peer_user_ids
)
)
https://github.com/python/mypy/issues/15459
A workaround that mypy accepts is
transaction.on_commit(
(
lambda event, peer_user_ids: lambda: send_event(
realm, event, peer_user_ids
)
)(event, peer_user_ids)
)
But that’s kind of ugly and potentially error-prone, so let’s make a
helper function for this very common pattern.
send_event_on_commit(realm, event, peer_user_ids)
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2023-06-17 20:53:07 +02:00
|
|
|
send_event_on_commit(realm, event, active_user_ids(realm.id))
|