2020-05-29 13:42:38 -04:00
|
|
|
from django.db import migrations
|
|
|
|
|
|
|
|
|
|
|
|
ACTIONS = ['view', 'add', 'change', 'delete']
|
|
|
|
|
|
|
|
|
|
|
|
def replicate_permissions(apps, schema_editor):
|
|
|
|
"""
|
|
|
|
Replicate all Permission assignments as ObjectPermissions.
|
|
|
|
"""
|
|
|
|
Permission = apps.get_model('auth', 'Permission')
|
|
|
|
ObjectPermission = apps.get_model('users', 'ObjectPermission')
|
|
|
|
|
|
|
|
# TODO: Optimize this iteration so that ObjectPermissions with identical sets of users and groups
|
|
|
|
# are combined into a single ObjectPermission instance.
|
|
|
|
for perm in Permission.objects.all():
|
2020-05-29 13:47:19 -04:00
|
|
|
if perm.codename.split('_')[0] in ACTIONS:
|
|
|
|
action = perm.codename.split('_')[0]
|
2020-06-02 09:47:31 -04:00
|
|
|
elif perm.codename == 'activate_userkey':
|
|
|
|
action = 'change'
|
2020-06-02 10:50:58 -04:00
|
|
|
elif perm.codename == 'run_script':
|
|
|
|
action = 'run'
|
2020-05-29 13:47:19 -04:00
|
|
|
else:
|
|
|
|
action = perm.codename
|
2020-05-29 13:42:38 -04:00
|
|
|
|
|
|
|
if perm.group_set.exists() or perm.user_set.exists():
|
|
|
|
obj_perm = ObjectPermission(actions=[action])
|
|
|
|
obj_perm.save()
|
2020-06-03 09:27:20 -04:00
|
|
|
obj_perm.object_types.add(perm.content_type)
|
2020-05-29 13:42:38 -04:00
|
|
|
if perm.group_set.exists():
|
|
|
|
obj_perm.groups.add(*list(perm.group_set.all()))
|
|
|
|
if perm.user_set.exists():
|
|
|
|
obj_perm.users.add(*list(perm.user_set.all()))
|
|
|
|
|
|
|
|
|
|
|
|
class Migration(migrations.Migration):
|
|
|
|
|
|
|
|
dependencies = [
|
|
|
|
('users', '0008_objectpermission'),
|
|
|
|
]
|
|
|
|
|
|
|
|
operations = [
|
|
|
|
migrations.RunPython(
|
|
|
|
code=replicate_permissions,
|
|
|
|
reverse_code=migrations.RunPython.noop
|
|
|
|
)
|
|
|
|
]
|