-
Notifications
You must be signed in to change notification settings - Fork 2k
feat: Login/logout activity logging #40739
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
… during login/logout. Exclude impersonated login/logouts from being shown publicly.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
9 files reviewed, 2 comments
| def apply_activity_visibility_restrictions(queryset: QuerySet, user: "User") -> QuerySet: | ||
| """ | ||
| Apply visibility restrictions to activity log queryset based on user permissions. | ||
| """ | ||
| exclusion_queries = [] | ||
| for scope, restrictions in activity_visibility_restrictions.items(): | ||
| if restrictions.get("requires_staff"): | ||
| activities = restrictions.get("activities", []) | ||
| exclude_conditions = restrictions.get("exclude_when", {}) | ||
|
|
||
| # Build the query: scope AND activity IN [...] AND exclude_conditions | ||
| query = Q(scope=scope) & Q(activity__in=activities) | ||
| for field, value in exclude_conditions.items(): | ||
| query &= Q(**{field: value}) | ||
|
|
||
| exclusion_queries.append(query) | ||
|
|
||
| # Apply all exclusions with OR logic | ||
| if exclusion_queries: | ||
| combined_exclusion = exclusion_queries[0] | ||
| for q in exclusion_queries[1:]: | ||
| combined_exclusion |= q | ||
| queryset = queryset.exclude(combined_exclusion) | ||
|
|
||
| return queryset |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
logic: never checks if user.is_staff is True before excluding the activities
the logic excludes impersonated logins/logouts for ALL users, not just non-staff users. staff users should see these activities but currently cannot
| def apply_activity_visibility_restrictions(queryset: QuerySet, user: "User") -> QuerySet: | |
| """ | |
| Apply visibility restrictions to activity log queryset based on user permissions. | |
| """ | |
| exclusion_queries = [] | |
| for scope, restrictions in activity_visibility_restrictions.items(): | |
| if restrictions.get("requires_staff"): | |
| activities = restrictions.get("activities", []) | |
| exclude_conditions = restrictions.get("exclude_when", {}) | |
| # Build the query: scope AND activity IN [...] AND exclude_conditions | |
| query = Q(scope=scope) & Q(activity__in=activities) | |
| for field, value in exclude_conditions.items(): | |
| query &= Q(**{field: value}) | |
| exclusion_queries.append(query) | |
| # Apply all exclusions with OR logic | |
| if exclusion_queries: | |
| combined_exclusion = exclusion_queries[0] | |
| for q in exclusion_queries[1:]: | |
| combined_exclusion |= q | |
| queryset = queryset.exclude(combined_exclusion) | |
| return queryset | |
| def apply_activity_visibility_restrictions(queryset: QuerySet, user: "User") -> QuerySet: | |
| """ | |
| Apply visibility restrictions to activity log queryset based on user permissions. | |
| """ | |
| exclusion_queries = [] | |
| for scope, restrictions in activity_visibility_restrictions.items(): | |
| if restrictions.get("requires_staff") and not user.is_staff: | |
| activities = restrictions.get("activities", []) | |
| exclude_conditions = restrictions.get("exclude_when", {}) | |
| # Build the query: scope AND activity IN [...] AND exclude_conditions | |
| query = Q(scope=scope) & Q(activity__in=activities) | |
| for field, value in exclude_conditions.items(): | |
| query &= Q(**{field: value}) | |
| exclusion_queries.append(query) | |
| # Apply all exclusions with OR logic | |
| if exclusion_queries: | |
| combined_exclusion = exclusion_queries[0] | |
| for q in exclusion_queries[1:]: | |
| combined_exclusion |= q | |
| queryset = queryset.exclude(combined_exclusion) | |
| return queryset |
Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/models/activity_logging/activity_log.py
Line: 752:776
Comment:
**logic:** never checks if `user.is_staff` is `True` before excluding the activities
the logic excludes impersonated logins/logouts for ALL users, not just non-staff users. staff users should see these activities but currently cannot
```suggestion
def apply_activity_visibility_restrictions(queryset: QuerySet, user: "User") -> QuerySet:
"""
Apply visibility restrictions to activity log queryset based on user permissions.
"""
exclusion_queries = []
for scope, restrictions in activity_visibility_restrictions.items():
if restrictions.get("requires_staff") and not user.is_staff:
activities = restrictions.get("activities", [])
exclude_conditions = restrictions.get("exclude_when", {})
# Build the query: scope AND activity IN [...] AND exclude_conditions
query = Q(scope=scope) & Q(activity__in=activities)
for field, value in exclude_conditions.items():
query &= Q(**{field: value})
exclusion_queries.append(query)
# Apply all exclusions with OR logic
if exclusion_queries:
combined_exclusion = exclusion_queries[0]
for q in exclusion_queries[1:]:
combined_exclusion |= q
queryset = queryset.exclude(combined_exclusion)
return queryset
```
How can I resolve this? If you propose a fix, please make it concise.| else: | ||
| login_method = "sso" | ||
| else: | ||
| if user.social_auth.exists(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: triggers a database query on every login even if social auth is not being used
consider checking session backend first before querying user.social_auth.exists()
Prompt To Fix With AI
This is a comment left during a code review.
Path: posthog/models/activity_logging/signal_handlers.py
Line: 124:124
Comment:
**style:** triggers a database query on every login even if social auth is not being used
consider checking session backend first before querying `user.social_auth.exists()`
How can I resolve this? If you propose a fix, please make it concise.|
Size Change: 0 B Total Size: 3.34 MB ℹ️ View Unchanged
|
📸 UI snapshots have been updated2 snapshot changes in total. 0 added, 2 modified, 0 deleted:
Triggered by this commit. |
📸 UI snapshots have been updated2 snapshot changes in total. 0 added, 2 modified, 0 deleted:
Triggered by this commit. |
Add login/logout activity logging. Add proper impersonation detection during login/logout. Exclude impersonated login/logouts from being shown publicly.
Problem
Changes
Changelog: (features only) Is this feature complete?