From 9215ad7a8f706f57507140b3213ee874e8c37cda Mon Sep 17 00:00:00 2001 From: Hepatica Date: Fri, 16 Aug 2024 15:59:05 +0200 Subject: [PATCH] Introduce ban logic and adjust sp cloud diagramm --- .$SpCloud.drawio.dtmp | 181 +++++++++++++++++++++++++ SpCloud.drawio | 115 ++++++++-------- SpCloudMain/CMakeLists.txt | 2 +- SpCloudMain/Models/App.cpp | 50 +++++++ SpCloudMain/Service/MongoDbService.cpp | 121 +++++++++++------ SpCloudMain/SpCloudMain.cpp | 22 ++- 6 files changed, 395 insertions(+), 96 deletions(-) create mode 100644 .$SpCloud.drawio.dtmp create mode 100644 SpCloudMain/Models/App.cpp diff --git a/.$SpCloud.drawio.dtmp b/.$SpCloud.drawio.dtmp new file mode 100644 index 0000000..7da353a --- /dev/null +++ b/.$SpCloud.drawio.dtmp @@ -0,0 +1,181 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/SpCloud.drawio b/SpCloud.drawio index 4d161dc..a64815b 100644 --- a/SpCloud.drawio +++ b/SpCloud.drawio @@ -1,6 +1,6 @@ - + - + @@ -16,7 +16,7 @@ - + @@ -25,145 +25,154 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - + - - + + - - + + - - + + - + + + + + + + + + + diff --git a/SpCloudMain/CMakeLists.txt b/SpCloudMain/CMakeLists.txt index f656800..ffe4bb5 100644 --- a/SpCloudMain/CMakeLists.txt +++ b/SpCloudMain/CMakeLists.txt @@ -13,7 +13,7 @@ add_executable(SpCloudMain "Service/DiscordService.cpp" "Service/MongoDbService.cpp" "Service/Logger.cpp" - "Models/User.cpp") + "Models/User.cpp" "Models/App.cpp") if (CMAKE_VERSION VERSION_GREATER 3.12) set_property(TARGET SpCloudMain PROPERTY CXX_STANDARD 20) diff --git a/SpCloudMain/Models/App.cpp b/SpCloudMain/Models/App.cpp new file mode 100644 index 0000000..874271b --- /dev/null +++ b/SpCloudMain/Models/App.cpp @@ -0,0 +1,50 @@ +#include + +class App { +private: + int id; + std::string name; + std::string authToken; + bool isBanned; + +public: + // Конструктор + App(int id, const std::string& name, const std::string& authToken, bool isBanned) + : id(id), name(name), authToken(authToken), isBanned(isBanned) {} + + // Геттеры и сеттеры для Id + int getId() const { + return id; + } + + void setId(int id) { + this->id = id; + } + + // Геттеры и сеттеры для Name + std::string getName() const { + return name; + } + + void setName(const std::string& name) { + this->name = name; + } + + // Геттеры и сеттеры для AuthToken + std::string getAuthToken() const { + return authToken; + } + + void setAuthToken(const std::string& authToken) { + this->authToken = authToken; + } + + // Геттеры и сеттеры для IsBanned + bool getIsBanned() const { + return isBanned; + } + + void setIsBanned(bool isBanned) { + this->isBanned = isBanned; + } +}; diff --git a/SpCloudMain/Service/MongoDbService.cpp b/SpCloudMain/Service/MongoDbService.cpp index 2e76b9d..b107176 100644 --- a/SpCloudMain/Service/MongoDbService.cpp +++ b/SpCloudMain/Service/MongoDbService.cpp @@ -12,48 +12,18 @@ class MongoDbService public: MongoDbService() { - //mongocxx::instance inst{}; - //const auto uri = mongocxx::uri{ "mongodb+srv://loker:@spcloudcluster.xwpnw.mongodb.net/?retryWrites=true&w=majority&appName=SpCloudCluster" }; - //// Set the version of the Stable API on the client - //mongocxx::options::client client_options; - //const auto api = mongocxx::options::server_api{ mongocxx::options::server_api::version::k_version_1 }; - //client_options.server_api_opts(api); - //// Setup the connection and get a handle on the "admin" database. - //mongocxx::client conn{ uri, client_options }; - //mongocxx::database db = conn["admin"]; - //// Ping the database. - //const auto ping_cmd = bsoncxx::builder::basic::make_document(bsoncxx::builder::basic::kvp("ping", 1)); - //db.run_command(ping_cmd.view()); - //std::cout << "Pinged your deployment. You successfully connected to MongoDB!" << std::endl; } std::string get_user_info(std::string user_id) { - /*std::string json_data = R"({ - "dataSource": "SpCloudCluster", - "database": "SpCloud", - "collection": "User", - "document": { - "name": "Item Name", - "value": "Item Value" - } - })"; - std::string command = "curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/insertOne' " - "--header 'Content-Type: application/json' " - "--header 'api-key: Q1NfSCrruUAzsxdrjhZd3sjSwiqbdSFmCLeaCatZiuohUXsvEq9RtEAeG0JL2Jd7' " - "--data-raw '" + json_data + "'"; - - auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command); -*/ - std::string json_data = R"({ - "dataSource": "SpCloudCluster", - "database": "SpCloud", - "collection": "AllowedUsers", - "filter": { - "discord_id": ")" + user_id + R"(" - } -})"; + "dataSource": "SpCloudCluster", + "database": "SpCloud", + "collection": "AllowedUsers", + "filter": { + "discord_id": ")" + user_id + R"(" + } + })"; std::string command = "curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/findOne' " "--header 'Content-Type: application/json' " @@ -65,7 +35,82 @@ public: std::string response = request.get(); return response; - //return response.find("\"document\": null") == std::string::npos; + } + + std::string is_user_can_publish(std::string auth_token) + { + std::string json_data = R"({ + "dataSource": "SpCloudCluster", + "database": "SpCloud", + "collection": "AllowedUsers", + "filter": { + "auth_token": ")" + auth_token + R"(" + } + })"; + + std::string command = "curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/findOne' " + "--header 'Content-Type: application/json' " + "--header 'api-key: Q1NfSCrruUAzsxdrjhZd3sjSwiqbdSFmCLeaCatZiuohUXsvEq9RtEAeG0JL2Jd7' " + "--data-raw '" + json_data + "'"; + + auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command); + + std::string response = request.get(); + + size_t pos_is_user_banned = response.find("\"is_banned\":"); + + std::string is_banned_value = response.substr(pos_is_user_banned + 12, response.find_first_of(",}", pos_is_user_banned) - pos_is_user_banned - 12); + + if (is_banned_value == "true") + { + return "Fail publish: user is banned"; + } + + size_t pos_apps_limit_count = response.find("\"apps_limit_count\":"); + + std::string apps_limit_count = response.substr(pos_apps_limit_count + 19, response.find_first_of(",}", pos_apps_limit_count) - pos_apps_limit_count - 19); + + int apps_limit_count_int = std::stoi(apps_limit_count); + + size_t pos_app_count = response.find("\"app_count\":"); + + std::string app_count = response.substr(pos_app_count + 12, response.find_first_of(",}", pos_app_count) - pos_app_count - 12); + + int app_count_int = std::stoi(app_count); + + if (app_count_int >= apps_limit_count_int) + { + return "Fail publish: user reached publish limit user have " + app_count + " apps with user limit " + apps_limit_count; + } + + return "Success"; + } + + std::string add_app(std::string name, std::string user_id, std::string url, std::string url_on_local_mahcine, std::string target) + { + std::string json_data = R"({ + "dataSource": "SpCloudCluster", + "database": "SpCloud", + "collection": "Apps", + "document": { + "name": ")" + name + R"(", + "user_id": ")" + user_id + R"(", + "url": ")" + url + R"(", + "url_on_local_machine": ")" + url_on_local_mahcine + R"(", + "target": ")" + target + R"(" + } + })"; + + std::string command = "curl --location 'https://eu-central-1.aws.data.mongodb-api.com/app/data-zvcqvrr/endpoint/data/v1/action/insertOne' " + "--header 'Content-Type: application/json' " + "--header 'api-key: Q1NfSCrruUAzsxdrjhZd3sjSwiqbdSFmCLeaCatZiuohUXsvEq9RtEAeG0JL2Jd7' " + "--data-raw '" + json_data + "'"; + + auto request = std::async(std::launch::async, &MongoDbService::execute_command, this, command); + + std::string response = request.get(); + + return response; } std::string execute_command(const std::string& command) { diff --git a/SpCloudMain/SpCloudMain.cpp b/SpCloudMain/SpCloudMain.cpp index 4dcb45a..595d2ea 100644 --- a/SpCloudMain/SpCloudMain.cpp +++ b/SpCloudMain/SpCloudMain.cpp @@ -36,25 +36,39 @@ int main() httplib::Headers test = req.headers; }); + std::cout << "Server is running at http://localhost:8081" << '\n'; + AuthorizationService authorization_service; auto file_processing = std::make_shared(logger); PublishController publish_controller(svr, authorization_service, file_processing, logger); - std::cout << "Server is running at http://localhost:8081" << '\n'; + DiscordService discord_service; + + MongoDbService mongo_service; svr.Post("/publish", [&](const httplib::Request& req, httplib::Response& res) { logger.log(INFO, "Start publish from main"); - publish_controller.process_publish(req, res); + string is_user_can_publish_response = mongo_service.is_user_can_publish("khBuvDWPHOhPSiQNVQZm9PM0VF29dqAaDBjWX4BnxJKzRvg0Gm");//TODO UNCOMMENT AND FIX + + if (is_user_can_publish_response != "Success") + { + res.set_content(is_user_can_publish_response, "text/plain");//Todo add app address showing + + return; + } + + //publish_controller.process_publish(req, res);//TODO UNCOMMENT AND FIX + + //mongo_service.add_app("test", "test", "test", "test", "test");//TODO UNCOMMENT AND FIX + res.set_content("App is running on address ????", "text/plain");//Todo add app address showing }); - DiscordService discord_service; - MongoDbService mongo_service; svr.Post("/login", [&](const httplib::Request& req, httplib::Response& res) {