From 020393c48b5f6f38bc5016170a558ad20a461eba Mon Sep 17 00:00:00 2001 From: Jan Koppe Date: Fri, 1 May 2020 15:05:26 +0200 Subject: [PATCH] update migrations; management command that creates default group with default perms; add new users to default group --- core/__init__.py | 0 core/apps.py | 10 +++++ .../management/commands/createdefaultgroup.py | 24 ++++++++++++ portier/common/handlers.py | 9 +++++ portier/settings.py | 3 ++ .../migrations/0002_auto_20200501_1302.py | 39 +++++++++++++++++++ rtmp/migrations/0004_auto_20200501_1302.py | 34 ++++++++++++++++ 7 files changed, 119 insertions(+) create mode 100644 core/__init__.py create mode 100644 core/apps.py create mode 100644 core/management/commands/createdefaultgroup.py create mode 100644 restream/migrations/0002_auto_20200501_1302.py create mode 100644 rtmp/migrations/0004_auto_20200501_1302.py diff --git a/core/__init__.py b/core/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/core/apps.py b/core/apps.py new file mode 100644 index 0000000..aa337c1 --- /dev/null +++ b/core/apps.py @@ -0,0 +1,10 @@ +from django.apps import AppConfig + +class CoreConfig(AppConfig): + name = 'core' + + def ready(self): + from django.contrib.auth.models import User + from django.db.models.signals import post_save + from portier.common import handlers + post_save.connect(handlers.add_to_default_group, sender=User) diff --git a/core/management/commands/createdefaultgroup.py b/core/management/commands/createdefaultgroup.py new file mode 100644 index 0000000..726b8c6 --- /dev/null +++ b/core/management/commands/createdefaultgroup.py @@ -0,0 +1,24 @@ +from django.core.management.base import BaseCommand +from django.contrib.auth.models import Group +from django.contrib.auth.models import Permission +from django.conf import settings + +PERMISSIONS = [ + 'add_stream', + 'add_restreamconfig' +] + + +class Command(BaseCommand): + help = 'Creates default user group with default permissions available to all users' + + def handle(self, *args, **options): + default_group, _ = Group.objects.get_or_create(name=settings.DEFAULT_GROUP) + for permission in PERMISSIONS: + try: + perm = Permission.objects.get(codename=permission) + except Permission.DoesNotExist: + print("permission '%s' does not exist" % permission) + continue + + default_group.permissions.add(perm) diff --git a/portier/common/handlers.py b/portier/common/handlers.py index 39451de..43499d7 100644 --- a/portier/common/handlers.py +++ b/portier/common/handlers.py @@ -2,6 +2,15 @@ from django.contrib.contenttypes.models import ContentType from django.db.models import Q from guardian.models import UserObjectPermission from guardian.models import GroupObjectPermission +from django.contrib.auth.models import User, Group +from django.conf import settings + + +def add_to_default_group(sender, **kwargs): + user = kwargs["instance"] + if kwargs["created"]: + group = Group.objects.get(name=settings.DEFAULT_GROUP) + user.groups.add(group) def remove_obj_perms_connected_with_user(sender, instance, **kwargs): diff --git a/portier/settings.py b/portier/settings.py index 2d47842..76b1b8e 100644 --- a/portier/settings.py +++ b/portier/settings.py @@ -27,6 +27,8 @@ DEBUG = int(os.environ.get("DEBUG", default=0)) ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", default="*").split(" ") +DEFAULT_GROUP = 'default' + # Application definition INSTALLED_APPS = [ @@ -40,6 +42,7 @@ INSTALLED_APPS = [ 'django_registration', 'bootstrap4', 'fa', + 'core.apps.CoreConfig', 'portal.apps.PortalConfig', 'rtmp.apps.RtmpConfig', 'concierge.apps.ConciergeConfig', diff --git a/restream/migrations/0002_auto_20200501_1302.py b/restream/migrations/0002_auto_20200501_1302.py new file mode 100644 index 0000000..ea5e137 --- /dev/null +++ b/restream/migrations/0002_auto_20200501_1302.py @@ -0,0 +1,39 @@ +# Generated by Django 3.0.5 on 2020-05-01 13:02 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('rtmp', '0004_auto_20200501_1302'), + ('restream', '0001_initial'), + ] + + operations = [ + migrations.AlterModelOptions( + name='restreamconfig', + options={'verbose_name': 'restreamconfig_verbose_name', 'verbose_name_plural': 'restreamconfig_verbose_name_plural'}, + ), + migrations.AlterField( + model_name='restreamconfig', + name='active', + field=models.BooleanField(help_text='restreamconfig_activate_help'), + ), + migrations.AlterField( + model_name='restreamconfig', + name='name', + field=models.CharField(help_text='restreamconfig_name_help', max_length=100), + ), + migrations.AlterField( + model_name='restreamconfig', + name='stream', + field=models.ForeignKey(help_text='restreamconfig_stream_help', on_delete=django.db.models.deletion.CASCADE, to='rtmp.Stream'), + ), + migrations.AlterField( + model_name='restreamconfig', + name='target', + field=models.CharField(help_text='restreamconfig_target_help', max_length=500), + ), + ] diff --git a/rtmp/migrations/0004_auto_20200501_1302.py b/rtmp/migrations/0004_auto_20200501_1302.py new file mode 100644 index 0000000..6f8aa83 --- /dev/null +++ b/rtmp/migrations/0004_auto_20200501_1302.py @@ -0,0 +1,34 @@ +# Generated by Django 3.0.5 on 2020-05-01 13:02 + +from django.db import migrations, models +import django.db.models.deletion +import uuid + + +class Migration(migrations.Migration): + + dependencies = [ + ('rtmp', '0003_auto_20200426_1834'), + ] + + operations = [ + migrations.AlterModelOptions( + name='application', + options={'verbose_name': 'application_verbose_name', 'verbose_name_plural': 'application_verbose_name_plural'}, + ), + migrations.AlterField( + model_name='stream', + name='application', + field=models.ForeignKey(help_text='stream_application_help', on_delete=django.db.models.deletion.CASCADE, to='rtmp.Application'), + ), + migrations.AlterField( + model_name='stream', + name='name', + field=models.CharField(help_text='stream_name_help', max_length=100), + ), + migrations.AlterField( + model_name='stream', + name='stream', + field=models.UUIDField(default=uuid.uuid4, help_text='stream_stream_help', unique=True), + ), + ]