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
79 lines
3.3 KiB
C++
79 lines
3.3 KiB
C++
#include <users/users.hpp>
|
|
#include <colors.h>
|
|
#include <includes.hpp>
|
|
#include <auth/auth.hpp>
|
|
#include <jwt.hpp>
|
|
#include <syslog.h>
|
|
|
|
namespace AuthService
|
|
{
|
|
namespace Users
|
|
{
|
|
// function for delete user
|
|
void delete_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", "*");
|
|
AuthService::Includes::check_body_empty(request, response, DELETE_USER_REQUIRED_STRING);
|
|
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
|
AuthService::Includes::check_body_data(json_body, "token", response, DELETE_USER_REQUIRED_STRING);
|
|
AuthService::Auth::verify_auth(json_body["token"], response);
|
|
|
|
std::string userid = JWT::get_payload(json_body["token"], JWT_SECRET_KEY)["userId"];
|
|
if (userid.empty()) {
|
|
response.set_content(STRING403, JSON_TYPE);
|
|
syslog(LOG_ERR, "access reject in delete user request");
|
|
return;
|
|
}
|
|
nlohmann::json all_users;
|
|
{
|
|
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
|
|
std::ifstream usersfile(USERS_FILE);
|
|
all_users = nlohmann::json::parse(usersfile);
|
|
usersfile.close();
|
|
}
|
|
std::string deluserid = userid;
|
|
if (json_body["userid"] != nullptr) {
|
|
deluserid = json_body["userid"];
|
|
nlohmann::json response_user_data = nullptr;
|
|
for (nlohmann::json& user : all_users) {
|
|
if (user["id"] == userid) {
|
|
response_user_data = user;
|
|
}
|
|
}
|
|
if (response_user_data == nullptr) {
|
|
response.set_content(STRING403, JSON_TYPE);
|
|
syslog(LOG_ERR, "access reject in delete user request");
|
|
return;
|
|
}
|
|
if (response_user_data["is_superuser"] != "1") {
|
|
response.set_content(STRING403, JSON_TYPE);
|
|
syslog(LOG_ERR, "access reject in delete user request");
|
|
return;
|
|
}
|
|
}
|
|
|
|
bool found = false;
|
|
for (size_t i = 0; i < all_users.size(); i++) {
|
|
if (all_users[i]["id"] == deluserid) {
|
|
std::string syslogstring = "user deleted [" + std::string(all_users[i]["username"]) + "]";
|
|
all_users.erase(i);
|
|
found = true;
|
|
syslog(LOG_INFO, syslogstring.c_str());
|
|
break;
|
|
}
|
|
}
|
|
if (!found) {
|
|
response.set_content(R"({"status":"can't find this user"})", JSON_TYPE);
|
|
syslog(LOG_ERR, "can't find this user in delete user request");
|
|
return;
|
|
}
|
|
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
|
|
std::ofstream usersfilew(USERS_FILE);
|
|
usersfilew << all_users.dump(4);
|
|
usersfilew.close();
|
|
response.set_content(R"({"status":"ok"})", JSON_TYPE);
|
|
}
|
|
}
|
|
}
|