-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[IMPROVEMENT] Implement Django permissions for Rule editor #96
To avoid issues with common rules accidentally being disabled, require rule_change permissions and _change permission on the content_object when editing or deleting rules. This also sets the foundation for enforcing permissions for more objects in the future * Adds a Default group controlled by PROMGEN_DEFAULT_GROUP * Migration adds all existing users to Default group * post_save signal to add newly created users to Default group
- Loading branch information
Showing
6 changed files
with
200 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
# Generated by Django 2.0.7 on 2018-07-31 08:09 | ||
|
||
from django.db import migrations | ||
from django.conf import settings | ||
|
||
|
||
def create_group(apps, schema_editor): | ||
if not settings.PROMGEN_DEFAULT_GROUP: | ||
return | ||
|
||
# Create Default Group | ||
group, created = apps.get_model('auth', 'Group').objects.get_or_create( | ||
name=settings.PROMGEN_DEFAULT_GROUP | ||
) | ||
|
||
# Create default permissions. We skip the permissions that are | ||
# generally for admin rules (Shards, Prometheus, Audit) and skip | ||
# custom permissions for label/annotations but list everything else | ||
Permission = apps.get_model('auth', 'Permission') | ||
group.permissions.set( | ||
Permission.objects.filter( | ||
content_type__app_label='promgen', | ||
content_type__model__in=[ | ||
'exporter', | ||
'farm', | ||
'host', | ||
'project', | ||
'rule', | ||
'sender', | ||
'service', | ||
'url', | ||
], | ||
) | ||
) | ||
|
||
# Add users to default group | ||
User = apps.get_model('auth', 'User') | ||
for user in User.objects.all(): | ||
user.groups.add(group) | ||
|
||
|
||
def remove_group(apps, schema_editor): | ||
apps.get_model('auth', 'Group').objects.filter( | ||
name=settings.PROMGEN_DEFAULT_GROUP | ||
).delete() | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [('promgen', '0002_auto_20180316_0525')] | ||
|
||
operations = [migrations.RunPython(create_group, remove_group)] | ||
|
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 |
---|---|---|
|
@@ -48,6 +48,8 @@ | |
else: | ||
PROMGEN = {} | ||
|
||
PROMGEN_DEFAULT_GROUP = 'Default' | ||
|
||
ALLOWED_HOSTS = ['*'] | ||
|
||
|
||
|
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
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