-
-
Notifications
You must be signed in to change notification settings - Fork 184
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
feat(ProjectHistoryLogs): create ph logs for permissions changes TASK-944 #5297
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8be6200
feat: create ph logs for permissions changes
rgraber 0f70aa4
fixup!: merge artifact
rgraber b5a9bf7
fixup!: fix test
rgraber 824c747
fixup!: typo
rgraber ddfe6b5
Merge branch 'main' into rsgraber/TASK-944-permissions-better
rgraber 70f195f
fixup!: Update test_signals.py
rgraber 2d350fc
fixup!: Update models.py
rgraber 2e1b902
Merge branch 'main' into rsgraber/TASK-944-permissions-better
rgraber cea5bcf
fixup!: comment
rgraber File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
from collections import defaultdict | ||
|
||
from celery.signals import task_success | ||
from django.contrib.auth.signals import user_logged_in | ||
from django.dispatch import receiver | ||
from django_userforeignkey.request import get_current_request | ||
|
||
from kpi.models import ImportTask | ||
from kpi.constants import ASSET_TYPE_SURVEY, PERM_PARTIAL_SUBMISSIONS | ||
from kpi.models import Asset, ImportTask | ||
from kpi.tasks import import_in_background | ||
from kpi.utils.log import logging | ||
|
||
from kpi.utils.object_permission import ( | ||
post_assign_partial_perm, | ||
post_assign_perm, | ||
post_remove_partial_perms, | ||
post_remove_perm, | ||
) | ||
from .models import AccessLog, ProjectHistoryLog | ||
|
||
|
||
|
@@ -24,3 +33,61 @@ def create_access_log(sender, user, **kwargs): | |
def create_ph_log_for_import(sender, result, **kwargs): | ||
task = ImportTask.objects.get(uid=result) | ||
ProjectHistoryLog.create_from_import_task(task) | ||
|
||
|
||
def initialize_permission_lists_if_necessary(request): | ||
if getattr(request, 'permissions_added', None) is None: | ||
request.permissions_added = defaultdict(set) | ||
if getattr(request, 'permissions_removed', None) is None: | ||
request.permissions_removed = defaultdict(set) | ||
if getattr(request, 'partial_permissions_added', None) is None: | ||
request.partial_permissions_added = defaultdict(list) | ||
|
||
|
||
def initialize_request(): | ||
request = get_current_request() | ||
if request is None: | ||
return None | ||
initialize_permission_lists_if_necessary(request) | ||
return request | ||
|
||
|
||
@receiver(post_assign_perm, sender=Asset) | ||
def add_assigned_perms(sender, instance, user, codename, deny, **kwargs): | ||
request = initialize_request() | ||
if not request or instance.asset_type != ASSET_TYPE_SURVEY or deny: | ||
return | ||
request.permissions_added[user.username].add(codename) | ||
request.permissions_removed[user.username].discard(codename) | ||
|
||
|
||
@receiver(post_remove_perm, sender=Asset) | ||
def add_removed_perms(sender, instance, user, codename, **kwargs): | ||
request = initialize_request() | ||
if not request or instance.asset_type != ASSET_TYPE_SURVEY: | ||
return | ||
request.permissions_removed[user.username].add(codename) | ||
request.permissions_added[user.username].discard(codename) | ||
|
||
|
||
@receiver(post_assign_partial_perm, sender=Asset) | ||
def add_assigned_partial_perms(sender, instance, user, perms, **kwargs): | ||
request = initialize_request() | ||
if not request or instance.asset_type != ASSET_TYPE_SURVEY: | ||
return | ||
perms_as_list_of_dicts = [{'code': k, 'filters': v} for k, v in perms.items()] | ||
# partial permissions are replaced rather than added | ||
request.partial_permissions_added[user.username] = perms_as_list_of_dicts | ||
|
||
|
||
@receiver(post_remove_partial_perms, sender=Asset) | ||
def remove_partial_perms(sender, instance, user, **kwargs): | ||
request = initialize_request() | ||
if not request or instance.asset_type != ASSET_TYPE_SURVEY: | ||
return | ||
request.partial_permissions_added[user.username] = [] | ||
# in case we somehow deleted partial permissions | ||
# without deleting 'partial_submissions', take care of that now since | ||
# we can't have one without the other | ||
request.permissions_added[user.username].discard(PERM_PARTIAL_SUBMISSIONS) | ||
request.permissions_removed[user.username].add(PERM_PARTIAL_SUBMISSIONS) | ||
Comment on lines
+55
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A -> Z π There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to alphabetize everything in one big PR after all the functionality is done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
typo ;-)