Skip to content

Commit

Permalink
Update permissions of roles
Browse files Browse the repository at this point in the history
- grant permission to publish events to editor
- revoke permission to view feedback and imprint for editor and events manager roles
  • Loading branch information
timobrembeck committed Feb 21, 2022
1 parent e1618e8 commit 1776dd2
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 7 deletions.
4 changes: 2 additions & 2 deletions integreat_cms/cms/migrations/0002_roles.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def add_roles(apps, schema_editor):
"""
Add the default roles for users
:param apps:
: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
Expand Down Expand Up @@ -266,7 +266,7 @@ def remove_roles(apps, schema_editor):
"""
Remove the default roles for users
:param apps: The
: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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def add_roles(apps, schema_editor):
"""
Add the default roles for users
:param apps:
: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
Expand All @@ -28,7 +28,7 @@ def remove_roles(apps, schema_editor):
"""
Remove the default roles for users
:param apps: The
: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
Expand Down
98 changes: 98 additions & 0 deletions integreat_cms/cms/migrations/0007_change_role_permissions.py
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),
]
6 changes: 3 additions & 3 deletions tests/cms/views/view_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
PRIV_STAFF_ROLES + [MANAGEMENT],
{"username": "new_username", "email": "new@email.address", "role": 1},
),
("region_feedback", ROLES),
("region_feedback", STAFF_ROLES + [MANAGEMENT]),
("region_users", STAFF_ROLES + [MANAGEMENT]),
("translation_coverage", ROLES),
("user_settings", ROLES),
Expand Down Expand Up @@ -320,10 +320,10 @@
("sitemap:region_language", ALL_ROLES),
("archived_pages", STAFF_ROLES + [MANAGEMENT, EDITOR]),
("archived_pois", ROLES),
("edit_imprint", ROLES),
("edit_imprint", STAFF_ROLES + [MANAGEMENT]),
(
"edit_imprint",
PRIV_STAFF_ROLES + [MANAGEMENT, EDITOR],
PRIV_STAFF_ROLES + [MANAGEMENT],
{"title": "imprint", "submit_draft": True},
),
("events", ROLES),
Expand Down

0 comments on commit 1776dd2

Please sign in to comment.