#include "user.h" void register_user(struct mg_connection *c, struct mg_http_message *hm) { if (hm->body.len == 0) { mg_http_reply(c, 400, "", "{%m:%m}", MG_ESC("error"), MG_ESC("must have , , , , ")); return; } cJSON *jsonbody = cJSON_Parse(hm->body.buf); cJSON *username = cJSON_GetObjectItem(jsonbody, "username"); cJSON *password = cJSON_GetObjectItem(jsonbody, "password"); cJSON *email = cJSON_GetObjectItem(jsonbody, "email"); cJSON *group = cJSON_GetObjectItem(jsonbody, "group"); cJSON *is_superuser = cJSON_GetObjectItem(jsonbody, "is_superuser"); if (username == nullptr || password == nullptr || email == nullptr) { mg_http_reply(c, 400, "", "{%m:%m}", MG_ESC("error"), MG_ESC("must have , , , , ")); return; } if (group == nullptr) { group = cJSON_CreateString("users"); } if (is_superuser == nullptr) { is_superuser = cJSON_CreateBool(false); } const User *new_user_data = new_user(0, username->valuestring, password->valuestring, email->valuestring, group->valuestring, is_superuser->valueint); if (new_user_data == nullptr) { mg_http_reply(c, 500, "", "{%m:%m}", MG_ESC("error"), MG_ESC("failed to create user")); return; } char responsedata[2048] = {0}; snprintf(responsedata, sizeof(responsedata), "{\"id\": %d, \"username\": \"%s\", \"email\": \"%s\", \"group\": " "\"%s\", \"is_superuser\": %d}", new_user_data->id, new_user_data->username, new_user_data->email, new_user_data->group, new_user_data->is_superuser); mg_http_reply(c, 200, "Content-Type: application/json\r\n", responsedata); }