| | |
| | | #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> |
| | |
| | | |
| | | 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()); |
| | | } |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |
| | | |