Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 11s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Successful in 9s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Successful in 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Successful in 12s
54 lines
1.9 KiB
C
54 lines
1.9 KiB
C
#include "security/jwt.h"
|
|
#include "user.h"
|
|
#include "utils/utils.h"
|
|
|
|
void delete_user(struct mg_connection *c, struct mg_http_message *hm) {
|
|
if (hm->body.len == 0) {
|
|
mg_http_reply(c, 400, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
|
MG_ESC("must have token"));
|
|
return;
|
|
}
|
|
request_with_token *requestdata = check_body(hm);
|
|
if (requestdata->error != NULL) {
|
|
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
|
MG_ESC(requestdata->error));
|
|
return;
|
|
}
|
|
const bool valid = jwt_verifyJWT(requestdata->token, JWT_DEFAULT_SECRET);
|
|
if (!valid) {
|
|
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
|
MG_ESC("invalid token"));
|
|
free_request_with_token(requestdata);
|
|
return;
|
|
}
|
|
cJSON *payload = jwt_get_payload(requestdata->token);
|
|
int id = cJSON_GetObjectItem(payload, "id")->valueint;
|
|
cJSON *useridfromjson = cJSON_GetObjectItem(requestdata->body, "userid");
|
|
cJSON_Print(useridfromjson);
|
|
if (useridfromjson == NULL) {
|
|
bool user_deleted = db_delete_user(id);
|
|
char responsedata[2048] = {0};
|
|
snprintf(responsedata, sizeof(responsedata), "{\"success\": %s}",
|
|
user_deleted ? "true" : "false");
|
|
|
|
mg_http_reply(c, 200, HTTP_HEADERS, responsedata);
|
|
free_request_with_token(requestdata);
|
|
cJSON_Delete(payload);
|
|
return;
|
|
}
|
|
bool is_superuser = cJSON_GetObjectItem(payload, "is_superuser")->valueint;
|
|
if (!is_superuser) {
|
|
mg_http_reply(c, 403, HTTP_HEADERS, "{%m:%m}", MG_ESC("error"),
|
|
MG_ESC("user is not superuser"));
|
|
return;
|
|
}
|
|
bool user_deleted = db_delete_user(useridfromjson->valueint);
|
|
char responsedata[2048] = {0};
|
|
snprintf(responsedata, sizeof(responsedata), "{\"success\": %s}",
|
|
user_deleted ? "true" : "false");
|
|
|
|
mg_http_reply(c, 200, HTTP_HEADERS, responsedata);
|
|
free_request_with_token(requestdata);
|
|
cJSON_Delete(payload);
|
|
}
|