Escalator: Allow failed password attempts to be retried

There is a limit of 3 attempts before quitting, but the user can try
again after that.

The error messages now display to help the user understand the issue.
This commit is contained in:
Hugh Davenport 2024-03-03 08:01:58 +13:00 committed by Tim Flynn
parent 74b655d035
commit d8f756ce1d
2 changed files with 18 additions and 12 deletions

View file

@ -54,12 +54,20 @@ EscalatorWindow::EscalatorWindow(StringView executable, Vector<StringView> argum
m_icon_image_widget->set_bitmap(app_icon.bitmap_for_size(32)); m_icon_image_widget->set_bitmap(app_icon.bitmap_for_size(32));
m_ok_button = *main_widget->find_descendant_of_type_named<GUI::DialogButton>("ok_button"); m_ok_button = *main_widget->find_descendant_of_type_named<GUI::DialogButton>("ok_button");
m_ok_button->on_click = [this](auto) { size_t password_attempts = 0;
auto result = check_password(); m_ok_button->on_click = [this, password_attempts](auto) mutable {
if (result.is_error()) { if (check_password()) {
GUI::MessageBox::show_error(this, ByteString::formatted("Failed to execute command: {}", result.error())); auto result = execute_command();
if (result.is_error()) {
GUI::MessageBox::show_error(this, ByteString::formatted("Failed to execute command: {}", result.error()));
}
close();
} else {
if (++password_attempts >= 3) {
GUI::MessageBox::show_error(this, ByteString::formatted("Too many failed attempts"));
close();
}
} }
close();
}; };
m_ok_button->set_default(true); m_ok_button->set_default(true);
@ -72,12 +80,12 @@ EscalatorWindow::EscalatorWindow(StringView executable, Vector<StringView> argum
m_password_input->set_focus(true); m_password_input->set_focus(true);
} }
ErrorOr<void> EscalatorWindow::check_password() bool EscalatorWindow::check_password()
{ {
ByteString password = m_password_input->text(); ByteString password = m_password_input->text();
if (password.is_empty()) { if (password.is_empty()) {
GUI::MessageBox::show_error(this, "Please enter a password."sv); GUI::MessageBox::show_error(this, "Please enter a password."sv);
return {}; return false;
} }
// FIXME: PasswordBox really should store its input directly as a SecretString. // FIXME: PasswordBox really should store its input directly as a SecretString.
@ -85,12 +93,10 @@ ErrorOr<void> EscalatorWindow::check_password()
if (!m_current_user.authenticate(password_secret)) { if (!m_current_user.authenticate(password_secret)) {
GUI::MessageBox::show_error(this, "Incorrect or disabled password."sv); GUI::MessageBox::show_error(this, "Incorrect or disabled password."sv);
m_password_input->select_all(); m_password_input->select_all();
return {}; return false;
} }
// Caller will close Escalator if error is returned. return true;
TRY(execute_command());
return {};
} }
ErrorOr<void> EscalatorWindow::execute_command() ErrorOr<void> EscalatorWindow::execute_command()

View file

@ -36,7 +36,7 @@ public:
private: private:
EscalatorWindow(StringView executable, Vector<StringView> arguments, Options const& options); EscalatorWindow(StringView executable, Vector<StringView> arguments, Options const& options);
ErrorOr<void> check_password(); bool check_password();
Vector<StringView> m_arguments; Vector<StringView> m_arguments;
StringView m_executable; StringView m_executable;