117 lines
3.0 KiB
Python
117 lines
3.0 KiB
Python
|
|
# /SGMP_PROD/solicitacoes/intf_winthor.py
|
||
|
|
import os
|
||
|
|
import oracledb
|
||
|
|
|
||
|
|
# Tenta inicializar o Oracle Client usando a variável de ambiente ou caminho padrão
|
||
|
|
# No Docker, usa /opt/oracle/instantclient_21_13 (definido no Dockerfile)
|
||
|
|
# Em desenvolvimento local, pode usar /usr/lib/oracle/23/client64/lib
|
||
|
|
_oracle_client_initialized = False
|
||
|
|
|
||
|
|
def _init_oracle_client():
|
||
|
|
"""Inicializa o Oracle Client de forma lazy e segura."""
|
||
|
|
global _oracle_client_initialized
|
||
|
|
|
||
|
|
if _oracle_client_initialized:
|
||
|
|
return
|
||
|
|
|
||
|
|
oracle_lib_dir = os.getenv("ORACLE_LIB_DIR", "/opt/oracle/instantclient_21_13")
|
||
|
|
|
||
|
|
# Tenta inicializar com o caminho do Docker primeiro
|
||
|
|
try:
|
||
|
|
oracledb.init_oracle_client(lib_dir=oracle_lib_dir)
|
||
|
|
_oracle_client_initialized = True
|
||
|
|
return
|
||
|
|
except (oracledb.exceptions.DatabaseError, Exception):
|
||
|
|
pass
|
||
|
|
|
||
|
|
# Se falhar, tenta o caminho alternativo (desenvolvimento local)
|
||
|
|
if oracle_lib_dir != "/usr/lib/oracle/23/client64/lib":
|
||
|
|
try:
|
||
|
|
oracledb.init_oracle_client(lib_dir="/usr/lib/oracle/23/client64/lib")
|
||
|
|
_oracle_client_initialized = True
|
||
|
|
return
|
||
|
|
except (oracledb.exceptions.DatabaseError, Exception):
|
||
|
|
pass
|
||
|
|
|
||
|
|
# Se ambos falharem, marca como inicializado para não tentar novamente
|
||
|
|
# O erro será lançado quando tentar usar as funções
|
||
|
|
_oracle_client_initialized = True
|
||
|
|
|
||
|
|
def get_oracle_connection():
|
||
|
|
_init_oracle_client()
|
||
|
|
|
||
|
|
dsn = oracledb.makedsn(
|
||
|
|
host="10.1.1.241",
|
||
|
|
port=1521,
|
||
|
|
service_name="WINT"
|
||
|
|
)
|
||
|
|
|
||
|
|
return oracledb.connect(
|
||
|
|
user="SEVEN",
|
||
|
|
password="usr54sev",
|
||
|
|
dsn=dsn
|
||
|
|
)
|
||
|
|
|
||
|
|
def buscar_colaborador_oracle(cpf: str):
|
||
|
|
conn = get_oracle_connection()
|
||
|
|
cursor = conn.cursor()
|
||
|
|
|
||
|
|
query = """
|
||
|
|
SELECT
|
||
|
|
MATRICULA,
|
||
|
|
NOME,
|
||
|
|
CPF,
|
||
|
|
SITUACAO,
|
||
|
|
ADMISSAO,
|
||
|
|
DT_EXCLUSAO,
|
||
|
|
ENDERECO,
|
||
|
|
BAIRRO,
|
||
|
|
CIDADE,
|
||
|
|
ESTADO
|
||
|
|
FROM
|
||
|
|
PCEMPR
|
||
|
|
WHERE
|
||
|
|
CPF = :cpf
|
||
|
|
"""
|
||
|
|
cursor.execute(query, {"cpf": cpf})
|
||
|
|
row = cursor.fetchone()
|
||
|
|
colunas = [c[0].lower() for c in cursor.description] if row else []
|
||
|
|
cursor.close()
|
||
|
|
conn.close()
|
||
|
|
|
||
|
|
if not row:
|
||
|
|
return None
|
||
|
|
|
||
|
|
return dict(zip(colunas, row))
|
||
|
|
|
||
|
|
def autenticar_usuario(login: str, senha: str):
|
||
|
|
conn = get_oracle_connection()
|
||
|
|
cursor = conn.cursor()
|
||
|
|
|
||
|
|
query = """
|
||
|
|
SELECT
|
||
|
|
MATRICULA,
|
||
|
|
USUARIOBD,
|
||
|
|
NOME,
|
||
|
|
EMAIL,
|
||
|
|
CPF,
|
||
|
|
CODFILIAL,
|
||
|
|
SITUACAO
|
||
|
|
FROM PCEMPR
|
||
|
|
WHERE USUARIOBD = UPPER(:login)
|
||
|
|
AND SENHABD = CRYPT(UPPER(:senha), USUARIOBD)
|
||
|
|
AND SITUACAO = 'A'
|
||
|
|
"""
|
||
|
|
|
||
|
|
cursor.execute(query, {"login": login, "senha": senha})
|
||
|
|
row = cursor.fetchone()
|
||
|
|
colunas = [c[0].lower() for c in cursor.description] if row else []
|
||
|
|
cursor.close()
|
||
|
|
conn.close()
|
||
|
|
|
||
|
|
if not row:
|
||
|
|
return None # login ou senha inválidos
|
||
|
|
|
||
|
|
return dict(zip(colunas, row))
|
||
|
|
|