TOMToolkit / tom_base

The base Django project for a Target and Observation Manager
https://tom-toolkit.readthedocs.io
GNU General Public License v3.0
26 stars 47 forks source link

Permissions issues for recent Target migration #997

Closed jchate6 closed 3 months ago

jchate6 commented 4 months ago

SNex2 experienced multiple sets of permissions being created for view_target after the migration to 2.19 This caused several pages to crash as they got multiple conflicting permissions. We need to run some tests to see what circumstances cause these conflicting basetarget and target permissions to be stored in the DB.

This was caused by pointing to the same DB from multiple versions of the Tom Toolkit

There are, however some actual concerns about how the extended Target Model interacts with Permissions: Once Target has been renamed to BaseTarget we seem to then have 2 sets of permissions for view_targets. This does not seem to cause any problems, but could cause confusion in the admin interface.

Also, If an extended user Target is actually made, then the old Targets that are upgraded through the management command would not be automatically connected to the users and groups they had before. Permissions need to be manually updated during this process.

jchate6 commented 3 months ago

See these threads in #snex2 slack channel: https://lcogt.slack.com/archives/CFJ0D9PNH/p1722467894595529 https://lcogt.slack.com/archives/CFJ0D9PNH/p1722467180327669

jchate6 commented 3 months ago

A snippit for checking content Types and Permissions

from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from tom_targets.models import Target

tct = ContentType.objects.get_for_model(Target)
pt_target = Permission.objects.filter(content_type=tct)
for pt in pt_target:
    print(pt.codename,"|", pt.name)
jchate6 commented 3 months ago

Update target permissions if the user has extended a target model.

from tom_targets.models import Target
from guardian.models import GroupObjectPermission, UserObjectPermission

for target in Target.objects.all():
    group_set = set([gop.group for gop in GroupObjectPermission.objects.filter(object_pk=target.pk)])
    for group in group_set:
        target.give_user_access(group)
    user_set = set([uop.user for uop in UserObjectPermission.objects.filter(object_pk=target.pk)])
    for user in user_set:
        target.give_user_access(user)