do_set_realm_authentication_methods: Use transaction.atomic.

This commit is contained in:
Mateusz Mandera 2021-12-11 14:31:02 +01:00 committed by Tim Abbott
parent 21ab5e3a55
commit dc9aac9253
1 changed files with 20 additions and 18 deletions

View File

@ -873,24 +873,26 @@ def do_set_realm_authentication_methods(
realm: Realm, authentication_methods: Dict[str, bool], *, acting_user: Optional[UserProfile] realm: Realm, authentication_methods: Dict[str, bool], *, acting_user: Optional[UserProfile]
) -> None: ) -> None:
old_value = realm.authentication_methods_dict() old_value = realm.authentication_methods_dict()
for key, value in list(authentication_methods.items()): with transaction.atomic():
index = getattr(realm.authentication_methods, key).number for key, value in list(authentication_methods.items()):
realm.authentication_methods.set_bit(index, int(value)) index = getattr(realm.authentication_methods, key).number
realm.save(update_fields=["authentication_methods"]) realm.authentication_methods.set_bit(index, int(value))
updated_value = realm.authentication_methods_dict() realm.save(update_fields=["authentication_methods"])
RealmAuditLog.objects.create( updated_value = realm.authentication_methods_dict()
realm=realm, RealmAuditLog.objects.create(
event_type=RealmAuditLog.REALM_PROPERTY_CHANGED, realm=realm,
event_time=timezone_now(), event_type=RealmAuditLog.REALM_PROPERTY_CHANGED,
acting_user=acting_user, event_time=timezone_now(),
extra_data=orjson.dumps( acting_user=acting_user,
{ extra_data=orjson.dumps(
RealmAuditLog.OLD_VALUE: old_value, {
RealmAuditLog.NEW_VALUE: updated_value, RealmAuditLog.OLD_VALUE: old_value,
"property": "authentication_methods", RealmAuditLog.NEW_VALUE: updated_value,
} "property": "authentication_methods",
).decode(), }
) ).decode(),
)
event = dict( event = dict(
type="realm", type="realm",
op="update_dict", op="update_dict",