2021-10-31 13:22:40 +00:00
|
|
|
use crate::{
|
|
|
|
components::router::AppRoute,
|
|
|
|
infra::{
|
|
|
|
api::HostService,
|
|
|
|
common_component::{CommonComponent, CommonComponentParts},
|
|
|
|
},
|
|
|
|
};
|
2021-09-19 19:02:23 +00:00
|
|
|
use anyhow::{bail, Context, Result};
|
2021-08-30 07:50:10 +00:00
|
|
|
use graphql_client::GraphQLQuery;
|
2021-08-31 14:29:49 +00:00
|
|
|
use lldap_auth::{opaque, registration};
|
2021-09-19 19:02:23 +00:00
|
|
|
use validator_derive::Validate;
|
2021-06-01 15:30:57 +00:00
|
|
|
use yew::prelude::*;
|
2021-10-31 13:22:40 +00:00
|
|
|
use yew::services::ConsoleService;
|
2021-09-19 19:02:23 +00:00
|
|
|
use yew_form_derive::Model;
|
2021-06-01 15:30:57 +00:00
|
|
|
use yew_router::{
|
|
|
|
agent::{RouteAgentDispatcher, RouteRequest},
|
|
|
|
route::Route,
|
|
|
|
};
|
|
|
|
|
2021-08-30 07:50:10 +00:00
|
|
|
#[derive(GraphQLQuery)]
|
|
|
|
#[graphql(
|
|
|
|
schema_path = "../schema.graphql",
|
|
|
|
query_path = "queries/create_user.graphql",
|
|
|
|
response_derives = "Debug",
|
2021-09-12 09:54:47 +00:00
|
|
|
custom_scalars_module = "crate::infra::graphql"
|
2021-08-30 07:50:10 +00:00
|
|
|
)]
|
|
|
|
pub struct CreateUser;
|
|
|
|
|
2021-06-01 15:30:57 +00:00
|
|
|
pub struct CreateUserForm {
|
2021-10-31 13:22:40 +00:00
|
|
|
common: CommonComponentParts<Self>,
|
2021-06-01 15:30:57 +00:00
|
|
|
route_dispatcher: RouteAgentDispatcher,
|
2021-09-19 19:02:23 +00:00
|
|
|
form: yew_form::Form<CreateUserModel>,
|
2021-06-01 15:30:57 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 04:48:39 +00:00
|
|
|
#[derive(Model, Validate, PartialEq, Eq, Clone, Default)]
|
2021-09-19 19:02:23 +00:00
|
|
|
pub struct CreateUserModel {
|
|
|
|
#[validate(length(min = 1, message = "Username is required"))]
|
|
|
|
username: String,
|
2021-10-11 18:31:35 +00:00
|
|
|
#[validate(email(message = "A valid email is required"))]
|
2021-09-19 19:02:23 +00:00
|
|
|
email: String,
|
|
|
|
display_name: String,
|
|
|
|
first_name: String,
|
|
|
|
last_name: String,
|
|
|
|
#[validate(custom(
|
|
|
|
function = "empty_or_long",
|
|
|
|
message = "Password should be longer than 8 characters (or left empty)"
|
|
|
|
))]
|
|
|
|
password: String,
|
|
|
|
#[validate(must_match(other = "password", message = "Passwords must match"))]
|
|
|
|
confirm_password: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn empty_or_long(value: &str) -> Result<(), validator::ValidationError> {
|
|
|
|
if value.is_empty() || value.len() >= 8 {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(validator::ValidationError::new(""))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-01 15:30:57 +00:00
|
|
|
pub enum Msg {
|
2021-09-19 19:02:23 +00:00
|
|
|
Update,
|
2021-06-01 15:30:57 +00:00
|
|
|
SubmitForm,
|
2021-09-19 19:02:23 +00:00
|
|
|
CreateUserResponse(Result<create_user::ResponseData>),
|
2021-06-24 16:23:23 +00:00
|
|
|
SuccessfulCreation,
|
2021-09-19 19:02:23 +00:00
|
|
|
RegistrationStartResponse(
|
|
|
|
(
|
|
|
|
opaque::client::registration::ClientRegistration,
|
|
|
|
Result<Box<registration::ServerRegistrationStartResponse>>,
|
|
|
|
),
|
|
|
|
),
|
2021-06-24 16:23:23 +00:00
|
|
|
RegistrationFinishResponse(Result<()>),
|
|
|
|
}
|
|
|
|
|
2021-10-31 13:22:40 +00:00
|
|
|
impl CommonComponent<CreateUserForm> for CreateUserForm {
|
2021-09-19 19:02:23 +00:00
|
|
|
fn handle_msg(&mut self, msg: <Self as Component>::Message) -> Result<bool> {
|
2021-06-24 16:23:23 +00:00
|
|
|
match msg {
|
2021-09-19 19:02:23 +00:00
|
|
|
Msg::Update => Ok(true),
|
2021-06-24 16:23:23 +00:00
|
|
|
Msg::SubmitForm => {
|
2021-09-19 19:02:23 +00:00
|
|
|
if !self.form.validate() {
|
|
|
|
bail!("Check the form for errors");
|
|
|
|
}
|
|
|
|
let model = self.form.model();
|
|
|
|
let to_option = |s: String| if s.is_empty() { None } else { Some(s) };
|
2021-08-30 07:50:10 +00:00
|
|
|
let req = create_user::Variables {
|
2021-09-01 08:00:51 +00:00
|
|
|
user: create_user::CreateUserInput {
|
2021-09-19 19:02:23 +00:00
|
|
|
id: model.username,
|
|
|
|
email: model.email,
|
|
|
|
displayName: to_option(model.display_name),
|
|
|
|
firstName: to_option(model.first_name),
|
|
|
|
lastName: to_option(model.last_name),
|
2022-08-03 21:53:25 +00:00
|
|
|
avatar: None,
|
2021-08-30 07:50:10 +00:00
|
|
|
},
|
2021-06-24 16:23:23 +00:00
|
|
|
};
|
2021-10-31 13:22:40 +00:00
|
|
|
self.common.call_graphql::<CreateUser, _>(
|
2021-08-30 07:50:10 +00:00
|
|
|
req,
|
2021-10-31 13:22:40 +00:00
|
|
|
Msg::CreateUserResponse,
|
2021-08-30 07:50:10 +00:00
|
|
|
"Error trying to create user",
|
2021-10-31 13:22:40 +00:00
|
|
|
);
|
2021-09-19 19:02:23 +00:00
|
|
|
Ok(true)
|
2021-06-24 16:23:23 +00:00
|
|
|
}
|
|
|
|
Msg::CreateUserResponse(r) => {
|
2021-08-30 07:50:10 +00:00
|
|
|
match r {
|
|
|
|
Err(e) => return Err(e),
|
|
|
|
Ok(r) => ConsoleService::log(&format!(
|
|
|
|
"Created user '{}' at '{}'",
|
|
|
|
&r.create_user.id, &r.create_user.creation_date
|
|
|
|
)),
|
|
|
|
};
|
2021-09-19 19:02:23 +00:00
|
|
|
let model = self.form.model();
|
|
|
|
let user_id = model.username;
|
|
|
|
let password = model.password;
|
|
|
|
if !password.is_empty() {
|
2021-06-24 16:23:23 +00:00
|
|
|
// User was successfully created, let's register the password.
|
|
|
|
let mut rng = rand::rngs::OsRng;
|
2021-09-19 19:02:23 +00:00
|
|
|
let opaque::client::registration::ClientRegistrationStartResult {
|
|
|
|
state,
|
|
|
|
message,
|
|
|
|
} = opaque::client::registration::start_registration(&password, &mut rng)?;
|
2021-06-24 16:23:23 +00:00
|
|
|
let req = registration::ClientRegistrationStartRequest {
|
|
|
|
username: user_id,
|
2021-09-19 19:02:23 +00:00
|
|
|
registration_start_request: message,
|
2021-06-24 16:23:23 +00:00
|
|
|
};
|
2021-10-31 13:22:40 +00:00
|
|
|
self.common
|
|
|
|
.call_backend(HostService::register_start, req, move |r| {
|
|
|
|
Msg::RegistrationStartResponse((state, r))
|
|
|
|
})
|
|
|
|
.context("Error trying to create user")?;
|
2021-06-24 16:23:23 +00:00
|
|
|
} else {
|
|
|
|
self.update(Msg::SuccessfulCreation);
|
|
|
|
}
|
2021-09-19 19:02:23 +00:00
|
|
|
Ok(false)
|
2021-06-24 16:23:23 +00:00
|
|
|
}
|
2021-09-19 19:02:23 +00:00
|
|
|
Msg::RegistrationStartResponse((registration_start, response)) => {
|
2021-06-24 16:23:23 +00:00
|
|
|
let response = response?;
|
|
|
|
let mut rng = rand::rngs::OsRng;
|
|
|
|
let registration_upload = opaque::client::registration::finish_registration(
|
2021-09-19 19:02:23 +00:00
|
|
|
registration_start,
|
2021-06-24 16:23:23 +00:00
|
|
|
response.registration_response,
|
|
|
|
&mut rng,
|
|
|
|
)?;
|
|
|
|
let req = registration::ClientRegistrationFinishRequest {
|
|
|
|
server_data: response.server_data,
|
|
|
|
registration_upload: registration_upload.message,
|
|
|
|
};
|
2021-10-31 13:22:40 +00:00
|
|
|
self.common
|
|
|
|
.call_backend(
|
|
|
|
HostService::register_finish,
|
2021-06-24 16:23:23 +00:00
|
|
|
req,
|
2021-10-31 13:22:40 +00:00
|
|
|
Msg::RegistrationFinishResponse,
|
2021-06-24 16:23:23 +00:00
|
|
|
)
|
2021-10-31 13:22:40 +00:00
|
|
|
.context("Error trying to register user")?;
|
2021-09-19 19:02:23 +00:00
|
|
|
Ok(false)
|
2021-06-24 16:23:23 +00:00
|
|
|
}
|
|
|
|
Msg::RegistrationFinishResponse(response) => {
|
2021-09-19 19:02:23 +00:00
|
|
|
response?;
|
|
|
|
self.handle_msg(Msg::SuccessfulCreation)
|
2021-06-24 16:23:23 +00:00
|
|
|
}
|
|
|
|
Msg::SuccessfulCreation => {
|
|
|
|
self.route_dispatcher
|
2021-10-11 16:54:53 +00:00
|
|
|
.send(RouteRequest::ChangeRoute(Route::from(AppRoute::ListUsers)));
|
2021-09-19 19:02:23 +00:00
|
|
|
Ok(true)
|
2021-06-01 15:30:57 +00:00
|
|
|
}
|
2021-06-24 16:23:23 +00:00
|
|
|
}
|
2021-06-01 15:30:57 +00:00
|
|
|
}
|
2021-10-31 13:22:40 +00:00
|
|
|
|
|
|
|
fn mut_common(&mut self) -> &mut CommonComponentParts<Self> {
|
|
|
|
&mut self.common
|
|
|
|
}
|
2021-06-01 15:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Component for CreateUserForm {
|
|
|
|
type Message = Msg;
|
|
|
|
type Properties = ();
|
|
|
|
|
2021-10-31 13:22:40 +00:00
|
|
|
fn create(props: Self::Properties, link: ComponentLink<Self>) -> Self {
|
2021-06-01 15:30:57 +00:00
|
|
|
Self {
|
2021-10-31 13:22:40 +00:00
|
|
|
common: CommonComponentParts::<Self>::create(props, link),
|
2021-06-01 15:30:57 +00:00
|
|
|
route_dispatcher: RouteAgentDispatcher::new(),
|
2021-09-19 19:02:23 +00:00
|
|
|
form: yew_form::Form::<CreateUserModel>::new(CreateUserModel::default()),
|
2021-06-01 15:30:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self, msg: Self::Message) -> ShouldRender {
|
2021-10-31 13:22:40 +00:00
|
|
|
CommonComponentParts::<Self>::update(self, msg)
|
2021-06-01 15:30:57 +00:00
|
|
|
}
|
|
|
|
|
2022-06-30 07:27:51 +00:00
|
|
|
fn change(&mut self, props: Self::Properties) -> ShouldRender {
|
|
|
|
self.common.change(props)
|
2021-06-01 15:30:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn view(&self) -> Html {
|
2021-09-19 19:02:23 +00:00
|
|
|
type Field = yew_form::Field<CreateUserModel>;
|
2021-06-01 15:30:57 +00:00
|
|
|
html! {
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="row justify-content-center">
|
2022-11-03 14:40:02 +00:00
|
|
|
<form class="form py-3" style="max-width: 636px">
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="row mb-3">
|
|
|
|
<h5 class="fw-bold">{"Create a user"}</h5>
|
|
|
|
</div>
|
|
|
|
<div class="form-group row mb-3">
|
2021-09-19 17:44:53 +00:00
|
|
|
<label for="username"
|
2021-10-15 08:20:37 +00:00
|
|
|
class="form-label col-4 col-form-label">
|
2022-11-03 14:40:02 +00:00
|
|
|
{"User name"}
|
|
|
|
<span class="text-danger">{"*"}</span>
|
|
|
|
{":"}
|
2021-09-19 17:44:53 +00:00
|
|
|
</label>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="col-8">
|
2021-09-19 19:02:23 +00:00
|
|
|
<Field
|
|
|
|
form=&self.form
|
|
|
|
field_name="username"
|
2021-09-19 17:44:53 +00:00
|
|
|
class="form-control"
|
2021-09-19 19:02:23 +00:00
|
|
|
class_invalid="is-invalid has-error"
|
|
|
|
class_valid="has-success"
|
2021-09-19 17:44:53 +00:00
|
|
|
autocomplete="username"
|
2021-10-31 13:22:40 +00:00
|
|
|
oninput=self.common.callback(|_| Msg::Update) />
|
2021-09-19 19:02:23 +00:00
|
|
|
<div class="invalid-feedback">
|
|
|
|
{&self.form.field_message("username")}
|
|
|
|
</div>
|
2021-06-01 15:30:57 +00:00
|
|
|
</div>
|
2021-09-19 17:44:53 +00:00
|
|
|
</div>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="form-group row mb-3">
|
2021-09-19 17:44:53 +00:00
|
|
|
<label for="email"
|
2021-10-15 08:20:37 +00:00
|
|
|
class="form-label col-4 col-form-label">
|
2022-11-03 14:40:02 +00:00
|
|
|
{"Email"}
|
|
|
|
<span class="text-danger">{"*"}</span>
|
|
|
|
{":"}
|
2021-09-19 17:44:53 +00:00
|
|
|
</label>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="col-8">
|
2021-09-19 19:02:23 +00:00
|
|
|
<Field
|
|
|
|
form=&self.form
|
|
|
|
input_type="email"
|
|
|
|
field_name="email"
|
2021-09-19 17:44:53 +00:00
|
|
|
class="form-control"
|
2021-09-19 19:02:23 +00:00
|
|
|
class_invalid="is-invalid has-error"
|
|
|
|
class_valid="has-success"
|
2021-09-19 17:44:53 +00:00
|
|
|
autocomplete="email"
|
2021-10-31 13:22:40 +00:00
|
|
|
oninput=self.common.callback(|_| Msg::Update) />
|
2021-09-19 19:02:23 +00:00
|
|
|
<div class="invalid-feedback">
|
|
|
|
{&self.form.field_message("email")}
|
|
|
|
</div>
|
2021-06-01 15:30:57 +00:00
|
|
|
</div>
|
2021-09-19 17:44:53 +00:00
|
|
|
</div>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="form-group row mb-3">
|
2021-09-19 17:44:53 +00:00
|
|
|
<label for="display-name"
|
2021-10-15 08:20:37 +00:00
|
|
|
class="form-label col-4 col-form-label">
|
2022-11-21 08:13:25 +00:00
|
|
|
{"Display name:"}
|
2021-09-19 17:44:53 +00:00
|
|
|
</label>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="col-8">
|
2021-09-19 19:02:23 +00:00
|
|
|
<Field
|
|
|
|
form=&self.form
|
2021-09-19 17:44:53 +00:00
|
|
|
autocomplete="name"
|
|
|
|
class="form-control"
|
2021-09-19 19:02:23 +00:00
|
|
|
class_invalid="is-invalid has-error"
|
|
|
|
class_valid="has-success"
|
|
|
|
field_name="display_name"
|
2021-10-31 13:22:40 +00:00
|
|
|
oninput=self.common.callback(|_| Msg::Update) />
|
2021-09-19 19:02:23 +00:00
|
|
|
<div class="invalid-feedback">
|
|
|
|
{&self.form.field_message("display_name")}
|
2021-09-19 17:44:53 +00:00
|
|
|
</div>
|
2021-09-19 19:02:23 +00:00
|
|
|
</div>
|
2021-09-19 17:44:53 +00:00
|
|
|
</div>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="form-group row mb-3">
|
2021-09-19 17:44:53 +00:00
|
|
|
<label for="first-name"
|
2021-10-15 08:20:37 +00:00
|
|
|
class="form-label col-4 col-form-label">
|
2021-09-19 17:44:53 +00:00
|
|
|
{"First name:"}
|
|
|
|
</label>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="col-8">
|
2021-09-19 19:02:23 +00:00
|
|
|
<Field
|
|
|
|
form=&self.form
|
2021-09-19 17:44:53 +00:00
|
|
|
autocomplete="given-name"
|
|
|
|
class="form-control"
|
2021-09-19 19:02:23 +00:00
|
|
|
class_invalid="is-invalid has-error"
|
|
|
|
class_valid="has-success"
|
|
|
|
field_name="first_name"
|
2021-10-31 13:22:40 +00:00
|
|
|
oninput=self.common.callback(|_| Msg::Update) />
|
2021-09-19 19:02:23 +00:00
|
|
|
<div class="invalid-feedback">
|
|
|
|
{&self.form.field_message("first_name")}
|
|
|
|
</div>
|
2021-06-01 15:30:57 +00:00
|
|
|
</div>
|
2021-09-19 17:44:53 +00:00
|
|
|
</div>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="form-group row mb-3">
|
2021-09-19 17:44:53 +00:00
|
|
|
<label for="last-name"
|
2021-10-15 08:20:37 +00:00
|
|
|
class="form-label col-4 col-form-label">
|
2021-09-19 17:44:53 +00:00
|
|
|
{"Last name:"}
|
|
|
|
</label>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="col-8">
|
2021-09-19 19:02:23 +00:00
|
|
|
<Field
|
|
|
|
form=&self.form
|
2021-09-19 17:44:53 +00:00
|
|
|
autocomplete="family-name"
|
|
|
|
class="form-control"
|
2021-09-19 19:02:23 +00:00
|
|
|
class_invalid="is-invalid has-error"
|
|
|
|
class_valid="has-success"
|
|
|
|
field_name="last_name"
|
2021-10-31 13:22:40 +00:00
|
|
|
oninput=self.common.callback(|_| Msg::Update) />
|
2021-09-19 19:02:23 +00:00
|
|
|
<div class="invalid-feedback">
|
|
|
|
{&self.form.field_message("last_name")}
|
|
|
|
</div>
|
2021-06-01 15:30:57 +00:00
|
|
|
</div>
|
2021-09-19 17:44:53 +00:00
|
|
|
</div>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="form-group row mb-3">
|
2021-09-19 17:44:53 +00:00
|
|
|
<label for="password"
|
2021-10-15 08:20:37 +00:00
|
|
|
class="form-label col-4 col-form-label">
|
2021-09-19 17:44:53 +00:00
|
|
|
{"Password:"}
|
|
|
|
</label>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="col-8">
|
2021-09-19 19:02:23 +00:00
|
|
|
<Field
|
|
|
|
form=&self.form
|
|
|
|
input_type="password"
|
|
|
|
field_name="password"
|
2021-09-19 17:44:53 +00:00
|
|
|
class="form-control"
|
2021-09-19 19:02:23 +00:00
|
|
|
class_invalid="is-invalid has-error"
|
|
|
|
class_valid="has-success"
|
2021-09-19 17:44:53 +00:00
|
|
|
autocomplete="new-password"
|
2021-10-31 13:22:40 +00:00
|
|
|
oninput=self.common.callback(|_| Msg::Update) />
|
2021-09-19 19:02:23 +00:00
|
|
|
<div class="invalid-feedback">
|
|
|
|
{&self.form.field_message("password")}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="form-group row mb-3">
|
2021-09-19 19:02:23 +00:00
|
|
|
<label for="confirm_password"
|
2021-10-15 08:20:37 +00:00
|
|
|
class="form-label col-4 col-form-label">
|
2021-09-19 19:02:23 +00:00
|
|
|
{"Confirm password:"}
|
|
|
|
</label>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="col-8">
|
2021-09-19 19:02:23 +00:00
|
|
|
<Field
|
|
|
|
form=&self.form
|
|
|
|
input_type="password"
|
|
|
|
field_name="confirm_password"
|
|
|
|
class="form-control"
|
|
|
|
class_invalid="is-invalid has-error"
|
|
|
|
class_valid="has-success"
|
|
|
|
autocomplete="new-password"
|
2021-10-31 13:22:40 +00:00
|
|
|
oninput=self.common.callback(|_| Msg::Update) />
|
2021-09-19 19:02:23 +00:00
|
|
|
<div class="invalid-feedback">
|
|
|
|
{&self.form.field_message("confirm_password")}
|
|
|
|
</div>
|
2021-06-01 15:30:57 +00:00
|
|
|
</div>
|
2021-09-19 17:44:53 +00:00
|
|
|
</div>
|
2021-10-15 08:20:37 +00:00
|
|
|
<div class="form-group row justify-content-center">
|
2021-09-19 17:44:53 +00:00
|
|
|
<button
|
2021-10-15 08:20:37 +00:00
|
|
|
class="btn btn-primary col-auto col-form-label mt-4"
|
2021-10-31 13:22:40 +00:00
|
|
|
disabled=self.common.is_task_running()
|
2021-10-12 03:02:20 +00:00
|
|
|
type="submit"
|
2021-10-31 13:22:40 +00:00
|
|
|
onclick=self.common.callback(|e: MouseEvent| {e.prevent_default(); Msg::SubmitForm})>
|
2022-11-03 14:40:02 +00:00
|
|
|
<i class="bi-save me-2"></i>
|
2021-09-19 19:02:23 +00:00
|
|
|
{"Submit"}
|
|
|
|
</button>
|
2021-09-19 17:44:53 +00:00
|
|
|
</div>
|
2021-06-01 15:30:57 +00:00
|
|
|
</form>
|
2022-11-03 14:40:02 +00:00
|
|
|
{
|
|
|
|
if let Some(e) = &self.common.error {
|
2021-09-19 17:44:53 +00:00
|
|
|
html! {
|
|
|
|
<div class="alert alert-danger">
|
|
|
|
{e.to_string() }
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
} else { html! {} }
|
|
|
|
}
|
2021-10-15 08:20:37 +00:00
|
|
|
</div>
|
2021-06-01 15:30:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|