-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- grant permission to publish events to editor - revoke permission to view feedback and imprint for editor and events manager roles
- Loading branch information
1 parent
e1618e8
commit 1776dd2
Showing
4 changed files
with
105 additions
and
7 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
98 changes: 98 additions & 0 deletions
98
integreat_cms/cms/migrations/0007_change_role_permissions.py
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,98 @@ | ||
# Generated by Django 3.2.12 on 2022-02-21 13:05 | ||
from django.db import migrations | ||
|
||
|
||
ROLES = [ | ||
{ | ||
"name": "EDITOR", | ||
"add_permissions": [ | ||
"publish_event", | ||
], | ||
"remove_permissions": [ | ||
"change_feedback", | ||
"change_imprintpage", | ||
"view_feedback", | ||
"view_imprintpage", | ||
], | ||
}, | ||
{ | ||
"name": "EVENT_MANAGER", | ||
"add_permissions": [], | ||
"remove_permissions": [ | ||
"change_feedback", | ||
"view_feedback", | ||
"view_imprintpage", | ||
], | ||
}, | ||
] | ||
|
||
# pylint: disable=unused-argument | ||
def update_roles(apps, schema_editor): | ||
""" | ||
Update the permissions of roles | ||
:param apps: The configuration of installed applications | ||
:type apps: ~django.apps.registry.Apps | ||
:param schema_editor: The database abstraction layer that creates actual SQL code | ||
:type schema_editor: ~django.db.backends.base.schema.BaseDatabaseSchemaEditor | ||
""" | ||
# We can't import the Person model directly as it may be a newer | ||
# version than this migration expects. We use the historical version. | ||
Group = apps.get_model("auth", "Group") | ||
Permission = apps.get_model("auth", "Permission") | ||
|
||
for role_conf in ROLES: | ||
group = Group.objects.get(name=role_conf.get("name")) | ||
add_permissions = Permission.objects.filter( | ||
codename__in=role_conf.get("add_permissions") | ||
) | ||
group.permissions.add(*add_permissions) | ||
remove_permissions = Permission.objects.filter( | ||
codename__in=role_conf.get("remove_permissions") | ||
) | ||
group.permissions.remove(*remove_permissions) | ||
|
||
|
||
# pylint: disable=unused-argument | ||
def revert_roles(apps, schema_editor): | ||
""" | ||
Revert the permission changes of this migration | ||
:param apps: The configuration of installed applications | ||
:type apps: ~django.apps.registry.Apps | ||
:param schema_editor: The database abstraction layer that creates actual SQL code | ||
:type schema_editor: ~django.db.backends.base.schema.BaseDatabaseSchemaEditor | ||
""" | ||
# We can't import the Person model directly as it may be a newer | ||
# version than this migration expects. We use the historical version. | ||
Group = apps.get_model("auth", "Group") | ||
Permission = apps.get_model("auth", "Permission") | ||
|
||
for role_conf in ROLES: | ||
group = Group.objects.get(name=role_conf.get("name")) | ||
# The permissions that were added with this migration need to be removed | ||
add_permissions = Permission.objects.filter( | ||
codename__in=role_conf.get("add_permissions") | ||
) | ||
group.permissions.remove(*add_permissions) | ||
# The migrations that were removed with this migration need to be added again | ||
remove_permissions = Permission.objects.filter( | ||
codename__in=role_conf.get("remove_permissions") | ||
) | ||
group.permissions.add(*remove_permissions) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
""" | ||
Migration file to update permissions of roles | ||
""" | ||
|
||
dependencies = [ | ||
("cms", "0006_region_custom_prefix"), | ||
] | ||
|
||
operations = [ | ||
migrations.RunPython(update_roles, revert_roles), | ||
] |
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