mirror of
https://github.com/nitnelave/lldap.git
synced 2023-04-12 14:25:13 +00:00
Propagate the admin status to the top component
This commit is contained in:
parent
6744a89922
commit
5e0b41998f
@ -123,7 +123,7 @@ impl HostService {
|
|||||||
|
|
||||||
pub fn login_finish(
|
pub fn login_finish(
|
||||||
request: login::ClientLoginFinishRequest,
|
request: login::ClientLoginFinishRequest,
|
||||||
callback: Callback<Result<String>>,
|
callback: Callback<Result<(String, bool)>>,
|
||||||
) -> Result<FetchTask> {
|
) -> Result<FetchTask> {
|
||||||
call_server(
|
call_server(
|
||||||
"/auth/opaque/login/finish",
|
"/auth/opaque/login/finish",
|
||||||
@ -134,8 +134,12 @@ impl HostService {
|
|||||||
get_claims_from_jwt(&data)
|
get_claims_from_jwt(&data)
|
||||||
.map_err(|e| anyhow!("Could not parse response: {}", e))
|
.map_err(|e| anyhow!("Could not parse response: {}", e))
|
||||||
.and_then(|jwt_claims| {
|
.and_then(|jwt_claims| {
|
||||||
|
let is_admin = jwt_claims.groups.contains("lldap_admin");
|
||||||
set_cookie("user_id", &jwt_claims.user, &jwt_claims.exp)
|
set_cookie("user_id", &jwt_claims.user, &jwt_claims.exp)
|
||||||
.map(|_| jwt_claims.user.clone())
|
.map(|_| {
|
||||||
|
set_cookie("is_admin", &is_admin.to_string(), &jwt_claims.exp)
|
||||||
|
})
|
||||||
|
.map(|_| (jwt_claims.user.clone(), is_admin))
|
||||||
.map_err(|e| anyhow!("Error clearing cookie: {}", e))
|
.map_err(|e| anyhow!("Error clearing cookie: {}", e))
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
|
@ -15,13 +15,13 @@ use yew_router::{
|
|||||||
|
|
||||||
pub struct App {
|
pub struct App {
|
||||||
link: ComponentLink<Self>,
|
link: ComponentLink<Self>,
|
||||||
user_name: Option<String>,
|
user_info: Option<(String, bool)>,
|
||||||
redirect_to: String,
|
redirect_to: Option<String>,
|
||||||
route_dispatcher: RouteAgentDispatcher,
|
route_dispatcher: RouteAgentDispatcher,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
Login(String),
|
Login((String, bool)),
|
||||||
Logout,
|
Logout,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -33,6 +33,8 @@ pub enum AppRoute {
|
|||||||
ListUsers,
|
ListUsers,
|
||||||
#[to = "/create_user"]
|
#[to = "/create_user"]
|
||||||
CreateUser,
|
CreateUser,
|
||||||
|
#[to = "/details/{user_id}"]
|
||||||
|
UserDetails(String),
|
||||||
#[to = "/"]
|
#[to = "/"]
|
||||||
Index,
|
Index,
|
||||||
}
|
}
|
||||||
@ -46,35 +48,47 @@ impl Component for App {
|
|||||||
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
fn create(_: Self::Properties, link: ComponentLink<Self>) -> Self {
|
||||||
let mut app = Self {
|
let mut app = Self {
|
||||||
link,
|
link,
|
||||||
user_name: get_cookie("user_id").unwrap_or_else(|e| {
|
user_info: get_cookie("user_id")
|
||||||
ConsoleService::error(&e.to_string());
|
.unwrap_or_else(|e| {
|
||||||
None
|
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
|
||||||
|
})
|
||||||
|
}),
|
||||||
redirect_to: Self::get_redirect_route(),
|
redirect_to: Self::get_redirect_route(),
|
||||||
route_dispatcher: RouteAgentDispatcher::new(),
|
route_dispatcher: RouteAgentDispatcher::new(),
|
||||||
};
|
};
|
||||||
if app.user_name.is_none() {
|
app.apply_initial_redirections();
|
||||||
ConsoleService::info("Redirecting to login");
|
|
||||||
app.route_dispatcher
|
|
||||||
.send(RouteRequest::ReplaceRoute(Route::new_no_state("/login")));
|
|
||||||
};
|
|
||||||
app
|
app
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
||||||
match msg {
|
match msg {
|
||||||
Msg::Login(user_name) => {
|
Msg::Login((user_name, is_admin)) => {
|
||||||
self.user_name = Some(user_name);
|
self.user_info = Some((user_name.clone(), is_admin));
|
||||||
|
let user_route = "/details/".to_string() + &user_name;
|
||||||
self.route_dispatcher
|
self.route_dispatcher
|
||||||
.send(RouteRequest::ChangeRoute(Route::new_no_state(
|
.send(RouteRequest::ChangeRoute(Route::new_no_state(
|
||||||
&self.redirect_to,
|
self.redirect_to.as_deref().unwrap_or_else(|| {
|
||||||
|
if is_admin {
|
||||||
|
"/users"
|
||||||
|
} else {
|
||||||
|
&user_route
|
||||||
|
}
|
||||||
|
}),
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
Msg::Logout => {
|
Msg::Logout => {
|
||||||
self.user_name = None;
|
self.user_info = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if self.user_name.is_none() {
|
if self.user_info.is_none() {
|
||||||
self.route_dispatcher
|
self.route_dispatcher
|
||||||
.send(RouteRequest::ReplaceRoute(Route::new_no_state("/login")));
|
.send(RouteRequest::ReplaceRoute(Route::new_no_state("/login")));
|
||||||
}
|
}
|
||||||
@ -108,7 +122,12 @@ impl Component for App {
|
|||||||
<UserTable />
|
<UserTable />
|
||||||
<Link route=AppRoute::CreateUser>{"Create a user"}</Link>
|
<Link route=AppRoute::CreateUser>{"Create a user"}</Link>
|
||||||
</div>
|
</div>
|
||||||
}
|
},
|
||||||
|
AppRoute::UserDetails(username) => html! {
|
||||||
|
<div>
|
||||||
|
{"details about "} {&username}
|
||||||
|
</div>
|
||||||
|
},
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
/>
|
/>
|
||||||
@ -118,13 +137,42 @@ impl Component for App {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
fn get_redirect_route() -> String {
|
fn get_redirect_route() -> Option<String> {
|
||||||
let route_service = RouteService::<()>::new();
|
let route_service = RouteService::<()>::new();
|
||||||
let current_route = route_service.get_path();
|
let current_route = route_service.get_path();
|
||||||
if current_route.is_empty() || current_route.contains("login") {
|
if current_route.is_empty() || current_route.contains("login") {
|
||||||
String::from("/")
|
None
|
||||||
} else {
|
} else {
|
||||||
current_route
|
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)),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,7 +38,7 @@ pub fn get_cookie(cookie_name: &str) -> Result<Option<String>> {
|
|||||||
.split(';')
|
.split(';')
|
||||||
.filter_map(|c| c.split_once('='))
|
.filter_map(|c| c.split_once('='))
|
||||||
.find_map(|(name, value)| {
|
.find_map(|(name, value)| {
|
||||||
if name == cookie_name {
|
if name.trim() == cookie_name {
|
||||||
if value.is_empty() {
|
if value.is_empty() {
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
|
@ -8,7 +8,7 @@ use yew::FocusEvent;
|
|||||||
|
|
||||||
pub struct LoginForm {
|
pub struct LoginForm {
|
||||||
link: ComponentLink<Self>,
|
link: ComponentLink<Self>,
|
||||||
on_logged_in: Callback<String>,
|
on_logged_in: Callback<(String, bool)>,
|
||||||
error: Option<anyhow::Error>,
|
error: Option<anyhow::Error>,
|
||||||
node_ref: NodeRef,
|
node_ref: NodeRef,
|
||||||
login_start: Option<opaque::client::login::ClientLogin>,
|
login_start: Option<opaque::client::login::ClientLogin>,
|
||||||
@ -18,13 +18,13 @@ pub struct LoginForm {
|
|||||||
|
|
||||||
#[derive(Clone, PartialEq, Properties)]
|
#[derive(Clone, PartialEq, Properties)]
|
||||||
pub struct Props {
|
pub struct Props {
|
||||||
pub on_logged_in: Callback<String>,
|
pub on_logged_in: Callback<(String, bool)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum Msg {
|
pub enum Msg {
|
||||||
Submit,
|
Submit,
|
||||||
AuthenticationStartResponse(Result<Box<login::ServerLoginStartResponse>>),
|
AuthenticationStartResponse(Result<Box<login::ServerLoginStartResponse>>),
|
||||||
AuthenticationFinishResponse(Result<String>),
|
AuthenticationFinishResponse(Result<(String, bool)>),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_form_field(field_id: &str) -> Option<String> {
|
fn get_form_field(field_id: &str) -> Option<String> {
|
||||||
@ -106,8 +106,8 @@ impl LoginForm {
|
|||||||
"Could not log in (invalid response to login start): {}",
|
"Could not log in (invalid response to login start): {}",
|
||||||
e
|
e
|
||||||
)),
|
)),
|
||||||
Msg::AuthenticationFinishResponse(Ok(user_id)) => {
|
Msg::AuthenticationFinishResponse(Ok(user_info)) => {
|
||||||
self.on_logged_in.emit(user_id);
|
self.on_logged_in.emit(user_info);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
Msg::AuthenticationFinishResponse(Err(e)) => Err(anyhow!("Could not log in: {}", e)),
|
Msg::AuthenticationFinishResponse(Err(e)) => Err(anyhow!("Could not log in: {}", e)),
|
||||||
|
Loading…
Reference in New Issue
Block a user