dispatch: require explicit send_message permission

This commit is contained in:
Brian S. Stephan 2015-06-19 16:45:10 -05:00
parent bece1745b3
commit 9296123f25
3 changed files with 41 additions and 1 deletions

View File

@ -0,0 +1,23 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('dispatch', '0002_auto_20150619_1124'),
]
operations = [
migrations.AlterModelOptions(
name='dispatcher',
options={'permissions': (('send_message', 'Can send messages to dispatchers'),)},
),
migrations.AlterField(
model_name='dispatcheraction',
name='dispatcher',
field=models.ForeignKey(related_name='actions', to='dispatch.Dispatcher'),
),
]

View File

@ -14,6 +14,11 @@ class Dispatcher(models.Model):
key = models.CharField(max_length=16, unique=True) key = models.CharField(max_length=16, unique=True)
class Meta:
permissions = (
('send_message', "Can send messages to dispatchers"),
)
def __unicode__(self): def __unicode__(self):
"""String representation.""" """String representation."""

View File

@ -9,8 +9,9 @@ import xmlrpclib
from django.conf import settings from django.conf import settings
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from rest_framework.response import Response
from rest_framework import generics, status from rest_framework import generics, status
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from dispatch.models import Dispatcher, DispatcherAction from dispatch.models import Dispatcher, DispatcherAction
from dispatch.serializers import DispatchMessageSerializer, DispatcherSerializer, DispatcherActionSerializer from dispatch.serializers import DispatchMessageSerializer, DispatcherSerializer, DispatcherActionSerializer
@ -19,6 +20,15 @@ from dispatch.serializers import DispatchMessageSerializer, DispatcherSerializer
log = logging.getLogger('dispatch.views') log = logging.getLogger('dispatch.views')
class HasSendMessagePermission(IsAuthenticated):
def has_permission(self, request, view):
if request.user.has_perm('dispatch.send_message'):
return True
return False
class DispatcherList(generics.ListAPIView): class DispatcherList(generics.ListAPIView):
"""List all dispatchers.""" """List all dispatchers."""
@ -39,6 +49,8 @@ class DispatchMessage(generics.GenericAPIView):
"""Send a message to the given dispatcher.""" """Send a message to the given dispatcher."""
permission_classes = (HasSendMessagePermission,)
queryset = Dispatcher.objects.all() queryset = Dispatcher.objects.all()
serializer_class = DispatchMessageSerializer serializer_class = DispatchMessageSerializer