From 9943e84a965033800e696063bb18d2bdbf7612d8 Mon Sep 17 00:00:00 2001 From: Oskar Hahn Date: Fri, 23 Nov 2012 09:35:41 +0100 Subject: [PATCH] default permissions for the registered group plus some cleanup for the registered and the anonymous group --- openslides/config/views.py | 19 ++----------------- openslides/participant/api.py | 29 +++++++++++++++++++++++++++-- openslides/participant/forms.py | 4 ++-- openslides/participant/models.py | 4 ++-- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/openslides/config/views.py b/openslides/config/views.py index e93e08f18..dfeb6182e 100644 --- a/openslides/config/views.py +++ b/openslides/config/views.py @@ -12,7 +12,6 @@ from django.conf import settings from django.contrib import messages -from django.contrib.auth.models import Group, Permission from django.core.urlresolvers import reverse from django.utils.importlib import import_module from django.utils.translation import ugettext as _ @@ -24,6 +23,7 @@ from openslides.utils.views import FormView, TemplateView from openslides.config.forms import GeneralConfigForm from openslides.config.models import config +from openslides.participant.api import get_or_create_anonymous_group class GeneralConfig(FormView): @@ -61,22 +61,7 @@ class GeneralConfig(FormView): # system if form.cleaned_data['system_enable_anonymous']: config['system_enable_anonymous'] = True - # check for Anonymous group and (re)create it as needed - try: - anonymous = Group.objects.get(name='Anonymous') - except Group.DoesNotExist: - default_perms = ['can_see_agenda', 'can_see_projector', - 'can_see_motion', 'can_see_assignment', - 'can_see_dashboard'] - anonymous = Group() - anonymous.name = 'Anonymous' - anonymous.save() - anonymous.permissions = Permission.objects.filter( - codename__in=default_perms) - anonymous.save() - messages.success(self.request, - _('Anonymous access enabled. Please modify the "Anonymous" ' \ - 'group to fit your required permissions.')) + anonymous = get_or_create_anonymous_group() else: config['system_enable_anonymous'] = False diff --git a/openslides/participant/api.py b/openslides/participant/api.py index 938eacca6..5544af64c 100644 --- a/openslides/participant/api.py +++ b/openslides/participant/api.py @@ -17,12 +17,17 @@ from random import choice import string import csv -from django.contrib.auth.models import User +from django.contrib.auth.models import Permission from django.db import transaction from openslides.utils import csv_ext -from openslides.participant.models import User +from openslides.participant.models import User, Group + + +DEFAULT_PERMS = ['can_see_agenda', 'can_see_projector', + 'can_see_motion', 'can_see_assignment', + 'can_see_dashboard'] def gen_password(): @@ -93,3 +98,23 @@ def import_users(csv_file): except UnicodeDecodeError: error_messages.appen(_('Import file has wrong character encoding, only UTF-8 is supported!')) return (count_success, error_messages) + + +def get_or_create_registered_group(): + registered, created = Group.objects.get_or_create( + name__iexact='Registered', defaults={'name': 'Registered'}) + if created: + registered.permissions = Permission.objects.filter( + codename__in=DEFAULT_PERMS) + registered.save() + return registered + + +def get_or_create_anonymous_group(): + anonymous, created = Group.objects.get_or_create( + name__iexact='Anonymous', defaults={'name': 'Anonymous'}) + if created: + anonymous.permissions = Permission.objects.filter( + codename__in=DEFAULT_PERMS) + anonymous.save() + return anonymous diff --git a/openslides/participant/forms.py b/openslides/participant/forms.py index a630dae26..b1fcea525 100644 --- a/openslides/participant/forms.py +++ b/openslides/participant/forms.py @@ -18,6 +18,7 @@ from openslides.utils.forms import ( CssClassMixin, LocalizedModelMultipleChoiceField) from openslides.participant.models import User, Group +from openslides.participant.api import get_or_create_registered_group class UserCreateForm(forms.ModelForm, CssClassMixin): @@ -28,8 +29,7 @@ class UserCreateForm(forms.ModelForm, CssClassMixin): def __init__(self, *args, **kwargs): if kwargs.get('instance', None) is None: initial = kwargs.setdefault('initial', {}) - registered, created = Group.objects.get_or_create( - name__iexact='Registered', defaults={'name': 'Registered'}) + registered = get_or_create_registered_group() initial['groups'] = [registered.pk] super(UserCreateForm, self).__init__(*args, **kwargs) diff --git a/openslides/participant/models.py b/openslides/participant/models.py index 7f2270f0f..7f047a9c4 100644 --- a/openslides/participant/models.py +++ b/openslides/participant/models.py @@ -251,9 +251,9 @@ def djangogroup_post_save(sender, instance, signal, *args, **kwargs): @receiver(signals.post_save, sender=User) def user_post_save(sender, instance, *args, **kwargs): + from openslides.participant.api import get_or_create_registered_group if not kwargs['created']: return - registered, created = Group.objects.get_or_create( - name__iexact='Registered', defaults={'name': 'Registered'}) + registered = get_or_create_registered_group() instance.groups.add(registered) instance.save()