Skip to content

Commit b67293e

Browse files
committed
Refactor login in graphical shell
1 parent a65ec0f commit b67293e

File tree

3 files changed

+48
-39
lines changed

3 files changed

+48
-39
lines changed

Modules/Executables/Shell/Graphical/Tests/Integration_test.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ fn main() {
9292

9393
Authentication::Create_group(
9494
Virtual_file_system::Get_instance(),
95-
"users",
95+
"alix_anneraud",
9696
Some(Group_identifier),
9797
)
9898
.unwrap();

Modules/Executables/Shell/Graphical/src/Error.rs

+4
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ pub enum Error_type {
1111
Failed_to_set_environment_variable(Task::Error_type),
1212
Invalid_UTF_8(core::str::Utf8Error),
1313
Authentication_failed(Authentication::Error_type),
14+
Failed_to_set_task_user(Task::Error_type),
1415
}
1516

1617
impl Error_type {
@@ -49,6 +50,9 @@ impl Display for Error_type {
4950
Self::Authentication_failed(Error) => {
5051
write!(Formatter, "Authentication failed: {}", Error)
5152
}
53+
Self::Failed_to_set_task_user(Error) => {
54+
write!(Formatter, "Failed to set task user: {}", Error)
55+
}
5256
}
5357
}
5458
}

Modules/Executables/Shell/Graphical/src/Login.rs

+43-38
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,44 @@ impl Login_type {
100100
}
101101
}
102102

103+
pub fn Authenticate(&mut self) -> Result_type<()> {
104+
let (User_name, Password) = unsafe {
105+
let User_name = LVGL::lv_textarea_get_text(self.User_name_text_area);
106+
let User_name = CStr::from_ptr(User_name);
107+
108+
let User_name = User_name.to_str().map_err(Error_type::Invalid_UTF_8)?;
109+
110+
let Password = LVGL::lv_textarea_get_text(self.Password_text_area);
111+
let Password = CStr::from_ptr(Password);
112+
let Password = Password.to_str().map_err(Error_type::Invalid_UTF_8)?;
113+
114+
(User_name, Password)
115+
};
116+
117+
// - Check the user name and the password
118+
let User_identifier = Authentication::Authenticate_user(
119+
Virtual_file_system::Get_instance(),
120+
User_name,
121+
Password,
122+
)
123+
.map_err(Error_type::Authentication_failed)?;
124+
125+
// - Set the user
126+
let Task_manager = Task::Get_instance();
127+
128+
let Task = Task_manager
129+
.Get_current_task_identifier()
130+
.map_err(Error_type::Failed_to_set_task_user)?;
131+
132+
Task_manager
133+
.Set_user(Task, User_identifier)
134+
.map_err(Error_type::Failed_to_set_task_user)?;
135+
136+
self.User = Some(User_identifier);
137+
138+
Ok(())
139+
}
140+
103141
pub fn Event_handler(&mut self) {
104142
while let Some(Event) = self.Window.Pop_event() {
105143
// If we are typing the user name or the password
@@ -113,44 +151,11 @@ impl Login_type {
113151
else if Event.Get_code() == Event_code_type::Clicked
114152
&& Event.Get_target() == self.Button
115153
{
116-
unsafe {
117-
let User_name = LVGL::lv_textarea_get_text(self.User_name_text_area);
118-
let User_name = CStr::from_ptr(User_name);
119-
120-
let User_name = match User_name.to_str().map_err(Error_type::Invalid_UTF_8) {
121-
Ok(User_name) => User_name,
122-
Err(Error) => {
123-
self.Print_error(Error);
124-
continue;
125-
}
126-
};
127-
128-
let Password = LVGL::lv_textarea_get_text(self.Password_text_area);
129-
let Password = CStr::from_ptr(Password);
130-
let Password = match Password.to_str().map_err(Error_type::Invalid_UTF_8) {
131-
Ok(Password) => Password,
132-
Err(Error) => {
133-
self.Print_error(Error);
134-
continue;
135-
}
136-
};
137-
138-
// - Check the user name and the password
139-
match Authentication::Authenticate_user(
140-
Virtual_file_system::Get_instance(),
141-
User_name,
142-
Password,
143-
)
144-
.map_err(Error_type::Authentication_failed)
145-
{
146-
Ok(User_identifier) => {
147-
self.User = Some(User_identifier);
148-
}
149-
Err(Error) => {
150-
self.Print_error(Error);
151-
}
152-
}
153-
};
154+
let Result = self.Authenticate();
155+
156+
if let Err(Error) = Result {
157+
self.Print_error(Error);
158+
}
154159
}
155160
}
156161
}

0 commit comments

Comments
 (0)