chenluhua1980
2026-01-19 44360bc2cdeee16be72f9cc4bfb42e0ac26b5b44
SourceCode/Bond/Servo/CUserManager2.cpp
@@ -1,12 +1,34 @@
#include "stdafx.h"
#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>
@@ -19,7 +41,7 @@
   return L"";
}
// 获取单例实例
// 获取单例实例
CUserManager2& CUserManager2::getInstance() {
   static CUserManager2 instance;
   return instance;
@@ -45,10 +67,33 @@
   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";
      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)
@@ -98,3 +143,108 @@
   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());
}