updated database and database_type in config file
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Failing after 2s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Failing after 2s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Failing after 2s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Failing after 1s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Failing after 2s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Failing after 2s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Failing after 2s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Failing after 2s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Failing after 2s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Failing after 1s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Failing after 2s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Failing after 2s
This commit is contained in:
1 parent
4c930e3b82
commit
648e92d2fa
18 files changed
+185
-71
No files matched your search
+10
-9
@@ -1,10 +1,11 @@
|
|||||||
{
|
{
|
||||||
"database": "users.db",
|
"database": "asdf.json",
|
||||||
"address": "0.0.0.0:8222",
|
"database_type": "file",
|
||||||
"tls_address": "0.0.0.0:8223",
|
"address": "0.0.0.0:8222",
|
||||||
"default_users_file": "users.json",
|
"tls_address": "0.0.0.0:8223",
|
||||||
"web": "./web",
|
"default_users_file": "users.json",
|
||||||
"tls_enabled": true,
|
"web": "./web",
|
||||||
"tls_cert": "/home/tola/certs/localhost/localhost.crt",
|
"tls_enabled": true,
|
||||||
"tls_key": "/home/tola/certs/localhost/localhost.key"
|
"tls_cert": "/home/tola/certs/localhost/localhost.crt",
|
||||||
}
|
"tls_key": "/home/tola/certs/localhost/localhost.key"
|
||||||
|
}
|
||||||
+22
-7
@@ -3,6 +3,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include "config/ConfigManager.h"
|
||||||
|
#include "config/config.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sqlite_db/sqlite.h"
|
#include "sqlite_db/sqlite.h"
|
||||||
@@ -10,17 +12,30 @@
|
|||||||
|
|
||||||
void init_db() {
|
void init_db() {
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
sqlite_init_db();
|
char *dbtype = cm_get_parameter_as_string(config, "database_type");
|
||||||
#else
|
if (!strcmp(dbtype, "sqlite")) {
|
||||||
return;
|
sqlite_init_db();
|
||||||
|
free(dbtype);
|
||||||
|
} else if (!strcmp(dbtype, "file")) {
|
||||||
|
free(dbtype);
|
||||||
|
#endif
|
||||||
|
return;
|
||||||
|
#ifdef USE_SQLITE3
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void migrate_db() {
|
void migrate_db() {
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
sqlite_migrate_db();
|
char *dbtype = cm_get_parameter_as_string(config, "database_type");
|
||||||
#else
|
if (!strcmp(dbtype, "sqlite")) {
|
||||||
return;
|
sqlite_migrate_db();
|
||||||
|
free(dbtype);
|
||||||
|
} else if (!strcmp(dbtype, "file")) {
|
||||||
|
free(dbtype);
|
||||||
|
#endif
|
||||||
|
return;
|
||||||
|
#ifdef USE_SQLITE3
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
#include "api/user.h"
|
#include "api/user.h"
|
||||||
|
#include "config/config.h"
|
||||||
|
#include "file_db/filedb.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sqlite_db/sqlite.h"
|
#include "sqlite_db/sqlite.h"
|
||||||
#else
|
|
||||||
#include "file_db/filedb.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool db_delete_user(const int id) {
|
bool db_delete_user(const int id) {
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
return sqlite_delete_user(id);
|
char *dbtype = cm_get_parameter_as_string(config, "database_type");
|
||||||
#else
|
if (!strcmp(dbtype, "sqlite")) {
|
||||||
|
free(dbtype);
|
||||||
|
return sqlite_delete_user(id);
|
||||||
|
} else if (!strcmp(dbtype, "file")) {
|
||||||
|
free(dbtype);
|
||||||
|
#endif
|
||||||
|
return filedb_delete_user(id);
|
||||||
|
#ifdef USE_SQLITE3
|
||||||
|
}
|
||||||
return filedb_delete_user(id);
|
return filedb_delete_user(id);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,23 @@
|
|||||||
#include "api/user.h"
|
#include "api/user.h"
|
||||||
|
#include "config/config.h"
|
||||||
|
#include "file_db/filedb.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sqlite_db/sqlite.h"
|
#include "sqlite_db/sqlite.h"
|
||||||
#else
|
|
||||||
#include "file_db/filedb.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char *db_get_all_users() {
|
char *db_get_all_users() {
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
return sqlite_get_all_users();
|
char *dbtype = cm_get_parameter_as_string(config, "database_type");
|
||||||
#else
|
if (!strcmp(dbtype, "sqlite")) {
|
||||||
|
free(dbtype);
|
||||||
|
return sqlite_get_all_users();
|
||||||
|
} else if (!strcmp(dbtype, "file")) {
|
||||||
|
free(dbtype);
|
||||||
|
#endif
|
||||||
|
return filedb_get_all_users();
|
||||||
|
#ifdef USE_SQLITE3
|
||||||
|
}
|
||||||
return filedb_get_all_users();
|
return filedb_get_all_users();
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,23 @@
|
|||||||
#include "api/user.h"
|
#include "api/user.h"
|
||||||
|
#include "config/config.h"
|
||||||
|
#include "file_db/filedb.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sqlite_db/sqlite.h"
|
#include "sqlite_db/sqlite.h"
|
||||||
#else
|
|
||||||
#include "file_db/filedb.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
User *db_get_user_by_id(const int id) {
|
User *db_get_user_by_id(const int id) {
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
return sqlite_get_user_by_id(id);
|
char *dbtype = cm_get_parameter_as_string(config, "database_type");
|
||||||
#else
|
if (!strcmp(dbtype, "sqlite")) {
|
||||||
|
free(dbtype);
|
||||||
|
return sqlite_get_user_by_id(id);
|
||||||
|
} else if (!strcmp(dbtype, "file")) {
|
||||||
|
free(dbtype);
|
||||||
|
#endif
|
||||||
|
return filedb_get_user_by_id(id);
|
||||||
|
#ifdef USE_SQLITE3
|
||||||
|
}
|
||||||
return filedb_get_user_by_id(id);
|
return filedb_get_user_by_id(id);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,23 @@
|
|||||||
#include "api/user.h"
|
#include "api/user.h"
|
||||||
|
#include "config/config.h"
|
||||||
|
#include "file_db/filedb.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sqlite_db/sqlite.h"
|
#include "sqlite_db/sqlite.h"
|
||||||
#else
|
|
||||||
#include "file_db/filedb.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
User *db_get_user_by_username(const char *username) {
|
User *db_get_user_by_username(const char *username) {
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
return sqlite_get_user_by_username(username);
|
char *dbtype = cm_get_parameter_as_string(config, "database_type");
|
||||||
#else
|
if (!strcmp(dbtype, "sqlite")) {
|
||||||
|
free(dbtype);
|
||||||
|
return sqlite_get_user_by_username(username);
|
||||||
|
} else if (!strcmp(dbtype, "file")) {
|
||||||
|
free(dbtype);
|
||||||
|
#endif
|
||||||
|
return filedb_get_user_by_username(username);
|
||||||
|
#ifdef USE_SQLITE3
|
||||||
|
}
|
||||||
return filedb_get_user_by_username(username);
|
return filedb_get_user_by_username(username);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -1,15 +1,23 @@
|
|||||||
#include "api/user.h"
|
#include "api/user.h"
|
||||||
|
#include "config/config.h"
|
||||||
|
#include "file_db/filedb.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sqlite_db/sqlite.h"
|
#include "sqlite_db/sqlite.h"
|
||||||
#else
|
|
||||||
#include "file_db/filedb.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
char *db_get_user_field_by_id(const int id, const char *field) {
|
char *db_get_user_field_by_id(const int id, const char *field) {
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
return sqlite_get_user_field_by_id(id, field);
|
char *dbtype = cm_get_parameter_as_string(config, "database_type");
|
||||||
#else
|
if (!strcmp(dbtype, "sqlite")) {
|
||||||
|
free(dbtype);
|
||||||
|
return sqlite_get_user_field_by_id(id, field);
|
||||||
|
} else if (!strcmp(dbtype, "file")) {
|
||||||
|
free(dbtype);
|
||||||
|
#endif
|
||||||
|
return filedb_get_user_field_by_id(id, field);
|
||||||
|
#ifdef USE_SQLITE3
|
||||||
|
}
|
||||||
return filedb_get_user_field_by_id(id, field);
|
return filedb_get_user_field_by_id(id, field);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -1,17 +1,25 @@
|
|||||||
#include "api/user.h"
|
#include "api/user.h"
|
||||||
|
#include "config/config.h"
|
||||||
|
#include "file_db/filedb.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sqlite_db/sqlite.h"
|
#include "sqlite_db/sqlite.h"
|
||||||
#else
|
|
||||||
#include "file_db/filedb.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
User *db_insert_user(const char *username, const char *email,
|
User *db_insert_user(const char *username, const char *email,
|
||||||
const char *password, const char *group,
|
const char *password, const char *group,
|
||||||
const bool *is_superuser) {
|
const bool *is_superuser) {
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
return sqlite_insert_user(username, email, password, group, is_superuser);
|
char *dbtype = cm_get_parameter_as_string(config, "database_type");
|
||||||
#else
|
if (!strcmp(dbtype, "sqlite")) {
|
||||||
|
free(dbtype);
|
||||||
|
return sqlite_insert_user(username, email, password, group, is_superuser);
|
||||||
|
} else if (!strcmp(dbtype, "file")) {
|
||||||
|
free(dbtype);
|
||||||
|
#endif
|
||||||
|
return filedb_insert_user(username, email, password, group, is_superuser);
|
||||||
|
#ifdef USE_SQLITE3
|
||||||
|
}
|
||||||
return filedb_insert_user(username, email, password, group, is_superuser);
|
return filedb_insert_user(username, email, password, group, is_superuser);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -1,18 +1,27 @@
|
|||||||
#include "api/user.h"
|
#include "api/user.h"
|
||||||
|
#include "config/config.h"
|
||||||
|
#include "file_db/filedb.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sqlite_db/sqlite.h"
|
#include "sqlite_db/sqlite.h"
|
||||||
#else
|
|
||||||
#include "file_db/filedb.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
User *db_update_user(const int userid, const char *username, const char *email,
|
User *db_update_user(const int userid, const char *username, const char *email,
|
||||||
const char *password, const char *group,
|
const char *password, const char *group,
|
||||||
const bool *is_superuser) {
|
const bool *is_superuser) {
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
return sqlite_update_user(userid, username, email, password, group,
|
char *dbtype = cm_get_parameter_as_string(config, "database_type");
|
||||||
is_superuser);
|
if (!strcmp(dbtype, "sqlite")) {
|
||||||
#else
|
free(dbtype);
|
||||||
|
return sqlite_update_user(userid, username, email, password, group,
|
||||||
|
is_superuser);
|
||||||
|
} else if (!strcmp(dbtype, "file")) {
|
||||||
|
free(dbtype);
|
||||||
|
#endif
|
||||||
|
return filedb_update_user(userid, username, email, password, group,
|
||||||
|
is_superuser);
|
||||||
|
#ifdef USE_SQLITE3
|
||||||
|
}
|
||||||
return filedb_update_user(userid, username, email, password, group,
|
return filedb_update_user(userid, username, email, password, group,
|
||||||
is_superuser);
|
is_superuser);
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,15 +1,23 @@
|
|||||||
#include "api/user.h"
|
#include "api/user.h"
|
||||||
|
#include "config/config.h"
|
||||||
|
#include "file_db/filedb.h"
|
||||||
|
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
#include "sqlite_db/sqlite.h"
|
#include "sqlite_db/sqlite.h"
|
||||||
#else
|
|
||||||
#include "file_db/filedb.h"
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool db_update_user_field(const int id, const char *field, const char *value) {
|
bool db_update_user_field(const int id, const char *field, const char *value) {
|
||||||
#ifdef USE_SQLITE3
|
#ifdef USE_SQLITE3
|
||||||
return sqlite_update_user_field(id, field, value);
|
char *dbtype = cm_get_parameter_as_string(config, "database_type");
|
||||||
#else
|
if (!strcmp(dbtype, "sqlite")) {
|
||||||
|
free(dbtype);
|
||||||
|
return sqlite_update_user_field(id, field, value);
|
||||||
|
} else if (!strcmp(dbtype, "file")) {
|
||||||
|
free(dbtype);
|
||||||
|
#endif
|
||||||
|
return filedb_update_user_field(id, field, value);
|
||||||
|
#ifdef USE_SQLITE3
|
||||||
|
}
|
||||||
return filedb_update_user_field(id, field, value);
|
return filedb_update_user_field(id, field, value);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,15 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "utils/file.h"
|
|
||||||
#include "cJSON.h"
|
#include "cJSON.h"
|
||||||
|
#include "config/ConfigManager.h"
|
||||||
|
#include "config/config.h"
|
||||||
#include "filedb.h"
|
#include "filedb.h"
|
||||||
|
#include "utils/file.h"
|
||||||
|
|
||||||
bool filedb_delete_user(const int id) {
|
bool filedb_delete_user(const int id) {
|
||||||
pthread_mutex_lock(&users_file_mutex);
|
pthread_mutex_lock(&users_file_mutex);
|
||||||
char *data = read_file("users_db.json");
|
char *database_file = cm_get_parameter_as_string(config, "database");
|
||||||
|
char *data = read_file(database_file);
|
||||||
cJSON *users = cJSON_Parse(data);
|
cJSON *users = cJSON_Parse(data);
|
||||||
cJSON *user = nullptr;
|
cJSON *user = nullptr;
|
||||||
int index = 0;
|
int index = 0;
|
||||||
@@ -22,12 +25,16 @@ bool filedb_delete_user(const int id) {
|
|||||||
}
|
}
|
||||||
if (!found) {
|
if (!found) {
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
|
free(data);
|
||||||
|
free(database_file);
|
||||||
|
cJSON_Delete(users);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
char *new_data = cJSON_Print(users);
|
char *new_data = cJSON_Print(users);
|
||||||
write_file("users_db.json", new_data);
|
write_file(database_file, new_data);
|
||||||
free(new_data);
|
free(new_data);
|
||||||
free(data);
|
free(data);
|
||||||
|
free(database_file);
|
||||||
cJSON_Delete(users);
|
cJSON_Delete(users);
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include "utils/file.h"
|
#include "config/config.h"
|
||||||
#include "filedb.h"
|
#include "filedb.h"
|
||||||
|
#include "utils/file.h"
|
||||||
|
|
||||||
char *filedb_get_all_users() {
|
char *filedb_get_all_users() {
|
||||||
pthread_mutex_lock(&users_file_mutex);
|
pthread_mutex_lock(&users_file_mutex);
|
||||||
char *data = read_file("users_db.json");
|
char *database_file = cm_get_parameter_as_string(config, "database");
|
||||||
|
char *data = read_file(database_file);
|
||||||
|
free(database_file);
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
return data;
|
return data;
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include "utils/file.h"
|
#include "utils/file.h"
|
||||||
#include "filedb.h"
|
#include "filedb.h"
|
||||||
|
#include "config/config.h"
|
||||||
|
|
||||||
User *filedb_get_user_by_id(const int id) {
|
User *filedb_get_user_by_id(const int id) {
|
||||||
pthread_mutex_lock(&users_file_mutex);
|
pthread_mutex_lock(&users_file_mutex);
|
||||||
char *data = read_file("users_db.json");
|
char *database_file = cm_get_parameter_as_string(config, "database");
|
||||||
|
char *data = read_file(database_file);
|
||||||
cJSON *users = cJSON_Parse(data);
|
cJSON *users = cJSON_Parse(data);
|
||||||
cJSON *user = nullptr;
|
cJSON *user = nullptr;
|
||||||
User *found = nullptr;
|
User *found = nullptr;
|
||||||
@@ -27,11 +29,13 @@ User *filedb_get_user_by_id(const int id) {
|
|||||||
// if (user != nullptr) cJSON_Delete(user);
|
// if (user != nullptr) cJSON_Delete(user);
|
||||||
cJSON_Delete(users);
|
cJSON_Delete(users);
|
||||||
free(data);
|
free(data);
|
||||||
|
free(database_file);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
// if (user != nullptr) cJSON_Delete(user);
|
// if (user != nullptr) cJSON_Delete(user);
|
||||||
cJSON_Delete(users);
|
cJSON_Delete(users);
|
||||||
free(data);
|
free(data);
|
||||||
|
free(database_file);
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include "utils/file.h"
|
#include "utils/file.h"
|
||||||
#include "filedb.h"
|
#include "filedb.h"
|
||||||
|
#include "config/config.h"
|
||||||
|
|
||||||
User *filedb_get_user_by_username(const char *username) {
|
User *filedb_get_user_by_username(const char *username) {
|
||||||
pthread_mutex_lock(&users_file_mutex);
|
pthread_mutex_lock(&users_file_mutex);
|
||||||
char *data = read_file("users_db.json");
|
char *database_file = cm_get_parameter_as_string(config, "database");
|
||||||
|
char *data = read_file(database_file);
|
||||||
cJSON *users = cJSON_Parse(data);
|
cJSON *users = cJSON_Parse(data);
|
||||||
cJSON *user = nullptr;
|
cJSON *user = nullptr;
|
||||||
User *found = nullptr;
|
User *found = nullptr;
|
||||||
@@ -24,14 +26,14 @@ User *filedb_get_user_by_username(const char *username) {
|
|||||||
}
|
}
|
||||||
if (found == nullptr) {
|
if (found == nullptr) {
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
// if (user != nullptr) cJSON_Delete(user);
|
|
||||||
cJSON_Delete(users);
|
cJSON_Delete(users);
|
||||||
free(data);
|
free(data);
|
||||||
|
free(database_file);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
// if (user != nullptr) cJSON_Delete(user);
|
|
||||||
cJSON_Delete(users);
|
cJSON_Delete(users);
|
||||||
free(data);
|
free(data);
|
||||||
|
free(database_file);
|
||||||
return found;
|
return found;
|
||||||
}
|
}
|
||||||
@@ -1,10 +1,12 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include "utils/file.h"
|
#include "utils/file.h"
|
||||||
#include "filedb.h"
|
#include "filedb.h"
|
||||||
|
#include "config/config.h"
|
||||||
|
|
||||||
char *filedb_get_user_field_by_id(const int id, const char *field) {
|
char *filedb_get_user_field_by_id(const int id, const char *field) {
|
||||||
pthread_mutex_lock(&users_file_mutex);
|
pthread_mutex_lock(&users_file_mutex);
|
||||||
char *data = read_file("users_db.json");
|
char *database_file = cm_get_parameter_as_string(config, "database");
|
||||||
|
char *data = read_file(database_file);
|
||||||
cJSON *users = cJSON_Parse(data);
|
cJSON *users = cJSON_Parse(data);
|
||||||
cJSON *user = nullptr;
|
cJSON *user = nullptr;
|
||||||
User *found = nullptr;
|
User *found = nullptr;
|
||||||
@@ -24,14 +26,14 @@ char *filedb_get_user_field_by_id(const int id, const char *field) {
|
|||||||
}
|
}
|
||||||
if (found == nullptr) {
|
if (found == nullptr) {
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
// if (user != nullptr) cJSON_Delete(user);
|
|
||||||
cJSON_Delete(users);
|
cJSON_Delete(users);
|
||||||
free(data);
|
free(data);
|
||||||
|
free(database_file);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
// if (user != nullptr) cJSON_Delete(user);
|
|
||||||
cJSON_Delete(users);
|
cJSON_Delete(users);
|
||||||
free(data);
|
free(data);
|
||||||
|
free(database_file);
|
||||||
return found->password;
|
return found->password;
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,14 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include "utils/file.h"
|
#include "config/config.h"
|
||||||
#include "filedb.h"
|
#include "filedb.h"
|
||||||
|
#include "utils/file.h"
|
||||||
|
|
||||||
User *filedb_insert_user(const char *username, const char *email,
|
User *filedb_insert_user(const char *username, const char *email,
|
||||||
const char *password, const char *group,
|
const char *password, const char *group,
|
||||||
const bool *is_superuser) {
|
const bool *is_superuser) {
|
||||||
pthread_mutex_lock(&users_file_mutex);
|
pthread_mutex_lock(&users_file_mutex);
|
||||||
char *data = read_file("users_db.json");
|
char *database_file = cm_get_parameter_as_string(config, "database");
|
||||||
|
char *data = read_file(database_file);
|
||||||
cJSON *users = cJSON_Parse(data);
|
cJSON *users = cJSON_Parse(data);
|
||||||
cJSON *user = nullptr;
|
cJSON *user = nullptr;
|
||||||
int users_size = 0;
|
int users_size = 0;
|
||||||
@@ -26,6 +28,10 @@ User *filedb_insert_user(const char *username, const char *email,
|
|||||||
}
|
}
|
||||||
if (found == true) {
|
if (found == true) {
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
|
free(data);
|
||||||
|
cJSON_Delete(users);
|
||||||
|
if (user != nullptr) cJSON_Delete(user);
|
||||||
|
free(database_file);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
cJSON *new_user_data = cJSON_CreateObject();
|
cJSON *new_user_data = cJSON_CreateObject();
|
||||||
@@ -41,15 +47,15 @@ User *filedb_insert_user(const char *username, const char *email,
|
|||||||
}
|
}
|
||||||
cJSON_AddItemToArray(users, new_user_data);
|
cJSON_AddItemToArray(users, new_user_data);
|
||||||
char *new_data = cJSON_Print(users);
|
char *new_data = cJSON_Print(users);
|
||||||
write_file("users_db.json", new_data);
|
write_file(database_file, new_data);
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
User *return_user =
|
User *return_user =
|
||||||
new_user(users_size, username, email, password, group, is_superuser);
|
new_user(users_size, username, email, password, group, is_superuser);
|
||||||
free(new_data);
|
free(new_data);
|
||||||
free(data);
|
free(data);
|
||||||
free(new_user_data_string);
|
free(new_user_data_string);
|
||||||
// cJSON_Delete(new_user_data);
|
|
||||||
cJSON_Delete(users);
|
cJSON_Delete(users);
|
||||||
if (user != nullptr) cJSON_Delete(user);
|
if (user != nullptr) cJSON_Delete(user);
|
||||||
|
free(database_file);
|
||||||
return return_user;
|
return return_user;
|
||||||
}
|
}
|
||||||
@@ -1,12 +1,14 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include "utils/file.h"
|
#include "config/config.h"
|
||||||
#include "filedb.h"
|
#include "filedb.h"
|
||||||
|
#include "utils/file.h"
|
||||||
|
|
||||||
User *filedb_update_user(const int userid, const char *username,
|
User *filedb_update_user(const int userid, const char *username,
|
||||||
const char *email, const char *password,
|
const char *email, const char *password,
|
||||||
const char *group, const bool *is_superuser) {
|
const char *group, const bool *is_superuser) {
|
||||||
pthread_mutex_lock(&users_file_mutex);
|
pthread_mutex_lock(&users_file_mutex);
|
||||||
char *data = read_file("users_db.json");
|
char *database_file = cm_get_parameter_as_string(config, "database");
|
||||||
|
char *data = read_file(database_file);
|
||||||
cJSON *users = cJSON_Parse(data);
|
cJSON *users = cJSON_Parse(data);
|
||||||
cJSON *user = nullptr;
|
cJSON *user = nullptr;
|
||||||
cJSON_ArrayForEach(user, users) {
|
cJSON_ArrayForEach(user, users) {
|
||||||
@@ -23,10 +25,11 @@ User *filedb_update_user(const int userid, const char *username,
|
|||||||
cJSON_DeleteItemFromObject(user, "is_superuser");
|
cJSON_DeleteItemFromObject(user, "is_superuser");
|
||||||
cJSON_AddBoolToObject(user, "is_superuser", *is_superuser);
|
cJSON_AddBoolToObject(user, "is_superuser", *is_superuser);
|
||||||
char *new_data = cJSON_Print(users);
|
char *new_data = cJSON_Print(users);
|
||||||
write_file("users_db.json", new_data);
|
write_file(database_file, new_data);
|
||||||
free(new_data);
|
free(new_data);
|
||||||
free(data);
|
free(data);
|
||||||
cJSON_Delete(users);
|
cJSON_Delete(users);
|
||||||
|
free(database_file);
|
||||||
User *ret =
|
User *ret =
|
||||||
new_user(userid, username, password, email, group, is_superuser);
|
new_user(userid, username, password, email, group, is_superuser);
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
@@ -34,6 +37,7 @@ User *filedb_update_user(const int userid, const char *username,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
free(data);
|
free(data);
|
||||||
|
free(database_file);
|
||||||
cJSON_Delete(users);
|
cJSON_Delete(users);
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
return nullptr;
|
return nullptr;
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include "utils/file.h"
|
#include "config/config.h"
|
||||||
#include "filedb.h"
|
#include "filedb.h"
|
||||||
|
#include "utils/file.h"
|
||||||
|
|
||||||
bool filedb_update_user_field(const int id, const char *field,
|
bool filedb_update_user_field(const int id, const char *field,
|
||||||
const char *value) {
|
const char *value) {
|
||||||
pthread_mutex_lock(&users_file_mutex);
|
pthread_mutex_lock(&users_file_mutex);
|
||||||
char *data = read_file("users_db.json");
|
char *database_file = cm_get_parameter_as_string(config, "database");
|
||||||
|
char *data = read_file(database_file);
|
||||||
cJSON *users = cJSON_Parse(data);
|
cJSON *users = cJSON_Parse(data);
|
||||||
cJSON *user = nullptr;
|
cJSON *user = nullptr;
|
||||||
cJSON_ArrayForEach(user, users) {
|
cJSON_ArrayForEach(user, users) {
|
||||||
@@ -14,18 +16,21 @@ bool filedb_update_user_field(const int id, const char *field,
|
|||||||
cJSON *user_field = cJSON_GetObjectItem(user, field);
|
cJSON *user_field = cJSON_GetObjectItem(user, field);
|
||||||
if (cJSON_IsNumber(user_field)) {
|
if (cJSON_IsNumber(user_field)) {
|
||||||
// cJSON_SetNumberValue(user_field, value);
|
// cJSON_SetNumberValue(user_field, value);
|
||||||
|
free(database_file);
|
||||||
return false;
|
return false;
|
||||||
} else if (cJSON_IsBool(user_field)) {
|
} else if (cJSON_IsBool(user_field)) {
|
||||||
// cJSON_DeleteItemFromObject(user, field);
|
// cJSON_DeleteItemFromObject(user, field);
|
||||||
// cJSON_AddBoolToObject(user, field, value);
|
// cJSON_AddBoolToObject(user, field, value);
|
||||||
|
free(database_file);
|
||||||
return false;
|
return false;
|
||||||
} else if (cJSON_IsString(user_field)) {
|
} else if (cJSON_IsString(user_field)) {
|
||||||
cJSON_SetValuestring(user_field, value);
|
cJSON_SetValuestring(user_field, value);
|
||||||
}
|
}
|
||||||
char *new_data = cJSON_Print(users);
|
char *new_data = cJSON_Print(users);
|
||||||
write_file("users_db.json", new_data);
|
write_file(database_file, new_data);
|
||||||
free(new_data);
|
free(new_data);
|
||||||
free(data);
|
free(data);
|
||||||
|
free(database_file);
|
||||||
cJSON_Delete(users);
|
cJSON_Delete(users);
|
||||||
pthread_mutex_unlock(&users_file_mutex);
|
pthread_mutex_unlock(&users_file_mutex);
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
Reference in new issue
Block a user