35 lines
1.2 KiB
Rust
35 lines
1.2 KiB
Rust
use sqlx::{Postgres, Transaction};
|
|
pub async fn allowed(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
principal: &str,
|
|
permission: &str,
|
|
) -> Result<bool, sqlx::Error> {
|
|
sqlx::query_scalar("SELECT EXISTS(SELECT 1 FROM memberships WHERE principal=$1 AND revoked_at IS NULL AND $2=ANY(permissions)) AND (left($1,5)<>'oidc:' OR oidc_principal_enabled($1)) AND (left($1,4)<>'mcp:' OR mcp_principal_permission($1,$2))").bind(principal).bind(permission).fetch_one(&mut **tx).await
|
|
}
|
|
pub async fn require_principal(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
p: &crate::auth::Principal,
|
|
permission: &str,
|
|
) -> Result<(), crate::jobs::Error> {
|
|
if !p.permits_scope(permission) {
|
|
return Err(crate::jobs::Error(
|
|
axum::http::StatusCode::FORBIDDEN,
|
|
"FORBIDDEN",
|
|
));
|
|
}
|
|
require(tx, &p.name, permission).await
|
|
}
|
|
pub async fn require(
|
|
tx: &mut Transaction<'_, Postgres>,
|
|
principal: &str,
|
|
permission: &str,
|
|
) -> Result<(), crate::jobs::Error> {
|
|
if !allowed(tx, principal, permission).await? {
|
|
return Err(crate::jobs::Error(
|
|
axum::http::StatusCode::FORBIDDEN,
|
|
"FORBIDDEN",
|
|
));
|
|
}
|
|
Ok(())
|
|
}
|