Ver Mensaje Individual
  #1 (permalink)  
Antiguo 17/01/2014, 08:57
minimal
 
Fecha de Ingreso: diciembre-2011
Mensajes: 408
Antigüedad: 12 años, 10 meses
Puntos: 18
django setear permisos con has_perm

hola como estan tengo mi autenciacion bakend en django y quiero chekear que cuadno el usuario sea del staf retorne que no se puede logear en frontend

este es mi backend

Código Python:
Ver original
  1. class  CustomBackend(object):
  2.  
  3.     def authenticate(self, username=None, password=None):
  4.         UserModel = get_user_model()
  5.         if username is None:
  6.             username = kwargs.get(UserModel.USERNAME_FIELD)
  7.         try:
  8.             user =  UserModel._default_manager.get_by_natural_key(username)
  9.             if user.check_password(password):
  10.                 return user
  11.         except UserModel.DoesNotExist:
  12.             UserModel().set_password(password)
  13.        
  14.     def has_perm(self, user_obj, perm, obj=None):
  15.         if user_obj.is_staf:
  16.             return False
  17.         else:
  18.             return True
  19.  
  20.     def get_user(self, user_id):
  21.         UserModel = get_user_model()
  22.         try:
  23.             return UserModel._default_manager.get(pk=user_id)
  24.         except UserModel.DoesNotExist:
  25.             return None
Bakend Original de django el que usa django por defecto

Código Python:
Ver original
  1. from __future__ import unicode_literals
  2. from django.contrib.auth import get_user_model
  3. from django.contrib.auth.models import Permission
  4.  
  5.  
  6. class ModelBackend(object):
  7.     """
  8.    Authenticates against settings.AUTH_USER_MODEL.
  9.    """
  10.  
  11.     def authenticate(self, username=None, password=None, **kwargs):
  12.         UserModel = get_user_model()
  13.         if username is None:
  14.             username = kwargs.get(UserModel.USERNAME_FIELD)
  15.         try:
  16.             user = UserModel._default_manager.get_by_natural_key(username)
  17.             if user.check_password(password):
  18.                 return user
  19.         except UserModel.DoesNotExist:
  20.             # Run the default password hasher once to reduce the timing
  21.             # difference between an existing and a non-existing user (#20760).
  22.             UserModel().set_password(password)
  23.  
  24.     def get_group_permissions(self, user_obj, obj=None):
  25.         """
  26.        Returns a set of permission strings that this user has through his/her
  27.        groups.
  28.        """
  29.         if user_obj.is_anonymous() or obj is not None:
  30.             return set()
  31.         if not hasattr(user_obj, '_group_perm_cache'):
  32.             if user_obj.is_superuser:
  33.                 perms = Permission.objects.all()
  34.             else:
  35.                 user_groups_field = get_user_model()._meta.get_field('groups')
  36.                 user_groups_query = 'group__%s' % user_groups_field.related_query_name()
  37.                 perms = Permission.objects.filter(**{user_groups_query: user_obj})
  38.             perms = perms.values_list('content_type__app_label', 'codename').order_by()
  39.             user_obj._group_perm_cache = set("%s.%s" % (ct, name) for ct, name in perms)
  40.         return user_obj._group_perm_cache
  41.  
  42.     def get_all_permissions(self, user_obj, obj=None):
  43.         if user_obj.is_anonymous() or obj is not None:
  44.             return set()
  45.         if not hasattr(user_obj, '_perm_cache'):
  46.             user_obj._perm_cache = set("%s.%s" % (p.content_type.app_label, p.codename) for p in user_obj.user_permissions.select_related())
  47.             user_obj._perm_cache.update(self.get_group_permissions(user_obj))
  48.         return user_obj._perm_cache
  49.  
  50.     def has_perm(self, user_obj, perm, obj=None):
  51.         if not user_obj.is_active:
  52.             return False
  53.         return perm in self.get_all_permissions(user_obj, obj)
  54.  
  55.     def has_module_perms(self, user_obj, app_label):
  56.         """
  57.        Returns True if user_obj has any permissions in the given app_label.
  58.        """
  59.         if not user_obj.is_active:
  60.             return False
  61.         for perm in self.get_all_permissions(user_obj):
  62.             if perm[:perm.index('.')] == app_label:
  63.                 return True
  64.         return False
  65.  
  66.     def get_user(self, user_id):
  67.         UserModel = get_user_model()
  68.         try:
  69.             return UserModel._default_manager.get(pk=user_id)
  70.         except UserModel.DoesNotExist:
  71.             return None

Última edición por minimal; 17/01/2014 a las 18:54