From cfbd0168c317e13e06367ffcfbcd9a6ebf913031 Mon Sep 17 00:00:00 2001 From: proddy Date: Tue, 25 Feb 2025 20:46:43 +0100 Subject: [PATCH] new command 'set admin password' --- CHANGELOG_LATEST.md | 1 + src/core/console.cpp | 34 ++++++++++++++++++++++++++++++++++ src/core/locale_common.h | 1 + src/version.h | 2 +- 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/CHANGELOG_LATEST.md b/CHANGELOG_LATEST.md index 5b80b366c..4d32fbabd 100644 --- a/CHANGELOG_LATEST.md +++ b/CHANGELOG_LATEST.md @@ -16,6 +16,7 @@ For more details go to [docs.emsesp.org](https://docs.emsesp.org/). - CR11 thermostat [#2295](https://github.com/emsesp/EMS-ESP32/issues/2295) - Show ESP32's CPU temp in Hardware Status - vacation mode for the CR50 [#2403](https://github.com/emsesp/EMS-ESP32/issues/2403) +- new Console command "set admin password" to set WebUI admin password ## Fixed diff --git a/src/core/console.cpp b/src/core/console.cpp index f5679df84..87a262730 100644 --- a/src/core/console.cpp +++ b/src/core/console.cpp @@ -207,6 +207,40 @@ static void setup_commands(std::shared_ptr const & commands) { // // SET commands // + commands->add_command(ShellContext::MAIN, + CommandFlags::ADMIN, + string_vector{F_(set), F_(admin), F_(password)}, + [](Shell & shell, const std::vector & arguments) { + shell.enter_password(F_(new_password_prompt1), [](Shell & shell, bool completed, const std::string & password1) { + if (completed) { + shell.enter_password(F_(new_password_prompt2), [password1](Shell & shell, bool completed, const std::string & password2) { + if (completed) { + if (password1 == password2) { +#ifndef EMSESP_STANDALONE + EMSESP::esp32React.getSecuritySettingsService()->updateWithoutPropagation( + [&](SecuritySettings & securitySettings) { + // find the username 'admin' and update its password + for (auto & user : securitySettings.users) { + if (user.username == "admin") { + user.password = password2.c_str(); + return StateUpdateResult::CHANGED; + } + } + // otherwise add a new admin user + securitySettings.users.emplace_back(F_(admin), password2.c_str(), true); + return StateUpdateResult::CHANGED; + }); +#endif + shell.println("Admin user's password updated."); + } else { + shell.println("Passwords do not match"); + } + } + }); + } + }); + }); + commands->add_command(ShellContext::MAIN, CommandFlags::ADMIN, string_vector{F_(set), F_(wifi), F_(password)}, diff --git a/src/core/locale_common.h b/src/core/locale_common.h index f76293c45..92079bcae 100644 --- a/src/core/locale_common.h +++ b/src/core/locale_common.h @@ -35,6 +35,7 @@ MAKE_WORD(show) MAKE_WORD(su) MAKE_WORD(scan) MAKE_WORD(password) +MAKE_WORD(admin) MAKE_WORD(read) MAKE_WORD(values) MAKE_WORD(system) diff --git a/src/version.h b/src/version.h index 4e00281da..961d82e2e 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define EMSESP_APP_VERSION "3.7.2-dev.22" +#define EMSESP_APP_VERSION "3.7.3-dev.23"