|
| 1 | +//! Check reads use live person grants; SQL needs its own permission and is audited. |
| 2 | +
|
| 3 | +use axum::extract::{Path, Query, State}; |
| 4 | +use axum::{Extension, Json}; |
| 5 | +use uuid::Uuid; |
| 6 | + |
| 7 | +use crate::api::errors::ApiError; |
| 8 | +use crate::api::models::ReadQuery; |
| 9 | +use crate::credentials::models::{AuditContext, AuthenticatedPerson, DefaultRole}; |
| 10 | +use crate::store::models::ControlStore; |
| 11 | +use crate::warehouse_metadata::main::read_person_check::read_person_check; |
| 12 | +use crate::warehouse_metadata::main::read_person_check_sql::read_person_check_sql; |
| 13 | +use crate::warehouse_metadata::main::read_person_checks::read_person_checks; |
| 14 | +use crate::warehouse_metadata::models::{CheckResult, CheckSql}; |
| 15 | + |
| 16 | +#[utoipa::path(get, path = "/api/control/locations/{location_id}/targets/{target}/checks", operation_id = "listPersonChecks", |
| 17 | + params(("location_id" = Uuid, Path), ("target" = String, Path)), |
| 18 | + responses((status = 200, body = Vec<CheckResult>, description = "The newest result of every check"), |
| 19 | + (status = 401, description = "Session rejected"), |
| 20 | + (status = 403, body = crate::api::models::ApiErrorBody)), security(("session" = []), ("cliSession" = [])))] |
| 21 | +pub(crate) async fn list( |
| 22 | + (State(store), Extension(context)): (State<ControlStore>, Extension<AuditContext>), |
| 23 | + Extension(default_role): Extension<DefaultRole>, |
| 24 | + Path((location_id, target)): Path<(Uuid, String)>, |
| 25 | + Extension(AuthenticatedPerson(principal, _)): Extension<AuthenticatedPerson>, |
| 26 | +) -> Result<Json<Vec<CheckResult>>, ApiError> { |
| 27 | + Ok(Json( |
| 28 | + read_person_checks( |
| 29 | + &store, |
| 30 | + (&context, principal), |
| 31 | + (location_id, &target), |
| 32 | + default_role, |
| 33 | + ) |
| 34 | + .await?, |
| 35 | + )) |
| 36 | +} |
| 37 | + |
| 38 | +#[utoipa::path(get, path = "/api/control/locations/{location_id}/targets/{target}/checks/{binding_key}", operation_id = "readPersonCheckHistory", |
| 39 | + params(("location_id" = Uuid, Path), ("target" = String, Path), ("binding_key" = String, Path), |
| 40 | + ("limit" = Option<u32>, Query, description = "Maximum results, newest first; defaults to 50, capped at 1000")), |
| 41 | + responses((status = 200, body = Vec<CheckResult>, description = "Empty for an unknown check"), |
| 42 | + (status = 401, description = "Session rejected"), |
| 43 | + (status = 403, body = crate::api::models::ApiErrorBody)), security(("session" = []), ("cliSession" = [])))] |
| 44 | +pub(crate) async fn history( |
| 45 | + (State(store), Extension(context)): (State<ControlStore>, Extension<AuditContext>), |
| 46 | + Extension(default_role): Extension<DefaultRole>, |
| 47 | + (Path((location_id, target, binding_key)), Query(options)): ( |
| 48 | + Path<(Uuid, String, String)>, |
| 49 | + Query<ReadQuery>, |
| 50 | + ), |
| 51 | + Extension(AuthenticatedPerson(principal, _)): Extension<AuthenticatedPerson>, |
| 52 | +) -> Result<Json<Vec<CheckResult>>, ApiError> { |
| 53 | + Ok(Json( |
| 54 | + read_person_check( |
| 55 | + &store, |
| 56 | + (&context, principal), |
| 57 | + (location_id, &target, &binding_key), |
| 58 | + (options.limit.unwrap_or(50), default_role), |
| 59 | + ) |
| 60 | + .await?, |
| 61 | + )) |
| 62 | +} |
| 63 | + |
| 64 | +#[utoipa::path(get, path = "/api/control/locations/{location_id}/targets/{target}/checks/{binding_key}/sql", operation_id = "readPersonCheckSql", |
| 65 | + params(("location_id" = Uuid, Path), ("target" = String, Path), ("binding_key" = String, Path)), |
| 66 | + responses((status = 200, body = Option<CheckSql>), (status = 401, description = "Session rejected"), |
| 67 | + (status = 403, body = crate::api::models::ApiErrorBody)), security(("session" = []), ("cliSession" = [])))] |
| 68 | +pub(crate) async fn sql( |
| 69 | + (State(store), Extension(context)): (State<ControlStore>, Extension<AuditContext>), |
| 70 | + Path((location_id, target, binding_key)): Path<(Uuid, String, String)>, |
| 71 | + Extension(AuthenticatedPerson(principal, _)): Extension<AuthenticatedPerson>, |
| 72 | +) -> Result<Json<Option<CheckSql>>, ApiError> { |
| 73 | + Ok(Json( |
| 74 | + read_person_check_sql( |
| 75 | + &store, |
| 76 | + (&context, principal), |
| 77 | + (location_id, &target, &binding_key), |
| 78 | + ) |
| 79 | + .await?, |
| 80 | + )) |
| 81 | +} |
0 commit comments