#include "stdafx.h"
|
#include "CUserManager2.h"
|
#include "ToolUnits.h"
|
#include <vector>
|
#include <map>
|
#include <utility>
|
#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\nEngineer: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"Engineer");
|
}
|
}
|
|
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));
|
}
|
}
|
|
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());
|
}
|