chenluhua1980
2026-01-14 02a406fb7e3d635dca0ef87914fee54c7fd6bd3c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// UserX Óû§¹ÜÀí¿â£¨C ½Ó¿Ú£©/ UserX user management library (C API)
// ¸²¸Ç¹¦ÄÜ / Features: users, roles, actions, auth, logs£¨¹Ì¶¨ SQLite3 ³Ö¾Ã»¯ / SQLite3 persistence£©
 
#pragma once
 
#if defined(_WIN32)
#  ifdef USERXLIBRARY_EXPORTS
#    define UX_API extern "C" __declspec(dllexport)
#  else
#    define UX_API extern "C" __declspec(dllimport)
#  endif
#else
#  define UX_API extern "C"
#endif
 
#include <wchar.h>
 
 
#ifdef _COMPILE_AS_LIB
#warning "compiling as lib!"
#else
#ifdef _DEBUG
#ifndef _WIN64
#pragma comment(lib, "../USERXLibrary/lib/Win32/Debug/UserXLibrary.lib")
#else 
#pragma comment(lib, "../USERXLibrary/lib/x64/Debug/UserXLibrary.lib")
#endif
#else 
#ifndef _WIN64
#pragma comment(lib, "../USERXLibrary/lib/Win32/Release/UserXLibrary.lib")
#else 
#pragma comment(lib, "../USERXLibrary/lib/x64/Release/UserXLibrary.lib")
#endif
#endif
#endif // !BUILD_AS_LIB
 
 
// ¶îÍâ´íÎóÂë / Extra error code
#ifndef UX_ERR_BAD_PASSWORD
#define UX_ERR_BAD_PASSWORD        -10    // ÃÜÂë´íÎó / bad password
#endif
 
// ´íÎóÂëºê¶¨Òå / Error code macros
#define UX_OK                        0    // ³É¹¦ / success
#define UX_ERR_INVALID_ARGS         -1    // ²ÎÊý´íÎó / invalid arguments
#define UX_ERR_NOT_FOUND            -2    // Î´ÕÒµ½ / not found
#define UX_ERR_EXISTS               -3    // ÒÑ´æÔÚ / already exists
#define UX_ERR_PERMISSION           -4    // È¨ÏÞ²»×ã / permission denied
#define UX_ERR_NOT_LOGGED_IN        -5    // Î´µÇ¼ / not logged in
#define UX_ERR_BUFFER_TOO_SMALL     -6    // »º³åÇø²»×ã / buffer too small
#define UX_ERR_DB                   -7    // I/O »òÊý¾Ý¿â´íÎó / I/O or database error
#define UX_ERR_NOT_DEFINED          -8    // Î´¶¨Ò壨È綯×÷²»´æÔÚ£©/ not defined
#define UX_ERR_DB_NOT_EMPTY         -9    // Êý¾Ý¿â·Ç¿Õ£¨Òѳõʼ»¯£©/ database not empty
 
// ·µ»ØÂë / Return codes£¨Ïê¼ûÉÏ·½ºê£©
// UX_OK, UX_ERR_INVALID_ARGS, UX_ERR_NOT_FOUND, UX_ERR_EXISTS,
// UX_ERR_PERMISSION, UX_ERR_NOT_LOGGED_IN, UX_ERR_BUFFER_TOO_SMALL,
// UX_ERR_DB, UX_ERR_NOT_DEFINED, UX_ERR_DB_NOT_EMPTY
 
// ³õʼ»¯£ºÖ¸¶¨Êý¾ÝĿ¼£¬ÄÚ²¿½«´ò¿ª/´´½¨ SQLite Êý¾Ý¿âÎļþ UserX.db
// Init: provide data directory; opens/creates SQLite DB file UserX.db
UX_API int UX_Init(const wchar_t* storage_dir);
 
// ¿ÉÑ¡£º¸²¸ÇÊý¾Ý¿â·¾¶£¨½öʹÓõÚÒ»¸ö²ÎÊý×÷Ϊ .db Îļþ·¾¶£¬ÆäËûºöÂÔ£©
// Optional: override DB path (use first arg as .db path; others ignored)
UX_API int UX_SetStorage(const wchar_t* users_db_path,
                         const wchar_t* /*unused_logs*/,
                         const wchar_t* /*unused_roles*/,
                         const wchar_t* /*unused_actions*/);
 
// ÅäÖýÇÉ«£¨name:level °´ÐУ©/ Configure roles from lines "name:level"
// ½öÔÊÐíÔÚ¡°¿ÕÊý¾Ý¿â¡±£¨roles/users/actions/logs Ëıí¾ùÎÞÊý¾Ý£©Ê±µ÷Óã»·ñÔò·µ»Ø -9¡£
// Only allowed when DB is empty (roles/users/actions/logs all zero rows); otherwise returns -9.
// e.g. L"Admin:100\nEngineer:50\nOperator:10\n"
UX_API int UX_SetRoleDefinitions(const wchar_t* roles_text);
 
// »ñÈ¡½ÇÉ«ÁÐ±í£¨name:level °´ÐУ©£»buffer Îª¿Õ»ò²»×ãʱ·µ»ØËùÐè´óС
// Get roles list as lines; returns required size if buffer is null/too small
UX_API int UX_GetRoles(wchar_t* buffer, int buffer_chars);
 
// Óû§¹ÜÀí / User management
UX_API int UX_AddUser(const wchar_t* username,
                      const wchar_t* display_name,
                      const wchar_t* password,
                      const wchar_t* role_name);
 
UX_API int UX_DeleteUser(const wchar_t* username);
 
// ¸üÐÂÈÎÒâ×ֶΣ¨´« nullptr ±íʾ±£³Ö²»±ä£©/ Update subset; nullptr keeps field
UX_API int UX_UpdateUser(const wchar_t* username,
                         const wchar_t* new_display_name,
                         const wchar_t* new_password,
                         const wchar_t* new_role_name,
                         int enabled /* -1 keep, 0 disable, 1 enable */);
 
UX_API int UX_EnableUser(const wchar_t* username, int enabled /*0/1*/);
UX_API int UX_ResetPassword(const wchar_t* username, const wchar_t* new_password);
UX_API int UX_RenameUser(const wchar_t* username, const wchar_t* new_display_name);
 
// ¹ÜÀíԱȨÏÞ£ºÉ¾³ýËùÓÐÓû§ / Admin-only
UX_API int UX_DeleteAllUsers();
 
// »ñÈ¡Óû§ÁÐ±í£¨Ã¿ÐУºusername,display,level,enabled£©/ Query users
UX_API int UX_GetUsers(wchar_t* buffer, int buffer_chars);
 
// ÈÏÖ¤ / Authentication
UX_API int UX_Login(const wchar_t* username, const wchar_t* password);
UX_API void UX_Logout();
UX_API int UX_IsLoggedIn();
UX_API void UX_Shutdown(); // ÊÍ·Å×ÊÔ´ / shutdown and free resources
UX_API int UX_GetCurrentUser(wchar_t* buffer, int buffer_chars);
 
// ¶¯×÷ÓëȨÏÞ / Actions & permissions
// ¶¨Òå/¸²¸Ç¶¯×÷£¨Ãû³Æ¡¢ÃèÊö¡¢×îµÍ½ÇÉ«£©/ Define/overwrite action
UX_API int UX_DefineAction(const wchar_t* action_name,
                           const wchar_t* description,
                           const wchar_t* min_role_name);
 
// ¿ÉÖ´Ðзµ»Ø1£¬²»¿ÉÖ´Ðзµ»Ø0£»¸ºÊýΪ´íÎó / 1 allowed, 0 denied
UX_API int UX_CanExecute(const wchar_t* action_name);
 
// ¼Ç¼¶¯×÷µ½ÈÕÖ¾£¨Ê±¼ä¡¢Óû§¡¢¶¯×÷¡¢ÃèÊö£©/ Record action to logs
UX_API int UX_RecordAction(const wchar_t* action_name);
 
// »ñÈ¡¶¯×÷ÁÐ±í£¨Ã¿ÐУºname,desc,minlevel£©/ Get actions list
UX_API int UX_GetActions(wchar_t* buffer, int buffer_chars);
 
// ¼Ç¼³¢ÊÔ£¨ÔÊÐí/¾Ü¾ø£©/ record an attempt with allowed flag (1 allowed, 0 denied)
UX_API int UX_RecordAttempt(const wchar_t* action_name, int allowed /*1/0*/);
 
// ²éѯ×î½ü N ÌõÈÕÖ¾£¨Ã¿ÐУºISO8601ʱ¼ä,Óû§Ãû,¶¯×÷,ÃèÊö£©/ Query logs
UX_API int UX_QueryLogs(int last_n, wchar_t* buffer, int buffer_chars);
 
// ¸ù¾Ý´íÎóÂë·µ»ØÎÄ×ÖÃèÊö£¨ÖÐÎÄ/English£©
// Return a human-readable message for an error code
UX_API const wchar_t* UX_ErrorMessage(int code);