updated and added namespaces
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
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:
1 parent
886f0aed59
commit
a06da5a6d4
18 files changed
+698
-641
No files matched your search
+52
-46
@@ -5,54 +5,60 @@
|
||||
#include <jwt.hpp>
|
||||
#include <syslog.h>
|
||||
|
||||
// function for get all users data without password
|
||||
void get_all_users(const httplib::Request& request, httplib::Response& response)
|
||||
namespace AuthService
|
||||
{
|
||||
std::cout << GREEN << request.path << RESET << " " << request.method << std::endl;
|
||||
response.set_header("Access-Control-Allow-Origin", "*");
|
||||
if (request.body.empty()) {
|
||||
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
syslog(LOG_ERR, "get all users request body is empty");
|
||||
return;
|
||||
}
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
namespace Users
|
||||
{
|
||||
// function for get all users data without password
|
||||
void get_all_users(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(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
syslog(LOG_ERR, "get all users request body is empty");
|
||||
return;
|
||||
}
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
|
||||
if (json_body["token"] == nullptr) {
|
||||
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
syslog(LOG_ERR, "no token in get all users");
|
||||
return;
|
||||
}
|
||||
if (!verify_auth(json_body["token"])) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get all user request");
|
||||
return;
|
||||
}
|
||||
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 get all user request");
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
nlohmann::json all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
nlohmann::json response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
if (json_body["token"] == nullptr) {
|
||||
response.set_content(ACCESS_REQUIRED_STRING, JSON_TYPE);
|
||||
syslog(LOG_ERR, "no token in get all users");
|
||||
return;
|
||||
}
|
||||
if (!AuthService::Auth::verify_auth(json_body["token"])) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get all user request");
|
||||
return;
|
||||
}
|
||||
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 get all user request");
|
||||
return;
|
||||
}
|
||||
std::lock_guard<std::mutex> lock(AuthService::Includes::usersfilemutex);
|
||||
std::ifstream usersfile(USERS_FILE);
|
||||
nlohmann::json all_users = nlohmann::json::parse(usersfile);
|
||||
usersfile.close();
|
||||
nlohmann::json response_user_data = nullptr;
|
||||
for (nlohmann::json& user : all_users) {
|
||||
if (user["id"] == userid) {
|
||||
response_user_data = user;
|
||||
}
|
||||
user.erase("password");
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get all user request");
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get all user request");
|
||||
return;
|
||||
}
|
||||
response.set_content(all_users.dump(), JSON_TYPE);
|
||||
}
|
||||
user.erase("password");
|
||||
}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get all user request");
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
syslog(LOG_ERR, "access reject in get all user request");
|
||||
return;
|
||||
}
|
||||
response.set_content(all_users.dump(), JSON_TYPE);
|
||||
}
|
||||
Reference in new issue
Block a user