Security stage (user login) mostly completed.
This commit is contained in:
+4
-2
@@ -43,6 +43,8 @@
|
||||
"security.user_full_name": "User full name/description",
|
||||
"security.user_password": "User password",
|
||||
"security.reuse_user_password": "Reuse user password for root user",
|
||||
"security.root_password": "Root password"
|
||||
|
||||
"security.root_password": "Root password",
|
||||
"security.pass_low": "low security",
|
||||
"security.pass_middle": "normal security",
|
||||
"security.pass_hight": "hight security"
|
||||
}
|
||||
+149
-40
@@ -18,13 +18,46 @@
|
||||
This is Security stage, setup users logins and such
|
||||
*/
|
||||
|
||||
use crate::{
|
||||
kira_theming,
|
||||
stage::{StageAction, StageResult},
|
||||
};
|
||||
use iced::{Alignment, widget};
|
||||
use crate::stage::{StageAction, StageResult};
|
||||
use iced::{Alignment, Color, widget};
|
||||
use rust_i18n::t;
|
||||
|
||||
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
|
||||
enum PasswordStrenght {
|
||||
Low,
|
||||
Middle,
|
||||
Hight,
|
||||
}
|
||||
|
||||
impl PasswordStrenght {
|
||||
pub fn from_password(password: &str) -> Self {
|
||||
if password.len() < 8 {
|
||||
Self::Low
|
||||
} else if password.len() < 16 {
|
||||
Self::Middle
|
||||
} else {
|
||||
Self::Hight
|
||||
}
|
||||
}
|
||||
pub fn color(&self) -> Color {
|
||||
match self {
|
||||
Self::Low => Color::from_rgb8(212, 32, 32),
|
||||
Self::Middle => Color::from_rgb8(212, 212, 32),
|
||||
Self::Hight => Color::from_rgb8(32, 212, 32),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl std::fmt::Display for PasswordStrenght {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
match self {
|
||||
Self::Low => f.write_str(t!("security.pass_low").as_ref()),
|
||||
Self::Middle => f.write_str(t!("security.pass_middle").as_ref()),
|
||||
Self::Hight => f.write_str(t!("security.pass_hight").as_ref()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct SecurityStage {
|
||||
user_name: String,
|
||||
@@ -34,6 +67,8 @@ pub struct SecurityStage {
|
||||
reuse_user_password_for_roor: bool,
|
||||
user_pass_is_secure: bool,
|
||||
root_pass_is_secure: bool,
|
||||
user_password_strenght: PasswordStrenght,
|
||||
root_password_strenght: PasswordStrenght,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -46,7 +81,7 @@ pub enum Message {
|
||||
ToggleUserPassSecure,
|
||||
ToggleRootPassSecure,
|
||||
Next,
|
||||
Exit,
|
||||
Back,
|
||||
}
|
||||
|
||||
/// Mothod safely retain first n chars in utf-8 string avoiding cutting utf-8 char in the middle
|
||||
@@ -67,6 +102,8 @@ impl SecurityStage {
|
||||
reuse_user_password_for_roor: false,
|
||||
user_pass_is_secure: true,
|
||||
root_pass_is_secure: true,
|
||||
user_password_strenght: PasswordStrenght::Low,
|
||||
root_password_strenght: PasswordStrenght::Low,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,8 +118,11 @@ impl SecurityStage {
|
||||
|
||||
pub fn update(&mut self, message: Message) -> StageAction {
|
||||
match message {
|
||||
Message::ToggleReusePass(v) => {
|
||||
self.reuse_user_password_for_roor = v;
|
||||
Message::ToggleReusePass(reuse_pass) => {
|
||||
self.reuse_user_password_for_roor = reuse_pass;
|
||||
if reuse_pass {
|
||||
self.root_password = self.user_password.clone();
|
||||
}
|
||||
StageAction::None
|
||||
}
|
||||
Message::UserNameChange(u_name) => {
|
||||
@@ -97,65 +137,134 @@ impl SecurityStage {
|
||||
Message::UserFullNameChange(f_name) => {
|
||||
let mut res = f_name.clone();
|
||||
res.retain(|c| c.is_alphanumeric() || ['_', '-', ' ', '.', ','].contains(&c));
|
||||
self.user_full_name = retain_first_n(res.trim(), 128).to_string();
|
||||
self.user_full_name = retain_first_n(&res, 128).to_string();
|
||||
StageAction::None
|
||||
}
|
||||
Message::UserPasswordChange(u_pass) => {
|
||||
let mut res = u_pass.trim().to_string();
|
||||
let mut res = u_pass;
|
||||
res.retain(|c: char| !c.is_control());
|
||||
self.user_password = res.trim().to_string();
|
||||
self.user_password = res;
|
||||
self.user_password_strenght = PasswordStrenght::from_password(&self.user_password);
|
||||
if self.reuse_user_password_for_roor {
|
||||
self.root_password_strenght = self.user_password_strenght;
|
||||
self.root_password = self.user_password.clone();
|
||||
}
|
||||
StageAction::None
|
||||
}
|
||||
Message::RootPasswordChange(r_pass) => {
|
||||
let mut res = r_pass.trim().to_string();
|
||||
let mut res: String = r_pass;
|
||||
res.retain(|c: char| !c.is_control());
|
||||
self.root_password = res.trim().to_string();
|
||||
self.root_password = res;
|
||||
self.root_password_strenght = PasswordStrenght::from_password(&self.root_password);
|
||||
StageAction::None
|
||||
},
|
||||
}
|
||||
Message::ToggleUserPassSecure => {
|
||||
self.user_pass_is_secure = !self.user_pass_is_secure;
|
||||
StageAction::None
|
||||
},
|
||||
}
|
||||
Message::ToggleRootPassSecure => {
|
||||
self.root_pass_is_secure = !self.root_pass_is_secure;
|
||||
StageAction::None
|
||||
},
|
||||
Message::Exit => StageAction::Abort,
|
||||
}
|
||||
Message::Back => StageAction::Back,
|
||||
Message::Next => StageAction::Next(self.gen_result()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn view(&self) -> iced::Element<'_, Message> {
|
||||
let next_button = widget::button(widget::text(t!("button.next"))).on_press(Message::Next);
|
||||
let exit_button = widget::button(widget::text(t!("button.exit"))).on_press(Message::Exit);
|
||||
|
||||
let user_pass_sec_text = if self.user_pass_is_secure {widget::text("👁")} else {widget::text("*")};
|
||||
let root_pass_sec_text = if self.root_pass_is_secure {widget::text("👁")} else {widget::text("*")};
|
||||
let mut root_pass_input = widget::text_input("", &self.root_password).secure(self.root_pass_is_secure);
|
||||
if self.reuse_user_password_for_roor {root_pass_input = root_pass_input.on_input(Message::RootPasswordChange);}
|
||||
let back_button = widget::button(widget::text(t!("button.back"))).on_press(Message::Back);
|
||||
let next_button = if self.user_name.is_empty()
|
||||
|| self.user_password.is_empty()
|
||||
|| self.root_password.is_empty()
|
||||
{
|
||||
widget::button(widget::text(t!("button.next")))
|
||||
} else {
|
||||
widget::button(widget::text(t!("button.next"))).on_press(Message::Next)
|
||||
};
|
||||
|
||||
let user_pass_sec_text = if self.user_pass_is_secure {
|
||||
widget::text("👁")
|
||||
} else {
|
||||
widget::text("*")
|
||||
};
|
||||
let root_pass_sec_text = if self.root_pass_is_secure {
|
||||
widget::text("👁")
|
||||
} else {
|
||||
widget::text("*")
|
||||
};
|
||||
let user_pass_sec_text = user_pass_sec_text
|
||||
.width(12)
|
||||
.align_x(Alignment::Center)
|
||||
.align_y(Alignment::Center);
|
||||
let root_pass_sec_text = root_pass_sec_text
|
||||
.width(12)
|
||||
.align_x(Alignment::Center)
|
||||
.align_y(Alignment::Center);
|
||||
let mut root_pass_input =
|
||||
widget::text_input("", &self.root_password).secure(self.root_pass_is_secure);
|
||||
if !self.reuse_user_password_for_roor {
|
||||
root_pass_input = root_pass_input.on_input(Message::RootPasswordChange);
|
||||
}
|
||||
|
||||
let user_p_strenght_text = widget::text(self.user_password_strenght.to_string())
|
||||
.color(self.user_password_strenght.color());
|
||||
let root_p_strenght_text = widget::text(self.root_password_strenght.to_string())
|
||||
.color(self.root_password_strenght.color());
|
||||
|
||||
widget::column![
|
||||
widget::container(
|
||||
widget::column![
|
||||
widget::text(t!("security.user_name")),
|
||||
widget::text_input("", &self.user_name).on_input(Message::UserNameChange),
|
||||
widget::text(t!("security.user_full_name")),
|
||||
widget::text_input("", &self.user_full_name).on_input(Message::UserFullNameChange),
|
||||
widget::text(t!("security.user_password")),
|
||||
widget::row![
|
||||
widget::text_input("", &self.user_password).on_input(Message::UserPasswordChange)
|
||||
.secure(self.user_pass_is_secure),
|
||||
widget::button(user_pass_sec_text).on_press(Message::ToggleUserPassSecure),
|
||||
widget::column![
|
||||
widget::text(t!("security.user_name")),
|
||||
widget::text_input("", &self.user_name)
|
||||
.width(256)
|
||||
.on_input(Message::UserNameChange)
|
||||
],
|
||||
widget::column![
|
||||
widget::text(t!("security.user_full_name")),
|
||||
widget::text_input("", &self.user_full_name)
|
||||
.width(256)
|
||||
.on_input(Message::UserFullNameChange)
|
||||
],
|
||||
widget::column![
|
||||
widget::text(t!("security.user_password")),
|
||||
widget::row![
|
||||
widget::text_input("", &self.user_password)
|
||||
.width(512)
|
||||
.on_input(Message::UserPasswordChange)
|
||||
.secure(self.user_pass_is_secure),
|
||||
widget::button(user_pass_sec_text)
|
||||
.on_press(Message::ToggleUserPassSecure),
|
||||
widget::container(user_p_strenght_text)
|
||||
.align_x(Alignment::Center)
|
||||
.style(widget::container::bordered_box)
|
||||
.style(widget::container::rounded_box)
|
||||
.padding(6)
|
||||
.width(160),
|
||||
]
|
||||
.spacing(5)
|
||||
.align_y(Alignment::Center)
|
||||
],
|
||||
widget::rule::horizontal(2),
|
||||
widget::text(t!("security.root_password")),
|
||||
widget::row![
|
||||
root_pass_input,
|
||||
widget::button(root_pass_sec_text).on_press(Message::ToggleRootPassSecure),
|
||||
widget::column![
|
||||
widget::text(t!("security.root_password")),
|
||||
widget::row![
|
||||
root_pass_input.width(512),
|
||||
widget::button(root_pass_sec_text)
|
||||
.on_press(Message::ToggleRootPassSecure),
|
||||
widget::container(root_p_strenght_text)
|
||||
.align_x(Alignment::Center)
|
||||
.style(widget::container::rounded_box)
|
||||
.style(widget::container::bordered_box)
|
||||
.padding(6)
|
||||
.width(160),
|
||||
]
|
||||
.spacing(5)
|
||||
.align_y(Alignment::Center),
|
||||
widget::checkbox(self.reuse_user_password_for_roor)
|
||||
.label(t!("security.reuse_user_password"))
|
||||
.on_toggle(Message::ToggleReusePass),
|
||||
],
|
||||
|
||||
]
|
||||
.padding(10)
|
||||
.spacing(10)
|
||||
@@ -166,9 +275,9 @@ impl SecurityStage {
|
||||
.align_x(Alignment::Center)
|
||||
.align_y(Alignment::Center),
|
||||
widget::row![
|
||||
exit_button,
|
||||
back_button,
|
||||
widget::space::horizontal(), // Pushes the right button to the far right
|
||||
next_button
|
||||
next_button,
|
||||
]
|
||||
.width(iced::Length::Fill)
|
||||
.align_y(Alignment::End)
|
||||
|
||||
Reference in New Issue
Block a user