feat: implementacao inicial do sistema LeadRadar
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
from functools import wraps
|
||||
from flask import jsonify
|
||||
from flask_jwt_extended import get_jwt_identity, verify_jwt_in_request
|
||||
from app.extensions import db
|
||||
from app.models.user import User
|
||||
|
||||
def get_current_user():
|
||||
"""
|
||||
Retorna o objeto User autenticado no JWT atual.
|
||||
"""
|
||||
try:
|
||||
verify_jwt_in_request()
|
||||
user_id = get_jwt_identity()
|
||||
if not user_id:
|
||||
return None
|
||||
return db.session.get(User, user_id)
|
||||
except Exception:
|
||||
return None
|
||||
|
||||
def admin_required():
|
||||
"""
|
||||
Decorator que exige que o usuário autenticado tenha role == 'admin'.
|
||||
Caso contrário, bloqueia com 403 Forbidden.
|
||||
"""
|
||||
def decorator(fn):
|
||||
@wraps(fn)
|
||||
def wrapper(*args, **kwargs):
|
||||
verify_jwt_in_request()
|
||||
user_id = get_jwt_identity()
|
||||
user = db.session.get(User, user_id)
|
||||
if not user or not user.ativo:
|
||||
return jsonify({'error': 'Usuário inválido ou inativo.'}), 401
|
||||
if user.role != 'admin':
|
||||
return jsonify({'error': 'Acesso negado. Requer perfil de administrador.'}), 403
|
||||
return fn(*args, **kwargs)
|
||||
return wrapper
|
||||
return decorator
|
||||
@@ -0,0 +1,45 @@
|
||||
import re
|
||||
|
||||
def sanitize_phone(phone_str: str) -> str:
|
||||
"""
|
||||
Sanitiza um número de telefone para o formato E.164 brasileiro (ex: 5511999999999).
|
||||
Remove caracteres não numéricos e adiciona DDI 55 se ausente.
|
||||
"""
|
||||
if not phone_str:
|
||||
return ""
|
||||
|
||||
# Manter apenas digitos
|
||||
digits = re.sub(r'\D', '', str(phone_str))
|
||||
|
||||
if not digits:
|
||||
return ""
|
||||
|
||||
# Se já tiver o DDI 55 e tiver 12 ou 13 dígitos
|
||||
if digits.startswith('55') and len(digits) in (12, 13):
|
||||
return digits
|
||||
|
||||
# Se tiver 10 (fixo com DDD) ou 11 (celular com DDD) dígitos
|
||||
if len(digits) in (10, 11):
|
||||
return f"55{digits}"
|
||||
|
||||
return digits
|
||||
|
||||
def sanitize_cep(cep_str: str) -> str:
|
||||
"""
|
||||
Limpa o CEP retornando apenas 8 dígitos numéricos.
|
||||
"""
|
||||
if not cep_str:
|
||||
return ""
|
||||
digits = re.sub(r'\D', '', str(cep_str))
|
||||
if len(digits) == 8:
|
||||
return digits
|
||||
return digits
|
||||
|
||||
def validate_email(email_str: str) -> bool:
|
||||
"""
|
||||
Valida formato basico de email.
|
||||
"""
|
||||
if not email_str:
|
||||
return False
|
||||
regex = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
|
||||
return bool(re.match(regex, email_str.strip()))
|
||||
Reference in New Issue
Block a user