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 da395680e..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): @@ -25,6 +26,13 @@ class UserCreateForm(forms.ModelForm, CssClassMixin): queryset=Group.objects.exclude(name__iexact='anonymous'), label=_("Groups"), required=False) + def __init__(self, *args, **kwargs): + if kwargs.get('instance', None) is None: + initial = kwargs.setdefault('initial', {}) + registered = get_or_create_registered_group() + initial['groups'] = [registered.pk] + super(UserCreateForm, self).__init__(*args, **kwargs) + class Meta: model = User fields = ('first_name', 'last_name', 'is_active', 'groups', 'structure_level', @@ -76,13 +84,13 @@ class GroupForm(forms.ModelForm, CssClassMixin): # Do not allow to change the name "anonymous" or give another group # this name data = self.cleaned_data['name'] - if self.instance.name.lower() == 'anonymous': + if self.instance.name.lower() in ['anonymous', 'registered']: # Editing the anonymous-user if self.instance.name.lower() != data.lower(): raise forms.ValidationError( - _('You can not edit the name for the anonymous user')) + _('You can not edit the name for this group.')) else: - if data.lower() == 'anonymous': + if data.lower() in ['anonymous', 'registered']: raise forms.ValidationError( _('Group name "%s" is reserved for internal use.') % data) return data diff --git a/openslides/participant/models.py b/openslides/participant/models.py index 14758a16d..7f047a9c4 100644 --- a/openslides/participant/models.py +++ b/openslides/participant/models.py @@ -234,7 +234,7 @@ def default_config(sender, key, **kwargs): @receiver(signals.post_save, sender=DjangoUser) -def user_post_save(sender, instance, signal, *args, **kwargs): +def djangouser_post_save(sender, instance, signal, *args, **kwargs): try: instance.user except User.DoesNotExist: @@ -242,8 +242,18 @@ def user_post_save(sender, instance, signal, *args, **kwargs): @receiver(signals.post_save, sender=DjangoGroup) -def group_post_save(sender, instance, signal, *args, **kwargs): +def djangogroup_post_save(sender, instance, signal, *args, **kwargs): try: instance.group except Group.DoesNotExist: Group(django_group=instance).save_base(raw=True) + + +@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 = get_or_create_registered_group() + instance.groups.add(registered) + instance.save() diff --git a/openslides/participant/templates/participant/group_overview.html b/openslides/participant/templates/participant/group_overview.html index 8bcbf46c1..550fe2f56 100644 --- a/openslides/participant/templates/participant/group_overview.html +++ b/openslides/participant/templates/participant/group_overview.html @@ -17,7 +17,7 @@