53e5ef4d0b
- Updated error messages and comments in main.rs, openapi.rs, portfolio.rs, predictive.rs, ai.rs, health.rs, middleware.rs, models.rs, token_limits.rs, and webhooks.rs to Spanish. - Enhanced user experience by providing localized content for Spanish-speaking users.
41 lines
1.3 KiB
Rust
41 lines
1.3 KiB
Rust
use sqlx::{Postgres, Transaction};
|
|
|
|
pub async fn set_session_context(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
user_id: Option<uuid::Uuid>,
|
|
org_id: Option<uuid::Uuid>,
|
|
ip: Option<String>,
|
|
ua: Option<String>,
|
|
event_type: Option<String>,
|
|
) -> Result<(), sqlx::Error> {
|
|
if let Some(uid) = user_id {
|
|
sqlx::query(&format!("SET LOCAL app.current_user_id = '{}'", uid))
|
|
.execute(&mut **tx)
|
|
.await?;
|
|
}
|
|
if let Some(oid) = org_id {
|
|
sqlx::query(&format!("SET LOCAL app.current_org_id = '{}'", oid))
|
|
.execute(&mut **tx)
|
|
.await?;
|
|
}
|
|
if let Some(ip_addr) = ip {
|
|
sqlx::query(&format!("SET LOCAL app.client_ip = '{}'", ip_addr))
|
|
.execute(&mut **tx)
|
|
.await?;
|
|
}
|
|
if let Some(user_agent) = ua {
|
|
// Usar set_config para cadenas potencialmente largas para evitar inyección SQL o problemas de formato
|
|
sqlx::query("SELECT set_config('app.user_agent', $1, true)")
|
|
.bind(user_agent)
|
|
.execute(&mut **tx)
|
|
.await?;
|
|
}
|
|
if let Some(et) = event_type {
|
|
sqlx::query("SELECT set_config('app.event_type', $1, true)")
|
|
.bind(et)
|
|
.execute(&mut **tx)
|
|
.await?;
|
|
}
|
|
Ok(())
|
|
}
|