From 5d18e869ad5511ae02d7f0ec52479d62de4bcfbd Mon Sep 17 00:00:00 2001 From: Dario Date: Thu, 30 Jul 2020 13:13:22 +0200 Subject: [PATCH] add json interface for updating some user data from community server --- src/cpp/JSONInterface/JsonUpdateUserInfos.cpp | 105 ++++++++++++++++++ src/cpp/JSONInterface/JsonUpdateUserInfos.h | 24 ++++ 2 files changed, 129 insertions(+) create mode 100644 src/cpp/JSONInterface/JsonUpdateUserInfos.cpp create mode 100644 src/cpp/JSONInterface/JsonUpdateUserInfos.h diff --git a/src/cpp/JSONInterface/JsonUpdateUserInfos.cpp b/src/cpp/JSONInterface/JsonUpdateUserInfos.cpp new file mode 100644 index 000000000..9d857c902 --- /dev/null +++ b/src/cpp/JSONInterface/JsonUpdateUserInfos.cpp @@ -0,0 +1,105 @@ +#include "JsonUpdateUserInfos.h" + +Poco::JSON::Object* JsonUpdateUserInfos::handle(Poco::Dynamic::Var params) +{ + /* + 'session_id' => $session_id, + 'email' => $email, + 'update' => ['User.first_name' => 'first_name', 'User.last_name' => 'last_name', 'User.disabled' => 0|1, 'User.language' => 'de'] + */ + // incoming + int session_id = 0; + std::string email; + Poco::JSON::Array::Ptr updateArray; + + auto sm = SessionManager::getInstance(); + + // if is json object + if (params.type() == typeid(Poco::JSON::Object::Ptr)) { + Poco::JSON::Object::Ptr paramJsonObject = params.extract(); + /// Throws a RangeException if the value does not fit + /// into the result variable. + /// Throws a NotImplementedException if conversion is + /// not available for the given type. + /// Throws InvalidAccessException if Var is empty. + try { + paramJsonObject->get("email").convert(email); + paramJsonObject->get("session_id").convert(session_id); + askArray = paramJsonObject->getArray("ask"); + } + catch (Poco::Exception& ex) { + return stateError("json exception", ex.displayText()); + } + } + else { + return stateError("parameter format unknown"); + } + + if (!session_id) { + return stateError("session_id invalid"); + } + if (askArray.isNull()) { + return stateError("ask is zero or not an array"); + } + + auto session = sm->getSession(session_id); + if (!session) { + return customStateError("not found", "session not found"); + } + + auto user = controller::User::create(); + if (1 != user->load(email)) { + return customStateError("not found", "user not found"); + } + auto userModel = user->getModel(); + + + Poco::JSON::Object* result = new Poco::JSON::Object; + result->set("state", "success"); + Poco::JSON::Array jsonErrorsArray; + Poco::JSON::Object jsonUser; + Poco::JSON::Object jsonServer; + + for (auto it = askArray->begin(); it != askArray->end(); it++) { + auto parameter = *it; + std::string parameterString; + try { + parameter.convert(parameterString); + if (parameterString == "EmailVerificationCode.Register") { + try { + auto emailVerificationCode = controller::EmailVerificationCode::load( + userModel->getID(), model::table::EMAIL_OPT_IN_REGISTER + ); + if (!emailVerificationCode) { + emailVerificationCode = controller::EmailVerificationCode::create(userModel->getID(), model::table::EMAIL_OPT_IN_REGISTER); + UniLib::controller::TaskPtr insert = new model::table::ModelInsertTask(emailVerificationCode->getModel(), false); + insert->scheduleTask(insert); + } + jsonUser.set("EmailVerificationCode.Register", std::to_string(emailVerificationCode->getModel()->getCode())); + } + catch (Poco::Exception& ex) { + printf("exception: %s\n", ex.displayText().data()); + } + } + else if (parameterString == "loginServer.path") { + jsonServer.set("loginServer.path", ServerConfig::g_serverPath); + } + else if (parameterString == "user.pubkeyhex") { + jsonUser.set("pubkeyhex", userModel->getPublicKeyHex()); + } + else if (parameterString == "user.first_name") { + jsonUser.set("first_name", userModel->getFirstName()); + } + else if (parameterString == "user.last_name") { + jsonUser.set("last_name", userModel->getLastName()); + } + } + catch (Poco::Exception& ex) { + jsonErrorsArray.add("ask parameter invalid"); + } + } + result->set("errors", jsonErrorsArray); + result->set("userData", jsonUser); + result->set("server", jsonServer); + return result; +} \ No newline at end of file diff --git a/src/cpp/JSONInterface/JsonUpdateUserInfos.h b/src/cpp/JSONInterface/JsonUpdateUserInfos.h new file mode 100644 index 000000000..2c1ca94fc --- /dev/null +++ b/src/cpp/JSONInterface/JsonUpdateUserInfos.h @@ -0,0 +1,24 @@ +#ifndef __JSON_INTERFACE_JSON_UPDATE_USER_INFOS_ +#define __JSON_INTERFACE_JSON_UPDATE_USER_INFOS_ + +#include "JsonRequestHandler.h" + +/*! +* @author Dario Rekowski +* @date 2020-07-30 +* @brief to update non critical user data like first-name and last-name from community server with valid login-server session id +* +* works only for admins +*/ + +class JsonUpdateUserInfos : public JsonRequestHandler +{ +public: + Poco::JSON::Object* handle(Poco::Dynamic::Var params); + +protected: + + +}; + +#endif // __JSON_INTERFACE_JSON_UPDATE_USER_INFOS_ \ No newline at end of file