use uuid::Uuid;
use crate::components::ComponentHandles;
use crate::error::ApiError;
pub async fn check_permission_via_handles(
handles: &ComponentHandles,
user_id: Uuid,
dataset_id: Uuid,
perm: &str,
) -> Result<(), ApiError> {
if is_authorization_disabled() {
return Ok(());
}
let Some(ref acl) = handles.acl_db else {
return Ok(());
};
let allowed = acl
.has_permission_with_roles(user_id, dataset_id, perm)
.await
.map_err(|e| ApiError::Internal(anyhow::anyhow!("ACL check failed: {e}")))?;
if allowed {
Ok(())
} else {
Err(ApiError::Forbidden(format!(
"No {perm} permission on dataset {dataset_id}"
)))
}
}
pub fn is_authorization_required() -> bool {
!is_authorization_disabled()
}
fn is_authorization_disabled() -> bool {
matches!(
std::env::var("REQUIRE_AUTHORIZATION")
.as_deref()
.unwrap_or("true"),
"false" | "0" | "no"
)
}