graphs renamed to web
This commit is contained in:
59
web/src/pages/add_logic/loader.rs
Normal file
59
web/src/pages/add_logic/loader.rs
Normal file
@@ -0,0 +1,59 @@
|
||||
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<String>,
|
||||
) -> Result<AddLogicPageState, LoadError> {
|
||||
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 { tables, form, error })
|
||||
}
|
||||
|
||||
pub(crate) enum LoadError {
|
||||
Unauthenticated,
|
||||
Forbidden,
|
||||
Backend(String),
|
||||
}
|
||||
Reference in New Issue
Block a user