use axum::http::HeaderMap; use crate::{ AppState, auth::GetAuthorizationRequest, definitions::common::Empty, services::authenticated_request, }; use super::state::{AddLogicPageState, CreateLogicForm, TableOption}; pub(crate) async fn load_page( state: AppState, headers: &HeaderMap, form: CreateLogicForm, error: Option, ) -> Result { let request = authenticated_request(headers, GetAuthorizationRequest {}) .map_err(|_| LoadError::Unauthenticated)?; let mut auth = state.auth; let authorization = auth .get_authorization(request) .await .map_err(|error| match error.code() { tonic::Code::Unauthenticated => LoadError::Unauthenticated, _ => LoadError::Backend(error.message().to_string()), })? .into_inner(); if authorization.role != "admin" { return Err(LoadError::Forbidden); } let mut definitions = state.definitions; let tree = definitions .get_profile_tree( authenticated_request(headers, Empty {}).map_err(|_| LoadError::Unauthenticated)?, ) .await .map_err(|error| LoadError::Backend(error.message().to_string()))? .into_inner(); let tables = tree .profiles .into_iter() .flat_map(|profile| { profile.tables.into_iter().map(move |table| TableOption { id: table.id, profile_name: profile.name.clone(), table_name: table.name, }) }) .collect(); Ok(AddLogicPageState { nav: crate::ui::Nav::new(headers, "admin").with_role(authorization.role), tables, form, error, }) } pub(crate) enum LoadError { Unauthenticated, Forbidden, Backend(String), }