chenluhua1980
2026-01-06 4d9d8d22e3666076988c30afb4e7c6fe365c19aa
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include "stdafx.h"
#include "CUserManager2.h"
#include "ToolUnits.h"
#include <vector>
#include <map>
#include <utility>
#include <algorithm>
#include <sstream>
#include <cwchar>
 
std::vector<std::wstring> SplitLines(const std::wstring& text) 
{
    std::wstringstream ss(text); std::vector<std::wstring> v; std::wstring line; while (std::getline(ss, line)) v.push_back(line); return v;
}
 
std::vector<std::wstring> SplitByDelimiter(const std::wstring& text, wchar_t delimiter)
{
    std::vector<std::wstring> parts;
    size_t start = 0;
    while (start <= text.length()) {
        size_t pos = text.find(delimiter, start);
        if (pos == std::wstring::npos) {
            parts.push_back(text.substr(start));
            break;
        }
 
        parts.push_back(text.substr(start, pos - start));
        start = pos + 1;
    }
 
    return parts;
}
 
template<typename Fn>
std::wstring ReadBufferVia(Fn fn)
{
    int need = fn(nullptr, 0); if (need <= 0) return L"";
    std::wstring buf; buf.resize((size_t)need);
    int rc = fn(buf.data(), need);
    if (rc == 0) { if (!buf.empty() && buf.back() == L'\0') buf.pop_back(); return buf; }
    return L"";
}
 
// 获取单例实例
CUserManager2& CUserManager2::getInstance() {
    static CUserManager2 instance;
    return instance;
}
 
CUserManager2::CUserManager2()
{
 
}
 
CUserManager2::~CUserManager2()
{
 
}
 
void CUserManager2::init(const char* pszDir)
{
    std::wstring dir = CToolUnits::AnsiToWString(std::string(pszDir));
    UX_Init(dir.c_str());
 
    wchar_t buffer[1024];
    UX_GetUsers(buffer, 1024);
    bool hasAny = false;
    for (auto& ln : SplitLines(buffer)) { if (!ln.empty()) { hasAny = true; break; } }
    if (!hasAny) {
        const wchar_t* roles = L"Admin:100\nEE:80\nPE:50\nOperator:10\n";
        (void)UX_SetRoleDefinitions(roles);
        (void)UX_AddUser(L"admin", L"Administrator", L"admin123", L"Admin");
 
        UX_DefineAction(L"start", L"启动机台", L"Operator"); 
        UX_DefineAction(L"stop", L"停机", L"Operator");
        UX_DefineAction(L"recipe", L"编辑配方", L"PE");
        UX_DefineAction(L"delVarialbles", L"删除变量", L"PE");
        UX_DefineAction(L"addVarialbles", L"新增变量", L"PE");
        UX_DefineAction(L"editVarialbles", L"编辑变量", L"PE");
        UX_DefineAction(L"addReports", L"新增Report", L"PE");
        UX_DefineAction(L"editReports", L"编辑Report", L"PE");
        UX_DefineAction(L"delReports", L"删除Report", L"PE");
        UX_DefineAction(L"addEvents", L"新增Event", L"PE");
        UX_DefineAction(L"editEvents", L"编辑Event", L"PE");
        UX_DefineAction(L"delEvents", L"删除Event", L"PE");
    }
    // 确保权限定义存在(幂等)
    UX_DefineAction(L"addVarialbles", L"新增变量", L"PE");
    UX_DefineAction(L"editVarialbles", L"编辑变量", L"PE");
    UX_DefineAction(L"delVarialbles", L"删除变量", L"PE");
    UX_DefineAction(L"addReports", L"新增Report", L"PE");
    UX_DefineAction(L"editReports", L"编辑Report", L"PE");
    UX_DefineAction(L"delReports", L"删除Report", L"PE");
    UX_DefineAction(L"delEvents", L"删除Event", L"PE");
    UX_DefineAction(L"addEvents", L"新增Event", L"PE");
    UX_DefineAction(L"editEvents", L"编辑Event", L"PE");
}
 
bool CUserManager2::login(const char* pszAccount, const char* pszPwd)
{
    std::wstring strUser, strPwd;
    strUser = CToolUnits::AnsiToWString(std::string(pszAccount));
    strPwd = CToolUnits::AnsiToWString(std::string(pszPwd));
    int rc = UX_Login(strUser.c_str(), strPwd.c_str());
    return rc == UX_OK;
}
 
bool CUserManager2::isLoggedIn()
{
    return UX_IsLoggedIn();
}
 
std::string CUserManager2::getCurrentUserName()
{
    std::string strName;
 
    int need = UX_GetCurrentUser(nullptr, 0);
    std::wstring buf; buf.resize((size_t)need);
    if (UX_GetCurrentUser(buf.data(), need) == UX_OK) { 
        if (!buf.empty() && buf.back() == L'\0')
            buf.pop_back(); 
 
        strName = CToolUnits::WStringToAnsi(buf);
    }
 
    return strName;
}
 
bool CUserManager2::IsAdminCurrent() 
{
    if (UX_IsLoggedIn() != 1) return false;
    int need = UX_GetCurrentUser(nullptr, 0); if (need <= 0) return false;
    std::wstring user; user.resize((size_t)need);
    if (UX_GetCurrentUser(user.data(), need) != 0) return false;
    if (!user.empty() && user.back() == L'\0') user.pop_back();
    int maxLvl = 0; auto rolesTxt = ReadBufferVia([](wchar_t* b, int n) { return UX_GetRoles(b, n); });
    for (auto& ln : SplitLines(rolesTxt)) { size_t p = ln.find(L':'); if (p != std::wstring::npos) { int lvl = _wtoi(ln.substr(p + 1).c_str()); if (lvl > maxLvl) maxLvl = lvl; } }
    int myLvl = 0; auto usersTxt = ReadBufferVia([](wchar_t* b, int n) { return UX_GetUsers(b, n); });
    for (auto& ln : SplitLines(usersTxt)) {
        if (ln.empty()) continue; size_t p1 = ln.find(L','), p2 = ln.find(L',', p1 == std::wstring::npos ? 0 : p1 + 1), p3 = ln.find(L',', p2 == std::wstring::npos ? 0 : p2 + 1);
        std::wstring name = (p1 == std::wstring::npos ? ln : ln.substr(0, p1)); if (name == user) { if (p2 != std::wstring::npos) { std::wstring lvlS = ln.substr(p2 + 1, (p3 == std::wstring::npos ? ln.size() : p3) - (p2 + 1)); myLvl = _wtoi(lvlS.c_str()); } break; }
    }
 
    return (maxLvl > 0) && (myLvl >= maxLvl);
}
 
std::vector<CUserManager2::RoleInfo> CUserManager2::getRoles()
{
    std::vector<RoleInfo> roles;
    auto txt = ReadBufferVia([](wchar_t* b, int n) { return UX_GetRoles(b, n); });
    if (txt.empty()) {
        return roles;
    }
 
    for (auto& line : SplitLines(txt)) {
        if (line.empty()) continue;
        size_t pos = line.find(L':');
        RoleInfo info;
        info.name = (pos == std::wstring::npos) ? line : line.substr(0, pos);
        if (pos != std::wstring::npos) {
            info.level = _wtoi(line.substr(pos + 1).c_str());
        }
 
        if (!info.name.empty()) {
            roles.push_back(std::move(info));
        }
    }
 
    std::sort(roles.begin(), roles.end(), [](const RoleInfo& a, const RoleInfo& b) {
        if (a.level == b.level) {
            return a.name < b.name;
        }
        return a.level > b.level;
    });
 
    return roles;
}
std::vector<CUserManager2::UserInfo> CUserManager2::getUsers()
{
    std::vector<UserInfo> users;
    auto txt = ReadBufferVia([](wchar_t* b, int n) { return UX_GetUsers(b, n); });
    if (txt.empty()) {
        return users;
    }
 
    std::map<int, std::wstring> roleMap;
    for (auto& role : getRoles()) {
        roleMap[role.level] = role.name;
    }
 
    for (auto& line : SplitLines(txt)) {
        if (line.empty()) continue;
        auto parts = SplitByDelimiter(line, L',');
        UserInfo info;
        if (!parts.empty()) info.userName = parts[0];
        if (parts.size() > 1) info.displayName = parts[1];
        if (parts.size() > 2) info.roleLevel = _wtoi(parts[2].c_str());
        if (parts.size() > 3) info.enabled = (_wtoi(parts[3].c_str()) != 0);
        auto it = roleMap.find(info.roleLevel);
        if (it != roleMap.end()) {
            info.roleName = it->second;
        }
        users.push_back(std::move(info));
    }
 
    return users;
}
 
int CUserManager2::addUser(const std::wstring& userName, const std::wstring& displayName,
    const std::wstring& password, const std::wstring& roleName, bool enabled)
{
    int rc = UX_AddUser(userName.c_str(), displayName.c_str(), password.c_str(), roleName.c_str());
    if (rc == UX_OK && !enabled) {
        UX_EnableUser(userName.c_str(), 0);
    }
 
    return rc;
}
 
int CUserManager2::updateUser(const std::wstring& userName, const std::wstring& displayName,
    const std::wstring& password, const std::wstring& roleName, bool enabled)
{
    const wchar_t* disp = displayName.empty() ? nullptr : displayName.c_str();
    const wchar_t* pwd = password.empty() ? nullptr : password.c_str();
    const wchar_t* role = roleName.empty() ? nullptr : roleName.c_str();
    return UX_UpdateUser(userName.c_str(), disp, pwd, role, enabled ? 1 : 0);
}
 
int CUserManager2::deleteUser(const std::wstring& userName)
{
    return UX_DeleteUser(userName.c_str());
}
 
int CUserManager2::setUserEnabled(const std::wstring& userName, bool enabled)
{
    return UX_EnableUser(userName.c_str(), enabled ? 1 : 0);
}
 
int CUserManager2::resetPassword(const std::wstring& userName, const std::wstring& password)
{
    return UX_ResetPassword(userName.c_str(), password.c_str());
}