default permissions for the registered group

plus some cleanup for the registered and the anonymous group
This commit is contained in:
Oskar Hahn 2012-11-23 09:35:41 +01:00
parent fa1c384d28
commit 9943e84a96
4 changed files with 33 additions and 23 deletions

View File

@ -12,7 +12,6 @@
from django.conf import settings from django.conf import settings
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.models import Group, Permission
from django.core.urlresolvers import reverse from django.core.urlresolvers import reverse
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.translation import ugettext as _ 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.forms import GeneralConfigForm
from openslides.config.models import config from openslides.config.models import config
from openslides.participant.api import get_or_create_anonymous_group
class GeneralConfig(FormView): class GeneralConfig(FormView):
@ -61,22 +61,7 @@ class GeneralConfig(FormView):
# system # system
if form.cleaned_data['system_enable_anonymous']: if form.cleaned_data['system_enable_anonymous']:
config['system_enable_anonymous'] = True config['system_enable_anonymous'] = True
# check for Anonymous group and (re)create it as needed anonymous = get_or_create_anonymous_group()
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.'))
else: else:
config['system_enable_anonymous'] = False config['system_enable_anonymous'] = False

View File

@ -17,12 +17,17 @@ from random import choice
import string import string
import csv import csv
from django.contrib.auth.models import User from django.contrib.auth.models import Permission
from django.db import transaction from django.db import transaction
from openslides.utils import csv_ext 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(): def gen_password():
@ -93,3 +98,23 @@ def import_users(csv_file):
except UnicodeDecodeError: except UnicodeDecodeError:
error_messages.appen(_('Import file has wrong character encoding, only UTF-8 is supported!')) error_messages.appen(_('Import file has wrong character encoding, only UTF-8 is supported!'))
return (count_success, error_messages) 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

View File

@ -18,6 +18,7 @@ from openslides.utils.forms import (
CssClassMixin, LocalizedModelMultipleChoiceField) CssClassMixin, LocalizedModelMultipleChoiceField)
from openslides.participant.models import User, Group from openslides.participant.models import User, Group
from openslides.participant.api import get_or_create_registered_group
class UserCreateForm(forms.ModelForm, CssClassMixin): class UserCreateForm(forms.ModelForm, CssClassMixin):
@ -28,8 +29,7 @@ class UserCreateForm(forms.ModelForm, CssClassMixin):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
if kwargs.get('instance', None) is None: if kwargs.get('instance', None) is None:
initial = kwargs.setdefault('initial', {}) initial = kwargs.setdefault('initial', {})
registered, created = Group.objects.get_or_create( registered = get_or_create_registered_group()
name__iexact='Registered', defaults={'name': 'Registered'})
initial['groups'] = [registered.pk] initial['groups'] = [registered.pk]
super(UserCreateForm, self).__init__(*args, **kwargs) super(UserCreateForm, self).__init__(*args, **kwargs)

View File

@ -251,9 +251,9 @@ def djangogroup_post_save(sender, instance, signal, *args, **kwargs):
@receiver(signals.post_save, sender=User) @receiver(signals.post_save, sender=User)
def user_post_save(sender, instance, *args, **kwargs): def user_post_save(sender, instance, *args, **kwargs):
from openslides.participant.api import get_or_create_registered_group
if not kwargs['created']: if not kwargs['created']:
return return
registered, created = Group.objects.get_or_create( registered = get_or_create_registered_group()
name__iexact='Registered', defaults={'name': 'Registered'})
instance.groups.add(registered) instance.groups.add(registered)
instance.save() instance.save()