check for empty param

This commit is contained in:
proddy
2024-10-22 17:26:37 +02:00
parent 747aabef0a
commit 496b335780

View File

@@ -154,19 +154,31 @@ void WebStatusService::action(AsyncWebServerRequest * request, JsonVariant json)
auto * response = new AsyncJsonResponse(); auto * response = new AsyncJsonResponse();
JsonObject root = response->getRoot(); JsonObject root = response->getRoot();
// get action and any optional param // param is optional - https://arduinojson.org/news/2024/09/18/arduinojson-7-2/
std::string action = json["action"]; std::string param;
std::string param = json["param"]; // is optional bool has_param = false;
JsonVariant param_optional = json["param"];
if (json["param"].is<const char *>()) {
param = param_optional.as<std::string>();
has_param = true;
} else {
has_param = false;
}
// check if we're authenticated for admin tasks, some actions are only for admins // check if we're authenticated for admin tasks, some actions are only for admins
Authentication authentication = _securityManager->authenticateRequest(request); Authentication authentication = _securityManager->authenticateRequest(request);
bool is_admin = AuthenticationPredicates::IS_ADMIN(authentication); bool is_admin = AuthenticationPredicates::IS_ADMIN(authentication);
bool ok = true; // call action command
bool ok = false;
std::string action = json["action"];
if (action == "checkUpgrade") { if (action == "checkUpgrade") {
ok = checkUpgrade(root, param); ok = checkUpgrade(root, param); // param could be empty, if so only send back version
} else if (action == "export") { } else if (action == "export") {
if (has_param) {
ok = exportData(root, param); ok = exportData(root, param);
}
} else if (action == "customSupport") { } else if (action == "customSupport") {
ok = customSupport(root); ok = customSupport(root);
} else if (action == "uploadURL" && is_admin) { } else if (action == "uploadURL" && is_admin) {
@@ -191,19 +203,21 @@ void WebStatusService::action(AsyncWebServerRequest * request, JsonVariant json)
} }
// action = checkUpgrade // action = checkUpgrade
// returns true if there is an upgrade available
bool WebStatusService::checkUpgrade(JsonObject root, std::string & latest_version) { bool WebStatusService::checkUpgrade(JsonObject root, std::string & latest_version) {
version::Semver200_version settings_version(EMSESP_APP_VERSION); root["emsesp_version"] = EMSESP_APP_VERSION;
version::Semver200_version this_version(latest_version);
if (!latest_version.empty()) {
#if defined(EMSESP_DEBUG) #if defined(EMSESP_DEBUG)
emsesp::EMSESP::logger().debug("Checking for upgrade: %s > %s", EMSESP_APP_VERSION, latest_version.c_str()); emsesp::EMSESP::logger().debug("Checking for upgrade: %s > %s", EMSESP_APP_VERSION, latest_version.c_str());
#endif #endif
root["upgradeable"] = (this_version > settings_version); version::Semver200_version settings_version(EMSESP_APP_VERSION);
root["emsesp_version"] = EMSESP_APP_VERSION; version::Semver200_version this_version(latest_version);
return true; root["upgradeable"] = (this_version > settings_version);
}
return true; // always ok
} }
// action = allvalues // action = allvalues
@@ -258,8 +272,9 @@ bool WebStatusService::exportData(JsonObject root, std::string & type) {
root.clear(); // don't need the "type" key added to the output root.clear(); // don't need the "type" key added to the output
allvalues(root); allvalues(root);
} else { } else {
return false; return false; // error
} }
return true; return true;
} }