-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Permissions attribute for targets, filtering logic and migrations
Adds a permissions field on the Target that allows for filtering prior to doing row-level permissions for open/public targets. This significantly improves performance for TOMs with large datasets that are mostly open or public. Included migrations should handle the transition transparently. Todo: [] Extend filtering logic beyond the TargetList view. [] Update target creation logic away from the Public group. [] Apply logic to non-target models? [] Tests. [] Clean up any remaining 'Public' group logic. [] Update documentation.
- Loading branch information
Showing
4 changed files
with
104 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 4.2.18 on 2025-02-06 18:39 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('tom_targets', '0023_alter_basetarget_created'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='basetarget', | ||
name='permissions', | ||
field=models.CharField(choices=[('OPEN', 'Open'), ('PUBLIC', 'Public'), ('PRIVATE', 'Private')], default='PUBLIC', help_text='The acess level of this target, see the docs on public vs private targets.', max_length=100), | ||
), | ||
] |
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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Generated by Django 4.2.18 on 2025-02-06 20:17 | ||
|
||
from django.db import migrations | ||
from django.conf import settings | ||
|
||
def get_target_model(): | ||
try: | ||
custom_class = settings.TARGET_MODEL_CLASS | ||
return custom_class.split('.')[0], custom_class.split('.')[-1] | ||
except AttributeError: | ||
return 'tom_targets', 'BaseTarget' | ||
|
||
def remove_public_group(apps, schema_editor): | ||
target_app, target_model = get_target_model() | ||
Group = apps.get_model('auth', 'Group') | ||
Target = apps.get_model(target_app, target_model) | ||
UserObjectPermission = apps.get_model('guardian', 'UserObjectPermission') | ||
GroupObjectPermission = apps.get_model('guardian', 'GroupObjectPermission') | ||
|
||
group = Group.objects.get(name='Public') | ||
|
||
# Delete Target permissions for public group | ||
GroupObjectPermission.objects.filter(group=group, content_type__model=target_model.lower()).delete() | ||
|
||
# Any remaining permissions means target should be private | ||
private_group_permissions = GroupObjectPermission.objects.filter( | ||
content_type__model=target_model.lower() | ||
) | ||
private_user_permissions = UserObjectPermission.objects.filter( | ||
content_type__model=target_model.lower() | ||
) | ||
|
||
# get a list of target ids that still have permissions | ||
target_ids = set( | ||
list(private_group_permissions.values_list('object_pk', flat=True)) \ | ||
+ list(private_user_permissions.values_list('object_pk', flat=True)) | ||
) | ||
|
||
# Update targets to private | ||
Target.objects.filter(pk__in=target_ids).update(permissions='PRIVATE') | ||
|
||
# Delete public group | ||
group.delete() | ||
|
||
def set_all_to_public(apps, schema_editor): | ||
target_app, target_model = get_target_model() | ||
Target = apps.get_model(target_app, target_model) | ||
Target.objects.update(permissions='PUBLIC') | ||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('tom_targets', '0024_basetarget_permissions'), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(remove_public_group, set_all_to_public), | ||
] |
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