mirror of
https://github.com/nitnelave/lldap.git
synced 2023-04-12 14:25:13 +00:00
app: Allow admins to change passwords
This commit is contained in:
parent
005e18472e
commit
718da71d0d
@ -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,
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -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,21 +110,25 @@ impl ChangePasswordForm {
|
|||||||
if new_password != confirm_password {
|
if new_password != confirm_password {
|
||||||
bail!("Confirmation password doesn't match");
|
bail!("Confirmation password doesn't match");
|
||||||
}
|
}
|
||||||
let mut rng = rand::rngs::OsRng;
|
if self.is_admin {
|
||||||
let login_start_request =
|
self.handle_message(Msg::SubmitNewPassword)
|
||||||
opaque::client::login::start_login(&old_password, &mut rng)
|
} else {
|
||||||
.context("Could not initialize login")?;
|
let mut rng = rand::rngs::OsRng;
|
||||||
self.opaque_data = OpaqueData::Login(login_start_request.state);
|
let login_start_request =
|
||||||
let req = login::ClientLoginStartRequest {
|
opaque::client::login::start_login(&old_password, &mut rng)
|
||||||
username: self.username.clone(),
|
.context("Could not initialize login")?;
|
||||||
login_start_request: login_start_request.message,
|
self.opaque_data = OpaqueData::Login(login_start_request.state);
|
||||||
};
|
let req = login::ClientLoginStartRequest {
|
||||||
self.call_backend(
|
username: self.username.clone(),
|
||||||
HostService::login_start,
|
login_start_request: login_start_request.message,
|
||||||
req,
|
};
|
||||||
Msg::AuthenticationStartResponse,
|
self.call_backend(
|
||||||
)?;
|
HostService::login_start,
|
||||||
Ok(())
|
req,
|
||||||
|
Msg::AuthenticationStartResponse,
|
||||||
|
)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Msg::AuthenticationStartResponse(res) => {
|
Msg::AuthenticationStartResponse(res) => {
|
||||||
let res = res.context("Could not initiate login")?;
|
let res = res.context("Could not initiate login")?;
|
||||||
@ -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>
|
||||||
|
@ -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>
|
||||||
|
Loading…
Reference in New Issue
Block a user