2017-02-21 09:34:24 +01:00
|
|
|
from django.apps import apps
|
2015-09-16 00:55:27 +02:00
|
|
|
from django.contrib.auth.models import Permission
|
2015-02-18 15:58:12 +01:00
|
|
|
from django.db.models import Q
|
2013-03-12 20:58:22 +01:00
|
|
|
|
2016-12-17 09:30:20 +01:00
|
|
|
from ..utils.autoupdate import inform_changed_data
|
2015-09-16 00:55:27 +02:00
|
|
|
from .models import Group, User
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2013-03-12 20:58:22 +01:00
|
|
|
|
2017-02-21 09:34:24 +01:00
|
|
|
def get_permission_change_data(sender, permissions=None, **kwargs):
|
|
|
|
"""
|
2017-03-07 10:23:24 +01:00
|
|
|
Yields all necessary collections if 'users.can_see_name' permission changes.
|
2017-02-21 09:34:24 +01:00
|
|
|
"""
|
2017-03-06 16:34:20 +01:00
|
|
|
users_app = apps.get_app_config(app_label='users')
|
2017-02-21 09:34:24 +01:00
|
|
|
for permission in permissions:
|
2017-03-07 10:23:24 +01:00
|
|
|
# There could be only one 'users.can_see_name' and then we want to return data.
|
|
|
|
if permission.content_type.app_label == users_app.label and permission.codename == 'can_see_name':
|
2017-03-06 16:34:20 +01:00
|
|
|
yield from users_app.get_startup_elements()
|
2017-02-21 09:34:24 +01:00
|
|
|
|
|
|
|
|
2015-02-12 20:57:05 +01:00
|
|
|
def create_builtin_groups_and_admin(**kwargs):
|
2014-11-13 22:23:16 +01:00
|
|
|
"""
|
2016-08-08 09:37:46 +02:00
|
|
|
Creates the builtin groups: Default, Delegates, Staff and Committees.
|
2015-02-12 20:57:05 +01:00
|
|
|
|
|
|
|
Creates the builtin user: admin.
|
2014-11-13 22:23:16 +01:00
|
|
|
"""
|
2016-08-30 09:16:47 +02:00
|
|
|
# Check whether there are groups in the database.
|
|
|
|
if Group.objects.exists():
|
2016-09-08 11:40:58 +02:00
|
|
|
# Do completely nothing if there are already some groups in the database.
|
2014-10-11 14:34:49 +02:00
|
|
|
return
|
|
|
|
|
2015-02-18 15:58:12 +01:00
|
|
|
permission_strings = (
|
|
|
|
'agenda.can_be_speaker',
|
2015-03-26 05:36:10 +01:00
|
|
|
'agenda.can_manage',
|
2017-08-18 13:13:53 +02:00
|
|
|
'agenda.can_manage_list_of_speakers',
|
2015-03-26 05:36:10 +01:00
|
|
|
'agenda.can_see',
|
2015-10-24 19:02:43 +02:00
|
|
|
'agenda.can_see_hidden_items',
|
2015-03-26 05:36:10 +01:00
|
|
|
'assignments.can_manage',
|
|
|
|
'assignments.can_nominate_other',
|
|
|
|
'assignments.can_nominate_self',
|
|
|
|
'assignments.can_see',
|
2015-06-29 12:08:15 +02:00
|
|
|
'core.can_manage_config',
|
2017-03-31 13:48:41 +02:00
|
|
|
'core.can_manage_logos',
|
2015-02-18 15:58:12 +01:00
|
|
|
'core.can_manage_projector',
|
|
|
|
'core.can_manage_tags',
|
2016-10-17 12:00:18 +02:00
|
|
|
'core.can_manage_chat',
|
2016-01-25 21:22:22 +01:00
|
|
|
'core.can_see_frontpage',
|
2015-02-18 15:58:12 +01:00
|
|
|
'core.can_see_projector',
|
|
|
|
'core.can_use_chat',
|
2015-03-26 05:36:10 +01:00
|
|
|
'mediafiles.can_manage',
|
|
|
|
'mediafiles.can_see',
|
2016-12-12 16:15:04 +01:00
|
|
|
'mediafiles.can_see_hidden',
|
2015-03-26 05:36:10 +01:00
|
|
|
'mediafiles.can_upload',
|
|
|
|
'motions.can_create',
|
|
|
|
'motions.can_manage',
|
|
|
|
'motions.can_see',
|
2016-07-29 23:33:47 +02:00
|
|
|
'motions.can_see_and_manage_comments',
|
2015-03-26 05:36:10 +01:00
|
|
|
'motions.can_support',
|
2015-02-18 15:58:12 +01:00
|
|
|
'users.can_manage',
|
|
|
|
'users.can_see_extra_data',
|
|
|
|
'users.can_see_name', )
|
|
|
|
permission_query = Q()
|
2015-09-16 00:55:27 +02:00
|
|
|
permission_dict = {}
|
2015-02-18 15:58:12 +01:00
|
|
|
|
2015-09-16 00:55:27 +02:00
|
|
|
# Load all permissions
|
2015-02-18 15:58:12 +01:00
|
|
|
for permission_string in permission_strings:
|
|
|
|
app_label, codename = permission_string.split('.')
|
|
|
|
query_part = Q(content_type__app_label=app_label) & Q(codename=codename)
|
|
|
|
permission_query = permission_query | query_part
|
|
|
|
for permission in Permission.objects.select_related('content_type').filter(permission_query):
|
|
|
|
permission_string = '.'.join((permission.content_type.app_label, permission.codename))
|
|
|
|
permission_dict[permission_string] = permission
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2016-08-08 09:37:46 +02:00
|
|
|
# Default (pk 1)
|
2015-02-18 15:58:12 +01:00
|
|
|
base_permissions = (
|
2015-03-26 05:36:10 +01:00
|
|
|
permission_dict['agenda.can_see'],
|
2015-10-24 19:02:43 +02:00
|
|
|
permission_dict['agenda.can_see_hidden_items'],
|
2015-03-26 05:36:10 +01:00
|
|
|
permission_dict['assignments.can_see'],
|
2016-01-25 21:22:22 +01:00
|
|
|
permission_dict['core.can_see_frontpage'],
|
2015-02-18 15:58:12 +01:00
|
|
|
permission_dict['core.can_see_projector'],
|
2015-03-26 05:36:10 +01:00
|
|
|
permission_dict['mediafiles.can_see'],
|
|
|
|
permission_dict['motions.can_see'],
|
2015-02-18 15:58:12 +01:00
|
|
|
permission_dict['users.can_see_name'], )
|
2017-02-17 12:06:07 +01:00
|
|
|
group_default = Group.objects.create(name='Default')
|
2016-08-08 09:37:46 +02:00
|
|
|
group_default.permissions.add(*base_permissions)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2016-08-08 09:37:46 +02:00
|
|
|
# Delegates (pk 2)
|
2015-02-18 15:58:12 +01:00
|
|
|
delegates_permissions = (
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['agenda.can_see'],
|
|
|
|
permission_dict['agenda.can_see_hidden_items'],
|
|
|
|
permission_dict['agenda.can_be_speaker'],
|
|
|
|
permission_dict['assignments.can_see'],
|
2015-03-26 05:36:10 +01:00
|
|
|
permission_dict['assignments.can_nominate_other'],
|
|
|
|
permission_dict['assignments.can_nominate_self'],
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['core.can_see_frontpage'],
|
|
|
|
permission_dict['core.can_see_projector'],
|
|
|
|
permission_dict['mediafiles.can_see'],
|
2015-03-26 05:36:10 +01:00
|
|
|
permission_dict['mediafiles.can_upload'],
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['motions.can_see'],
|
2015-03-26 05:36:10 +01:00
|
|
|
permission_dict['motions.can_create'],
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['motions.can_support'],
|
|
|
|
permission_dict['users.can_see_name'], )
|
2017-02-17 12:06:07 +01:00
|
|
|
group_delegates = Group.objects.create(name='Delegates')
|
2015-02-18 15:58:12 +01:00
|
|
|
group_delegates.permissions.add(*delegates_permissions)
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2016-08-08 09:37:46 +02:00
|
|
|
# Staff (pk 3)
|
2015-02-18 15:58:12 +01:00
|
|
|
staff_permissions = (
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['agenda.can_see'],
|
|
|
|
permission_dict['agenda.can_see_hidden_items'],
|
|
|
|
permission_dict['agenda.can_be_speaker'],
|
2015-03-26 05:36:10 +01:00
|
|
|
permission_dict['agenda.can_manage'],
|
2017-08-18 13:13:53 +02:00
|
|
|
permission_dict['agenda.can_manage_list_of_speakers'],
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['assignments.can_see'],
|
2015-03-26 05:36:10 +01:00
|
|
|
permission_dict['assignments.can_manage'],
|
|
|
|
permission_dict['assignments.can_nominate_other'],
|
|
|
|
permission_dict['assignments.can_nominate_self'],
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['core.can_see_frontpage'],
|
|
|
|
permission_dict['core.can_see_projector'],
|
2015-06-29 12:08:15 +02:00
|
|
|
permission_dict['core.can_manage_config'],
|
2017-03-31 13:48:41 +02:00
|
|
|
permission_dict['core.can_manage_logos'],
|
2015-02-18 15:58:12 +01:00
|
|
|
permission_dict['core.can_manage_projector'],
|
|
|
|
permission_dict['core.can_manage_tags'],
|
|
|
|
permission_dict['core.can_use_chat'],
|
2016-10-17 12:00:18 +02:00
|
|
|
permission_dict['core.can_manage_chat'],
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['mediafiles.can_see'],
|
2015-03-26 05:36:10 +01:00
|
|
|
permission_dict['mediafiles.can_manage'],
|
|
|
|
permission_dict['mediafiles.can_upload'],
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['motions.can_see'],
|
2015-03-26 05:36:10 +01:00
|
|
|
permission_dict['motions.can_create'],
|
|
|
|
permission_dict['motions.can_manage'],
|
2016-07-29 23:33:47 +02:00
|
|
|
permission_dict['motions.can_see_and_manage_comments'],
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['users.can_see_name'],
|
2016-03-05 20:45:57 +01:00
|
|
|
permission_dict['users.can_manage'],
|
2016-05-14 14:49:24 +02:00
|
|
|
permission_dict['users.can_see_extra_data'],
|
2016-12-12 16:15:04 +01:00
|
|
|
permission_dict['mediafiles.can_see_hidden'],)
|
2017-02-17 12:06:07 +01:00
|
|
|
group_staff = Group.objects.create(name='Staff')
|
2015-02-18 15:58:12 +01:00
|
|
|
group_staff.permissions.add(*staff_permissions)
|
|
|
|
|
2016-03-05 20:45:57 +01:00
|
|
|
# Add users.can_see_name permission to staff
|
2015-09-16 00:55:27 +02:00
|
|
|
# group to ensure proper management possibilities
|
2015-02-12 20:57:05 +01:00
|
|
|
# TODO: Remove this redundancy after cleanup of the permission system.
|
2015-02-18 15:58:12 +01:00
|
|
|
group_staff.permissions.add(
|
|
|
|
permission_dict['users.can_see_name'])
|
2015-02-12 20:57:05 +01:00
|
|
|
|
2016-08-08 09:37:46 +02:00
|
|
|
# Committees (pk 4)
|
2016-06-30 13:06:23 +02:00
|
|
|
committees_permissions = (
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['agenda.can_see'],
|
|
|
|
permission_dict['agenda.can_see_hidden_items'],
|
|
|
|
permission_dict['agenda.can_be_speaker'],
|
|
|
|
permission_dict['assignments.can_see'],
|
|
|
|
permission_dict['core.can_see_frontpage'],
|
|
|
|
permission_dict['core.can_see_projector'],
|
|
|
|
permission_dict['mediafiles.can_see'],
|
2016-06-30 13:06:23 +02:00
|
|
|
permission_dict['mediafiles.can_upload'],
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['motions.can_see'],
|
2016-06-30 13:06:23 +02:00
|
|
|
permission_dict['motions.can_create'],
|
2016-08-08 09:37:46 +02:00
|
|
|
permission_dict['motions.can_support'],
|
|
|
|
permission_dict['users.can_see_name'], )
|
2017-02-17 12:06:07 +01:00
|
|
|
group_committee = Group.objects.create(name='Committees')
|
2016-06-30 13:06:23 +02:00
|
|
|
group_committee.permissions.add(*committees_permissions)
|
|
|
|
|
2015-09-16 00:55:27 +02:00
|
|
|
# Create or reset admin user
|
2015-02-12 20:57:05 +01:00
|
|
|
User.objects.create_or_reset_admin_user()
|
2016-12-17 09:30:20 +01:00
|
|
|
|
|
|
|
# After each group was created, the permissions (many to many fields) where
|
|
|
|
# added to the group. So we have to update the cache by calling
|
|
|
|
# inform_changed_data().
|
|
|
|
inform_changed_data((group_default, group_delegates, group_staff, group_committee))
|