Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake (push) Successful in 12s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson (push) Successful in 11s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Make-Without-sqlite3 (push) Successful in 8s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-CMake-Without-sqlite3 (push) Successful in 14s
Prepare Build and Testing executable binary / Prepare-Build-Testing-With-Meson-Without-sqlite3 (push) Successful in 12s
85 lines
2.7 KiB
C
85 lines
2.7 KiB
C
#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
|
|
}
|