added editorconfig and formated code
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Has been cancelled
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Has been cancelled

This commit is contained in:
wt committed 2025-02-13 19:35:42 +07:00
1 parent 77a483a38a
commit e5abe4bb02
23 files changed
+22332 -21478

No files matched your search

+12 -10
View File
@@ -8,7 +8,8 @@
#define ALPHABET "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// function for generate random string
std::string get_random_string(const char *alphabet, int quantity) {
std::string get_random_string(const char* alphabet, int quantity)
{
srand(time(nullptr));
std::string str;
for (int i = 0; i < quantity; i++) {
@@ -18,12 +19,13 @@ std::string get_random_string(const char *alphabet, int quantity) {
}
// function for add user
void add_user(const httplib::Request &request, httplib::Response &response) {
void add_user(const httplib::Request& request, httplib::Response& response)
{
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
response.set_header("Access-Control-Allow-Origin", "*");
if (request.body.empty()) {
response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE);
JSON_TYPE);
syslog(LOG_ERR, "add user request body is empty");
return;
}
@@ -31,7 +33,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
if (json_body["token"] == nullptr) {
response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE);
JSON_TYPE);
syslog(LOG_ERR, "no token in add user request");
return;
}
@@ -42,13 +44,13 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
}
if (json_body["username"] == nullptr) {
response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE);
JSON_TYPE);
syslog(LOG_ERR, "no username in add user request");
return;
}
if (json_body["password"] == nullptr) {
response.set_content(ADD_USER_REQUIRED_STRING,
JSON_TYPE);
JSON_TYPE);
syslog(LOG_ERR, "no password in add user request");
return;
}
@@ -65,7 +67,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
usersfile.close();
nlohmann::json response_user_data = nullptr;
for (nlohmann::json &user: all_users) {
for (nlohmann::json& user : all_users) {
if (user["id"] == userid) {
response_user_data = user;
}
@@ -84,7 +86,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
const std::string username = json_body["username"];
const std::string password = json_body["password"];
for (nlohmann::json user: all_users) {
for (nlohmann::json user : all_users) {
if ((user["username"] == username)) {
response.set_content(R"({"status":"user already exists"})", JSON_TYPE);
syslog(LOG_ERR, "user already exists in add user request");
@@ -95,7 +97,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
std::stringstream new_user_data;
new_user_data << R"({"id":")" << get_random_string(ALPHABET, 50) << R"(","username":")"
<< username << R"(","password":")" << password << R"(","group":)";
<< username << R"(","password":")" << password << R"(","group":)";
(json_body["group"] != nullptr) ? new_user_data << json_body["group"] : new_user_data << "\"users\"";
new_user_data << ",\"is_superuser\":";
(json_body["is_superuser"] != nullptr) ? new_user_data << json_body["is_superuser"] : new_user_data << "\"0\"";
@@ -106,7 +108,7 @@ void add_user(const httplib::Request &request, httplib::Response &response) {
usersfilew << all_users.dump(4);
usersfilew.close();
std::stringstream syslogstring;
syslogstring << "user added [" << username << "]";
syslogstring << "user added [" << username << "]";
syslog(LOG_INFO, syslogstring.str().c_str());
response.set_content(R"({"status":"ok"})", JSON_TYPE);
}