Cancelar cambio de password
This commit is contained in:
@@ -132,6 +132,111 @@ function enviarCodigoInterno()
|
||||
}
|
||||
}
|
||||
|
||||
function reenviarCodigoInterno()
|
||||
{
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// Validar sesión y datos requeridos
|
||||
if (!isset($_SESSION['usuario_id'], $_SESSION['usuario_email'])) {
|
||||
echo json_encode(['success' => false, 'message' => '⚠️ Sesión expirada. Vuelve a ingresar tu correo.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$conn = getConnection();
|
||||
if (!$conn) {
|
||||
echo json_encode(['success' => false, 'message' => '❌ Error de conexión con la base de datos.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$id_usuario = $_SESSION['usuario_id'];
|
||||
$emailEncrypted = encrypt($_SESSION['usuario_email']);
|
||||
|
||||
// Obtener el último código activo de recuperación
|
||||
$now = (new DateTime())->format('Y-m-d H:i:s');
|
||||
$sql = "SELECT TOP 1 codigo FROM recuperacion_password
|
||||
WHERE email = ? AND estatus = 0 AND expiracion > ?
|
||||
ORDER BY expiracion DESC";
|
||||
$stmt = sqlsrv_query($conn, $sql, [$emailEncrypted, $now]);
|
||||
|
||||
if (!$stmt || !($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))) {
|
||||
echo json_encode(['success' => false, 'message' => '❌ No se encontró un código activo para reenviar.']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$codigo = $row['codigo'];
|
||||
|
||||
// Llamar a la función para reenviar el correo al respaldo
|
||||
if (enviarCorreoRespaldo($conn, $id_usuario, $codigo)) {
|
||||
echo json_encode(['success' => true, 'message' => '📤 Código reenviado al correo de respaldo.']);
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'message' => '❌ Error al reenviar el código al correo de respaldo.']);
|
||||
}
|
||||
|
||||
exit;
|
||||
}
|
||||
|
||||
function enviarCorreoRespaldo($conn, $id_usuario, $codigo)
|
||||
{
|
||||
// Obtener correo de respaldo del usuario
|
||||
$sql = "SELECT correo FROM correo_respaldo WHERE id_usuario = ?";
|
||||
$stmt = sqlsrv_query($conn, $sql, [$id_usuario]);
|
||||
|
||||
if (!$stmt || !($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$correoRespaldo = $row['correo'];
|
||||
if (empty($correoRespaldo)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Enviar código por correo al correo de respaldo
|
||||
try {
|
||||
$mail = new PHPMailer(true);
|
||||
$mail->isSMTP();
|
||||
$mail->Host = 'secure.emailsrvr.com';
|
||||
$mail->SMTPAuth = true;
|
||||
$mail->Username = 'noreply@aduanasoft.com.mx';
|
||||
$mail->Password = $_ENV['SMTP_PASS'];
|
||||
$mail->SMTPSecure = PHPMailer::ENCRYPTION_STARTTLS;
|
||||
$mail->Port = 587;
|
||||
|
||||
$mail->setFrom('noreply@aduanasoft.com.mx', 'SIIH | AduanaSoft');
|
||||
$mail->addAddress($correoRespaldo);
|
||||
$mail->CharSet = 'UTF-8';
|
||||
$mail->isHTML(true);
|
||||
$mail->Subject = 'Código para cambio de contraseña';
|
||||
|
||||
$mail->Body = "
|
||||
<div style='font-family: Segoe UI, sans-serif; background-color: #f4f6f9; padding: 40px;'>
|
||||
<div style='max-width: 600px; margin: auto; background: #fff; border: 1px solid #ddd; border-radius: 10px; overflow: hidden;'>
|
||||
<div style='background: linear-gradient(to right, #003366, #0055A5); padding: 20px; text-align: center;'>
|
||||
<h2 style='color: white;'>Cambio de contraseña</h2>
|
||||
</div>
|
||||
<div style='padding: 30px; color: #333; font-size: 16px;'>
|
||||
<p>Has solicitado cambiar tu contraseña desde tu cuenta.</p>
|
||||
<div style='text-align: center; margin: 30px 0;'>
|
||||
<p>Tu código de verificación es:</p>
|
||||
<h2 style='color:#333; background: #f8f9fa; padding: 15px; border-radius: 8px; border: 2px dashed #0055A5;'>$codigo</h2>
|
||||
</div>
|
||||
<p>Este código expirará en 10 minutos.</p>
|
||||
<p><strong>Si no solicitaste este cambio, ignora este correo.</strong></p>
|
||||
</div>
|
||||
<div style='background: #e9ecef; text-align: center; padding: 15px; font-size: 13px; color: #666;'>
|
||||
© " . date('Y') . " SIIH · Sistema Integral para Importadores de Hidrocarburos
|
||||
</div>
|
||||
</div>
|
||||
</div>";
|
||||
|
||||
$mail->send();
|
||||
return true;
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error al enviar correo de respaldo: {$mail->ErrorInfo}");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function verificarCodigoInternoView()
|
||||
{
|
||||
// Verificar si el usuario ya está autenticado
|
||||
|
||||
@@ -12,6 +12,8 @@ $dos_factores_estado = obtenerEstadoDosFactores();
|
||||
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
|
||||
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
|
||||
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
||||
<!-- SweetAlert2 CDN -->
|
||||
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/2.1.2/sweetalert.min.js"></script>
|
||||
<style>
|
||||
body { font-family: 'Segoe UI', sans-serif; background-color: #f4f6f9; }
|
||||
@@ -68,6 +70,8 @@ $dos_factores_estado = obtenerEstadoDosFactores();
|
||||
.input-group { position: relative; }
|
||||
.clear-input { position: absolute; right: 10px; top: 50%; transform: translateY(-50%); background: none; border: none; color: #ccc; cursor: pointer; z-index: 5; display: none; }
|
||||
.clear-input:hover { color: #666; }
|
||||
/* Forzar SweetAlert2 al frente */
|
||||
.swal2-container { z-index: 11000 !important; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -137,6 +141,12 @@ $dos_factores_estado = obtenerEstadoDosFactores();
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
|
||||
<!-- Botón para cancelar el cambio -->
|
||||
<div class="text-center mt-2">
|
||||
<button type="button" id="btnCancelarCambio" class="btn btn-outline-danger" title="Cancelar cambio de contraseña">
|
||||
<i class="fas fa-times-circle me-1"></i> Cancelar cambio
|
||||
</button>
|
||||
</div>
|
||||
<!-- Contador de intentos -->
|
||||
<div id="intentosInfo" class="mt-2 text-center text-muted" style="font-size: 12px;">
|
||||
<!-- Se llenará dinámicamente -->
|
||||
@@ -526,7 +536,7 @@ $dos_factores_estado = obtenerEstadoDosFactores();
|
||||
this.disabled = true;
|
||||
this.innerHTML = '<i class="fas fa-spinner fa-spin me-1"></i>Enviando...';
|
||||
|
||||
fetch("/IMPORTADORES/reset/reenviarCodigo", {
|
||||
fetch("/IMPORTADORES/reset/reenviarCodigoInterno", {
|
||||
method: "POST",
|
||||
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
|
||||
body: "accion=reenviarCodigo"
|
||||
@@ -567,6 +577,52 @@ $dos_factores_estado = obtenerEstadoDosFactores();
|
||||
this.dispatchEvent(new Event('input'));
|
||||
});
|
||||
});
|
||||
document.getElementById('btnCancelarCambio').addEventListener('click', () => {
|
||||
Swal.fire({
|
||||
title: '¿Cancelar cambio de contraseña?',
|
||||
text: 'Se invalidará el código y se cancelará el proceso actual.',
|
||||
icon: 'warning',
|
||||
showCancelButton: true,
|
||||
confirmButtonColor: '#d33',
|
||||
cancelButtonColor: '#3085d6',
|
||||
confirmButtonText: 'Sí, cancelar',
|
||||
cancelButtonText: 'No, continuar'
|
||||
}).then((result) => {
|
||||
if (result.isConfirmed) {
|
||||
fetch('/IMPORTADORES/reset/cancelarCambioInterno', {
|
||||
method: 'POST'
|
||||
})
|
||||
.then(resp => resp.json())
|
||||
.then(data => {
|
||||
if (data.success) {
|
||||
Swal.fire({
|
||||
icon: 'success',
|
||||
title: 'Proceso cancelado',
|
||||
text: data.message,
|
||||
confirmButtonText: 'Aceptar'
|
||||
}).then(() => {
|
||||
// Redirigir al usuario
|
||||
window.location.href = '/IMPORTADORES/seguridad/index';
|
||||
});
|
||||
} else {
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
title: 'Error',
|
||||
text: 'No se pudo cancelar el proceso.'
|
||||
});
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('Error al cancelar:', err);
|
||||
Swal.fire({
|
||||
icon: 'error',
|
||||
title: 'Error',
|
||||
text: 'Ocurrió un error al intentar cancelar.'
|
||||
});
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
</script>
|
||||
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user