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,
login::LoginForm,
logout::LogoutButton,
router::{AppRoute, Link},
router::{AppRoute, NavButton},
user_details::UserDetails,
user_table::UserTable,
},
@ -91,6 +91,7 @@ impl Component for App {
fn view(&self) -> Html {
let link = self.link.clone();
let is_admin = self.is_admin();
html! {
<div>
<h1>{ "LLDAP" }</h1>
@ -110,7 +111,7 @@ impl Component for App {
<div>
<LogoutButton on_logged_out=link.callback(|_| Msg::Logout) />
<UserTable />
<Link route=AppRoute::CreateUser>{"Create a user"}</Link>
<NavButton route=AppRoute::CreateUser>{"Create a user"}</NavButton>
</div>
},
AppRoute::UserDetails(username) => html! {
@ -122,7 +123,7 @@ impl Component for App {
AppRoute::ChangePassword(username) => html! {
<div>
<LogoutButton on_logged_out=link.callback(|_| Msg::Logout) />
<ChangePasswordForm username=username.clone() />
<ChangePasswordForm username=username.clone() is_admin=is_admin />
</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,
opaque_data: OpaqueData,
successfully_changed_password: bool,
is_admin: bool,
// Used to keep the request alive long enough.
_task: Option<FetchTask>,
}
@ -43,11 +44,13 @@ pub struct ChangePasswordForm {
#[derive(Clone, PartialEq, Properties)]
pub struct Props {
pub username: String,
pub is_admin: bool,
}
pub enum Msg {
Submit,
AuthenticationStartResponse(Result<Box<login::ServerLoginStartResponse>>),
SubmitNewPassword,
RegistrationStartResponse(Result<Box<registration::ServerRegistrationStartResponse>>),
RegistrationFinishResponse(Result<()>),
}
@ -107,6 +110,9 @@ impl ChangePasswordForm {
if new_password != confirm_password {
bail!("Confirmation password doesn't match");
}
if self.is_admin {
self.handle_message(Msg::SubmitNewPassword)
} else {
let mut rng = rand::rngs::OsRng;
let login_start_request =
opaque::client::login::start_login(&old_password, &mut rng)
@ -123,6 +129,7 @@ impl ChangePasswordForm {
)?;
Ok(())
}
}
Msg::AuthenticationStartResponse(res) => {
let res = res.context("Could not initiate login")?;
match self.opaque_data.take() {
@ -141,6 +148,9 @@ impl ChangePasswordForm {
}
_ => panic!("Unexpected data in opaque_data field"),
};
self.handle_message(Msg::SubmitNewPassword)
}
Msg::SubmitNewPassword => {
let mut rng = rand::rngs::OsRng;
let new_password = get_form_field("newPassword")
.ok_or_else(|| anyhow!("Could not get new password from form"))?;
@ -207,6 +217,7 @@ impl Component for ChangePasswordForm {
node_ref: NodeRef::default(),
opaque_data: OpaqueData::None,
successfully_changed_password: false,
is_admin: props.is_admin,
_task: None,
}
}
@ -225,11 +236,12 @@ impl Component for ChangePasswordForm {
}
fn view(&self) -> Html {
let is_admin = self.is_admin;
html! {
<form ref=self.node_ref.clone() onsubmit=self.link.callback(|e: FocusEvent| { e.prevent_default(); Msg::Submit })>
<div>
<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>
<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 graphql_client::GraphQLQuery;
use yew::format::Json;
@ -80,7 +83,7 @@ impl Component for UserTable {
let make_user_row = |user: &User| {
html! {
<tr>
<td>{&user.id}</td>
<td><Link route=AppRoute::UserDetails(user.id.clone())>{&user.id}</Link></td>
<td>{&user.email}</td>
<td>{&user.display_name}</td>
<td>{&user.first_name}</td>