添加用户注册和权限管理功能,重构用户ID处理逻辑,增强错误处理
This commit is contained in:
parent
438c2f14b6
commit
479d38036e
@ -16,8 +16,14 @@ extern "C"
|
||||
|
||||
int login(const char* user_id, const char* password, int* result);
|
||||
|
||||
int registe(const char* user_id, const char* password);
|
||||
|
||||
int delete_user(const char* user_id);
|
||||
|
||||
int get_user_permission(const char* user_id, int* result);
|
||||
|
||||
int set_user_permission(const char* user_id, int permission);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
@ -5,12 +5,28 @@
|
||||
#include <print>
|
||||
|
||||
#include <leveldb/db.h>
|
||||
#include <leveldb/write_batch.h>
|
||||
|
||||
using leveldb_ptr = leveldb::DB*;
|
||||
|
||||
auto user_db = leveldb_ptr{nullptr};
|
||||
auto iter_round = 10000;
|
||||
|
||||
auto filter(const std::string_view id) -> bool
|
||||
{
|
||||
return id.contains(':');
|
||||
}
|
||||
|
||||
auto mangle_user_id(const std::string_view id) -> std::string
|
||||
{
|
||||
return std::format("{}:user_id", id);
|
||||
}
|
||||
|
||||
auto mangle_permission(const std::string_view id) -> std::string
|
||||
{
|
||||
return std::format("{}:permission", id);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
{
|
||||
auto open_user_db() -> int
|
||||
@ -35,8 +51,10 @@ extern "C"
|
||||
|
||||
auto check_user_exists(const char* user_id, int* result) -> int
|
||||
{
|
||||
if (filter(user_id)) return 0;
|
||||
|
||||
auto value = std::string{};
|
||||
auto status = user_db->Get(leveldb::ReadOptions{}, user_id, &value);
|
||||
auto status = user_db->Get(leveldb::ReadOptions{}, mangle_user_id(user_id), &value);
|
||||
if (status.ok()) {
|
||||
*result = 1;
|
||||
} else if (status.IsNotFound()) {
|
||||
@ -50,7 +68,10 @@ extern "C"
|
||||
|
||||
auto set_user_password(const char* user_id, const char* password) -> int
|
||||
{
|
||||
auto status = user_db->Put(leveldb::WriteOptions{}, user_id, generate_hash(password, iter_round));
|
||||
if (filter(user_id)) return 0;
|
||||
|
||||
auto status =
|
||||
user_db->Put(leveldb::WriteOptions{}, mangle_user_id(user_id), generate_hash(password, iter_round));
|
||||
if (!status.ok()) {
|
||||
std::println(stderr, "Failed to set user password: {}", status.ToString());
|
||||
return 0;
|
||||
@ -60,8 +81,10 @@ extern "C"
|
||||
|
||||
auto login(const char* user_id, const char* password, int* result) -> int
|
||||
{
|
||||
if (filter(user_id)) return 0;
|
||||
|
||||
auto value = std::string{};
|
||||
auto status = user_db->Get(leveldb::ReadOptions{}, user_id, &value);
|
||||
auto status = user_db->Get(leveldb::ReadOptions{}, mangle_user_id(user_id), &value);
|
||||
if (!status.ok()) {
|
||||
std::println(stderr, "Failed to login: {}", status.ToString());
|
||||
return 0;
|
||||
@ -70,10 +93,60 @@ extern "C"
|
||||
return 1;
|
||||
}
|
||||
|
||||
int registe(const char* user_id, const char* password)
|
||||
{
|
||||
if (filter(user_id)) return 0;
|
||||
|
||||
auto batch = leveldb::WriteBatch{};
|
||||
batch.Put(mangle_user_id(user_id), generate_hash(password, iter_round));
|
||||
batch.Put(mangle_permission(user_id), "1");
|
||||
auto status = user_db->Write(leveldb::WriteOptions{}, &batch);
|
||||
if (!status.ok()) {
|
||||
std::println(stderr, "Failed to register: {}", status.ToString());
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto delete_user(const char* user_id) -> int
|
||||
{
|
||||
auto write_options = leveldb::WriteOptions{};
|
||||
auto status = user_db->Delete(write_options, user_id);
|
||||
return status.ok();
|
||||
if (filter(user_id)) return 0;
|
||||
|
||||
auto batch = leveldb::WriteBatch{};
|
||||
batch.Delete(mangle_user_id(user_id));
|
||||
batch.Delete(mangle_permission(user_id));
|
||||
auto status = user_db->Write(leveldb::WriteOptions{}, &batch);
|
||||
if (!status.ok()) {
|
||||
std::println(stderr, "Failed to delete: {}", status.ToString());
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int get_user_permission(const char* user_id, int* result)
|
||||
{
|
||||
if (filter(user_id)) return 0;
|
||||
|
||||
auto value = std::string{};
|
||||
auto status = user_db->Get(leveldb::ReadOptions{}, mangle_permission(user_id), &value);
|
||||
if (status.ok()) {
|
||||
*result = std::stoi(value);
|
||||
} else {
|
||||
std::println(stderr, "Failed to get user permission: {}", status.ToString());
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int set_user_permission(const char* user_id, int permission)
|
||||
{
|
||||
if (filter(user_id)) return 0;
|
||||
|
||||
auto status = user_db->Put(leveldb::WriteOptions{}, mangle_permission(user_id), std::to_string(permission));
|
||||
if (!status.ok()) {
|
||||
std::println(stderr, "Failed to set user permission: {}", status.ToString());
|
||||
return 0;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user