2021-06-01 15:30:57 +00:00
|
|
|
use crate::{
|
|
|
|
cookies::get_cookie, create_user::CreateUserForm, login::LoginForm, logout::LogoutButton,
|
|
|
|
user_table::UserTable,
|
|
|
|
};
|
2021-05-08 09:34:55 +00:00
|
|
|
use yew::prelude::*;
|
2021-05-18 17:04:06 +00:00
|
|
|
use yew::services::ConsoleService;
|
2021-05-30 15:02:09 +00:00
|
|
|
use yew_router::{
|
|
|
|
agent::{RouteAgentDispatcher, RouteRequest},
|
2021-06-01 15:30:57 +00:00
|
|
|
components::RouterAnchor,
|
2021-05-30 15:02:09 +00:00
|
|
|
route::Route,
|
|
|
|
router::Router,
|
|
|
|
service::RouteService,
|
|
|
|
Switch,
|
|
|
|
};
|
2021-05-08 09:34:55 +00:00
|
|
|
|
2021-05-13 17:33:57 +00:00
|
|
|
pub struct App {
|
|
|
|
link: ComponentLink<Self>,
|
2021-07-04 09:19:37 +00:00
|
|
|
user_info: Option<(String, bool)>,
|
|
|
|
redirect_to: Option<String>,
|
2021-05-30 15:02:09 +00:00
|
|
|
route_dispatcher: RouteAgentDispatcher,
|
2021-05-13 17:33:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub enum Msg {
|
2021-07-04 09:19:37 +00:00
|
|
|
Login((String, bool)),
|
2021-05-18 17:04:06 +00:00
|
|
|
Logout,
|
2021-05-13 17:33:57 +00:00
|
|
|
}
|
2021-05-08 09:34:55 +00:00
|
|
|
|
2021-05-30 15:02:09 +00:00
|
|
|
#[derive(Switch, Debug, Clone)]
|
|
|
|
pub enum AppRoute {
|
|
|
|
#[to = "/login"]
|
|
|
|
Login,
|
|
|
|
#[to = "/users"]
|
|
|
|
ListUsers,
|
2021-06-01 15:30:57 +00:00
|
|
|
#[to = "/create_user"]
|
|
|
|
CreateUser,
|
2021-07-04 09:19:37 +00:00
|
|
|
#[to = "/details/{user_id}"]
|
|
|
|
UserDetails(String),
|
2021-05-30 15:02:09 +00:00
|
|
|
#[to = "/"]
|
|
|
|
Index,
|
|
|
|
}
|
|
|
|
|
2021-06-01 15:30:57 +00:00
|
|
|
type Link = RouterAnchor<AppRoute>;
|
|
|
|
|
2021-05-08 09:34:55 +00:00
|
|
|
impl Component for App {
|
|
|
|
type Message = Msg;
|
|
|
|
type Properties = ();
|
|
|
|
|
2021-05-13 17:33:57 +00:00
|
|
|
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
2021-05-30 15:02:09 +00:00
|
|
|
let mut app = Self {
|
2021-05-30 15:07:34 +00:00
|
|
|
link,
|
2021-07-04 09:19:37 +00:00
|
|
|
user_info: get_cookie("user_id")
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
ConsoleService::error(&e.to_string());
|
|
|
|
None
|
|
|
|
})
|
|
|
|
.and_then(|u| {
|
|
|
|
get_cookie("is_admin")
|
|
|
|
.map(|so| so.map(|s| (u, s == "true")))
|
|
|
|
.unwrap_or_else(|e| {
|
|
|
|
ConsoleService::error(&e.to_string());
|
|
|
|
None
|
|
|
|
})
|
|
|
|
}),
|
2021-05-30 15:02:09 +00:00
|
|
|
redirect_to: Self::get_redirect_route(),
|
|
|
|
route_dispatcher: RouteAgentDispatcher::new(),
|
|
|
|
};
|
2021-07-04 09:19:37 +00:00
|
|
|
app.apply_initial_redirections();
|
2021-05-30 15:02:09 +00:00
|
|
|
app
|
2021-05-08 09:34:55 +00:00
|
|
|
}
|
|
|
|
|
2021-05-13 17:33:57 +00:00
|
|
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
|
|
|
match msg {
|
2021-07-04 09:19:37 +00:00
|
|
|
Msg::Login((user_name, is_admin)) => {
|
|
|
|
self.user_info = Some((user_name.clone(), is_admin));
|
|
|
|
let user_route = "/details/".to_string() + &user_name;
|
2021-05-30 15:02:09 +00:00
|
|
|
self.route_dispatcher
|
|
|
|
.send(RouteRequest::ChangeRoute(Route::new_no_state(
|
2021-07-04 09:19:37 +00:00
|
|
|
self.redirect_to.as_deref().unwrap_or_else(|| {
|
|
|
|
if is_admin {
|
|
|
|
"/users"
|
|
|
|
} else {
|
|
|
|
&user_route
|
|
|
|
}
|
|
|
|
}),
|
2021-05-30 15:02:09 +00:00
|
|
|
)));
|
2021-05-13 17:33:57 +00:00
|
|
|
}
|
2021-05-18 17:04:06 +00:00
|
|
|
Msg::Logout => {
|
2021-07-04 09:19:37 +00:00
|
|
|
self.user_info = None;
|
2021-05-18 17:04:06 +00:00
|
|
|
}
|
2021-05-13 17:33:57 +00:00
|
|
|
}
|
2021-07-04 09:19:37 +00:00
|
|
|
if self.user_info.is_none() {
|
2021-05-30 15:02:09 +00:00
|
|
|
self.route_dispatcher
|
|
|
|
.send(RouteRequest::ReplaceRoute(Route::new_no_state("/login")));
|
|
|
|
}
|
2021-05-13 17:33:57 +00:00
|
|
|
true
|
2021-05-08 09:34:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn change(&mut self, _: Self::Properties) -> ShouldRender {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
2021-05-30 15:02:09 +00:00
|
|
|
let link = self.link.clone();
|
2021-05-08 09:34:55 +00:00
|
|
|
html! {
|
2021-05-09 11:26:50 +00:00
|
|
|
<div>
|
|
|
|
<h1>{ "LLDAP" }</h1>
|
2021-05-30 15:02:09 +00:00
|
|
|
<Router<AppRoute>
|
|
|
|
render = Router::render(move |switch: AppRoute| {
|
|
|
|
match switch {
|
|
|
|
AppRoute::Login => html! {
|
2021-05-30 15:07:34 +00:00
|
|
|
<LoginForm on_logged_in=link.callback(Msg::Login)/>
|
2021-05-30 15:02:09 +00:00
|
|
|
},
|
2021-06-01 15:30:57 +00:00
|
|
|
AppRoute::CreateUser => html! {
|
|
|
|
<div>
|
|
|
|
<LogoutButton on_logged_out=link.callback(|_| Msg::Logout) />
|
|
|
|
<CreateUserForm/>
|
|
|
|
</div>
|
|
|
|
},
|
2021-05-30 15:02:09 +00:00
|
|
|
AppRoute::Index | AppRoute::ListUsers => html! {
|
|
|
|
<div>
|
|
|
|
<LogoutButton on_logged_out=link.callback(|_| Msg::Logout) />
|
|
|
|
<UserTable />
|
2021-06-01 15:30:57 +00:00
|
|
|
<Link route=AppRoute::CreateUser>{"Create a user"}</Link>
|
2021-05-30 15:02:09 +00:00
|
|
|
</div>
|
2021-07-04 09:19:37 +00:00
|
|
|
},
|
|
|
|
AppRoute::UserDetails(username) => html! {
|
|
|
|
<div>
|
|
|
|
{"details about "} {&username}
|
|
|
|
</div>
|
|
|
|
},
|
2021-05-30 15:02:09 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
/>
|
2021-05-09 11:26:50 +00:00
|
|
|
</div>
|
2021-05-08 09:34:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-30 15:02:09 +00:00
|
|
|
|
|
|
|
impl App {
|
2021-07-04 09:19:37 +00:00
|
|
|
fn get_redirect_route() -> Option<String> {
|
2021-05-30 15:02:09 +00:00
|
|
|
let route_service = RouteService::<()>::new();
|
|
|
|
let current_route = route_service.get_path();
|
|
|
|
if current_route.is_empty() || current_route.contains("login") {
|
2021-07-04 09:19:37 +00:00
|
|
|
None
|
2021-05-30 15:02:09 +00:00
|
|
|
} else {
|
2021-07-04 09:19:37 +00:00
|
|
|
Some(current_route)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn apply_initial_redirections(&mut self) {
|
|
|
|
match &self.user_info {
|
|
|
|
None => {
|
|
|
|
ConsoleService::info("Redirecting to login");
|
|
|
|
self.route_dispatcher
|
|
|
|
.send(RouteRequest::ReplaceRoute(Route::new_no_state("/login")));
|
|
|
|
}
|
|
|
|
Some((user_name, is_admin)) => match &self.redirect_to {
|
|
|
|
Some(url) => {
|
|
|
|
ConsoleService::info(&format!("Redirecting to specified url: {}", url));
|
|
|
|
self.route_dispatcher
|
|
|
|
.send(RouteRequest::ReplaceRoute(Route::new_no_state(url)));
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
if *is_admin {
|
|
|
|
ConsoleService::info("Redirecting to user list");
|
|
|
|
self.route_dispatcher
|
|
|
|
.send(RouteRequest::ReplaceRoute(Route::new_no_state("/users")));
|
|
|
|
} else {
|
|
|
|
ConsoleService::info("Redirecting to user view");
|
|
|
|
self.route_dispatcher.send(RouteRequest::ReplaceRoute(
|
|
|
|
Route::new_no_state(&("/details/".to_string() + user_name)),
|
|
|
|
));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
2021-05-30 15:02:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|