Skip to content

Commit

Permalink
* Temporary migration. This will be removed in the next commit.
Browse files Browse the repository at this point in the history
  • Loading branch information
bgroff committed Oct 2, 2018
1 parent 8e9b7c6 commit 654d7a6
Showing 1 changed file with 42 additions and 35 deletions.
77 changes: 42 additions & 35 deletions django_kala/auth/migrations/0003_auto_20181002_0015.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,64 @@
# Generated by Django 2.0.8 on 2018-10-02 00:15
from django.contrib.auth import get_user_model
from django.contrib.auth.models import Permission
from django.contrib.contenttypes.models import ContentType
from django.db import migrations
from django.contrib.auth.management import create_permissions

from auth.models import Permissions

User = get_user_model()


def create_permission(content_type, name):
def create_permission(Permission, content_type, name):
permissions = []
permissions.append(
{
"update": Permission.objects.create(name='Can manage {0}'.format(name), content_type=content_type, codename='can_manage'),
"existing": Permission.objects.get(content_type=content_type, codename='delete_{0}'.format(name[:-1]))
}
)
permissions.append(
{
"update": Permission.objects.create(name='Can invite users to {0}'.format(name), content_type=content_type, codename='can_invite'),
"existing": Permission.objects.get(content_type=content_type, codename='change_{0}'.format(name[:-1]))
}
)
permissions.append(
{
"update": Permission.objects.create(name='Can create {0}'.format(name), content_type=content_type, codename='can_create'),
"existing": Permission.objects.get(content_type=content_type, codename='add_{0}'.format(name[:-1]))
}
)
try:
permissions.append(
{
"update": Permission.objects.get_or_create(name='Can manage {0}'.format(name), content_type=content_type, codename='can_manage')[0],
"existing": Permission.objects.get(content_type=content_type, codename='delete_{0}'.format(name[:-1]))
}
)
permissions.append(
{
"update": Permission.objects.get_or_create(name='Can invite users to {0}'.format(name), content_type=content_type, codename='can_invite')[0],
"existing": Permission.objects.get(content_type=content_type, codename='change_{0}'.format(name[:-1]))
}
)
permissions.append(
{
"update": Permission.objects.get_or_create(name='Can create {0}'.format(name), content_type=content_type, codename='can_create')[0],
"existing": Permission.objects.get(content_type=content_type, codename='add_{0}'.format(name[:-1]))
}
)
except:
pass # Probably tests
return permissions


def create_permissions(apps, schema_editor):
permissions = create_permission(ContentType.objects.get(app_label='documents', model='document'), 'documents')
update_permissions(permissions)
permissions = create_permission(ContentType.objects.get(app_label='projects', model='project'), 'projects')
update_permissions(permissions)
permissions = create_permission(ContentType.objects.get(app_label='organizations', model='organization'), 'organizations')
update_permissions(permissions)
for app_config in apps.get_app_configs():
app_config.models_module = True
create_permissions(app_config, apps=apps, verbosity=0)
app_config.models_module = None

ContentType = apps.get_model("contenttypes", "ContentType")
Permission = apps.get_model("auth", "Permission")
Permissions = apps.get_model("kala_auth", "Permissions")
User = apps.get_registered_model('kala_auth', 'User')
permissions = create_permission(Permission, ContentType.objects.get(app_label='documents', model='document'), 'documents')
update_permissions(Permissions, permissions)
permissions = create_permission(Permission, ContentType.objects.get(app_label='projects', model='project'), 'projects')
update_permissions(Permissions, permissions)
permissions = create_permission(Permission, ContentType.objects.get(app_label='organizations', model='organization'), 'organizations')
update_permissions(Permissions, permissions)

# The old permission system had each permission specified seperatly, then new system uses roles,
# So we need to go back through all the users can remove what are "extra" privilages
# i.e. if a user had create, update, delete, now they are just a manager, so remove the extra
# creator, invitor roles
fix_permissions()
fix_permissions(User, Permissions)


def update_permissions(permissions):
def update_permissions(Permissions, permissions):
for permission in permissions:
Permissions.objects.filter(permission=permission['existing']).update(permission=permission['update'])


def fix_permissions():
def fix_permissions(User, Permissions):
for user in User.objects.all():
objects = Permissions.objects.filter(user=user).distinct('object_uuid').values_list('object_uuid', flat=True)
for obj in objects:
Expand Down

0 comments on commit 654d7a6

Please sign in to comment.