Skip to content

Commit

Permalink
Log user group membership change events
Browse files Browse the repository at this point in the history
  • Loading branch information
nas-tabchiche committed Feb 19, 2024
1 parent f04fd00 commit a8210c2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
16 changes: 16 additions & 0 deletions backend/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,22 @@ def create(self, validated_data):
)
return user

def update(self, instance: User, validated_data: Any) -> User:
user_groups_data = validated_data.get("user_groups")
if user_groups_data is not None:
initial_groups = set(instance.user_groups.all())
new_groups = set(group for group in user_groups_data)

if initial_groups != new_groups:
logger.info(
"user groups updated",
user=instance,
initial_user_groups=initial_groups,
new_user_groups=new_groups,
)
# instance.user_groups.set(user_groups_data)
return super().update(instance, validated_data)


class UserGroupReadSerializer(BaseModelSerializer):
name = serializers.CharField(source="__str__")
Expand Down
4 changes: 4 additions & 0 deletions backend/iam/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,10 @@ def delete(self, *args, **kwargs):
super().delete(*args, **kwargs)
logger.info("user deleted", user=self)

def save(self, *args, **kwargs):
super().save(*args, **kwargs)
logger.info("user saved", user=self)

def __str__(self):
return (
f"{self.first_name} {self.last_name}"
Expand Down

0 comments on commit a8210c2

Please sign in to comment.