mirror of
https://github.com/zulip/zulip.git
synced 2025-11-11 01:16:19 +00:00
realm: Create RealmAuditLog entry while adding a new allowed domain.
This commit also adds 'acting_user' argument for do_add_realm_domain function. Fixes a part of #21268.
This commit is contained in:
@@ -252,6 +252,7 @@ from zerver.models import (
|
|||||||
get_huddle_user_ids,
|
get_huddle_user_ids,
|
||||||
get_old_unclaimed_attachments,
|
get_old_unclaimed_attachments,
|
||||||
get_realm,
|
get_realm,
|
||||||
|
get_realm_domains,
|
||||||
get_realm_playgrounds,
|
get_realm_playgrounds,
|
||||||
get_stream,
|
get_stream,
|
||||||
get_stream_by_id_in_realm,
|
get_stream_by_id_in_realm,
|
||||||
@@ -8086,10 +8087,26 @@ def do_update_linkifier(realm: Realm, id: int, pattern: str, url_format_string:
|
|||||||
notify_linkifiers(realm)
|
notify_linkifiers(realm)
|
||||||
|
|
||||||
|
|
||||||
def do_add_realm_domain(realm: Realm, domain: str, allow_subdomains: bool) -> (RealmDomain):
|
def do_add_realm_domain(
|
||||||
|
realm: Realm, domain: str, allow_subdomains: bool, *, acting_user: Optional[UserProfile]
|
||||||
|
) -> (RealmDomain):
|
||||||
realm_domain = RealmDomain.objects.create(
|
realm_domain = RealmDomain.objects.create(
|
||||||
realm=realm, domain=domain, allow_subdomains=allow_subdomains
|
realm=realm, domain=domain, allow_subdomains=allow_subdomains
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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),
|
||||||
|
"added_domain": {"domain": domain, "allow_subdomains": allow_subdomains},
|
||||||
|
}
|
||||||
|
).decode(),
|
||||||
|
)
|
||||||
|
|
||||||
event = dict(
|
event = dict(
|
||||||
type="realm_domains",
|
type="realm_domains",
|
||||||
op="add",
|
op="add",
|
||||||
@@ -8098,6 +8115,7 @@ def do_add_realm_domain(realm: Realm, domain: str, allow_subdomains: bool) -> (R
|
|||||||
),
|
),
|
||||||
)
|
)
|
||||||
send_event(realm, event, active_user_ids(realm.id))
|
send_event(realm, event, active_user_ids(realm.id))
|
||||||
|
|
||||||
return realm_domain
|
return realm_domain
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -4062,6 +4062,7 @@ class AbstractRealmAuditLog(models.Model):
|
|||||||
REALM_CREATED = 215
|
REALM_CREATED = 215
|
||||||
REALM_DEFAULT_USER_SETTINGS_CHANGED = 216
|
REALM_DEFAULT_USER_SETTINGS_CHANGED = 216
|
||||||
REALM_ORG_TYPE_CHANGED = 217
|
REALM_ORG_TYPE_CHANGED = 217
|
||||||
|
REALM_DOMAIN_ADDED = 218
|
||||||
|
|
||||||
SUBSCRIPTION_CREATED = 301
|
SUBSCRIPTION_CREATED = 301
|
||||||
SUBSCRIPTION_ACTIVATED = 302
|
SUBSCRIPTION_ACTIVATED = 302
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ from zerver.lib.actions import (
|
|||||||
bulk_add_subscriptions,
|
bulk_add_subscriptions,
|
||||||
bulk_remove_subscriptions,
|
bulk_remove_subscriptions,
|
||||||
do_activate_mirror_dummy_user,
|
do_activate_mirror_dummy_user,
|
||||||
|
do_add_realm_domain,
|
||||||
do_change_avatar_fields,
|
do_change_avatar_fields,
|
||||||
do_change_bot_owner,
|
do_change_bot_owner,
|
||||||
do_change_default_all_public_streams,
|
do_change_default_all_public_streams,
|
||||||
@@ -47,6 +48,7 @@ from zerver.models import (
|
|||||||
Subscription,
|
Subscription,
|
||||||
UserProfile,
|
UserProfile,
|
||||||
get_realm,
|
get_realm,
|
||||||
|
get_realm_domains,
|
||||||
get_stream,
|
get_stream,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -658,3 +660,28 @@ class TestRealmAuditLog(ZulipTestCase):
|
|||||||
1,
|
1,
|
||||||
)
|
)
|
||||||
self.assertEqual(getattr(user, setting), value)
|
self.assertEqual(getattr(user, setting), value)
|
||||||
|
|
||||||
|
def test_realm_domain_entries(self) -> None:
|
||||||
|
user = self.example_user("iago")
|
||||||
|
initial_domains = get_realm_domains(user.realm)
|
||||||
|
|
||||||
|
now = timezone_now()
|
||||||
|
do_add_realm_domain(user.realm, "zulip.org", False, acting_user=user)
|
||||||
|
added_domain: Dict[str, Union[str, bool]] = {
|
||||||
|
"domain": "zulip.org",
|
||||||
|
"allow_subdomains": False,
|
||||||
|
}
|
||||||
|
expected_extra_data = {
|
||||||
|
"realm_domains": initial_domains + [added_domain],
|
||||||
|
"added_domain": added_domain,
|
||||||
|
}
|
||||||
|
self.assertEqual(
|
||||||
|
RealmAuditLog.objects.filter(
|
||||||
|
realm=user.realm,
|
||||||
|
event_type=RealmAuditLog.REALM_DOMAIN_ADDED,
|
||||||
|
event_time__gte=now,
|
||||||
|
acting_user=user,
|
||||||
|
extra_data=orjson.dumps(expected_extra_data).decode(),
|
||||||
|
).count(),
|
||||||
|
1,
|
||||||
|
)
|
||||||
|
|||||||
@@ -1686,7 +1686,9 @@ class NormalActionsTest(BaseAction):
|
|||||||
|
|
||||||
def test_realm_domain_events(self) -> None:
|
def test_realm_domain_events(self) -> None:
|
||||||
events = self.verify_action(
|
events = self.verify_action(
|
||||||
lambda: do_add_realm_domain(self.user_profile.realm, "zulip.org", False)
|
lambda: do_add_realm_domain(
|
||||||
|
self.user_profile.realm, "zulip.org", False, acting_user=None
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
check_realm_domains_add("events[0]", events[0])
|
check_realm_domains_add("events[0]", events[0])
|
||||||
|
|||||||
@@ -1042,7 +1042,7 @@ class MessagePOSTTest(ZulipTestCase):
|
|||||||
|
|
||||||
zephyr_realm = get_realm("zephyr")
|
zephyr_realm = get_realm("zephyr")
|
||||||
self.make_stream("Verona", zephyr_realm)
|
self.make_stream("Verona", zephyr_realm)
|
||||||
do_add_realm_domain(zephyr_realm, "zulip.com", False)
|
do_add_realm_domain(zephyr_realm, "zulip.com", False, acting_user=None)
|
||||||
result = self.api_post(
|
result = self.api_post(
|
||||||
user,
|
user,
|
||||||
"/api/v1/messages",
|
"/api/v1/messages",
|
||||||
|
|||||||
@@ -34,7 +34,9 @@ def create_realm_domain(
|
|||||||
raise JsonableError(
|
raise JsonableError(
|
||||||
_("The domain {domain} is already a part of your organization.").format(domain=domain)
|
_("The domain {domain} is already a part of your organization.").format(domain=domain)
|
||||||
)
|
)
|
||||||
realm_domain = do_add_realm_domain(user_profile.realm, domain, allow_subdomains)
|
realm_domain = do_add_realm_domain(
|
||||||
|
user_profile.realm, domain, allow_subdomains, acting_user=user_profile
|
||||||
|
)
|
||||||
return json_success(request, data={"new_domain": [realm_domain.id, realm_domain.domain]})
|
return json_success(request, data={"new_domain": [realm_domain.id, realm_domain.domain]})
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user