diff options
author | Saturneric <[email protected]> | 2022-02-06 13:13:03 +0000 |
---|---|---|
committer | Saturneric <[email protected]> | 2022-02-06 13:13:03 +0000 |
commit | aa3e4dcef32828707f9da8d9800ac13caa24c547 (patch) | |
tree | f1f5001d0093eb0420eeab0761d3f3b432859d23 /src/ui/struct/SettingsObject.cpp | |
parent | <fix>(src): Do not catch any exceptions when debugging (diff) | |
download | GpgFrontend-aa3e4dcef32828707f9da8d9800ac13caa24c547.tar.gz GpgFrontend-aa3e4dcef32828707f9da8d9800ac13caa24c547.zip |
<feat>(ui): Improve the security settings auxiliary class
1. Do a type check
2. Do a null check
3. Add some necessary logs
Diffstat (limited to 'src/ui/struct/SettingsObject.cpp')
-rw-r--r-- | src/ui/struct/SettingsObject.cpp | 56 |
1 files changed, 46 insertions, 10 deletions
diff --git a/src/ui/struct/SettingsObject.cpp b/src/ui/struct/SettingsObject.cpp index 1bd426a6..5405611e 100644 --- a/src/ui/struct/SettingsObject.cpp +++ b/src/ui/struct/SettingsObject.cpp @@ -29,12 +29,29 @@ #include "SettingsObject.h" nlohmann::json& GpgFrontend::UI::SettingsObject::Check( - const std::string& key, nlohmann::json default_value) { + const std::string& key, const nlohmann::json& default_value) { + // check if the self null + if (this->nlohmann::json::is_null()) { + LOG(INFO) << "SettingsObject is null, creating new one"; + this->nlohmann::json::operator=(nlohmann::json::object()); + } + + LOG(INFO) << "Checking key: " << key; + try { - if (!this->nlohmann::json::contains(key)) - this->nlohmann::json::operator[](key) = std::move(default_value); - return this->nlohmann::json::operator[](key); - } catch (const std::exception& e) { + if (!this->nlohmann::json::contains(key) || + this->nlohmann::json::at(key).is_null() || + this->nlohmann::json::at(key).type() != default_value.type()) { + LOG(INFO) << "Added missing key: " << key; + if (default_value.is_null()) { + LOG(WARNING) << "Default value is null, using empty object"; + this->nlohmann::json::operator[](key) = nlohmann::json::object(); + } else { + this->nlohmann::json::operator[](key) = default_value; + } + } + return this->nlohmann::json::at(key); + } catch (nlohmann::json::exception& e) { LOG(ERROR) << e.what(); throw e; } @@ -42,7 +59,20 @@ nlohmann::json& GpgFrontend::UI::SettingsObject::Check( GpgFrontend::UI::SettingsObject GpgFrontend::UI::SettingsObject::Check( const std::string& key) { - if (!nlohmann::json::contains(key)) nlohmann::json::operator[](key) = {}; + // check if the self null + if (this->nlohmann::json::is_null()) { + LOG(INFO) << "SettingsObject is null, creating new one"; + this->nlohmann::json::operator=(nlohmann::json::object()); + } + + LOG(INFO) << "Checking key: " << key; + + if (!nlohmann::json::contains(key) || + this->nlohmann::json::at(key).is_null() || + this->nlohmann::json::at(key).type() != nlohmann::json::value_t::object) { + LOG(INFO) << "Added missing key: " << key; + this->nlohmann::json::operator[](key) = nlohmann::json::object(); + } return SettingsObject{nlohmann::json::operator[](key), false}; } @@ -50,13 +80,19 @@ GpgFrontend::UI::SettingsObject::SettingsObject(std::string settings_name) : settings_name_(std::move(settings_name)) { try { auto _json_optional = - GpgFrontend::DataObjectOperator::GetInstance().GetDataObject(settings_name_); + GpgFrontend::DataObjectOperator::GetInstance().GetDataObject( + settings_name_); if (_json_optional.has_value()) { + LOG(INFO) << "SettingsObject: " << settings_name_ << " loaded."; nlohmann::json::operator=(_json_optional.value()); + } else { + LOG(INFO) << "SettingsObject: " << settings_name_ << " not found."; + nlohmann::json::operator=({}); } - } catch (...) { + } catch (std::exception& e) { + LOG(ERROR) << e.what(); } } @@ -65,6 +101,6 @@ GpgFrontend::UI::SettingsObject::SettingsObject(nlohmann::json _sub_json, bool) GpgFrontend::UI::SettingsObject::~SettingsObject() { if (!settings_name_.empty()) - GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj( - settings_name_, *this); + GpgFrontend::DataObjectOperator::GetInstance().SaveDataObj(settings_name_, + *this); } |