Example #1
0
 def unattach(self, request, *args, **kwargs):
     response = super(InstanceGroupMembershipMixin,
                      self).unattach(request, *args, **kwargs)
     if status.is_success(response.status_code):
         sub_id = request.data.get('id', None)
         if self.parent_model is Instance:
             inst_name = self.get_parent_object().hostname
         else:
             inst_name = get_object_or_400(self.model, pk=sub_id).hostname
         with transaction.atomic():
             ig_qs = InstanceGroup.objects.select_for_update()
             if self.parent_model is Instance:
                 ig_obj = get_object_or_400(ig_qs, pk=sub_id)
             else:
                 # similar to get_parent_object, but selected for update
                 parent_filter = {
                     self.lookup_field:
                     self.kwargs.get(self.lookup_field, None),
                 }
                 ig_obj = get_object_or_404(ig_qs, **parent_filter)
             if inst_name in ig_obj.policy_instance_list:
                 ig_obj.policy_instance_list.pop(
                     ig_obj.policy_instance_list.index(inst_name))
                 ig_obj.save(update_fields=['policy_instance_list'])
     return response
Example #2
0
 def check_post_permissions(self, request, view, obj=None):
     if hasattr(view, 'parent_model'):
         parent_obj = get_object_or_400(view.parent_model,
                                        pk=view.kwargs['pk'])
         if not check_user_access(request.user, view.parent_model, 'read',
                                  parent_obj):
             return False
         if hasattr(view, 'parent_key'):
             if not check_user_access(request.user, view.model, 'add',
                                      {view.parent_key: parent_obj.pk}):
                 return False
         return True
     elif getattr(view, 'is_job_start', False):
         if not obj:
             return True
         return check_user_access(request.user, view.model, 'start', obj)
     elif getattr(view, 'is_job_cancel', False):
         if not obj:
             return True
         return check_user_access(request.user, view.model, 'cancel', obj)
     else:
         if obj:
             return True
         return check_user_access(request.user, view.model, 'add',
                                  request.data)
Example #3
0
 def unattach_validate(self, request):
     (sub_id, res) = super(InstanceGroupMembershipMixin, self).unattach_validate(request)
     if res:
         return (sub_id, res)
     sub = get_object_or_400(self.model, pk=sub_id)
     attach_errors = self.is_valid_relation(None, sub)
     if attach_errors:
         return (sub_id, Response(attach_errors, status=status.HTTP_400_BAD_REQUEST))
     return (sub_id, res)
Example #4
0
    def attach(self, request, *args, **kwargs):
        created = False
        parent = self.get_parent_object()
        relationship = getattrd(parent, self.relationship)
        data = request.data

        sub_id, res = self.attach_validate(request)
        if res:
            return res

        # Create the sub object if an ID is not provided.
        if not sub_id:
            response = self.create(request, *args, **kwargs)
            if response.status_code != status.HTTP_201_CREATED:
                return response
            sub_id = response.data['id']
            data = response.data
            try:
                location = response['Location']
            except KeyError:
                location = None
            created = True

        # Retrive the sub object (whether created or by ID).
        sub = get_object_or_400(self.model, pk=sub_id)

        # Verify we have permission to attach.
        if not request.user.can_access(self.parent_model,
                                       'attach',
                                       parent,
                                       sub,
                                       self.relationship,
                                       data,
                                       skip_sub_obj_read_check=created):
            raise PermissionDenied()

        # Verify that the relationship to be added is valid.
        attach_errors = self.is_valid_relation(parent, sub, created=created)
        if attach_errors is not None:
            if created:
                sub.delete()
            return Response(attach_errors, status=status.HTTP_400_BAD_REQUEST)

        # Attach the object to the collection.
        if sub not in relationship.all():
            relationship.add(sub)

        if created:
            headers = {}
            if location:
                headers['Location'] = location
            return Response(data,
                            status=status.HTTP_201_CREATED,
                            headers=headers)
        else:
            return Response(status=status.HTTP_204_NO_CONTENT)
Example #5
0
 def check_get_permissions(self, request, view, obj=None):
     if hasattr(view, 'parent_model'):
         parent_obj = get_object_or_400(view.parent_model,
                                        pk=view.kwargs['pk'])
         if not check_user_access(request.user, view.parent_model, 'read',
                                  parent_obj):
             return False
     if not obj:
         return True
     return check_user_access(request.user, view.model, 'read', obj)
Example #6
0
 def attach_validate(self, request):
     parent = self.get_parent_object()
     sub_id, res = super().attach_validate(request)
     if res:  # handle an error
         return sub_id, res
     sub = get_object_or_400(self.model, pk=sub_id)
     attach_errors = self.is_valid_relation(parent, sub)
     if attach_errors:
         return sub_id, Response(attach_errors,
                                 status=status.HTTP_400_BAD_REQUEST)
     return sub_id, res
Example #7
0
    def unattach_by_id(self, request, sub_id):
        parent = self.get_parent_object()
        parent_key = getattr(self, 'parent_key', None)
        relationship = getattrd(parent, self.relationship)
        sub = get_object_or_400(self.model, pk=sub_id)

        if not request.user.can_access(self.parent_model, 'unattach', parent, sub, self.relationship, request.data):
            raise PermissionDenied()

        if parent_key:
            sub.delete()
        else:
            relationship.remove(sub)

        return Response(status=status.HTTP_204_NO_CONTENT)
Example #8
0
    def unattach_by_id(self, request, sub_id):
        parent = self.get_parent_object()
        parent_key = getattr(self, 'parent_key', None)
        relationship = getattrd(parent, self.relationship)
        sub = get_object_or_400(self.model, pk=sub_id)

        if not request.user.can_access(self.parent_model, 'unattach', parent,
                                       sub, self.relationship, request.data):
            raise PermissionDenied()

        # Verify that removing the relationship is valid.
        unattach_errors = self.is_valid_removal(parent, sub)
        if unattach_errors is not None:
            return Response(unattach_errors,
                            status=status.HTTP_400_BAD_REQUEST)

        if parent_key:
            sub.delete()
        else:
            relationship.remove(sub)

        return Response(status=status.HTTP_204_NO_CONTENT)
 def check_post_permissions(self, request, view, obj=None):
     inventory = get_object_or_400(view.model, pk=view.kwargs['pk'])
     return check_user_access(request.user, view.model, 'update', inventory)
 def check_post_permissions(self, request, view, obj=None):
     project = get_object_or_400(view.model, pk=view.kwargs['pk'])
     return check_user_access(request.user, view.model, 'start', project)
Example #11
0
 def check_post_permissions(self, request, view, obj=None):
     approval = get_object_or_400(view.model, pk=view.kwargs['pk'])
     return check_user_access(request.user, view.model, 'approve_or_deny',
                              approval)
Example #12
0
 def check_post_permissions(self, request, view, obj=None):
     if hasattr(view, 'queryset'):  #This is the Ipam_ apis
         return True  #2019/01/22
     inventory = get_object_or_400(view.model, pk=view.kwargs['pk'])
     return check_user_access(request.user, view.model, 'update', inventory)
Example #13
0
 def check_get_permissions(self, request, view, obj=None):
     if hasattr(view, 'queryset'):  #This is the Ipam_ apis
         return True  #2019/01/22
     project = get_object_or_400(view.model, pk=view.kwargs['pk'])
     return check_user_access(request.user, view.model, 'read', project)