Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Handle sessions in one place #2026

Merged
merged 1 commit into from
Jul 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 15 additions & 35 deletions iml-gui/crate/src/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use web_sys::HtmlDocument;
#[derive(Default)]
pub struct Model {
session: Option<Session>,
pub state: State,
request_controller: Option<fetch::RequestController>,
cancel: Option<oneshot::Sender<()>>,
}
Expand All @@ -25,46 +24,26 @@ impl Model {
}
}

#[derive(PartialEq, Eq)]
pub enum State {
Fetching,
Stopped,
}

impl Default for State {
fn default() -> Self {
Self::Fetching
}
}

#[allow(clippy::large_enum_variant)]
#[derive(Clone, Debug)]
pub enum Msg {
Fetch,
Fetched(fetch::FetchObject<Session>),
SetSession(Session),
Logout,
LoggedIn,
Loop,
Stop,
Noop,
}

pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg, GMsg>) {
match msg {
Msg::Fetch => {
model.state = State::Fetching;
model.cancel = None;

let request = fetch_session().controller(|controller| model.request_controller = Some(controller));

orders.skip().perform_cmd(request.fetch_json(Msg::Fetched));
}
Msg::SetSession(session) => {
Copy link
Member Author

@ip1981 ip1981 Jul 1, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this and this was the cause of #2030.

if session.needs_login() {
orders.send_g_msg(GMsg::RouteChange(Route::Login.into()));
}

model.session = Some(session);
}
Msg::Fetched(data) => {
match data.response() {
Err(fail_reason) => {
Expand Down Expand Up @@ -95,27 +74,28 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg, GMsg>)
}
};
}
Msg::Loop => {
Msg::LoggedIn => {
orders.skip();
orders.send_msg(Msg::Fetch);
orders.send_g_msg(GMsg::RouteChange(Route::Dashboard.into()));
}

if model.state == State::Stopped {
return;
}
Msg::Logout => {
orders.perform_g_cmd(
ip1981 marked this conversation as resolved.
Show resolved Hide resolved
fetch_session()
.method(fetch::Method::Delete)
.fetch(|_| GMsg::AuthProxy(Box::new(Msg::LoggedIn))),
Copy link
Member Author

@ip1981 ip1981 Jun 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A samurai without a sword is like a samurai with one, but only without one. -- Re #1805

);
}
Msg::Loop => {
orders.skip();

let (cancel, fut) = sleep_with_handle(Duration::from_secs(10), Msg::Fetch, Msg::Noop);

model.cancel = Some(cancel);

orders.perform_cmd(fut);
}
Msg::Stop => {
model.state = State::Stopped;
model.cancel = None;

if let Some(c) = model.request_controller.take() {
c.abort();
}
}
Msg::Noop => {}
};
}
Expand Down
2 changes: 1 addition & 1 deletion iml-gui/crate/src/components/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// license that can be found in the LICENSE file.

use crate::{
components::{attrs, alert_indicator, font_awesome, paging, Placement, tooltip},
components::{alert_indicator, attrs, font_awesome, paging, tooltip, Placement},
generated::css_classes::C,
route::RouteId,
GMsg, Route,
Expand Down
3 changes: 0 additions & 3 deletions iml-gui/crate/src/generated/css_classes.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@

// DO NOT EDIT THIS FILE - IT'S GENERATED, CHANGES WILL BE LOST!

#[allow(non_snake_case, dead_code)]
pub struct CssClasses<'a> {

/**
width: 100%;
max-width: 569px; @media (min-width: 569px)
Expand Down Expand Up @@ -93733,7 +93731,6 @@ pub struct CssClasses<'a> {
}

pub static C: CssClasses = CssClasses {

/**
width: 100%;
max-width: 569px; @media (min-width: 569px)
Expand Down
43 changes: 1 addition & 42 deletions iml-gui/crate/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ use futures::channel::oneshot;
use generated::css_classes::C;
use iml_wire_types::{
warp_drive::{self, ArcRecord},
Conf, GroupType, Session,
Conf, GroupType,
};
use lazy_static::lazy_static;
use page::{Page, RecordChange};
Expand Down Expand Up @@ -157,7 +157,6 @@ pub struct Model {
conf: Conf,
loading: Loading,
locks: warp_drive::Locks,
logging_out: bool,
manage_menu_state: WatchState,
menu_visibility: Visibility,
notification: notification::Model,
Expand Down Expand Up @@ -225,7 +224,6 @@ fn after_mount(url: Url, orders: &mut impl Orders<Msg, GMsg>) -> AfterMount<Mode
conf: Some(conf_tx),
},
locks: im::hashmap!(),
logging_out: false,
manage_menu_state: WatchState::default(),
menu_visibility: Visible,
notification: notification::Model::default(),
Expand Down Expand Up @@ -302,13 +300,9 @@ pub enum Msg {
EventSourceMessage(MessageEvent),
FetchConf,
FetchedConf(fetch::ResponseDataResult<Conf>),
GetSession,
GotSession(fetch::ResponseDataResult<Session>),
HideMenu,
LoadPage,
Locks(warp_drive::Locks),
LoggedOut(fetch::FetchObject<()>),
Logout,
ManageMenuState,
Notification(notification::Msg),
RecordChange(Box<warp_drive::RecordChange>),
Expand All @@ -332,10 +326,6 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg, GMsg>)
Msg::RouteChanged(url) => {
model.route = Route::from(url);

if model.route == Route::Login {
orders.send_msg(Msg::Logout);
}

if model.route == Route::Dashboard {
model.breadcrumbs.clear();
}
Expand Down Expand Up @@ -409,23 +399,6 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg, GMsg>)
Msg::EventSourceError(_) => {
log("EventSource error.");
}
Msg::GetSession => {
orders
.skip()
.perform_cmd(auth::fetch_session().fetch_json_data(Msg::GotSession));
}
Msg::GotSession(data_result) => match data_result {
Ok(resp) => {
orders.send_g_msg(GMsg::AuthProxy(Box::new(auth::Msg::SetSession(resp))));

model.logging_out = false;
}
Err(fail_reason) => {
error!("Error fetching login session {:?}", fail_reason.message());

orders.skip();
}
},
Msg::Records(records) => {
model.records = (&*records).into();

Expand Down Expand Up @@ -582,20 +555,6 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg, GMsg>)
&mut orders.proxy(Msg::StatusSection),
);
}
Msg::Logout => {
model.logging_out = true;

orders.proxy(Msg::Auth).send_msg(Box::new(auth::Msg::Stop));

orders.perform_cmd(
auth::fetch_session()
.method(fetch::Method::Delete)
.fetch(Msg::LoggedOut),
);
}
Msg::LoggedOut(_) => {
orders.send_msg(Msg::GetSession);
}
Msg::WindowClick => {
if model.manage_menu_state.should_update() {
model.manage_menu_state.update();
Expand Down
29 changes: 3 additions & 26 deletions iml-gui/crate/src/page/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use crate::{
auth,
components::{ddn_logo, ddn_logo_lettering, whamcloud_logo},
generated::css_classes::C,
FailReasonExt, GMsg, MergeAttrs, Route,
GMsg, MergeAttrs,
};
use core::fmt;
use iml_wire_types::{Branding, Session};
use iml_wire_types::Branding;
use seed::{browser::service::fetch, prelude::*, *};

#[derive(Clone, Default, serde::Serialize)]
Expand Down Expand Up @@ -45,8 +45,6 @@ pub enum Msg {
PasswordChange(String),
SubmitResp(fetch::FetchObject<Errors>),
Submit,
GetSession,
GotSession(fetch::ResponseDataResult<Session>),
}

impl fmt::Debug for Msg {
Expand Down Expand Up @@ -80,7 +78,7 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg, GMsg>)
Err(e) => error!("Response error {:?}", e),
Ok(x) => {
if x.status.code < 400 {
orders.skip().send_msg(Msg::GetSession);
orders.skip().send_g_msg(GMsg::AuthProxy(Box::new(auth::Msg::LoggedIn)));
} else {
model.logging_in = false;

Expand All @@ -92,27 +90,6 @@ pub fn update(msg: Msg, model: &mut Model, orders: &mut impl Orders<Msg, GMsg>)
}
};
}
Msg::GetSession => {
orders
.skip()
.perform_cmd(auth::fetch_session().fetch_json_data(Msg::GotSession));
}
Msg::GotSession(data_result) => {
match data_result {
Ok(resp) => {
orders
.send_g_msg(GMsg::AuthProxy(Box::new(auth::Msg::SetSession(resp))))
.send_g_msg(GMsg::RouteChange(Route::Dashboard.into()));

model.logging_in = false;
}
Err(fail_reason) => {
error!("Error fetching login session {:?}", fail_reason.message());

orders.skip();
}
};
}
}
}

Expand Down
9 changes: 3 additions & 6 deletions iml-gui/crate/src/page/partial/footer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,17 @@ pub fn view(conf: &Conf) -> impl View<Msg> {
}
};

let footer_text = match conf.branding {
let footer_text = match conf.branding {
Branding::Whamcloud => div![
footer_string,
year.to_string(),
" DDN. All rights reserved.".to_string(),
],
_ => div![footer_string]
_ => div![footer_string],
};

footer![
class![C.h_5, C.flex, C.justify_center],
div![
class![C.px_5, C.text_sm, C.items_center,],
footer_text
]
div![class![C.px_5, C.text_sm, C.items_center,], footer_text]
]
}
11 changes: 4 additions & 7 deletions iml-gui/crate/src/page/partial/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ fn nav(model: &Model) -> Node<Msg> {
C.lg__h_16,
],
main_menu_items(model),
auth_view(&model.auth, model.logging_out),
auth_view(&model.auth),
]
} else {
empty![]
Expand All @@ -255,14 +255,12 @@ fn nav(model: &Model) -> Node<Msg> {

/// Show the logged in user if available.
/// Also show the Login / Logout link
pub fn auth_view(auth: &auth::Model, logging_out: bool) -> Node<Msg> {
pub fn auth_view(auth: &auth::Model) -> Node<Msg> {
let x = match auth.get_session() {
Some(session) => session,
None => return empty![],
};

let disabled = attrs! { At::Disabled => logging_out.as_at_value() };

let cls = class![
C.block,
C.border_b_2,
Expand All @@ -283,14 +281,14 @@ pub fn auth_view(auth: &auth::Model, logging_out: bool) -> Node<Msg> {
C.text_gray_300
];

let mut auth_link = a![&cls, &disabled, if !x.has_user() { "Login" } else { "Logout" }];
let mut auth_link = a![&cls, if !x.has_user() { "Login" } else { "Logout" }];

let auth_link = if !x.has_user() {
auth_link.merge_attrs(attrs! {
At::Href => Route::Login.to_href(),
})
} else {
auth_link.add_listener(simple_ev(Ev::Click, Msg::Logout));
auth_link.add_listener(simple_ev(Ev::Click, Msg::Auth(Box::new(auth::Msg::Logout))));

auth_link
};
Expand All @@ -301,7 +299,6 @@ pub fn auth_view(auth: &auth::Model, logging_out: bool) -> Node<Msg> {
Some(user) => {
a![
&cls,
&disabled,
attrs! {
At::Href => Route::User(user.id.into()).to_href()
},
Expand Down