refactor and review
This commit is contained in:
@@ -1,41 +1,62 @@
|
||||
#include "users.hpp"
|
||||
#include "../colors.h"
|
||||
#include "../includes.hpp"
|
||||
#include "../auth/auth.hpp"
|
||||
#include <jwt-cpp/jwt.h>
|
||||
|
||||
// functtion for change password for user
|
||||
void change_password(const httplib::Request& request, httplib::Response& response) {
|
||||
void change_password(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 == "") {response.set_content(
|
||||
"{\"required\":\"[token,old_password,new_password] or if you superuser [token,userid,new_password]\"}",
|
||||
"application/json"
|
||||
);return;}
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
|
||||
if (json_body["token"] == nullptr) {response.set_content(
|
||||
"{\"required\":\"[token,old_password,new_password] or if you superuser [token,userid,new_password]\"}",
|
||||
"application/json"
|
||||
);return;}
|
||||
if ((json_body["userid"] == nullptr) && (json_body["old_password"] == nullptr)) {
|
||||
response.set_content("{\"required\":\"[token,old_password,new_password] or if you superuser [token,userid,new_password]\"}", "application/json");
|
||||
if (request.body.empty()) {
|
||||
response.set_content(
|
||||
CHANGE_PASSWORD_REQUIRED_STRING,
|
||||
JSON_TYPE
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (json_body["new_password"] == nullptr) {response.set_content("{\"required\":\"[token] or if you superuser [token,userid]\"}", "application/json");return;}
|
||||
if (!verify_auth(json_body["token"])) {response.set_content("{\"status\":\"403\"}", "application/json");return;}
|
||||
nlohmann::json json_body = nlohmann::json::parse(request.body);
|
||||
|
||||
std::string old_password = "";
|
||||
if (json_body["token"] == nullptr) {
|
||||
response.set_content(
|
||||
CHANGE_PASSWORD_REQUIRED_STRING,
|
||||
JSON_TYPE
|
||||
);
|
||||
return;
|
||||
}
|
||||
if ((json_body["userid"] == nullptr) && (json_body["old_password"] == nullptr)) {
|
||||
response.set_content(
|
||||
CHANGE_PASSWORD_REQUIRED_STRING,
|
||||
JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
if (json_body["new_password"] == nullptr) {
|
||||
response.set_content(R"({"required":"[token] or if you superuser [token,userid]"})",
|
||||
JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
if (!verify_auth(json_body["token"])) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
|
||||
std::string old_password;
|
||||
const std::string new_password = json_body["new_password"];
|
||||
|
||||
if (json_body["userid"] == nullptr) {old_password = json_body["old_password"];}
|
||||
if (json_body["userid"] == nullptr) { old_password = json_body["old_password"]; }
|
||||
|
||||
jwt::decoded_jwt<jwt::traits::kazuho_picojson> decoded_token = jwt::decode(json_body["token"]);
|
||||
std::string userid = "";
|
||||
for (auto& e : decoded_token.get_payload_json()) {
|
||||
std::string userid;
|
||||
for (std::pair<const std::string, picojson::value> &e: decoded_token.get_payload_json()) {
|
||||
if (e.first == "userId") {
|
||||
userid = e.second.to_str();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (userid == "") {response.set_content("{\"status\":\"403\"}", "application/json");return;}
|
||||
if (userid.empty()) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
|
||||
std::ifstream usersfile("users.json");
|
||||
nlohmann::json all_users = nlohmann::json::parse(usersfile);
|
||||
@@ -45,36 +66,45 @@ void change_password(const httplib::Request& request, httplib::Response& respons
|
||||
if (json_body["userid"] != nullptr) {
|
||||
changepassuserid = json_body["userid"];
|
||||
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;
|
||||
}
|
||||
}
|
||||
if (response_user_data == nullptr) {response.set_content("{\"status\":\"403\"}", "application/json");return;}
|
||||
if (response_user_data["is_superuser"] != "1") {response.set_content("{\"status\":\"403\"}", "application/json");return;}
|
||||
if (response_user_data == nullptr) {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
if (response_user_data["is_superuser"] != "1") {
|
||||
response.set_content(STRING403, JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
bool found = false;
|
||||
if (json_body["userid"] != nullptr) {
|
||||
for (int i = 0; i < all_users.size(); i++) {
|
||||
if ((all_users[i]["id"] == changepassuserid)) {
|
||||
all_users[i]["password"] = new_password;
|
||||
for (nlohmann::basic_json<> &all_user: all_users) {
|
||||
if ((all_user["id"] == changepassuserid)) {
|
||||
all_user["password"] = new_password;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (int i = 0; i < all_users.size(); i++) {
|
||||
if ((all_users[i]["id"] == changepassuserid) && (all_users[i]["password"] == old_password)) {
|
||||
all_users[i]["password"] = new_password;
|
||||
for (nlohmann::basic_json<> &all_user: all_users) {
|
||||
if ((all_user["id"] == changepassuserid) && (all_user["password"] == old_password)) {
|
||||
all_user["password"] = new_password;
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!found) {response.set_content("{\"status\":\"can't find this user\"}", "application/json");return;}
|
||||
if (!found) {
|
||||
response.set_content(R"({"status":"can't find this user"})", JSON_TYPE);
|
||||
return;
|
||||
}
|
||||
std::ofstream usersfilew("users.json");
|
||||
usersfilew << all_users.dump(4);
|
||||
usersfilew.close();
|
||||
response.set_content("{\"status\":\"ok\"}", "application/json");
|
||||
response.set_content(R"({"status":"ok"})", JSON_TYPE);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user