feat: agrega rol 'publico' con UserProfile y permisos diferenciados
- Crea modelo UserProfile (OneToOne con User) con user_type: publico/user/administrator - Señal post_save que crea UserProfile automáticamente al crear User - UserSerializer.get_role() lee desde profile.user_type - Nuevos permisos: IsNotPublico, IsPublico y actualiza IsAdministrator - IsNotPublico restringe acceso a vistas de gestión (productos, clientes, ventas) - CatalogSaleView.create permite publico; otras acciones requieren IsNotPublico - UserProfile inline en admin de Usuarios - Data migration para backfill de usuarios existentes
This commit is contained in:
@@ -1,6 +1,26 @@
|
||||
from rest_framework.permissions import BasePermission
|
||||
|
||||
|
||||
def _get_user_type(user):
|
||||
if not user or not user.is_authenticated:
|
||||
return None
|
||||
profile = getattr(user, "profile", None)
|
||||
if profile:
|
||||
return profile.user_type
|
||||
return "administrator" if user.is_staff else "user"
|
||||
|
||||
|
||||
class IsAdministrator(BasePermission):
|
||||
def has_permission(self, request, view):
|
||||
return request.user and request.user.is_staff
|
||||
return _get_user_type(request.user) == "administrator"
|
||||
|
||||
|
||||
class IsNotPublico(BasePermission):
|
||||
def has_permission(self, request, view):
|
||||
user_type = _get_user_type(request.user)
|
||||
return user_type is not None and user_type != "publico"
|
||||
|
||||
|
||||
class IsPublico(BasePermission):
|
||||
def has_permission(self, request, view):
|
||||
return _get_user_type(request.user) == "publico"
|
||||
|
||||
Reference in New Issue
Block a user