Merge pull request 'develop' (#4) from develop into main
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 10s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Successful in 8s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Successful in 13s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 10s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Successful in 8s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Successful in 13s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Successful in 12s
Reviewed-on: https://gitea.home.com:3000/TolaMironcenko/auth_service_c/pulls/4
This commit was merged in pull request #4.
This commit is contained in:
commit
499a9567a1
72 files changed
+947
-742
No files matched your search
+7
-3
@@ -4,11 +4,14 @@ project(auth_service LANGUAGES C)
|
||||
set(CMAKE_C_STANDARD 23)
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||
set(CMAKE_C_FLAGS "-ffunction-sections -fdata-sections")
|
||||
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--gc-sections")
|
||||
if (APPLE)
|
||||
elseif (UNIX)
|
||||
set(CMAKE_EXE_LINKER_FLAGS "-Wl,--gc-sections")
|
||||
endif()
|
||||
|
||||
include_directories(./src/lib/ ${SQLite3_INCLUDE_DIRS})
|
||||
include_directories(./src/lib/ ./src ${SQLite3_INCLUDE_DIRS})
|
||||
|
||||
FILE(GLOB_RECURSE SOURCES src/*.c)
|
||||
FILE(GLOB SOURCES src/*.c src/lib/*.c src/file_db/*.c src/config/*.c src/api/*.c src/database/*.c src/security/*.c src/utils/*.c)
|
||||
|
||||
option(USE_SQLITE3 "Enable SQLite3 support" OFF)
|
||||
option(USE_WERROR "Enable SQLite3 support" OFF)
|
||||
@@ -16,6 +19,7 @@ option(USE_WERROR "Enable SQLite3 support" OFF)
|
||||
if(USE_SQLITE3)
|
||||
find_package(SQLite3 REQUIRED)
|
||||
add_definitions(-DUSE_SQLITE3)
|
||||
FILE(GLOB SOURCES src/*.c src/lib/*.c src/sqlite_db/*.c src/file_db/*.c src/config/*.c src/api/*.c src/database/*.c src/security/*.c src/utils/*.c)
|
||||
endif()
|
||||
|
||||
if(USE_WERROR)
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
CC = gcc
|
||||
CFLAGS = -Wall -g -std=gnu2x -DMG_TLS=MG_TLS_OPENSSL -ffunction-sections -fdata-sections
|
||||
LDFLAGS = -Wl,--gc-sections
|
||||
LDFLAGS =
|
||||
INCLUDES = -I./src/lib -I./src
|
||||
SRCS = $(wildcard src/*/*.c src/*.c)
|
||||
SRCS = $(wildcard src/lib/*.c src/file_db/*.c src/*.c src/config/*.c src/database/*.c src/api/*.c src/security/*.c src/utils/*.c)
|
||||
OBJS = $(SRCS:.c=.o)
|
||||
TARGET = auth_service
|
||||
BUILD_DIR=./build
|
||||
@@ -11,9 +11,14 @@ LIBS = -lpthread -lcrypto -lssl
|
||||
USE_SQLITE3 ?= 0
|
||||
WERROR ?= 0
|
||||
|
||||
ifeq ($(shell uname), Linux)
|
||||
LDFLAGS += -Wl,--gc-sections
|
||||
endif
|
||||
|
||||
ifeq ($(USE_SQLITE3), 1)
|
||||
CFLAGS += -DUSE_SQLITE3
|
||||
LIBS += -lsqlite3
|
||||
SRCS = $(wildcard src/lib/*.c src/sqlite_db/*.c src/file_db/*.c src/*.c src/config/*.c src/database/*.c src/api/*.c src/security/*.c src/utils/*.c)
|
||||
endif
|
||||
|
||||
ifeq ($(WERROR), 1)
|
||||
|
||||
+65
-31
@@ -2,17 +2,80 @@ project('auth_service', 'c', default_options: ['c_std=gnu2x'])
|
||||
include_dirs = include_directories('src', 'src/lib')
|
||||
deps = [dependency('openssl')]
|
||||
extra_c_args = ['-ffunction-sections', '-fdata-sections']
|
||||
extra_c_link_args = ['-Wl,--gc-sections']
|
||||
extra_c_link_args = []
|
||||
system = host_machine.system()
|
||||
if system == 'linux'
|
||||
extra_c_link_args += ['-Wl,--gc-sections']
|
||||
endif
|
||||
|
||||
sqlite3_dep = dependency('sqlite3', required: false)
|
||||
use_sqlite3 = get_option('use_sqlite3')
|
||||
use_werror = get_option('use_werror')
|
||||
sources = [
|
||||
'src/main.c',
|
||||
'src/api/user.c',
|
||||
'src/api/add_user.c',
|
||||
'src/api/change_password.c',
|
||||
'src/api/change_user.c',
|
||||
'src/api/delete_user.c',
|
||||
'src/api/get_all_users.c',
|
||||
'src/api/get_token.c',
|
||||
'src/api/get_user.c',
|
||||
'src/api/register_user.c',
|
||||
'src/api/verify_token.c',
|
||||
'src/utils/utils.c',
|
||||
'src/security/jwt.c',
|
||||
'src/lib/mongoose.c',
|
||||
'src/lib/cJSON.c',
|
||||
'src/database/database.c',
|
||||
'src/database/db_get_all_users.c',
|
||||
'src/database/db_get_user_by_username.c',
|
||||
'src/database/db_get_users_from_file.c',
|
||||
'src/utils/file.c',
|
||||
'src/database/db_insert_user.c',
|
||||
'src/database/db_update_user.c',
|
||||
'src/database/db_delete_user.c',
|
||||
'src/database/db_get_user_by_id.c',
|
||||
'src/config/ConfigManager.c',
|
||||
'src/config/config.c',
|
||||
'src/api/static.c',
|
||||
'src/security/cors.c',
|
||||
'src/database/db_get_user_field_by_id.c',
|
||||
'src/database/db_update_user_field.c',
|
||||
]
|
||||
|
||||
with_filedb_sources = [
|
||||
'src/file_db/filedb.c',
|
||||
'src/file_db/filedb_delete_user.c',
|
||||
'src/file_db/filedb_get_all_users.c',
|
||||
'src/file_db/filedb_get_user_by_id.c',
|
||||
'src/file_db/filedb_get_user_by_username.c',
|
||||
'src/file_db/filedb_get_user_field_by_id.c',
|
||||
'src/file_db/filedb_insert_user.c',
|
||||
'src/file_db/filedb_update_user.c',
|
||||
'src/file_db/filedb_update_user_field.c',
|
||||
]
|
||||
|
||||
with_sqlite3_sources = [
|
||||
'src/sqlite_db/sqlite.c',
|
||||
'src/sqlite_db/sqlite_delete_user.c',
|
||||
'src/sqlite_db/sqlite_get_all_users.c',
|
||||
'src/sqlite_db/sqlite_get_user_by_id.c',
|
||||
'src/sqlite_db/sqlite_get_user_by_username.c',
|
||||
'src/sqlite_db/sqlite_get_user_field_by_id.c',
|
||||
'src/sqlite_db/sqlite_init_db.c',
|
||||
'src/sqlite_db/sqlite_insert_user.c',
|
||||
'src/sqlite_db/sqlite_migrate_db.c',
|
||||
'src/sqlite_db/sqlite_update_user.c',
|
||||
'src/sqlite_db/sqlite_update_user_field.c',
|
||||
]
|
||||
|
||||
if use_sqlite3
|
||||
if not sqlite3_dep.found()
|
||||
error('SQLite3 requested but not found')
|
||||
endif
|
||||
deps += sqlite3_dep
|
||||
sources += with_sqlite3_sources
|
||||
add_project_arguments('-DUSE_SQLITE3', language: 'c')
|
||||
endif
|
||||
|
||||
@@ -22,36 +85,7 @@ endif
|
||||
|
||||
executable(
|
||||
'auth_service',
|
||||
'src/main.c',
|
||||
'src/user.c',
|
||||
'src/add_user.c',
|
||||
'src/change_password.c',
|
||||
'src/change_user.c',
|
||||
'src/delete_user.c',
|
||||
'src/get_all_users.c',
|
||||
'src/get_token.c',
|
||||
'src/get_user.c',
|
||||
'src/register_user.c',
|
||||
'src/verify_token.c',
|
||||
'src/utils.c',
|
||||
'src/jwt.c',
|
||||
'src/lib/mongoose.c',
|
||||
'src/lib/cJSON.c',
|
||||
'src/database.c',
|
||||
'src/db_get_all_users.c',
|
||||
'src/db_get_user_by_username.c',
|
||||
'src/db_get_users_from_file.c',
|
||||
'src/file.c',
|
||||
'src/db_insert_user.c',
|
||||
'src/db_update_user.c',
|
||||
'src/db_delete_user.c',
|
||||
'src/db_get_user_by_id.c',
|
||||
'src/ConfigManager.c',
|
||||
'src/config.c',
|
||||
'src/static.c',
|
||||
'src/cors.c',
|
||||
'src/db_get_user_field_by_id.c',
|
||||
'src/db_update_user_field.c',
|
||||
sources + with_filedb_sources,
|
||||
include_directories: include_dirs,
|
||||
dependencies: deps,
|
||||
c_args: extra_c_args,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "jwt.h"
|
||||
#include "security/jwt.h"
|
||||
#include "user.h"
|
||||
#include "utils.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
void add_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
request_with_token *requestdata = check_body(hm);
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "cJSON.h"
|
||||
#include "jwt.h"
|
||||
#include "security/jwt.h"
|
||||
#include "user.h"
|
||||
#include "utils.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
void change_password(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "jwt.h"
|
||||
#include "security/jwt.h"
|
||||
#include "user.h"
|
||||
#include "utils.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
void change_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "jwt.h"
|
||||
#include "security/jwt.h"
|
||||
#include "user.h"
|
||||
#include "utils.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
void delete_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "jwt.h"
|
||||
#include "security/jwt.h"
|
||||
#include "user.h"
|
||||
#include "utils.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
void get_all_users(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
request_with_token *data = check_body(hm);
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "jwt.h"
|
||||
#include "security/jwt.h"
|
||||
#include "user.h"
|
||||
|
||||
void get_token(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
@@ -1,6 +1,6 @@
|
||||
#include "jwt.h"
|
||||
#include "security/jwt.h"
|
||||
#include "user.h"
|
||||
#include "utils.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
void get_user(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
File renamed without changes.
File renamed without changes.
@@ -1,9 +1,9 @@
|
||||
#include "static.h"
|
||||
|
||||
#include "config.h"
|
||||
#include "config/config.h"
|
||||
|
||||
void root_handler(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
struct mg_http_serve_opts opts = {
|
||||
.root_dir = cm_get_parameter_as_string(config, "web")};
|
||||
mg_http_serve_dir(c, hm, &opts);
|
||||
}
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,6 +1,6 @@
|
||||
#include "jwt.h"
|
||||
#include "security/jwt.h"
|
||||
#include "user.h"
|
||||
#include "utils.h"
|
||||
#include "utils/utils.h"
|
||||
|
||||
void verify_token(struct mg_connection *c, struct mg_http_message *hm) {
|
||||
request_with_token *data = check_body(hm);
|
||||
@@ -6,7 +6,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "file.h"
|
||||
#include "utils/file.h"
|
||||
|
||||
typedef struct ConfigManager {
|
||||
cJSON *config;
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -1,15 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <pthread.h>
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include <sqlite3.h>
|
||||
extern sqlite3 *db;
|
||||
extern pthread_mutex_t db_mutex;
|
||||
#else
|
||||
extern pthread_mutex_t users_file_mutex;
|
||||
#endif
|
||||
|
||||
void init_db();
|
||||
void migrate_db();
|
||||
char *json_escape(const char *str);
|
||||
@@ -5,25 +5,12 @@
|
||||
#include <string.h>
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "config.h"
|
||||
#include "sql.h"
|
||||
#endif
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
sqlite3 *db = nullptr;
|
||||
pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
#else
|
||||
pthread_mutex_t users_file_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
#include "sqlite_db/sqlite.h"
|
||||
#endif
|
||||
|
||||
void init_db() {
|
||||
#ifdef USE_SQLITE3
|
||||
if (sqlite3_open(cm_get_parameter_as_string(config, "database"), &db) !=
|
||||
SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_close(db);
|
||||
exit(1);
|
||||
}
|
||||
sqlite_init_db();
|
||||
#else
|
||||
return;
|
||||
#endif
|
||||
@@ -31,16 +18,7 @@ void init_db() {
|
||||
|
||||
void migrate_db() {
|
||||
#ifdef USE_SQLITE3
|
||||
char *err_msg = 0;
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (sqlite3_exec(db, CREATE_TABLE_USERS, nullptr, nullptr, &err_msg) !=
|
||||
SQLITE_OK) {
|
||||
fprintf(stderr, "SQL error: %s\n", err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
sqlite3_close(db);
|
||||
exit(1);
|
||||
}
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
sqlite_migrate_db();
|
||||
#else
|
||||
return;
|
||||
#endif
|
||||
@@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
void init_db();
|
||||
void migrate_db();
|
||||
char *json_escape(const char *str);
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "api/user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sqlite_db/sqlite.h"
|
||||
#else
|
||||
#include "file_db/filedb.h"
|
||||
#endif
|
||||
|
||||
bool db_delete_user(const int id) {
|
||||
#ifdef USE_SQLITE3
|
||||
return sqlite_delete_user(id);
|
||||
#else
|
||||
return filedb_delete_user(id);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "api/user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sqlite_db/sqlite.h"
|
||||
#else
|
||||
#include "file_db/filedb.h"
|
||||
#endif
|
||||
|
||||
char *db_get_all_users() {
|
||||
#ifdef USE_SQLITE3
|
||||
return sqlite_get_all_users();
|
||||
#else
|
||||
return filedb_get_all_users();
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "api/user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sqlite_db/sqlite.h"
|
||||
#else
|
||||
#include "file_db/filedb.h"
|
||||
#endif
|
||||
|
||||
User *db_get_user_by_id(const int id) {
|
||||
#ifdef USE_SQLITE3
|
||||
return sqlite_get_user_by_id(id);
|
||||
#else
|
||||
return filedb_get_user_by_id(id);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "api/user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sqlite_db/sqlite.h"
|
||||
#else
|
||||
#include "file_db/filedb.h"
|
||||
#endif
|
||||
|
||||
User *db_get_user_by_username(const char *username) {
|
||||
#ifdef USE_SQLITE3
|
||||
return sqlite_get_user_by_username(username);
|
||||
#else
|
||||
return filedb_get_user_by_username(username);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "api/user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sqlite_db/sqlite.h"
|
||||
#else
|
||||
#include "file_db/filedb.h"
|
||||
#endif
|
||||
|
||||
char *db_get_user_field_by_id(const int id, const char *field) {
|
||||
#ifdef USE_SQLITE3
|
||||
return sqlite_get_user_field_by_id(id, field);
|
||||
#else
|
||||
return filedb_get_user_field_by_id(id, field);
|
||||
#endif
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
#include <cJSON.h>
|
||||
#include "file.h"
|
||||
#include "user.h"
|
||||
#include "utils/file.h"
|
||||
#include "api/user.h"
|
||||
|
||||
void db_get_users_from_file(const char *path) {
|
||||
char *users_string = read_file(path);
|
||||
@@ -0,0 +1,17 @@
|
||||
#include "api/user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sqlite_db/sqlite.h"
|
||||
#else
|
||||
#include "file_db/filedb.h"
|
||||
#endif
|
||||
|
||||
User *db_insert_user(const char *username, const char *email,
|
||||
const char *password, const char *group,
|
||||
const bool *is_superuser) {
|
||||
#ifdef USE_SQLITE3
|
||||
return sqlite_insert_user(username, email, password, group, is_superuser);
|
||||
#else
|
||||
return filedb_insert_user(username, email, password, group, is_superuser);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "api/user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sqlite_db/sqlite.h"
|
||||
#else
|
||||
#include "file_db/filedb.h"
|
||||
#endif
|
||||
|
||||
User *db_update_user(const int userid, const char *username, const char *email,
|
||||
const char *password, const char *group,
|
||||
const bool *is_superuser) {
|
||||
#ifdef USE_SQLITE3
|
||||
return sqlite_update_user(userid, username, email, password, group,
|
||||
is_superuser);
|
||||
#else
|
||||
return filedb_update_user(userid, username, email, password, group,
|
||||
is_superuser);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
#include "api/user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sqlite_db/sqlite.h"
|
||||
#else
|
||||
#include "file_db/filedb.h"
|
||||
#endif
|
||||
|
||||
bool db_update_user_field(const int id, const char *field, const char *value) {
|
||||
#ifdef USE_SQLITE3
|
||||
return sqlite_update_user_field(id, field, value);
|
||||
#else
|
||||
return filedb_update_user_field(id, field, value);
|
||||
#endif
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
#include "database.h"
|
||||
#include "user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sql.h"
|
||||
#else
|
||||
#include "file.h"
|
||||
#endif
|
||||
|
||||
bool db_delete_user(const int id) {
|
||||
#ifdef USE_SQLITE3
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Database not initialized\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_prepare_v2(db, DELETE_USER_SQL, -1, &stmt, 0);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
int changes = sqlite3_changes(db);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if (changes > 0) {
|
||||
printf("User deleted successfully. ID: %d\n", id);
|
||||
} else {
|
||||
printf("No user found with ID: %d\n", id);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return true;
|
||||
#else
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
int index = 0;
|
||||
bool found = false;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
if (user_id->valueint == id) {
|
||||
cJSON_DeleteItemFromArray(users, index);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
if (!found) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return false;
|
||||
}
|
||||
char *new_data = cJSON_Print(users);
|
||||
write_file("users_db.json", new_data);
|
||||
free(new_data);
|
||||
free(data);
|
||||
cJSON_Delete(users);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return true;
|
||||
#endif
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
#include "database.h"
|
||||
#include "user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sql.h"
|
||||
#else
|
||||
#include "cJSON.h"
|
||||
#include "file.h"
|
||||
#endif
|
||||
|
||||
User *db_get_user_by_id(const int id) {
|
||||
#ifdef USE_SQLITE3
|
||||
User *user = nullptr;
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Database not initialized\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, SELECT_USER_BY_ID_SQL, -1, &stmt, nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
int id = sqlite3_column_int(stmt, 0);
|
||||
const unsigned char *db_username = sqlite3_column_text(stmt, 1);
|
||||
const unsigned char *db_email = sqlite3_column_text(stmt, 2);
|
||||
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
|
||||
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
|
||||
int db_is_superuser = sqlite3_column_int(stmt, 5);
|
||||
user = new_user(id, (char *) db_username, (char *) db_password,
|
||||
(char *) db_email, (char *) db_group, db_is_superuser);
|
||||
}
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "Error reading data: %s\n", sqlite3_errmsg(db));
|
||||
free_user(user);
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return user;
|
||||
#else
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
User *found = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser");
|
||||
if (user_id->valueint == id) {
|
||||
found = new_user(user_id->valueint, user_username->valuestring,
|
||||
user_password->valuestring, user_email->valuestring,
|
||||
user_group->valuestring, user_is_superuser->valueint);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found == nullptr) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return found;
|
||||
#endif
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
#include "database.h"
|
||||
#include "user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sql.h"
|
||||
#else
|
||||
#include "cJSON.h"
|
||||
#include "file.h"
|
||||
#endif
|
||||
|
||||
User *db_get_user_by_username(const char *username) {
|
||||
#ifdef USE_SQLITE3
|
||||
User *user = nullptr;
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Database not initialized\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
sqlite3_stmt *stmt;
|
||||
int rc =
|
||||
sqlite3_prepare_v2(db, SELECT_USER_BY_USERNAME_SQL, -1, &stmt, nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
|
||||
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
int id = sqlite3_column_int(stmt, 0);
|
||||
const unsigned char *db_username = sqlite3_column_text(stmt, 1);
|
||||
const unsigned char *db_email = sqlite3_column_text(stmt, 2);
|
||||
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
|
||||
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
|
||||
int db_is_superuser = sqlite3_column_int(stmt, 5);
|
||||
user = new_user(id, (char *) db_username, (char *) db_password,
|
||||
(char *) db_email, (char *) db_group, db_is_superuser);
|
||||
}
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "Error reading data: %s\n", sqlite3_errmsg(db));
|
||||
free_user(user);
|
||||
sqlite3_finalize(stmt);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return user;
|
||||
#else
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
User *found = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser");
|
||||
if (!strcmp(user_username->valuestring, username)) {
|
||||
found = new_user(user_id->valueint, user_username->valuestring,
|
||||
user_password->valuestring, user_email->valuestring,
|
||||
user_group->valuestring, user_is_superuser->valueint);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found == nullptr) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return found;
|
||||
#endif
|
||||
}
|
||||
@@ -1,84 +0,0 @@
|
||||
#include <pthread.h>
|
||||
#include "database.h"
|
||||
#include "user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sql.h"
|
||||
#else
|
||||
#include "file.h"
|
||||
#endif
|
||||
|
||||
char *db_get_user_field_by_id(const int id, const char *field) {
|
||||
#ifdef USE_SQLITE3
|
||||
char *password = nullptr;
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Database not initialized\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
char sql[256];
|
||||
snprintf(sql, sizeof(sql), SELECT_USER_FIELD_SQL, field);
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc == SQLITE_ROW) {
|
||||
const unsigned char *db_password = sqlite3_column_text(stmt, 0);
|
||||
password = malloc(strlen((const char *) db_password) + 1);
|
||||
if (password) {
|
||||
strcpy(password, (const char *) db_password);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
} else if (rc == SQLITE_DONE) {
|
||||
fprintf(stderr, "Can't find user");
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
} else {
|
||||
fprintf(stderr, "Error executing query: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return password;
|
||||
#else
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
User *found = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser");
|
||||
if (user_id->valueint == id) {
|
||||
found = new_user(user_id->valueint, user_username->valuestring,
|
||||
user_password->valuestring, user_email->valuestring,
|
||||
user_group->valuestring, user_is_superuser->valueint);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found == nullptr) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return found->password;
|
||||
#endif
|
||||
}
|
||||
@@ -1,119 +0,0 @@
|
||||
#include "database.h"
|
||||
#include "user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sql.h"
|
||||
#else
|
||||
#include "cJSON.h"
|
||||
#include "file.h"
|
||||
#endif
|
||||
|
||||
User *db_insert_user(const char *username, const char *email,
|
||||
const char *password, const char *group,
|
||||
const bool *is_superuser) {
|
||||
#ifdef USE_SQLITE3
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Databse not initialized\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
if (!username || !email || !password) {
|
||||
fprintf(stderr, "username, email, password connot be empty\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
const char *sql = nullptr;
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
if (!group || !is_superuser) {
|
||||
sql = INSERT_USER_USERNAME_EMAIL_PASSWORD_SQL;
|
||||
} else if (!group) {
|
||||
sql = INSERT_USER_USERNAME_EMAIL_PASSWORD_GROUP_SQL;
|
||||
} else {
|
||||
sql = INSERT_USER_FULL_SQL;
|
||||
}
|
||||
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, email, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 3, password, -1, SQLITE_STATIC);
|
||||
|
||||
if (group) {
|
||||
sqlite3_bind_text(stmt, 4, group, -1, SQLITE_STATIC);
|
||||
}
|
||||
if (is_superuser) {
|
||||
sqlite3_bind_int(stmt, 5, *is_superuser);
|
||||
}
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int inserted_id = sqlite3_last_insert_rowid(db);
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
printf("User inserted successfully. ID: %d\n", inserted_id);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
User *new_db_user =
|
||||
new_user(inserted_id, username, email, password, group, is_superuser);
|
||||
return new_db_user;
|
||||
#else
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
int users_size = 0;
|
||||
bool found = false;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
if (!strcmp(user_username->valuestring, username) &&
|
||||
!strcmp(user_password->valuestring, password) &&
|
||||
!strcmp(user_email->valuestring, email) &&
|
||||
!strcmp(user_group->valuestring, group)) {
|
||||
found = true;
|
||||
}
|
||||
users_size++;
|
||||
}
|
||||
if (found == true) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
cJSON *new_user_data = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(new_user_data, "id", users_size);
|
||||
cJSON_AddStringToObject(new_user_data, "username", username);
|
||||
cJSON_AddStringToObject(new_user_data, "password", password);
|
||||
cJSON_AddStringToObject(new_user_data, "email", email);
|
||||
cJSON_AddStringToObject(new_user_data, "group", group);
|
||||
cJSON_AddBoolToObject(new_user_data, "is_superuser", *is_superuser);
|
||||
char *new_user_data_string = cJSON_Print(new_user_data);
|
||||
if (users_size == 0) {
|
||||
users = cJSON_CreateArray();
|
||||
}
|
||||
cJSON_AddItemToArray(users, new_user_data);
|
||||
char *new_data = cJSON_Print(users);
|
||||
write_file("users_db.json", new_data);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
User *return_user =
|
||||
new_user(users_size, username, email, password, group, is_superuser);
|
||||
free(new_data);
|
||||
free(data);
|
||||
free(new_user_data_string);
|
||||
// cJSON_Delete(new_user_data);
|
||||
cJSON_Delete(users);
|
||||
if (user != nullptr) cJSON_Delete(user);
|
||||
return return_user;
|
||||
#endif
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
#include <pthread.h>
|
||||
#include "database.h"
|
||||
#include "user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sql.h"
|
||||
#else
|
||||
#include "cJSON.h"
|
||||
#include "file.h"
|
||||
#endif
|
||||
|
||||
User *db_update_user(const int userid, const char *username, const char *email,
|
||||
const char *password, const char *group,
|
||||
const bool *is_superuser) {
|
||||
#ifdef USE_SQLITE3
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Databse not initialized\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
if (!username || !email || !password || !group || !is_superuser) {
|
||||
fprintf(stderr,
|
||||
"username, email, password, group, is_superuser connot be empty\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_prepare_v2(db, UPDATE_USER_FULL_SQL, -1, &stmt, 0);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, email, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 3, password, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 4, group, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int(stmt, 5, *is_superuser);
|
||||
sqlite3_bind_int(stmt, 6, userid);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int changes = sqlite3_changes(db);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if (changes > 0) {
|
||||
printf("User updated successfully. ID: %d\n", userid);
|
||||
} else {
|
||||
printf("No user found with ID: %d\n", userid);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return new_user(userid, username, email, password, group, is_superuser);
|
||||
#else
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
if (user_id->valueint == userid) {
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
cJSON_SetValuestring(user_username, username);
|
||||
cJSON_SetValuestring(user_password, password);
|
||||
cJSON_SetValuestring(user_email, email);
|
||||
cJSON_SetValuestring(user_group, group);
|
||||
cJSON_DeleteItemFromObject(user, "is_superuser");
|
||||
cJSON_AddBoolToObject(user, "is_superuser", *is_superuser);
|
||||
char *new_data = cJSON_Print(users);
|
||||
write_file("users_db.json", new_data);
|
||||
free(new_data);
|
||||
free(data);
|
||||
cJSON_Delete(users);
|
||||
User *ret =
|
||||
new_user(userid, username, password, email, group, is_superuser);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
free(data);
|
||||
cJSON_Delete(users);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return nullptr;
|
||||
#endif
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
#include "database.h"
|
||||
#include "user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include "sql.h"
|
||||
#else
|
||||
#include "cJSON.h"
|
||||
#include "file.h"
|
||||
#endif
|
||||
|
||||
bool db_update_user_field(const int id, const char *field, const char *value) {
|
||||
#ifdef USE_SQLITE3
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db || !field || !value) {
|
||||
fprintf(stderr, "invalid parameters\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
char sql[256];
|
||||
snprintf(sql, sizeof(sql), UPDATE_USER_FIELD_SQL, field);
|
||||
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "failed to prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int(stmt, 2, id);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "execution failed: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return sqlite3_changes(db) > 0;
|
||||
#else
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
if (user_id->valueint == id) {
|
||||
cJSON *user_field = cJSON_GetObjectItem(user, field);
|
||||
if (cJSON_IsNumber(user_field)) {
|
||||
// cJSON_SetNumberValue(user_field, value);
|
||||
return false;
|
||||
} else if (cJSON_IsBool(user_field)) {
|
||||
// cJSON_DeleteItemFromObject(user, field);
|
||||
// cJSON_AddBoolToObject(user, field, value);
|
||||
return false;
|
||||
} else if (cJSON_IsString(user_field)) {
|
||||
cJSON_SetValuestring(user_field, value);
|
||||
}
|
||||
char *new_data = cJSON_Print(users);
|
||||
write_file("users_db.json", new_data);
|
||||
free(new_data);
|
||||
free(data);
|
||||
cJSON_Delete(users);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
free(data);
|
||||
cJSON_Delete(users);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
#include "filedb.h"
|
||||
#include <pthread.h>
|
||||
|
||||
pthread_mutex_t users_file_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include "api/user.h"
|
||||
|
||||
extern pthread_mutex_t users_file_mutex;
|
||||
|
||||
bool filedb_delete_user(const int id);
|
||||
char *filedb_get_all_users();
|
||||
User *filedb_get_user_by_id(const int id);
|
||||
User *filedb_get_user_by_username(const char *username);
|
||||
char *filedb_get_user_field_by_id(const int id, const char *field);
|
||||
User *filedb_insert_user(const char *username, const char *email,
|
||||
const char *password, const char *group,
|
||||
const bool *is_superuser);
|
||||
User *filedb_update_user(const int userid, const char *username,
|
||||
const char *email, const char *password,
|
||||
const char *group, const bool *is_superuser);
|
||||
bool filedb_update_user_field(const int id, const char *field,
|
||||
const char *value);
|
||||
@@ -0,0 +1,34 @@
|
||||
#include <pthread.h>
|
||||
#include <stdlib.h>
|
||||
#include "utils/file.h"
|
||||
#include "cJSON.h"
|
||||
#include "filedb.h"
|
||||
|
||||
bool filedb_delete_user(const int id) {
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
int index = 0;
|
||||
bool found = false;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
if (user_id->valueint == id) {
|
||||
cJSON_DeleteItemFromArray(users, index);
|
||||
found = true;
|
||||
break;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
if (!found) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return false;
|
||||
}
|
||||
char *new_data = cJSON_Print(users);
|
||||
write_file("users_db.json", new_data);
|
||||
free(new_data);
|
||||
free(data);
|
||||
cJSON_Delete(users);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return true;
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
#include <pthread.h>
|
||||
#include "utils/file.h"
|
||||
#include "filedb.h"
|
||||
|
||||
char *filedb_get_all_users() {
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return data;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <pthread.h>
|
||||
#include "utils/file.h"
|
||||
#include "filedb.h"
|
||||
|
||||
User *filedb_get_user_by_id(const int id) {
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
User *found = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser");
|
||||
if (user_id->valueint == id) {
|
||||
found = new_user(user_id->valueint, user_username->valuestring,
|
||||
user_password->valuestring, user_email->valuestring,
|
||||
user_group->valuestring, user_is_superuser->valueint);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found == nullptr) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return found;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <pthread.h>
|
||||
#include "utils/file.h"
|
||||
#include "filedb.h"
|
||||
|
||||
User *filedb_get_user_by_username(const char *username) {
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
User *found = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser");
|
||||
if (!strcmp(user_username->valuestring, username)) {
|
||||
found = new_user(user_id->valueint, user_username->valuestring,
|
||||
user_password->valuestring, user_email->valuestring,
|
||||
user_group->valuestring, user_is_superuser->valueint);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found == nullptr) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return found;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <pthread.h>
|
||||
#include "utils/file.h"
|
||||
#include "filedb.h"
|
||||
|
||||
char *filedb_get_user_field_by_id(const int id, const char *field) {
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
User *found = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
cJSON *user_is_superuser = cJSON_GetObjectItem(user, "is_superuser");
|
||||
if (user_id->valueint == id) {
|
||||
found = new_user(user_id->valueint, user_username->valuestring,
|
||||
user_password->valuestring, user_email->valuestring,
|
||||
user_group->valuestring, user_is_superuser->valueint);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (found == nullptr) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
// if (user != nullptr) cJSON_Delete(user);
|
||||
cJSON_Delete(users);
|
||||
free(data);
|
||||
return found->password;
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
#include <pthread.h>
|
||||
#include "utils/file.h"
|
||||
#include "filedb.h"
|
||||
|
||||
User *filedb_insert_user(const char *username, const char *email,
|
||||
const char *password, const char *group,
|
||||
const bool *is_superuser) {
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
int users_size = 0;
|
||||
bool found = false;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
if (!strcmp(user_username->valuestring, username) &&
|
||||
!strcmp(user_password->valuestring, password) &&
|
||||
!strcmp(user_email->valuestring, email) &&
|
||||
!strcmp(user_group->valuestring, group)) {
|
||||
found = true;
|
||||
}
|
||||
users_size++;
|
||||
}
|
||||
if (found == true) {
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
cJSON *new_user_data = cJSON_CreateObject();
|
||||
cJSON_AddNumberToObject(new_user_data, "id", users_size);
|
||||
cJSON_AddStringToObject(new_user_data, "username", username);
|
||||
cJSON_AddStringToObject(new_user_data, "password", password);
|
||||
cJSON_AddStringToObject(new_user_data, "email", email);
|
||||
cJSON_AddStringToObject(new_user_data, "group", group);
|
||||
cJSON_AddBoolToObject(new_user_data, "is_superuser", *is_superuser);
|
||||
char *new_user_data_string = cJSON_Print(new_user_data);
|
||||
if (users_size == 0) {
|
||||
users = cJSON_CreateArray();
|
||||
}
|
||||
cJSON_AddItemToArray(users, new_user_data);
|
||||
char *new_data = cJSON_Print(users);
|
||||
write_file("users_db.json", new_data);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
User *return_user =
|
||||
new_user(users_size, username, email, password, group, is_superuser);
|
||||
free(new_data);
|
||||
free(data);
|
||||
free(new_user_data_string);
|
||||
// cJSON_Delete(new_user_data);
|
||||
cJSON_Delete(users);
|
||||
if (user != nullptr) cJSON_Delete(user);
|
||||
return return_user;
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <pthread.h>
|
||||
#include "utils/file.h"
|
||||
#include "filedb.h"
|
||||
|
||||
User *filedb_update_user(const int userid, const char *username,
|
||||
const char *email, const char *password,
|
||||
const char *group, const bool *is_superuser) {
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
if (user_id->valueint == userid) {
|
||||
cJSON *user_username = cJSON_GetObjectItem(user, "username");
|
||||
cJSON *user_password = cJSON_GetObjectItem(user, "password");
|
||||
cJSON *user_email = cJSON_GetObjectItem(user, "email");
|
||||
cJSON *user_group = cJSON_GetObjectItem(user, "group");
|
||||
cJSON_SetValuestring(user_username, username);
|
||||
cJSON_SetValuestring(user_password, password);
|
||||
cJSON_SetValuestring(user_email, email);
|
||||
cJSON_SetValuestring(user_group, group);
|
||||
cJSON_DeleteItemFromObject(user, "is_superuser");
|
||||
cJSON_AddBoolToObject(user, "is_superuser", *is_superuser);
|
||||
char *new_data = cJSON_Print(users);
|
||||
write_file("users_db.json", new_data);
|
||||
free(new_data);
|
||||
free(data);
|
||||
cJSON_Delete(users);
|
||||
User *ret =
|
||||
new_user(userid, username, password, email, group, is_superuser);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
free(data);
|
||||
cJSON_Delete(users);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <pthread.h>
|
||||
#include "utils/file.h"
|
||||
#include "filedb.h"
|
||||
|
||||
bool filedb_update_user_field(const int id, const char *field,
|
||||
const char *value) {
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
cJSON *users = cJSON_Parse(data);
|
||||
cJSON *user = nullptr;
|
||||
cJSON_ArrayForEach(user, users) {
|
||||
cJSON *user_id = cJSON_GetObjectItem(user, "id");
|
||||
if (user_id->valueint == id) {
|
||||
cJSON *user_field = cJSON_GetObjectItem(user, field);
|
||||
if (cJSON_IsNumber(user_field)) {
|
||||
// cJSON_SetNumberValue(user_field, value);
|
||||
return false;
|
||||
} else if (cJSON_IsBool(user_field)) {
|
||||
// cJSON_DeleteItemFromObject(user, field);
|
||||
// cJSON_AddBoolToObject(user, field, value);
|
||||
return false;
|
||||
} else if (cJSON_IsString(user_field)) {
|
||||
cJSON_SetValuestring(user_field, value);
|
||||
}
|
||||
char *new_data = cJSON_Print(users);
|
||||
write_file("users_db.json", new_data);
|
||||
free(new_data);
|
||||
free(data);
|
||||
cJSON_Delete(users);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
free(data);
|
||||
cJSON_Delete(users);
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return false;
|
||||
}
|
||||
+7
-7
@@ -5,13 +5,13 @@
|
||||
#include <sqlite3.h>
|
||||
#endif
|
||||
|
||||
#include "config.h"
|
||||
#include "cors.h"
|
||||
#include "database.h"
|
||||
#include "file.h"
|
||||
#include "route.h"
|
||||
#include "static.h"
|
||||
#include "user.h"
|
||||
#include "config/config.h"
|
||||
#include "security/cors.h"
|
||||
#include "database/database.h"
|
||||
#include "utils/file.h"
|
||||
#include "api/route.h"
|
||||
#include "api/static.h"
|
||||
#include "api/user.h"
|
||||
|
||||
static const struct route routes[] = {
|
||||
{"GET", "/#", root_handler},
|
||||
|
||||
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,5 @@
|
||||
#include "sqlite.h"
|
||||
#include "pthread.h"
|
||||
|
||||
sqlite3 *db = nullptr;
|
||||
pthread_mutex_t db_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <sqlite3.h>
|
||||
#include "api/user.h"
|
||||
|
||||
extern sqlite3 *db;
|
||||
extern pthread_mutex_t db_mutex;
|
||||
|
||||
void sqlite_init_db();
|
||||
void sqlite_migrate_db();
|
||||
bool sqlite_delete_user(const int id);
|
||||
char *sqlite_get_all_users();
|
||||
User *sqlite_get_user_by_id(const int id);
|
||||
User *sqlite_get_user_by_username(const char *username);
|
||||
char *sqlite_get_user_field_by_id(const int id, const char *field);
|
||||
User *sqlite_insert_user(const char *username, const char *email,
|
||||
const char *password, const char *group,
|
||||
const bool *is_superuser);
|
||||
User *sqlite_update_user(const int userid, const char *username,
|
||||
const char *email, const char *password,
|
||||
const char *group, const bool *is_superuser);
|
||||
bool sqlite_update_user_field(const int id, const char *field,
|
||||
const char *value);
|
||||
@@ -0,0 +1,46 @@
|
||||
#include "sqlite.h"
|
||||
#include "sql.h"
|
||||
#include <stdio.h>
|
||||
#include "pthread.h"
|
||||
|
||||
bool sqlite_delete_user(const int id) {
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Database not initialized\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_prepare_v2(db, DELETE_USER_SQL, -1, &stmt, 0);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
int changes = sqlite3_changes(db);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if (changes > 0) {
|
||||
printf("User deleted successfully. ID: %d\n", id);
|
||||
} else {
|
||||
printf("No user found with ID: %d\n", id);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return true;
|
||||
}
|
||||
@@ -1,14 +1,12 @@
|
||||
#include "database.h"
|
||||
#include "user.h"
|
||||
|
||||
#ifdef USE_SQLITE3
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "database/database.h"
|
||||
#include "sql.h"
|
||||
#else
|
||||
#include "file.h"
|
||||
#endif
|
||||
#include "sqlite.h"
|
||||
|
||||
char *db_get_all_users() {
|
||||
#ifdef USE_SQLITE3
|
||||
char *sqlite_get_all_users() {
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Database not initialized\n");
|
||||
@@ -101,10 +99,4 @@ char *db_get_all_users() {
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return result;
|
||||
#else
|
||||
pthread_mutex_lock(&users_file_mutex);
|
||||
char *data = read_file("users_db.json");
|
||||
pthread_mutex_unlock(&users_file_mutex);
|
||||
return data;
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "sql.h"
|
||||
#include "sqlite.h"
|
||||
#include "pthread.h"
|
||||
|
||||
User *sqlite_get_user_by_id(const int id) {
|
||||
User *user = nullptr;
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Database not initialized\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, SELECT_USER_BY_ID_SQL, -1, &stmt, nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
int id = sqlite3_column_int(stmt, 0);
|
||||
const unsigned char *db_username = sqlite3_column_text(stmt, 1);
|
||||
const unsigned char *db_email = sqlite3_column_text(stmt, 2);
|
||||
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
|
||||
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
|
||||
int db_is_superuser = sqlite3_column_int(stmt, 5);
|
||||
user = new_user(id, (char *) db_username, (char *) db_password,
|
||||
(char *) db_email, (char *) db_group, db_is_superuser);
|
||||
}
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "Error reading data: %s\n", sqlite3_errmsg(db));
|
||||
free_user(user);
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return user;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "sql.h"
|
||||
#include "sqlite.h"
|
||||
#include "pthread.h"
|
||||
|
||||
User *sqlite_get_user_by_username(const char *username) {
|
||||
User *user = nullptr;
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Database not initialized\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
sqlite3_stmt *stmt;
|
||||
int rc =
|
||||
sqlite3_prepare_v2(db, SELECT_USER_BY_USERNAME_SQL, -1, &stmt, nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
|
||||
|
||||
while ((rc = sqlite3_step(stmt)) == SQLITE_ROW) {
|
||||
int id = sqlite3_column_int(stmt, 0);
|
||||
const unsigned char *db_username = sqlite3_column_text(stmt, 1);
|
||||
const unsigned char *db_email = sqlite3_column_text(stmt, 2);
|
||||
const unsigned char *db_password = sqlite3_column_text(stmt, 3);
|
||||
const unsigned char *db_group = sqlite3_column_text(stmt, 4);
|
||||
int db_is_superuser = sqlite3_column_int(stmt, 5);
|
||||
user = new_user(id, (char *) db_username, (char *) db_password,
|
||||
(char *) db_email, (char *) db_group, db_is_superuser);
|
||||
}
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "Error reading data: %s\n", sqlite3_errmsg(db));
|
||||
free_user(user);
|
||||
sqlite3_finalize(stmt);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return user;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#include "pthread.h"
|
||||
#include "sql.h"
|
||||
#include "sqlite.h"
|
||||
|
||||
char *sqlite_get_user_field_by_id(const int id, const char *field) {
|
||||
char *password = nullptr;
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Database not initialized\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
char sql[256];
|
||||
snprintf(sql, sizeof(sql), SELECT_USER_FIELD_SQL, field);
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, nullptr);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
sqlite3_bind_int(stmt, 1, id);
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc == SQLITE_ROW) {
|
||||
const unsigned char *db_password = sqlite3_column_text(stmt, 0);
|
||||
password = malloc(strlen((const char *) db_password) + 1);
|
||||
if (password) {
|
||||
strcpy(password, (const char *) db_password);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
} else if (rc == SQLITE_DONE) {
|
||||
fprintf(stderr, "Can't find user");
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
} else {
|
||||
fprintf(stderr, "Error executing query: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return password;
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#include "../config/config.h"
|
||||
#include "sqlite.h"
|
||||
|
||||
void sqlite_init_db() {
|
||||
if (sqlite3_open(cm_get_parameter_as_string(config, "database"), &db) !=
|
||||
SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_close(db);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
#include "pthread.h"
|
||||
#include "sql.h"
|
||||
#include "sqlite.h"
|
||||
|
||||
User *sqlite_insert_user(const char *username, const char *email,
|
||||
const char *password, const char *group,
|
||||
const bool *is_superuser) {
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Databse not initialized\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
if (!username || !email || !password) {
|
||||
fprintf(stderr, "username, email, password connot be empty\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
const char *sql = nullptr;
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
if (!group || !is_superuser) {
|
||||
sql = INSERT_USER_USERNAME_EMAIL_PASSWORD_SQL;
|
||||
} else if (!group) {
|
||||
sql = INSERT_USER_USERNAME_EMAIL_PASSWORD_GROUP_SQL;
|
||||
} else {
|
||||
sql = INSERT_USER_FULL_SQL;
|
||||
}
|
||||
rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Cannot prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, email, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 3, password, -1, SQLITE_STATIC);
|
||||
|
||||
if (group) {
|
||||
sqlite3_bind_text(stmt, 4, group, -1, SQLITE_STATIC);
|
||||
}
|
||||
if (is_superuser) {
|
||||
sqlite3_bind_int(stmt, 5, *is_superuser);
|
||||
}
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int inserted_id = sqlite3_last_insert_rowid(db);
|
||||
|
||||
sqlite3_finalize(stmt);
|
||||
printf("User inserted successfully. ID: %d\n", inserted_id);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
User *new_db_user =
|
||||
new_user(inserted_id, username, email, password, group, is_superuser);
|
||||
return new_db_user;
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
#include "pthread.h"
|
||||
#include "sql.h"
|
||||
#include "sqlite.h"
|
||||
|
||||
void sqlite_migrate_db() {
|
||||
char *err_msg = 0;
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (sqlite3_exec(db, CREATE_TABLE_USERS, nullptr, nullptr, &err_msg) !=
|
||||
SQLITE_OK) {
|
||||
fprintf(stderr, "SQL error: %s\n", err_msg);
|
||||
sqlite3_free(err_msg);
|
||||
sqlite3_close(db);
|
||||
exit(1);
|
||||
}
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
#include "pthread.h"
|
||||
#include "sql.h"
|
||||
#include "sqlite.h"
|
||||
|
||||
User *sqlite_update_user(const int userid, const char *username,
|
||||
const char *email, const char *password,
|
||||
const char *group, const bool *is_superuser) {
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db) {
|
||||
fprintf(stderr, "Databse not initialized\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
if (!username || !email || !password || !group || !is_superuser) {
|
||||
fprintf(stderr,
|
||||
"username, email, password, group, is_superuser connot be empty\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
sqlite3_stmt *stmt;
|
||||
int rc;
|
||||
|
||||
rc = sqlite3_prepare_v2(db, UPDATE_USER_FULL_SQL, -1, &stmt, 0);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "Failed to prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
sqlite3_bind_text(stmt, 1, username, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 2, email, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 3, password, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_text(stmt, 4, group, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int(stmt, 5, *is_superuser);
|
||||
sqlite3_bind_int(stmt, 6, userid);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "Execution failed: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_finalize(stmt);
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int changes = sqlite3_changes(db);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if (changes > 0) {
|
||||
printf("User updated successfully. ID: %d\n", userid);
|
||||
} else {
|
||||
printf("No user found with ID: %d\n", userid);
|
||||
return nullptr;
|
||||
}
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return new_user(userid, username, email, password, group, is_superuser);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
#include "pthread.h"
|
||||
#include "sql.h"
|
||||
#include "sqlite.h"
|
||||
|
||||
bool sqlite_update_user_field(const int id, const char *field,
|
||||
const char *value) {
|
||||
pthread_mutex_lock(&db_mutex);
|
||||
if (!db || !field || !value) {
|
||||
fprintf(stderr, "invalid parameters\n");
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
char sql[256];
|
||||
snprintf(sql, sizeof(sql), UPDATE_USER_FIELD_SQL, field);
|
||||
|
||||
sqlite3_stmt *stmt;
|
||||
int rc = sqlite3_prepare_v2(db, sql, -1, &stmt, 0);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "failed to prepare statement: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
sqlite3_bind_text(stmt, 1, value, -1, SQLITE_STATIC);
|
||||
sqlite3_bind_int(stmt, 2, id);
|
||||
|
||||
rc = sqlite3_step(stmt);
|
||||
sqlite3_finalize(stmt);
|
||||
|
||||
if (rc != SQLITE_DONE) {
|
||||
fprintf(stderr, "execution failed: %s\n", sqlite3_errmsg(db));
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return false;
|
||||
}
|
||||
pthread_mutex_unlock(&db_mutex);
|
||||
return sqlite3_changes(db) > 0;
|
||||
}
|
||||
File renamed without changes.
File renamed without changes.
@@ -3,7 +3,7 @@
|
||||
#include <cJSON.h>
|
||||
#include <mongoose.h>
|
||||
|
||||
#include "user.h"
|
||||
#include "api/user.h"
|
||||
|
||||
request_with_token *check_body(const struct mg_http_message *hm) {
|
||||
if (hm->body.len == 0) {
|
||||
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "user.h"
|
||||
#include "api/user.h"
|
||||
|
||||
request_with_token *check_body(const struct mg_http_message *hm);
|
||||
Reference in new issue
Block a user