feat(reception,udeci): v3.8.48 - forced scanner focus, spanish keyboard gun normalization, reception default dashboard, and udeci commission listings
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import re
|
||||
import hmac
|
||||
import hashlib
|
||||
import json
|
||||
@@ -62,8 +63,9 @@ class QRSecurityService:
|
||||
"""
|
||||
Decodifica y valida el token escaneado. Soporta:
|
||||
1. Token JWT firmado institucional.
|
||||
2. Formato prefijado 'UNICABA:QR:<token>'.
|
||||
3. Formato fallback por DNI 'DNI:<documento>' o número de documento directo.
|
||||
2. Formato prefijado 'UNICABA:QR:<token>' (o con 'Ñ' por pistola en teclado español).
|
||||
3. Formato institucional por ID o DNI 'UNICABA:USR:<id>:DNI:<doc>' (o 'UNICABAÑUSRÑ<id>ÑDNIÑ<doc>').
|
||||
4. Formato fallback por DNI 'DNI:<documento>' o número de documento directo.
|
||||
|
||||
Retorna: (User or None, status: 'AUTORIZADO' | 'DENEGADO' | 'RESTRINGIDO', reason: str)
|
||||
"""
|
||||
@@ -72,15 +74,44 @@ class QRSecurityService:
|
||||
|
||||
clean = raw_input.strip()
|
||||
|
||||
# Si viene con prefijo UNICABA:QR:
|
||||
if clean.startswith("UNICABA:QR:"):
|
||||
clean = clean[len("UNICABA:QR:"):].strip()
|
||||
# Normalizar pistola óptica en teclado en español:
|
||||
# Los lectores ópticos emulan teclado US, por lo que envían 'Ñ' en lugar de ':' en sistemas operativos con distribución en español.
|
||||
clean_normalized = clean.replace("Ñ", ":").replace("ñ", ":")
|
||||
|
||||
# 1. Intentar validar como JWT institucional
|
||||
if clean.count(".") == 2:
|
||||
# Si viene con prefijo UNICABA:QR:
|
||||
if clean_normalized.startswith("UNICABA:QR:"):
|
||||
clean_normalized = clean_normalized[len("UNICABA:QR:"):].strip()
|
||||
|
||||
# A. Intentar extraer patrón institucional UNICABA:USR:<id>
|
||||
usr_match = re.search(r"USR:(\d+)", clean_normalized)
|
||||
if usr_match:
|
||||
try:
|
||||
u_id = int(usr_match.group(1))
|
||||
if u_id > 0:
|
||||
user_by_id = User.query.get(u_id)
|
||||
if user_by_id:
|
||||
if not user_by_id.is_active:
|
||||
return user_by_id, "RESTRINGIDO", "Usuario inactivo o suspendido en el sistema institucional."
|
||||
return user_by_id, "AUTORIZADO", f"Credencial institucional validada por ID #{user_by_id.id}."
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# B. Intentar extraer patrón institucional DNI:<doc>
|
||||
dni_match = re.search(r"DNI:([a-zA-Z0-9]+)", clean_normalized)
|
||||
if dni_match:
|
||||
doc = dni_match.group(1).strip()
|
||||
if doc and doc.upper() != "N/A":
|
||||
user_by_dni = User.query.filter_by(document_number=doc).first()
|
||||
if user_by_dni:
|
||||
if not user_by_dni.is_active:
|
||||
return user_by_dni, "RESTRINGIDO", "Usuario inactivo en el sistema institucional."
|
||||
return user_by_dni, "AUTORIZADO", f"Identificación validada por documento de identidad (DNI {doc})."
|
||||
|
||||
# C. Intentar validar como JWT institucional
|
||||
if clean_normalized.count(".") == 2:
|
||||
try:
|
||||
payload = jwt.decode(
|
||||
clean,
|
||||
clean_normalized,
|
||||
cls._get_secret(),
|
||||
algorithms=["HS256"],
|
||||
options={"verify_exp": True}
|
||||
@@ -102,18 +133,25 @@ class QRSecurityService:
|
||||
except jwt.PyJWTError as e:
|
||||
return None, "DENEGADO", f"Firma criptográfica inválida: {str(e)}"
|
||||
|
||||
# 2. Fallback: Lectura de pistola óptica o documento directo (ej: 'DNI:12345678' o '12345678')
|
||||
doc_num = clean
|
||||
# D. Fallback: Lectura de pistola óptica o documento directo (ej: 'DNI:12345678' o '12345678')
|
||||
doc_num = clean_normalized
|
||||
if doc_num.upper().startswith("DNI:"):
|
||||
doc_num = doc_num[4:].strip()
|
||||
|
||||
user = User.query.filter_by(document_number=doc_num).first()
|
||||
if user:
|
||||
if not user.is_active:
|
||||
return user, "RESTRINGIDO", "Usuario inactivo en el sistema institucional."
|
||||
return user, "AUTORIZADO", "Identificación validada por documento de identidad."
|
||||
# Quitar cualquier carácter residual no alfanumérico si es número de DNI
|
||||
clean_doc = re.sub(r'[^a-zA-Z0-9]', '', doc_num)
|
||||
if clean_doc:
|
||||
user = User.query.filter_by(document_number=clean_doc).first()
|
||||
if not user and clean_doc.isdigit():
|
||||
# Si no coincide con documento, verificar si coincide con ID institucional directo
|
||||
user = User.query.get(int(clean_doc))
|
||||
|
||||
return None, "DENEGADO", f"No se encontró ningún estudiante o docente con DNI {doc_num}."
|
||||
if user:
|
||||
if not user.is_active:
|
||||
return user, "RESTRINGIDO", "Usuario inactivo en el sistema institucional."
|
||||
return user, "AUTORIZADO", f"Identificación validada por documento de identidad ({user.document_number or user.id})."
|
||||
|
||||
return None, "DENEGADO", f"No se encontró ningún estudiante o docente con DNI o credencial '{raw_input.strip()}'."
|
||||
|
||||
@classmethod
|
||||
def get_todays_classes_for_user(cls, user: User, target_date: Optional[date] = None) -> List[TodayClassDTO]:
|
||||
|
||||
Reference in New Issue
Block a user