1
0
mirror of https://github.com/ansible/awx.git synced 2024-11-02 18:21:12 +03:00
awx/lib/main/views.py

57 lines
1.9 KiB
Python
Raw Normal View History

from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from lib.main.models import *
from lib.main.serializers import *
2013-03-21 08:34:59 +04:00
from lib.main.rbac import *
from django.contrib.auth.models import AnonymousUser
from rest_framework import mixins
from rest_framework import generics
from rest_framework import permissions
import exceptions
class OrganizationsList(generics.ListCreateAPIView):
model = Organization
serializer_class = OrganizationSerializer
permission_classes = (CustomRbac,)
#def pre_save(self, obj):
# obj.owner = self.request.user
2013-03-21 07:14:09 +04:00
def get_queryset(self):
2013-03-21 08:34:59 +04:00
2013-03-21 07:14:09 +04:00
if self.request.user.is_superuser:
2013-03-21 08:31:07 +04:00
return Organization.objects.filter(active=True)
return Organization.objects.filter(active = True, admins__in = [ self.request.user.application_user ]).distinct() | \
Organization.objects.filter(active = True, users__in = [ self.request.user.application_user ]).distinct()
2013-03-21 07:14:09 +04:00
def list_permissions_check(self, request, obj=None):
if request.method == 'GET':
# everybody can call get, but it's filtered
return True
if request.method == 'POST':
# superusers have already been cleared, so deny regular users
return False
raise exceptions.NotImplementedError
2013-03-21 07:14:09 +04:00
class OrganizationsDetail(generics.RetrieveUpdateDestroyAPIView):
model = Organization
serializer_class = OrganizationSerializer
permission_classes = (CustomRbac,)
#def pre_save(self, obj):
# obj.owner = self.request.user
def item_permissions_check(self, request, obj):
admin = request.user.application_user in obj.admins.all()
user = request.user.application_user in obj.users.all()
if request.method == 'GET':
return admin or user
if request.method == 'PUT':
return admin
2013-03-21 18:44:01 +04:00