text: # -*- coding: utf-8 -*-
  __author__ = 'bobby'
  from rest_framework import permissions
  from rbac.models import IpWhiteList
  
  
  class IsOwnerOrReadOnly(permissions.BasePermission):
      """
      Object-level permission to only allow owners of an object to edit it.
      Assumes the model instance has an `owner` attribute.
      """
  
      def has_object_permission(self, request, view, obj):
          # Read permissions are allowed to any request,
          # so we'll always allow GET, HEAD or OPTIONS requests.
          if request.method in permissions.SAFE_METHODS:
              return True
  
          # Instance must have an attribute named `owner`.
          return obj.user == request.user
  
  
  class IpWhiteListPermission(permissions.BasePermission):
      """
      IP访问白名单
      """
  
      def has_permission(self, request, view):
          ip_addr = request.META['REMOTE_ADDR']
          blacklisted = IpWhiteList.objects.filter(ip_addr=ip_addr).exists()
          return blacklisted
  
  
  class ModelPermissions(permissions.DjangoModelPermissions):
  
      def get_custom_perms(self, view, method):
          if hasattr(view, "extra_perm_map"):
              if isinstance(view.extra_perm_map, dict):
                  return view.extra_perm_map.get(method,[])
          return []
  
      def has_permission(self, request, view):
          # Workaround to ensure DjangoModelPermissions are not applied
          # to the root view when using DefaultRouter.
          if getattr(view, '_ignore_model_permissions', False):
              return True
  
          if not request.user or (
             not request.user.is_authenticated and self.authenticated_users_only):
              return False
  
          queryset = self._queryset(view)
          perms = self.get_required_permissions(request.method, queryset.model)
          perms.extend(self.get_custom_perms(view, request.method))
          return request.user.has_perms(perms)
  
  
  class Permissions(permissions.DjangoModelPermissions):
      perms_map = {
          'GET': ['%(app_label)s.view_%(model_name)s'],
          'OPTIONS': [],
          'HEAD': [],
          'POST': ['%(app_label)s.add_%(model_name)s'],
          'PUT': ['%(app_label)s.change_%(model_name)s'],
          'PATCH': ['%(app_label)s.change_%(model_name)s'],
          'DELETE': ['%(app_label)s.delete_%(model_name)s'],
      }