app: Allow admins to change passwords

This commit is contained in:
Valentin Tolmer 2021-09-14 09:58:04 +02:00 committed by nitnelave
parent 005e18472e
commit 718da71d0d
3 changed files with 44 additions and 21 deletions

View File

@ -4,7 +4,7 @@ use crate::{
create_user::CreateUserForm, create_user::CreateUserForm,
login::LoginForm, login::LoginForm,
logout::LogoutButton, logout::LogoutButton,
router::{AppRoute, Link}, router::{AppRoute, NavButton},
user_details::UserDetails, user_details::UserDetails,
user_table::UserTable, user_table::UserTable,
}, },
@ -91,6 +91,7 @@ impl Component for App {
fn view(&self) -> Html { fn view(&self) -> Html {
let link = self.link.clone(); let link = self.link.clone();
let is_admin = self.is_admin();
html! { html! {
<div> <div>
<h1>{ "LLDAP" }</h1> <h1>{ "LLDAP" }</h1>
@ -110,7 +111,7 @@ impl Component for App {
<div> <div>
<LogoutButton on_logged_out=link.callback(|_| Msg::Logout) /> <LogoutButton on_logged_out=link.callback(|_| Msg::Logout) />
<UserTable /> <UserTable />
<Link route=AppRoute::CreateUser>{"Create a user"}</Link> <NavButton route=AppRoute::CreateUser>{"Create a user"}</NavButton>
</div> </div>
}, },
AppRoute::UserDetails(username) => html! { AppRoute::UserDetails(username) => html! {
@ -122,7 +123,7 @@ impl Component for App {
AppRoute::ChangePassword(username) => html! { AppRoute::ChangePassword(username) => html! {
<div> <div>
<LogoutButton on_logged_out=link.callback(|_| Msg::Logout) /> <LogoutButton on_logged_out=link.callback(|_| Msg::Logout) />
<ChangePasswordForm username=username.clone() /> <ChangePasswordForm username=username.clone() is_admin=is_admin />
</div> </div>
} }
} }
@ -170,4 +171,11 @@ impl App {
}, },
} }
} }
fn is_admin(&self) -> bool {
match &self.user_info {
None => false,
Some((_, is_admin)) => *is_admin,
}
}
} }

View File

@ -36,6 +36,7 @@ pub struct ChangePasswordForm {
node_ref: NodeRef, node_ref: NodeRef,
opaque_data: OpaqueData, opaque_data: OpaqueData,
successfully_changed_password: bool, successfully_changed_password: bool,
is_admin: bool,
// Used to keep the request alive long enough. // Used to keep the request alive long enough.
_task: Option<FetchTask>, _task: Option<FetchTask>,
} }
@ -43,11 +44,13 @@ pub struct ChangePasswordForm {
#[derive(Clone, PartialEq, Properties)] #[derive(Clone, PartialEq, Properties)]
pub struct Props { pub struct Props {
pub username: String, pub username: String,
pub is_admin: bool,
} }
pub enum Msg { pub enum Msg {
Submit, Submit,
AuthenticationStartResponse(Result<Box<login::ServerLoginStartResponse>>), AuthenticationStartResponse(Result<Box<login::ServerLoginStartResponse>>),
SubmitNewPassword,
RegistrationStartResponse(Result<Box<registration::ServerRegistrationStartResponse>>), RegistrationStartResponse(Result<Box<registration::ServerRegistrationStartResponse>>),
RegistrationFinishResponse(Result<()>), RegistrationFinishResponse(Result<()>),
} }
@ -107,6 +110,9 @@ impl ChangePasswordForm {
if new_password != confirm_password { if new_password != confirm_password {
bail!("Confirmation password doesn't match"); bail!("Confirmation password doesn't match");
} }
if self.is_admin {
self.handle_message(Msg::SubmitNewPassword)
} else {
let mut rng = rand::rngs::OsRng; let mut rng = rand::rngs::OsRng;
let login_start_request = let login_start_request =
opaque::client::login::start_login(&old_password, &mut rng) opaque::client::login::start_login(&old_password, &mut rng)
@ -123,6 +129,7 @@ impl ChangePasswordForm {
)?; )?;
Ok(()) Ok(())
} }
}
Msg::AuthenticationStartResponse(res) => { Msg::AuthenticationStartResponse(res) => {
let res = res.context("Could not initiate login")?; let res = res.context("Could not initiate login")?;
match self.opaque_data.take() { match self.opaque_data.take() {
@ -141,6 +148,9 @@ impl ChangePasswordForm {
} }
_ => panic!("Unexpected data in opaque_data field"), _ => panic!("Unexpected data in opaque_data field"),
}; };
self.handle_message(Msg::SubmitNewPassword)
}
Msg::SubmitNewPassword => {
let mut rng = rand::rngs::OsRng; let mut rng = rand::rngs::OsRng;
let new_password = get_form_field("newPassword") let new_password = get_form_field("newPassword")
.ok_or_else(|| anyhow!("Could not get new password from form"))?; .ok_or_else(|| anyhow!("Could not get new password from form"))?;
@ -207,6 +217,7 @@ impl Component for ChangePasswordForm {
node_ref: NodeRef::default(), node_ref: NodeRef::default(),
opaque_data: OpaqueData::None, opaque_data: OpaqueData::None,
successfully_changed_password: false, successfully_changed_password: false,
is_admin: props.is_admin,
_task: None, _task: None,
} }
} }
@ -225,11 +236,12 @@ impl Component for ChangePasswordForm {
} }
fn view(&self) -> Html { fn view(&self) -> Html {
let is_admin = self.is_admin;
html! { html! {
<form ref=self.node_ref.clone() onsubmit=self.link.callback(|e: FocusEvent| { e.prevent_default(); Msg::Submit })> <form ref=self.node_ref.clone() onsubmit=self.link.callback(|e: FocusEvent| { e.prevent_default(); Msg::Submit })>
<div> <div>
<label for="oldPassword">{"Old password:"}</label> <label for="oldPassword">{"Old password:"}</label>
<input type="password" id="oldPassword" autocomplete="current-password" required=true /> <input type="password" id="oldPassword" autocomplete="current-password" required=true disabled=is_admin />
</div> </div>
<div> <div>
<label for="newPassword">{"New password:"}</label> <label for="newPassword">{"New password:"}</label>

View File

@ -1,4 +1,7 @@
use crate::infra::api::HostService; use crate::{
components::router::{AppRoute, Link},
infra::api::HostService,
};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use graphql_client::GraphQLQuery; use graphql_client::GraphQLQuery;
use yew::format::Json; use yew::format::Json;
@ -80,7 +83,7 @@ impl Component for UserTable {
let make_user_row = |user: &User| { let make_user_row = |user: &User| {
html! { html! {
<tr> <tr>
<td>{&user.id}</td> <td><Link route=AppRoute::UserDetails(user.id.clone())>{&user.id}</Link></td>
<td>{&user.email}</td> <td>{&user.email}</td>
<td>{&user.display_name}</td> <td>{&user.display_name}</td>
<td>{&user.first_name}</td> <td>{&user.first_name}</td>