2017-02-21 09:34:24 +01:00
|
|
|
from django.apps import apps
|
2016-01-25 14:48:00 +01:00
|
|
|
from django.contrib.auth.models import Permission
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.db.models import Q
|
2015-02-12 20:57:05 +01:00
|
|
|
from django.dispatch import Signal
|
2013-03-11 21:32:09 +01:00
|
|
|
|
2017-04-10 16:28:38 +02:00
|
|
|
from ..utils.auth import has_perm
|
2017-03-06 16:34:20 +01:00
|
|
|
from ..utils.collection import Collection
|
2017-04-10 16:28:38 +02:00
|
|
|
from .models import ChatMessage
|
2017-03-06 16:34:20 +01:00
|
|
|
|
2017-02-21 09:34:24 +01:00
|
|
|
# This signal is send when the migrate command is done. That means it is sent
|
2015-02-12 20:57:05 +01:00
|
|
|
# after post_migrate sending and creating all Permission objects. Don't use it
|
|
|
|
# for other things than dealing with Permission objects.
|
|
|
|
post_permission_creation = Signal()
|
|
|
|
|
2017-03-06 16:34:20 +01:00
|
|
|
# This signal is sent if a permission is changed (e. g. a group gets a new
|
|
|
|
# permission). Connected receivers may yield Collections.
|
2017-02-21 09:34:24 +01:00
|
|
|
permission_change = Signal()
|
|
|
|
|
2017-04-10 16:28:38 +02:00
|
|
|
# This signal is sent if someone wants to see basic user data. Connected
|
|
|
|
# receivers may answer True if the user data is required for the request user
|
|
|
|
# (this can be anything that is allowd as argument for utils.auth.has_perm())
|
|
|
|
# e. g. as motion submitter or assignment candidate.
|
|
|
|
user_data_required = Signal(providing_args=['request_user', 'user_data'])
|
|
|
|
|
2013-03-01 17:13:12 +01:00
|
|
|
|
2016-01-25 14:48:00 +01:00
|
|
|
def delete_django_app_permissions(sender, **kwargs):
|
|
|
|
"""
|
|
|
|
Deletes the permissions, Django creates by default. Only required
|
|
|
|
for auth, contenttypes and sessions.
|
|
|
|
"""
|
|
|
|
contenttypes = ContentType.objects.filter(
|
|
|
|
Q(app_label='auth') |
|
|
|
|
Q(app_label='contenttypes') |
|
|
|
|
Q(app_label='sessions'))
|
2016-10-01 01:30:55 +02:00
|
|
|
Permission.objects.filter(content_type__in=contenttypes).delete()
|
2017-02-21 09:34:24 +01:00
|
|
|
|
|
|
|
|
2017-03-06 16:34:20 +01:00
|
|
|
def get_permission_change_data(sender, permissions, **kwargs):
|
2017-02-21 09:34:24 +01:00
|
|
|
"""
|
2017-03-06 16:34:20 +01:00
|
|
|
Yields all necessary collections if the respective permissions change.
|
2017-02-21 09:34:24 +01:00
|
|
|
"""
|
|
|
|
core_app = apps.get_app_config(app_label='core')
|
|
|
|
for permission in permissions:
|
|
|
|
if permission.content_type.app_label == core_app.label:
|
|
|
|
if permission.codename == 'can_see_projector':
|
|
|
|
yield Collection(core_app.get_model('Projector').get_collection_string())
|
|
|
|
elif permission.codename == 'can_manage_projector':
|
|
|
|
yield Collection(core_app.get_model('ProjectorMessage').get_collection_string())
|
|
|
|
yield Collection(core_app.get_model('Countdown').get_collection_string())
|
|
|
|
elif permission.codename == 'can_use_chat':
|
|
|
|
yield Collection(core_app.get_model('ChatMessage').get_collection_string())
|
2017-04-10 16:28:38 +02:00
|
|
|
|
|
|
|
|
2017-05-01 23:12:42 +02:00
|
|
|
def is_user_data_required(sender, request_user, **kwargs):
|
2017-04-10 16:28:38 +02:00
|
|
|
"""
|
2017-05-01 23:12:42 +02:00
|
|
|
Returns all user ids that are displayed as chatters if request_user can
|
|
|
|
use the chat. This function may return an empty set.
|
2017-04-10 16:28:38 +02:00
|
|
|
"""
|
2017-05-01 23:12:42 +02:00
|
|
|
chatters = set()
|
2017-04-10 16:28:38 +02:00
|
|
|
if has_perm(request_user, 'core.can_use_chat'):
|
|
|
|
for chat_message_collection_element in Collection(ChatMessage.get_collection_string()).element_generator():
|
|
|
|
full_data = chat_message_collection_element.get_full_data()
|
2017-05-01 23:12:42 +02:00
|
|
|
chatters.add(full_data['user_id'])
|
|
|
|
return chatters
|