kanidm/kanidmd_web_ui/src/manager.rs

127 lines
3.6 KiB
Rust
Raw Normal View History

2021-07-25 02:51:37 +02:00
//! This is the top level router of the web ui for kanidm. It decides based on the incoming
//! request, where to direct this too, and if the requirements for that request have been
//! met before rendering. For example, if you land here with an oauth request, but you are
//! not atuhenticated, this will determine that and send you to authentication first, then
//! will allow you to proceed with the oauth flow.
2021-12-31 00:11:20 +01:00
use gloo::console;
2022-10-01 08:08:51 +02:00
use serde::{Deserialize, Serialize};
use wasm_bindgen::UnwrapThrowExt;
2021-12-31 00:11:20 +01:00
use yew::functional::*;
2021-07-25 02:51:37 +02:00
use yew::prelude::*;
use yew_router::prelude::*;
use crate::credential::reset::CredentialResetApp;
2021-07-25 02:51:37 +02:00
use crate::login::LoginApp;
use crate::oauth2::Oauth2App;
use crate::views::{ViewRoute, ViewsApp};
2021-07-25 02:51:37 +02:00
// router to decide on state.
2022-09-02 06:21:20 +02:00
#[derive(Routable, PartialEq, Eq, Clone, Debug, Serialize, Deserialize)]
2021-07-25 02:51:37 +02:00
pub enum Route {
#[at("/")]
Landing,
#[at("/ui/view/*")]
Views,
2021-07-25 02:51:37 +02:00
#[at("/ui/login")]
Login,
#[at("/ui/oauth2")]
Oauth2,
#[at("/ui/reset")]
CredentialReset,
2021-07-25 02:51:37 +02:00
#[not_found]
#[at("/ui/404")]
2021-07-25 02:51:37 +02:00
NotFound,
}
2021-12-31 00:11:20 +01:00
#[function_component(Landing)]
fn landing() -> Html {
// Do this to allow use_history to work because lol.
use_history()
.expect_throw("Unable to access history")
.push(ViewRoute::Apps);
html! { <main></main> }
2021-12-31 00:11:20 +01:00
}
fn switch(route: &Route) -> Html {
2022-08-01 07:52:01 +02:00
#[cfg(debug)]
console::debug!("manager::switch");
match route {
#[allow(clippy::let_unit_value)]
2021-12-31 00:11:20 +01:00
Route::Landing => html! { <Landing /> },
#[allow(clippy::let_unit_value)]
2021-07-25 02:51:37 +02:00
Route::Login => html! { <LoginApp /> },
#[allow(clippy::let_unit_value)]
2021-07-25 02:51:37 +02:00
Route::Oauth2 => html! { <Oauth2App /> },
#[allow(clippy::let_unit_value)]
Route::Views => html! { <ViewsApp /> },
#[allow(clippy::let_unit_value)]
Route::CredentialReset => html! { <CredentialResetApp /> },
2021-07-25 02:51:37 +02:00
Route::NotFound => {
add_body_form_classes!();
2021-07-25 02:51:37 +02:00
html! {
<>
<main class="flex-shrink-0 form-signin text-center">
<img src="/pkg/img/logo-square.svg" alt="Kanidm" class="kanidm_logo"/>
// TODO: replace this with a call to domain info
<h3>{ "404 - Page not found" }</h3>
<div class="container">
<Link<ViewRoute> to={ ViewRoute::Apps }>
{ "Home" }
</Link<ViewRoute>>
</div>
</main>
{ crate::utils::do_footer() }
</>
2021-07-25 02:51:37 +02:00
}
}
}
}
pub struct ManagerApp {}
2021-07-25 02:51:37 +02:00
impl Component for ManagerApp {
type Message = ();
2021-07-25 02:51:37 +02:00
type Properties = ();
2021-12-31 00:11:20 +01:00
fn create(_ctx: &Context<Self>) -> Self {
2022-08-01 07:52:01 +02:00
#[cfg(debug)]
console::debug!("manager::create");
ManagerApp {}
2021-07-25 02:51:37 +02:00
}
2021-12-31 00:11:20 +01:00
fn changed(&mut self, _ctx: &Context<Self>) -> bool {
2022-08-01 07:52:01 +02:00
#[cfg(debug)]
console::debug!("manager::change");
2021-07-25 02:51:37 +02:00
false
}
fn update(&mut self, _ctx: &Context<Self>, _msg: Self::Message) -> bool {
2022-08-01 07:52:01 +02:00
#[cfg(debug)]
console::debug!("manager::update");
2021-07-25 02:51:37 +02:00
true
}
fn rendered(&mut self, _ctx: &Context<Self>, _first_render: bool) {
2022-08-01 07:52:01 +02:00
#[cfg(debug)]
console::debug!("manager::rendered");
// Can only access the current_route AFTER it renders.
2022-08-01 07:52:01 +02:00
// console::debug!(format!("{:?}", yew_router::current_route::<Route>()).as_str())
2021-07-25 02:51:37 +02:00
}
2021-12-31 00:11:20 +01:00
fn view(&self, _ctx: &Context<Self>) -> Html {
2021-07-25 02:51:37 +02:00
html! {
<BrowserRouter>
<Switch<Route> render={ Switch::render(switch) } />
</BrowserRouter>
2021-07-25 02:51:37 +02:00
}
}
}