mirror of
				https://github.com/nitnelave/lldap.git
				synced 2023-04-12 14:25:13 +00:00 
			
		
		
		
	app: Migrate change_password to CommonComponent
This commit is contained in:
		
							parent
							
								
									50e91a931a
								
							
						
					
					
						commit
						f0f618422e
					
				@ -1,14 +1,14 @@
 | 
				
			|||||||
use crate::{
 | 
					use crate::{
 | 
				
			||||||
    components::router::{AppRoute, NavButton},
 | 
					    components::router::{AppRoute, NavButton},
 | 
				
			||||||
    infra::api::HostService,
 | 
					    infra::{
 | 
				
			||||||
 | 
					        api::HostService,
 | 
				
			||||||
 | 
					        common_component::{CommonComponent, CommonComponentParts},
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use anyhow::{anyhow, bail, Context, Result};
 | 
					use anyhow::{anyhow, bail, Context, Result};
 | 
				
			||||||
use lldap_auth::*;
 | 
					use lldap_auth::*;
 | 
				
			||||||
use validator_derive::Validate;
 | 
					use validator_derive::Validate;
 | 
				
			||||||
use yew::{
 | 
					use yew::{prelude::*, services::ConsoleService};
 | 
				
			||||||
    prelude::*,
 | 
					 | 
				
			||||||
    services::{fetch::FetchTask, ConsoleService},
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
use yew_form::Form;
 | 
					use yew_form::Form;
 | 
				
			||||||
use yew_form_derive::Model;
 | 
					use yew_form_derive::Model;
 | 
				
			||||||
use yew_router::{
 | 
					use yew_router::{
 | 
				
			||||||
@ -58,13 +58,9 @@ fn empty_or_long(value: &str) -> Result<(), validator::ValidationError> {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
pub struct ChangePasswordForm {
 | 
					pub struct ChangePasswordForm {
 | 
				
			||||||
    link: ComponentLink<Self>,
 | 
					    common: CommonComponentParts<Self>,
 | 
				
			||||||
    props: Props,
 | 
					 | 
				
			||||||
    error: Option<anyhow::Error>,
 | 
					 | 
				
			||||||
    form: Form<FormModel>,
 | 
					    form: Form<FormModel>,
 | 
				
			||||||
    opaque_data: OpaqueData,
 | 
					    opaque_data: OpaqueData,
 | 
				
			||||||
    // Used to keep the request alive long enough.
 | 
					 | 
				
			||||||
    task: Option<FetchTask>,
 | 
					 | 
				
			||||||
    route_dispatcher: RouteAgentDispatcher,
 | 
					    route_dispatcher: RouteAgentDispatcher,
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -83,25 +79,16 @@ pub enum Msg {
 | 
				
			|||||||
    RegistrationFinishResponse(Result<()>),
 | 
					    RegistrationFinishResponse(Result<()>),
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl ChangePasswordForm {
 | 
					impl CommonComponent<ChangePasswordForm> for ChangePasswordForm {
 | 
				
			||||||
    fn call_backend<M, Req, C, Resp>(&mut self, method: M, req: Req, callback: C) -> Result<()>
 | 
					    fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
 | 
				
			||||||
    where
 | 
					 | 
				
			||||||
        M: Fn(Req, Callback<Resp>) -> Result<FetchTask>,
 | 
					 | 
				
			||||||
        C: Fn(Resp) -> <Self as Component>::Message + 'static,
 | 
					 | 
				
			||||||
    {
 | 
					 | 
				
			||||||
        self.task = Some(method(req, self.link.callback(callback))?);
 | 
					 | 
				
			||||||
        Ok(())
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    fn handle_message(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
 | 
					 | 
				
			||||||
        match msg {
 | 
					        match msg {
 | 
				
			||||||
            Msg::FormUpdate => Ok(true),
 | 
					            Msg::FormUpdate => Ok(true),
 | 
				
			||||||
            Msg::Submit => {
 | 
					            Msg::Submit => {
 | 
				
			||||||
                if !self.form.validate() {
 | 
					                if !self.form.validate() {
 | 
				
			||||||
                    bail!("Check the form for errors");
 | 
					                    bail!("Check the form for errors");
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                if self.props.is_admin {
 | 
					                if self.common.is_admin {
 | 
				
			||||||
                    self.handle_message(Msg::SubmitNewPassword)
 | 
					                    self.handle_msg(Msg::SubmitNewPassword)
 | 
				
			||||||
                } else {
 | 
					                } else {
 | 
				
			||||||
                    let old_password = self.form.model().old_password;
 | 
					                    let old_password = self.form.model().old_password;
 | 
				
			||||||
                    if old_password.is_empty() {
 | 
					                    if old_password.is_empty() {
 | 
				
			||||||
@ -113,10 +100,10 @@ impl ChangePasswordForm {
 | 
				
			|||||||
                            .context("Could not initialize login")?;
 | 
					                            .context("Could not initialize login")?;
 | 
				
			||||||
                    self.opaque_data = OpaqueData::Login(login_start_request.state);
 | 
					                    self.opaque_data = OpaqueData::Login(login_start_request.state);
 | 
				
			||||||
                    let req = login::ClientLoginStartRequest {
 | 
					                    let req = login::ClientLoginStartRequest {
 | 
				
			||||||
                        username: self.props.username.clone(),
 | 
					                        username: self.common.username.clone(),
 | 
				
			||||||
                        login_start_request: login_start_request.message,
 | 
					                        login_start_request: login_start_request.message,
 | 
				
			||||||
                    };
 | 
					                    };
 | 
				
			||||||
                    self.call_backend(
 | 
					                    self.common.call_backend(
 | 
				
			||||||
                        HostService::login_start,
 | 
					                        HostService::login_start,
 | 
				
			||||||
                        req,
 | 
					                        req,
 | 
				
			||||||
                        Msg::AuthenticationStartResponse,
 | 
					                        Msg::AuthenticationStartResponse,
 | 
				
			||||||
@ -142,7 +129,7 @@ impl ChangePasswordForm {
 | 
				
			|||||||
                    }
 | 
					                    }
 | 
				
			||||||
                    _ => panic!("Unexpected data in opaque_data field"),
 | 
					                    _ => panic!("Unexpected data in opaque_data field"),
 | 
				
			||||||
                };
 | 
					                };
 | 
				
			||||||
                self.handle_message(Msg::SubmitNewPassword)
 | 
					                self.handle_msg(Msg::SubmitNewPassword)
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            Msg::SubmitNewPassword => {
 | 
					            Msg::SubmitNewPassword => {
 | 
				
			||||||
                let mut rng = rand::rngs::OsRng;
 | 
					                let mut rng = rand::rngs::OsRng;
 | 
				
			||||||
@ -151,11 +138,11 @@ impl ChangePasswordForm {
 | 
				
			|||||||
                    opaque::client::registration::start_registration(&new_password, &mut rng)
 | 
					                    opaque::client::registration::start_registration(&new_password, &mut rng)
 | 
				
			||||||
                        .context("Could not initiate password change")?;
 | 
					                        .context("Could not initiate password change")?;
 | 
				
			||||||
                let req = registration::ClientRegistrationStartRequest {
 | 
					                let req = registration::ClientRegistrationStartRequest {
 | 
				
			||||||
                    username: self.props.username.clone(),
 | 
					                    username: self.common.username.clone(),
 | 
				
			||||||
                    registration_start_request: registration_start_request.message,
 | 
					                    registration_start_request: registration_start_request.message,
 | 
				
			||||||
                };
 | 
					                };
 | 
				
			||||||
                self.opaque_data = OpaqueData::Registration(registration_start_request.state);
 | 
					                self.opaque_data = OpaqueData::Registration(registration_start_request.state);
 | 
				
			||||||
                self.call_backend(
 | 
					                self.common.call_backend(
 | 
				
			||||||
                    HostService::register_start,
 | 
					                    HostService::register_start,
 | 
				
			||||||
                    req,
 | 
					                    req,
 | 
				
			||||||
                    Msg::RegistrationStartResponse,
 | 
					                    Msg::RegistrationStartResponse,
 | 
				
			||||||
@ -178,7 +165,7 @@ impl ChangePasswordForm {
 | 
				
			|||||||
                            server_data: res.server_data,
 | 
					                            server_data: res.server_data,
 | 
				
			||||||
                            registration_upload: registration_finish.message,
 | 
					                            registration_upload: registration_finish.message,
 | 
				
			||||||
                        };
 | 
					                        };
 | 
				
			||||||
                        self.call_backend(
 | 
					                        self.common.call_backend(
 | 
				
			||||||
                            HostService::register_finish,
 | 
					                            HostService::register_finish,
 | 
				
			||||||
                            req,
 | 
					                            req,
 | 
				
			||||||
                            Msg::RegistrationFinishResponse,
 | 
					                            Msg::RegistrationFinishResponse,
 | 
				
			||||||
@ -189,11 +176,11 @@ impl ChangePasswordForm {
 | 
				
			|||||||
                Ok(false)
 | 
					                Ok(false)
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            Msg::RegistrationFinishResponse(response) => {
 | 
					            Msg::RegistrationFinishResponse(response) => {
 | 
				
			||||||
                self.task = None;
 | 
					                self.common.task = None;
 | 
				
			||||||
                if response.is_ok() {
 | 
					                if response.is_ok() {
 | 
				
			||||||
                    self.route_dispatcher
 | 
					                    self.route_dispatcher
 | 
				
			||||||
                        .send(RouteRequest::ChangeRoute(Route::from(
 | 
					                        .send(RouteRequest::ChangeRoute(Route::from(
 | 
				
			||||||
                            AppRoute::UserDetails(self.props.username.clone()),
 | 
					                            AppRoute::UserDetails(self.common.username.clone()),
 | 
				
			||||||
                        )));
 | 
					                        )));
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
                response?;
 | 
					                response?;
 | 
				
			||||||
@ -201,6 +188,10 @@ impl ChangePasswordForm {
 | 
				
			|||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
 | 
				
			||||||
 | 
					        &mut self.common
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
impl Component for ChangePasswordForm {
 | 
					impl Component for ChangePasswordForm {
 | 
				
			||||||
@ -209,27 +200,15 @@ impl Component for ChangePasswordForm {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
 | 
					    fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
 | 
				
			||||||
        ChangePasswordForm {
 | 
					        ChangePasswordForm {
 | 
				
			||||||
            link,
 | 
					            common: CommonComponentParts::<Self>::create(props, link),
 | 
				
			||||||
            props,
 | 
					 | 
				
			||||||
            error: None,
 | 
					 | 
				
			||||||
            form: yew_form::Form::<FormModel>::new(FormModel::default()),
 | 
					            form: yew_form::Form::<FormModel>::new(FormModel::default()),
 | 
				
			||||||
            opaque_data: OpaqueData::None,
 | 
					            opaque_data: OpaqueData::None,
 | 
				
			||||||
            task: None,
 | 
					 | 
				
			||||||
            route_dispatcher: RouteAgentDispatcher::new(),
 | 
					            route_dispatcher: RouteAgentDispatcher::new(),
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn update(&mut self, msg: Self::Message) -> ShouldRender {
 | 
					    fn update(&mut self, msg: Self::Message) -> ShouldRender {
 | 
				
			||||||
        self.error = None;
 | 
					        CommonComponentParts::<Self>::update(self, msg)
 | 
				
			||||||
        match self.handle_message(msg) {
 | 
					 | 
				
			||||||
            Err(e) => {
 | 
					 | 
				
			||||||
                ConsoleService::error(&e.to_string());
 | 
					 | 
				
			||||||
                self.error = Some(e);
 | 
					 | 
				
			||||||
                self.task = None;
 | 
					 | 
				
			||||||
                true
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
            Ok(b) => b,
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn change(&mut self, _: Self::Properties) -> ShouldRender {
 | 
					    fn change(&mut self, _: Self::Properties) -> ShouldRender {
 | 
				
			||||||
@ -237,7 +216,7 @@ impl Component for ChangePasswordForm {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    fn view(&self) -> Html {
 | 
					    fn view(&self) -> Html {
 | 
				
			||||||
        let is_admin = self.props.is_admin;
 | 
					        let is_admin = self.common.is_admin;
 | 
				
			||||||
        type Field = yew_form::Field<FormModel>;
 | 
					        type Field = yew_form::Field<FormModel>;
 | 
				
			||||||
        html! {
 | 
					        html! {
 | 
				
			||||||
          <>
 | 
					          <>
 | 
				
			||||||
@ -257,7 +236,7 @@ impl Component for ChangePasswordForm {
 | 
				
			|||||||
                      class_invalid="is-invalid has-error"
 | 
					                      class_invalid="is-invalid has-error"
 | 
				
			||||||
                      class_valid="has-success"
 | 
					                      class_valid="has-success"
 | 
				
			||||||
                      autocomplete="current-password"
 | 
					                      autocomplete="current-password"
 | 
				
			||||||
                      oninput=self.link.callback(|_| Msg::FormUpdate) />
 | 
					                      oninput=self.common.link.callback(|_| Msg::FormUpdate) />
 | 
				
			||||||
                    <div class="invalid-feedback">
 | 
					                    <div class="invalid-feedback">
 | 
				
			||||||
                      {&self.form.field_message("old_password")}
 | 
					                      {&self.form.field_message("old_password")}
 | 
				
			||||||
                    </div>
 | 
					                    </div>
 | 
				
			||||||
@ -277,7 +256,7 @@ impl Component for ChangePasswordForm {
 | 
				
			|||||||
                    class_invalid="is-invalid has-error"
 | 
					                    class_invalid="is-invalid has-error"
 | 
				
			||||||
                    class_valid="has-success"
 | 
					                    class_valid="has-success"
 | 
				
			||||||
                    autocomplete="new-password"
 | 
					                    autocomplete="new-password"
 | 
				
			||||||
                    oninput=self.link.callback(|_| Msg::FormUpdate) />
 | 
					                    oninput=self.common.link.callback(|_| Msg::FormUpdate) />
 | 
				
			||||||
                  <div class="invalid-feedback">
 | 
					                  <div class="invalid-feedback">
 | 
				
			||||||
                    {&self.form.field_message("password")}
 | 
					                    {&self.form.field_message("password")}
 | 
				
			||||||
                  </div>
 | 
					                  </div>
 | 
				
			||||||
@ -296,7 +275,7 @@ impl Component for ChangePasswordForm {
 | 
				
			|||||||
                    class_invalid="is-invalid has-error"
 | 
					                    class_invalid="is-invalid has-error"
 | 
				
			||||||
                    class_valid="has-success"
 | 
					                    class_valid="has-success"
 | 
				
			||||||
                    autocomplete="new-password"
 | 
					                    autocomplete="new-password"
 | 
				
			||||||
                    oninput=self.link.callback(|_| Msg::FormUpdate) />
 | 
					                    oninput=self.common.link.callback(|_| Msg::FormUpdate) />
 | 
				
			||||||
                  <div class="invalid-feedback">
 | 
					                  <div class="invalid-feedback">
 | 
				
			||||||
                    {&self.form.field_message("confirm_password")}
 | 
					                    {&self.form.field_message("confirm_password")}
 | 
				
			||||||
                  </div>
 | 
					                  </div>
 | 
				
			||||||
@ -306,13 +285,13 @@ impl Component for ChangePasswordForm {
 | 
				
			|||||||
                <button
 | 
					                <button
 | 
				
			||||||
                  class="btn btn-primary col-sm-1 col-form-label"
 | 
					                  class="btn btn-primary col-sm-1 col-form-label"
 | 
				
			||||||
                  type="submit"
 | 
					                  type="submit"
 | 
				
			||||||
                  disabled=self.task.is_some()
 | 
					                  disabled=self.common.task.is_some()
 | 
				
			||||||
                  onclick=self.link.callback(|e: MouseEvent| {e.prevent_default(); Msg::Submit})>
 | 
					                  onclick=self.common.link.callback(|e: MouseEvent| {e.prevent_default(); Msg::Submit})>
 | 
				
			||||||
                  {"Submit"}
 | 
					                  {"Submit"}
 | 
				
			||||||
                </button>
 | 
					                </button>
 | 
				
			||||||
              </div>
 | 
					              </div>
 | 
				
			||||||
            </form>
 | 
					            </form>
 | 
				
			||||||
            { if let Some(e) = &self.error {
 | 
					            { if let Some(e) = &self.common.error {
 | 
				
			||||||
                html! {
 | 
					                html! {
 | 
				
			||||||
                  <div class="alert alert-danger">
 | 
					                  <div class="alert alert-danger">
 | 
				
			||||||
                    {e.to_string() }
 | 
					                    {e.to_string() }
 | 
				
			||||||
@ -323,7 +302,7 @@ impl Component for ChangePasswordForm {
 | 
				
			|||||||
            <div>
 | 
					            <div>
 | 
				
			||||||
              <NavButton
 | 
					              <NavButton
 | 
				
			||||||
                classes="btn btn-primary"
 | 
					                classes="btn btn-primary"
 | 
				
			||||||
                route=AppRoute::UserDetails(self.props.username.clone())>
 | 
					                route=AppRoute::UserDetails(self.common.username.clone())>
 | 
				
			||||||
                {"Back"}
 | 
					                {"Back"}
 | 
				
			||||||
              </NavButton>
 | 
					              </NavButton>
 | 
				
			||||||
            </div>
 | 
					            </div>
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user