2023-04-13 11:51:03 +08:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
using Aitex.Common.Util;
|
|
|
|
|
using Aitex.Core.RT.Event;
|
|
|
|
|
using Aitex.Core.RT.Log;
|
|
|
|
|
using Aitex.Core.Util;
|
|
|
|
|
using Aitex.Core.Utilities;
|
|
|
|
|
|
|
|
|
|
namespace Aitex.Core.Account
|
|
|
|
|
{
|
2023-08-28 11:30:57 +08:00
|
|
|
|
public class AccountManager : Singleton<AccountManager>
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
#region Variables
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
private readonly Dictionary<string, Tuple<Guid, DateTime, string>> _userList;
|
|
|
|
|
private readonly string _accountPath;
|
|
|
|
|
private readonly string _rolePath;
|
|
|
|
|
private readonly string _viewPath;
|
|
|
|
|
private readonly XmlDocument _accountXml;
|
|
|
|
|
private readonly XmlDocument _roleXml;
|
|
|
|
|
private readonly XmlDocument _viewsXml;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
#endregion
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
#region Constructors
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public AccountManager()
|
|
|
|
|
{
|
|
|
|
|
SerialNumber = "001";
|
|
|
|
|
Module = "System";
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_userList = new Dictionary<string, Tuple<Guid, DateTime, string>>();
|
|
|
|
|
_accountPath = Path.Combine(PathManager.GetAccountFilePath(), "Account.xml");
|
|
|
|
|
_rolePath = Path.Combine(PathManager.GetAccountFilePath(), "Roles.xml");
|
|
|
|
|
_viewPath = Path.Combine(PathManager.GetAccountFilePath(), "Views.xml");
|
|
|
|
|
_accountXml = new XmlDocument();
|
|
|
|
|
_roleXml = new XmlDocument();
|
|
|
|
|
var fileInfo = new FileInfo(_rolePath);
|
|
|
|
|
if (!fileInfo.Directory.Exists)
|
|
|
|
|
{
|
|
|
|
|
fileInfo.Directory.Create();
|
|
|
|
|
}
|
|
|
|
|
if (!fileInfo.Exists)
|
|
|
|
|
{
|
|
|
|
|
_roleXml.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><Aitex><Roles></Roles></Aitex>");
|
|
|
|
|
Save(_roleXml, _rolePath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_roleXml.Load(_rolePath);
|
|
|
|
|
}
|
|
|
|
|
var fileInfo2 = new FileInfo(_accountPath);
|
|
|
|
|
if (!fileInfo2.Directory.Exists)
|
|
|
|
|
{
|
|
|
|
|
fileInfo2.Directory.Create();
|
|
|
|
|
}
|
|
|
|
|
if (!fileInfo2.Exists)
|
|
|
|
|
{
|
|
|
|
|
_accountXml.LoadXml("<?xml version='1.0' encoding='utf-8' ?><AccountManagement></AccountManagement>");
|
|
|
|
|
Save(_accountXml, _accountPath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_accountXml.Load(_accountPath);
|
|
|
|
|
}
|
|
|
|
|
_viewsXml = new XmlDocument();
|
|
|
|
|
fileInfo2 = new FileInfo(_viewPath);
|
|
|
|
|
if (!fileInfo2.Directory.Exists)
|
|
|
|
|
{
|
|
|
|
|
fileInfo2.Directory.Create();
|
|
|
|
|
}
|
|
|
|
|
if (!fileInfo2.Exists)
|
|
|
|
|
{
|
|
|
|
|
_viewsXml.LoadXml("<?xml version='1.0' encoding='utf-8' ?><root><Views></Views></root>");
|
|
|
|
|
Save(_viewsXml, _viewPath);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_viewsXml.Load(_viewPath);
|
|
|
|
|
}
|
|
|
|
|
var text = Path.Combine(PathManager.GetCfgDir(), "RolePermission.xml");
|
|
|
|
|
if (!File.Exists(text))
|
|
|
|
|
{
|
|
|
|
|
var xmlDocument = new XmlDocument();
|
|
|
|
|
xmlDocument.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?><Aitex></Aitex>");
|
|
|
|
|
xmlDocument.Save(text);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
#endregion
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
#region Properties
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public string SerialNumber { get; }
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public string Module { get; }
|
|
|
|
|
|
|
|
|
|
public GetAccountListResult Accounts { get; private set; }
|
|
|
|
|
|
|
|
|
|
#endregion
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-08-28 11:30:57 +08:00
|
|
|
|
#region Methods
|
2023-04-13 11:51:03 +08:00
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public List<Account> GetLoginUserList()
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var list = new List<Account>();
|
|
|
|
|
foreach (var key in _userList.Keys)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var accountInfo = GetAccountInfo(key).AccountInfo;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
accountInfo.LoginIP = _userList[key].Item3;
|
|
|
|
|
list.Add(accountInfo);
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public void RegisterViews(List<string> views)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlNode = _viewsXml.SelectSingleNode("/root/Views");
|
|
|
|
|
foreach (var view in views)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
if (xmlNode.SelectSingleNode($"View[@Name='{view}']") == null)
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlElement = _viewsXml.CreateElement("View");
|
2023-04-13 11:51:03 +08:00
|
|
|
|
xmlElement.SetAttribute("Name", view);
|
|
|
|
|
xmlElement.SetAttribute("Description", view);
|
|
|
|
|
xmlNode.AppendChild(xmlElement);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Save(_viewsXml, _viewPath);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
private void Save(XmlDocument doc, string path)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
doc.Save(path);
|
|
|
|
|
FileSigner.Sign(path);
|
|
|
|
|
GetAccountList();
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public void Logout(string accountId)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
LOG.Write($"用户{accountId}注销登录");
|
|
|
|
|
accountId = accountId.ToLower();
|
|
|
|
|
if (_userList.ContainsKey(accountId))
|
|
|
|
|
{
|
|
|
|
|
_userList.Remove(accountId);
|
|
|
|
|
}
|
|
|
|
|
EV.PostMessage("System", EventEnum.UserLoggedOff, accountId);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex, $"注销用户{accountId}发生异常");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public void Kickout(string accountId, string kickOutReason)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
LOG.Write($"用户{accountId}强制注销登录");
|
|
|
|
|
accountId = accountId.ToLower();
|
|
|
|
|
if (_userList.ContainsKey(accountId))
|
|
|
|
|
{
|
|
|
|
|
EV.PostKickoutMessage($"用户{accountId}强制注销登录,{kickOutReason}");
|
|
|
|
|
_userList.Remove(accountId);
|
|
|
|
|
}
|
|
|
|
|
EV.PostMessage(Module, EventEnum.UserLoggedOff, accountId);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex, $"强制注销用户{accountId}发生异常");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public GetAccountInfoResult GetAccountInfo(string accountId)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
accountId = accountId.ToLower();
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var getAccountInfoResult = new GetAccountInfoResult();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (!FileSigner.IsValid(_accountPath))
|
|
|
|
|
{
|
|
|
|
|
getAccountInfoResult.Description = "账号文件数字签名校验失败";
|
|
|
|
|
getAccountInfoResult.ActSuccess = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var accountNode = GetAccountNode(accountId);
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (accountNode == null)
|
|
|
|
|
{
|
|
|
|
|
if (accountId == "admin")
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var account = new Account
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
Role = "Admin",
|
|
|
|
|
Permission = GetSingleRolePermission("Admin"),
|
|
|
|
|
AccountId = "admin",
|
|
|
|
|
RealName = "admin",
|
|
|
|
|
Email = "admin@admin.com",
|
|
|
|
|
Telephone = "86-21-88886666",
|
|
|
|
|
Touxian = "Admin",
|
|
|
|
|
Company = "MY Tech",
|
|
|
|
|
Department = "IT",
|
|
|
|
|
Description = "Administrator,拥有用户权限修改、菜单修改,定序器修改等权限.",
|
|
|
|
|
AccountStatus = true,
|
|
|
|
|
Md5Pwd = Md5Helper.GetMd5Hash("admin")
|
|
|
|
|
};
|
|
|
|
|
CreateAccount(account);
|
|
|
|
|
getAccountInfoResult.ActSuccess = true;
|
|
|
|
|
getAccountInfoResult.AccountInfo = account;
|
|
|
|
|
getAccountInfoResult.Description = $"成功获取账号信息{accountId}";
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
getAccountInfoResult.Description = $"账号{accountId}不存在";
|
|
|
|
|
getAccountInfoResult.ActSuccess = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
getAccountInfoResult.AccountInfo = new Account
|
|
|
|
|
{
|
|
|
|
|
Role = accountNode.SelectSingleNode("Role").InnerText,
|
2023-08-28 11:30:57 +08:00
|
|
|
|
Permission = GetSingleRolePermission((accountId == "admin")
|
|
|
|
|
? "Admin"
|
|
|
|
|
: accountNode.SelectSingleNode("Role").InnerText),
|
2023-04-13 11:51:03 +08:00
|
|
|
|
AccountId = accountId,
|
|
|
|
|
RealName = accountNode.SelectSingleNode("RealName").InnerText,
|
|
|
|
|
Email = accountNode.SelectSingleNode("Email").InnerText,
|
|
|
|
|
Telephone = accountNode.SelectSingleNode("Telephone").InnerText,
|
|
|
|
|
Touxian = accountNode.SelectSingleNode("Touxian").InnerText,
|
|
|
|
|
Company = accountNode.SelectSingleNode("Company").InnerText,
|
|
|
|
|
Department = accountNode.SelectSingleNode("Department").InnerText,
|
|
|
|
|
Description = accountNode.SelectSingleNode("Description").InnerText,
|
2023-08-28 11:30:57 +08:00
|
|
|
|
AccountStatus = (string.Compare(accountNode.SelectSingleNode("AccountState").InnerText,
|
|
|
|
|
"Enable", ignoreCase: true) == 0),
|
2023-04-13 11:51:03 +08:00
|
|
|
|
AccountCreationTime = accountNode.SelectSingleNode("CreationTime").InnerText,
|
|
|
|
|
LastAccountUpdateTime = accountNode.SelectSingleNode("LastUpdateTime").InnerText,
|
|
|
|
|
LastLoginTime = accountNode.SelectSingleNode("LastLoginTime").InnerText,
|
|
|
|
|
Md5Pwd = accountNode.SelectSingleNode("Password").InnerText
|
|
|
|
|
};
|
|
|
|
|
getAccountInfoResult.Description = $"获取账号{accountId}成功";
|
|
|
|
|
getAccountInfoResult.ActSuccess = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return getAccountInfoResult;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var text = $"获取账号{accountId}发生异常";
|
2023-04-13 11:51:03 +08:00
|
|
|
|
LOG.Write(ex, text);
|
|
|
|
|
return new GetAccountInfoResult
|
|
|
|
|
{
|
|
|
|
|
ActSuccess = false,
|
|
|
|
|
Description = text
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public ChangePwdResult ChangePassword(string accountId, string newPassword)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
LOG.Write($"修改账号{accountId}的密码");
|
|
|
|
|
accountId = accountId.ToLower();
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var changePwdResult = new ChangePwdResult();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (!FileSigner.IsValid(_accountPath))
|
|
|
|
|
{
|
|
|
|
|
changePwdResult.Description = "修改密码失败,账号文件数字签名损坏!";
|
|
|
|
|
changePwdResult.ActSucc = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var accountNode = GetAccountNode(accountId);
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (accountNode == null)
|
|
|
|
|
{
|
|
|
|
|
changePwdResult.Description = $"账号{accountId}不存在";
|
|
|
|
|
changePwdResult.ActSucc = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
accountNode.SelectSingleNode("Password").InnerText = Md5Helper.GetMd5Hash(newPassword);
|
|
|
|
|
Save(_accountXml, _accountPath);
|
|
|
|
|
changePwdResult.Description = "修改密码成功!";
|
|
|
|
|
changePwdResult.ActSucc = true;
|
|
|
|
|
EV.PostMessage(Module, EventEnum.PasswordChanged, accountId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return changePwdResult;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var text = $"修改账号{accountId}的密码失败";
|
2023-04-13 11:51:03 +08:00
|
|
|
|
LOG.Write(ex, text);
|
|
|
|
|
return new ChangePwdResult
|
|
|
|
|
{
|
|
|
|
|
ActSucc = false,
|
|
|
|
|
Description = text
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public CreateAccountResult CreateAccount(Account newAccount)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
LOG.Write($"创建账号{newAccount.AccountId}");
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var createAccountResult = new CreateAccountResult();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (newAccount == null)
|
|
|
|
|
{
|
|
|
|
|
createAccountResult.Description = "账号有误";
|
|
|
|
|
createAccountResult.ActSucc = false;
|
|
|
|
|
}
|
|
|
|
|
else if (!FileSigner.IsValid(_accountPath))
|
|
|
|
|
{
|
|
|
|
|
createAccountResult.Description = $"创建账号失败,数字签名损坏!";
|
|
|
|
|
createAccountResult.ActSucc = false;
|
|
|
|
|
}
|
|
|
|
|
else if (GetAccountNode(newAccount.AccountId) != null)
|
|
|
|
|
{
|
|
|
|
|
createAccountResult.Description = $"创建账号失败,账号 {newAccount.AccountId} 已存在!";
|
|
|
|
|
createAccountResult.ActSucc = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlElement = _accountXml.CreateElement("Account");
|
2023-04-13 11:51:03 +08:00
|
|
|
|
xmlElement.SetAttribute("AccountId", newAccount.AccountId.ToLower());
|
|
|
|
|
_accountXml.DocumentElement.AppendChild(xmlElement);
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlElement2 = _accountXml.CreateElement("RealName");
|
2023-04-13 11:51:03 +08:00
|
|
|
|
xmlElement2.InnerText = newAccount.RealName;
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
xmlElement2 = _accountXml.CreateElement("Role");
|
|
|
|
|
xmlElement2.InnerText = newAccount.Role.ToString();
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
xmlElement2 = _accountXml.CreateElement("Password");
|
|
|
|
|
xmlElement2.InnerText = Md5Helper.GetMd5Hash(newAccount.AccountId);
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
xmlElement2 = _accountXml.CreateElement("AccountState");
|
|
|
|
|
xmlElement2.InnerText = (newAccount.AccountStatus ? "Enable" : "Disable");
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
xmlElement2 = _accountXml.CreateElement("Email");
|
|
|
|
|
xmlElement2.InnerText = newAccount.Email;
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
xmlElement2 = _accountXml.CreateElement("Telephone");
|
|
|
|
|
xmlElement2.InnerText = newAccount.Telephone;
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
xmlElement2 = _accountXml.CreateElement("Touxian");
|
|
|
|
|
xmlElement2.InnerText = newAccount.Touxian;
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
xmlElement2 = _accountXml.CreateElement("Company");
|
|
|
|
|
xmlElement2.InnerText = newAccount.Company;
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
xmlElement2 = _accountXml.CreateElement("Department");
|
|
|
|
|
xmlElement2.InnerText = newAccount.Department;
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
xmlElement2 = _accountXml.CreateElement("Description");
|
|
|
|
|
xmlElement2.InnerText = newAccount.Description;
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
xmlElement2 = _accountXml.CreateElement("CreationTime");
|
|
|
|
|
xmlElement2.InnerText = DateTime.Now.ToString();
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
xmlElement2 = _accountXml.CreateElement("LastLoginTime");
|
|
|
|
|
xmlElement2.InnerText = string.Empty;
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
xmlElement2 = _accountXml.CreateElement("LastUpdateTime");
|
|
|
|
|
xmlElement2.InnerText = string.Empty;
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
|
|
|
|
Save(_accountXml, _accountPath);
|
|
|
|
|
createAccountResult.Description = $"创建新账号{newAccount.AccountId}成功";
|
|
|
|
|
createAccountResult.ActSucc = true;
|
|
|
|
|
EV.PostMessage(Module, EventEnum.AccountCreated, newAccount.AccountId);
|
|
|
|
|
}
|
|
|
|
|
return createAccountResult;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var text = $"创建账号{newAccount.AccountId}失败";
|
2023-04-13 11:51:03 +08:00
|
|
|
|
LOG.Write(ex, text);
|
|
|
|
|
return new CreateAccountResult
|
|
|
|
|
{
|
|
|
|
|
ActSucc = false,
|
|
|
|
|
Description = text
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public DeleteAccountResult DeleteAccount(string accountId)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
LOG.Write($"删除账号{accountId}");
|
|
|
|
|
accountId = accountId.ToLower();
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var deleteAccountResult = new DeleteAccountResult();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (accountId == "admin")
|
|
|
|
|
{
|
|
|
|
|
deleteAccountResult.Description = "Admin'admin'账号不能删除";
|
|
|
|
|
deleteAccountResult.ActSucc = false;
|
|
|
|
|
}
|
|
|
|
|
else if (!FileSigner.IsValid(_accountPath))
|
|
|
|
|
{
|
|
|
|
|
deleteAccountResult.Description = "删除账号失败,账号文件数字签名损坏!";
|
|
|
|
|
deleteAccountResult.ActSucc = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var accountNode = GetAccountNode(accountId);
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (accountNode == null)
|
|
|
|
|
{
|
|
|
|
|
deleteAccountResult.Description = $"删除账号 {accountId} 失败,账号不存在!";
|
|
|
|
|
deleteAccountResult.ActSucc = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_accountXml.DocumentElement.RemoveChild(accountNode);
|
|
|
|
|
Save(_accountXml, _accountPath);
|
|
|
|
|
deleteAccountResult.Description = $"删除账号 {accountId} 成功!";
|
|
|
|
|
deleteAccountResult.ActSucc = true;
|
|
|
|
|
EV.PostMessage(Module, EventEnum.AccountDeleted, accountId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return deleteAccountResult;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var text = $"删除账号{accountId}发生异常";
|
2023-04-13 11:51:03 +08:00
|
|
|
|
LOG.Write(ex, text);
|
|
|
|
|
return new DeleteAccountResult
|
|
|
|
|
{
|
|
|
|
|
ActSucc = false,
|
|
|
|
|
Description = text
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public UpdateAccountResult UpdateAccount(Account account)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var updateAccountResult = new UpdateAccountResult();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (account == null)
|
|
|
|
|
{
|
|
|
|
|
updateAccountResult.Description = "账号有误";
|
|
|
|
|
updateAccountResult.ActSucc = false;
|
|
|
|
|
}
|
|
|
|
|
else if (!FileSigner.IsValid(_accountPath))
|
|
|
|
|
{
|
|
|
|
|
updateAccountResult.Description = $"更新账号资料失败,账号文件数字签名损坏!";
|
|
|
|
|
updateAccountResult.ActSucc = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var accountNode = GetAccountNode(account.AccountId.ToLower());
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (accountNode == null)
|
|
|
|
|
{
|
|
|
|
|
updateAccountResult.Description = $"更新账号 {account.AccountId} 失败,账号不存在!";
|
|
|
|
|
updateAccountResult.ActSucc = false;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
accountNode.SelectSingleNode("RealName").InnerText = account.RealName;
|
|
|
|
|
accountNode.SelectSingleNode("Role").InnerText = ((account.AccountId.ToLower() == "admin") ? "Admin" : account.Role.ToString());
|
|
|
|
|
accountNode.SelectSingleNode("AccountState").InnerText = (account.AccountStatus ? "Enable" : "Disable");
|
|
|
|
|
accountNode.SelectSingleNode("Email").InnerText = account.Email;
|
|
|
|
|
accountNode.SelectSingleNode("Telephone").InnerText = account.Telephone;
|
|
|
|
|
accountNode.SelectSingleNode("Touxian").InnerText = account.Touxian;
|
|
|
|
|
accountNode.SelectSingleNode("Company").InnerText = account.Company;
|
|
|
|
|
accountNode.SelectSingleNode("Department").InnerText = account.Department;
|
|
|
|
|
accountNode.SelectSingleNode("Description").InnerText = account.Description;
|
|
|
|
|
accountNode.SelectSingleNode("CreationTime").InnerText = account.AccountCreationTime;
|
|
|
|
|
accountNode.SelectSingleNode("LastLoginTime").InnerText = account.LastLoginTime;
|
|
|
|
|
accountNode.SelectSingleNode("LastUpdateTime").InnerText = account.LastAccountUpdateTime;
|
|
|
|
|
Save(_accountXml, _accountPath);
|
|
|
|
|
updateAccountResult.Description = $"成功更新 {account.AccountId} 的账号资料!";
|
|
|
|
|
updateAccountResult.ActSucc = true;
|
|
|
|
|
EV.PostMessage(Module, EventEnum.AccountChanged, account.AccountId);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return updateAccountResult;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var text = $"更新账号{account.AccountId}资料发生异常";
|
2023-04-13 11:51:03 +08:00
|
|
|
|
LOG.Write(ex, text);
|
|
|
|
|
return new UpdateAccountResult
|
|
|
|
|
{
|
|
|
|
|
ActSucc = false,
|
|
|
|
|
Description = text
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public GetAccountListResult GetAccountList()
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
LOG.Write("获取所有的账号信息列表");
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var getAccountListResult = new GetAccountListResult();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (!FileSigner.IsValid(_accountPath))
|
|
|
|
|
{
|
|
|
|
|
getAccountListResult.Description = "获取账号列表失败,账号文件数字签名文件损坏!";
|
|
|
|
|
getAccountListResult.ActSuccess = false;
|
|
|
|
|
getAccountListResult.AccountList = null;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlNodeList = _accountXml.SelectNodes("AccountManagement/Account");
|
|
|
|
|
var list = new List<Account>();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
foreach (XmlNode item in xmlNodeList)
|
|
|
|
|
{
|
|
|
|
|
list.Add(GetAccountInfo(item.Attributes["AccountId"].Value).AccountInfo);
|
|
|
|
|
}
|
|
|
|
|
getAccountListResult.AccountList = list;
|
|
|
|
|
getAccountListResult.Description = "成功获取账号列表!";
|
|
|
|
|
getAccountListResult.ActSuccess = true;
|
|
|
|
|
}
|
|
|
|
|
Accounts = getAccountListResult;
|
|
|
|
|
return getAccountListResult;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var text = "获取账号列表发生异常";
|
2023-04-13 11:51:03 +08:00
|
|
|
|
LOG.Write(ex, text);
|
|
|
|
|
return new GetAccountListResult
|
|
|
|
|
{
|
|
|
|
|
AccountList = null,
|
|
|
|
|
ActSuccess = false,
|
|
|
|
|
Description = text
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public void CheckAlive(string accountId)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (_userList.ContainsKey(accountId))
|
|
|
|
|
{
|
|
|
|
|
_userList[accountId] = new Tuple<Guid, DateTime, string>(_userList[accountId].Item1, DateTime.Now, _userList[accountId].Item3);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
EV.PostKickoutMessage($"账号{accountId}已在服务器上注销");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
private XmlElement GetAccountNode(string accountId)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlNode = _accountXml.SelectSingleNode($"/AccountManagement/Account[@AccountId='{accountId.ToLower()}']");
|
2023-04-13 11:51:03 +08:00
|
|
|
|
return (XmlElement)xmlNode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static SerializableDictionary<string, string> GetAllViewList()
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var serializableDictionary = new SerializableDictionary<string, string>();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlDocument = new XmlDocument();
|
|
|
|
|
var filename = Path.Combine(PathManager.GetAccountFilePath(), "Views.xml");
|
2023-04-13 11:51:03 +08:00
|
|
|
|
xmlDocument.Load(filename);
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlNodeList = xmlDocument.SelectNodes("/root/Views/View");
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (xmlNodeList != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (XmlElement item in xmlNodeList)
|
|
|
|
|
{
|
|
|
|
|
serializableDictionary.Add(item.Attributes["Name"].Value, item.Attributes["Description"].Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex);
|
|
|
|
|
serializableDictionary = new SerializableDictionary<string, string>();
|
|
|
|
|
}
|
|
|
|
|
return serializableDictionary;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public bool SaveAllRolesPermission(Dictionary<string, Dictionary<string, ViewPermission>> data)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlElement = _roleXml.SelectSingleNode("/Aitex/Roles") as XmlElement;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
xmlElement.RemoveAll();
|
2023-08-28 10:30:53 +08:00
|
|
|
|
foreach (var datum in data)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
if (datum.Key == "Admin")
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlElement2 = _roleXml.CreateElement("Role");
|
2023-04-13 11:51:03 +08:00
|
|
|
|
xmlElement2.SetAttribute("Name", datum.Key);
|
|
|
|
|
xmlElement.AppendChild(xmlElement2);
|
2023-08-28 10:30:53 +08:00
|
|
|
|
foreach (var key in data[datum.Key].Keys)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlElement3 = _roleXml.CreateElement("View");
|
2023-04-13 11:51:03 +08:00
|
|
|
|
xmlElement2.AppendChild(xmlElement3);
|
|
|
|
|
xmlElement3.SetAttribute("Name", key);
|
|
|
|
|
xmlElement3.SetAttribute("Permission", data[datum.Key][key].ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
_roleXml.Save(_rolePath);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public IEnumerable<string> GetAllRoles()
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var list = new List<string>();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlNodeList = _roleXml.SelectNodes("/Aitex/Roles/Role");
|
2023-04-13 11:51:03 +08:00
|
|
|
|
foreach (XmlElement item in xmlNodeList)
|
|
|
|
|
{
|
|
|
|
|
list.Add(item.Attributes["Name"].Value);
|
|
|
|
|
}
|
|
|
|
|
if (!list.Contains("Admin"))
|
|
|
|
|
{
|
|
|
|
|
list.Add("Admin");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex);
|
|
|
|
|
list = new List<string>();
|
|
|
|
|
}
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public SerializableDictionary<string, ViewPermission> GetSingleRolePermission(string roleName)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var serializableDictionary = new SerializableDictionary<string, ViewPermission>();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var allViewList = GetAllViewList();
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (roleName == "Admin")
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
foreach (var item in allViewList)
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
serializableDictionary.Add(item.Key, ViewPermission.FullyControl);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var xmlNode = _roleXml.SelectSingleNode($"/Aitex/Roles/Role[@Name='{roleName}']");
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (xmlNode != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (XmlElement item2 in xmlNode)
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var value = item2.Attributes["Name"].Value;
|
|
|
|
|
var value2 = item2.Attributes["Permission"].Value;
|
2023-04-13 11:51:03 +08:00
|
|
|
|
if (allViewList.ContainsKey(value))
|
|
|
|
|
{
|
|
|
|
|
serializableDictionary.Add(value, (ViewPermission)Enum.Parse(typeof(ViewPermission), value2, ignoreCase: true));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex);
|
|
|
|
|
serializableDictionary = new SerializableDictionary<string, ViewPermission>();
|
|
|
|
|
}
|
|
|
|
|
return serializableDictionary;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-28 10:30:53 +08:00
|
|
|
|
public SerializableDictionary<string, SerializableDictionary<string, ViewPermission>> GetAllRolesPermission()
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-08-28 10:30:53 +08:00
|
|
|
|
var serializableDictionary = new SerializableDictionary<string, SerializableDictionary<string, ViewPermission>>();
|
|
|
|
|
foreach (var allRole in GetAllRoles())
|
2023-04-13 11:51:03 +08:00
|
|
|
|
{
|
|
|
|
|
serializableDictionary.Add(allRole, GetSingleRolePermission(allRole));
|
|
|
|
|
}
|
|
|
|
|
return serializableDictionary;
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
LOG.Write(ex);
|
|
|
|
|
return new SerializableDictionary<string, SerializableDictionary<string, ViewPermission>>();
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-28 11:30:57 +08:00
|
|
|
|
|
|
|
|
|
#endregion
|
2023-04-13 11:51:03 +08:00
|
|
|
|
}
|
|
|
|
|
}
|