AccountManager.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.IO;
  5. using System.Reflection;
  6. using System.Security.Cryptography;
  7. using System.Text;
  8. using System.Xml;
  9. using Aitex.Common.Util;
  10. using Aitex.Core.Util;
  11. using Aitex.Core.RT.Log;
  12. using Aitex.Core.Utilities;
  13. using Aitex.Core.RT.Event;
  14. namespace Aitex.Core.Account
  15. {
  16. public sealed class AccountManager
  17. {
  18. static Dictionary<string, Tuple<Guid, DateTime,string>> _userList; //已登录用户和客户端Guid之间的映射表
  19. private static string _accountPath;
  20. private static string _rolePath; //账号信息所对应的XML文件路径
  21. private static string _viewPath;
  22. private static XmlDocument _accountXml; //"Account.xml"
  23. private static XmlDocument _roleXml; //"Roles.xml"
  24. private static XmlDocument _viewsXml;
  25. const int MAX_LOGIN_USER_NUM = 16;
  26. public static string SerialNumber { get; private set; }
  27. public static string Module { get; private set; }
  28. /// <summary>
  29. /// 静态构造函数
  30. /// </summary>
  31. static AccountManager()
  32. {
  33. SerialNumber = "001";
  34. Module = "System";
  35. try
  36. {
  37. _userList = new Dictionary<string, Tuple<Guid, DateTime, string>>();
  38. _accountPath = Path.Combine(PathManager.GetAccountFilePath(), "Account.xml");
  39. _rolePath = Path.Combine(PathManager.GetAccountFilePath(), "Roles.xml");
  40. _viewPath = Path.Combine(PathManager.GetAccountFilePath(), "Views.xml");
  41. _accountXml = new XmlDocument();
  42. _roleXml = new XmlDocument();
  43. //检查Roles.xml是否存在,如果不存在则自动创建
  44. FileInfo roleFileInfo = new System.IO.FileInfo(_rolePath);
  45. if (!roleFileInfo.Directory.Exists)
  46. roleFileInfo.Directory.Create();
  47. if (!roleFileInfo.Exists)
  48. {
  49. _roleXml.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\"?><Aitex><Roles></Roles></Aitex>");
  50. Save(_roleXml, _rolePath);
  51. }
  52. else
  53. {
  54. _roleXml.Load(_rolePath);
  55. }
  56. //检查Account.xml文件是否存在,如果不存在则自动创建
  57. FileInfo fileInfo = new System.IO.FileInfo(_accountPath);
  58. if (!fileInfo.Directory.Exists)
  59. fileInfo.Directory.Create();
  60. if (!fileInfo.Exists)
  61. {
  62. _accountXml.LoadXml("<?xml version='1.0' encoding='utf-8' ?><AccountManagement></AccountManagement>");
  63. Save(_accountXml, _accountPath);
  64. }
  65. else
  66. {
  67. _accountXml.Load(_accountPath);
  68. }
  69. //检查views.xml文件是否存在,如果不存在则自动创建
  70. _viewsXml = new XmlDocument();
  71. fileInfo = new System.IO.FileInfo(_viewPath);
  72. if (!fileInfo.Directory.Exists)
  73. fileInfo.Directory.Create();
  74. if (!fileInfo.Exists)
  75. {
  76. _viewsXml.LoadXml("<?xml version='1.0' encoding='utf-8' ?><root><Views></Views></root>");
  77. Save(_viewsXml, _viewPath);
  78. }
  79. else
  80. {
  81. _viewsXml.Load(_viewPath);
  82. }
  83. string recipePermissionFile = System.IO.Path.Combine(PathManager.GetCfgDir(), "RolePermission.xml");
  84. if (!File.Exists(recipePermissionFile))
  85. {
  86. XmlDocument xmlRecipeFormat = new XmlDocument();
  87. xmlRecipeFormat.LoadXml("<?xml version=\"1.0\" encoding=\"utf-8\" ?><Aitex></Aitex>");
  88. xmlRecipeFormat.Save(recipePermissionFile);
  89. }
  90. }
  91. catch (Exception ex)
  92. {
  93. //LOG.Write(ex);
  94. }
  95. }
  96. /// <summary>
  97. /// 获取当前登录的用户列表
  98. /// </summary>
  99. /// <returns></returns>
  100. public static List<Account> GetLoginUserList()
  101. {
  102. List<Account> userList = new List<Account>();
  103. foreach (var accountId in _userList.Keys)
  104. {
  105. Account temp = GetAccountInfo(accountId).AccountInfo;
  106. temp.LoginIP = _userList[accountId].Item3;
  107. userList.Add(temp);
  108. }
  109. return userList;
  110. }
  111. public static void RegisterViews(List<string> views)
  112. {
  113. try
  114. {
  115. var nodes = _viewsXml.SelectSingleNode("/root/Views");
  116. foreach (var view in views)
  117. {
  118. if (nodes.SelectSingleNode(string.Format("View[@Name='{0}']", view)) != null)
  119. continue;
  120. XmlElement node = _viewsXml.CreateElement("View");
  121. node.SetAttribute("Name", view);
  122. node.SetAttribute("Description", view);
  123. nodes.AppendChild(node);
  124. }
  125. Save(_viewsXml, _viewPath);
  126. }
  127. catch (Exception ex)
  128. {
  129. //LOG.Write(ex);
  130. }
  131. }
  132. /// <summary>
  133. /// add xml signature here, during xml save
  134. /// </summary>
  135. private static void Save(XmlDocument doc, string path)
  136. {
  137. doc.Save(path); //write to xml file
  138. FileSigner.Sign(path); //write xml signature
  139. GetAccountList();
  140. }
  141. /// <summary>
  142. /// user login verify
  143. /// </summary>
  144. /// <param name="accountId"></param>
  145. /// <param name="password"></param>
  146. /// <returns></returns>
  147. public static LoginResult Login(string accountId, string accountPwd)
  148. {
  149. try
  150. {
  151. //LOG.Write(string.Format("用户{0}尝试登录系统", accountId));
  152. accountId = accountId.ToLower(); //账号大小写不敏感,先行转换为小写
  153. var ret = new LoginResult();
  154. if (accountId == "su" && accountPwd == "su") //判断是否为固定的秘密账号
  155. {
  156. ret.ActSucc = true;
  157. ret.AccountInfo = GetAccountInfo("admin").AccountInfo;
  158. ret.SessionId = Guid.NewGuid().ToString();
  159. }
  160. else if (!FileSigner.IsValid(_accountPath))
  161. {
  162. ret.Description = "File signer corrupt";
  163. ret.ActSucc = false;
  164. }
  165. else if (_userList.ContainsKey(accountId) )
  166. {
  167. ret.ActSucc = false;
  168. ret.Description = string.Format("account {0} already login", accountId);
  169. }
  170. else if (_userList.Count >= MAX_LOGIN_USER_NUM && accountId != "admin")
  171. {
  172. ret.ActSucc = false;
  173. ret.Description = string.Format("more than {0} users login", MAX_LOGIN_USER_NUM);
  174. }
  175. else
  176. {
  177. var account = GetAccountInfo(accountId).AccountInfo;
  178. if (account == null)
  179. {
  180. ret.ActSucc = false;
  181. ret.Description = string.Format("{0} not exist", accountId);
  182. }
  183. else if (account.Md5Pwd != Md5Helper.GetMd5Hash(accountPwd)
  184. && (account.Role != "Admin" || accountPwd != Md5Helper.GenerateDynamicPassword(SerialNumber))) //检查账号密码是否正确
  185. {
  186. ret.ActSucc = false;
  187. ret.Description = string.Format("password error");
  188. }
  189. else if (!account.AccountStatus)
  190. {
  191. ret.ActSucc = false;
  192. ret.Description = string.Format("account {0} is disabled", accountId);
  193. }
  194. else
  195. {
  196. //if(accountId != "admin" && accountId != "su")
  197. _userList.Add(accountId, new Tuple<Guid, DateTime, string>(NotificationService.ClientGuid, DateTime.Now, NotificationService.ClientHostName));
  198. ret.ActSucc = true;
  199. ret.Description = string.Format("{0} login succeed", accountId);
  200. ret.AccountInfo = account;
  201. ret.SessionId = Guid.NewGuid().ToString();
  202. EV.PostMessage(Module, EventEnum.UserLoggedIn, accountId);
  203. }
  204. }
  205. return ret;
  206. }
  207. catch (Exception ex)
  208. {
  209. string msg = string.Format("account system inner exception",accountId);
  210. //LOG.Write(ex, msg);
  211. return new LoginResult() { ActSucc = false, Description = msg };
  212. }
  213. }
  214. /// <summary>
  215. /// 用户注销
  216. /// </summary>
  217. /// <param name="accountId"></param>
  218. public static void Logout(string accountId)
  219. {
  220. try
  221. {
  222. //LOG.Write(string.Format("用户{0}注销登录", accountId));
  223. LOG.Write(eEvent.EV_LOGOFF_INFO, MECF.Framework.Common.Equipment.ModuleName.System, accountId);
  224. accountId = accountId.ToLower();
  225. if (_userList.ContainsKey(accountId))
  226. {
  227. _userList.Remove(accountId);
  228. }
  229. EV.PostMessage("System", EventEnum.UserLoggedOff, accountId);
  230. }
  231. catch
  232. {
  233. LOG.Write(eEvent.EV_LOGOFF_INFO, MECF.Framework.Common.Equipment.ModuleName.System, accountId);
  234. //LOG.Write(ex, string.Format("注销用户{0}发生异常", accountId));
  235. }
  236. }
  237. /// <summary>
  238. /// 用户被强制注销的理由
  239. /// </summary>
  240. /// <param name="accountId"></param>
  241. /// <param name="kickOutReason"></param>
  242. public static void Kickout(string accountId, string kickOutReason)
  243. {
  244. try
  245. {
  246. //LOG.Write(string.Format("用户{0}强制注销登录", accountId));
  247. accountId = accountId.ToLower();
  248. if (_userList.ContainsKey(accountId))
  249. {
  250. EV.PostKickoutMessage(string.Format("用户{0}强制注销登录,{1}", accountId,kickOutReason));
  251. _userList.Remove(accountId);
  252. }
  253. EV.PostMessage(Module, EventEnum.UserLoggedOff, accountId);
  254. }
  255. catch (Exception ex)
  256. {
  257. //LOG.Write(ex, string.Format("强制注销用户{0}发生异常", accountId));
  258. }
  259. }
  260. /// <summary>
  261. /// 返回指定用户的账号信息
  262. /// </summary>
  263. /// <param name="accountId"></param>
  264. /// <returns></returns>
  265. public static GetAccountInfoResult GetAccountInfo(string accountId)
  266. {
  267. try
  268. {
  269. //LOG.Write(string.Format("获取账号信息{0}", accountId));
  270. accountId = accountId.ToLower(); //账号转小写
  271. GetAccountInfoResult ret = new GetAccountInfoResult();
  272. if (!FileSigner.IsValid(_accountPath)) //检查账号文件的数字签名
  273. {
  274. ret.Description = "账号文件数字签名校验失败";
  275. ret.ActSuccess = false;
  276. }
  277. else
  278. {
  279. XmlElement userNode = GetAccountNode(accountId);
  280. if (userNode == null)
  281. {
  282. if (accountId == "admin") //如果没有admin账号,则创建默认的admin账号
  283. {
  284. Account adminAccount = new Account()
  285. {
  286. Role = "Admin",
  287. Permission = GetSingleRolePermission("Admin"),
  288. AccountId = "admin",
  289. RealName = "admin",
  290. Email = "admin@admin.com",
  291. Telephone = "86-21-88886666",
  292. Touxian = "Admin",
  293. Company = "MY Tech",
  294. Department = "IT",
  295. Description = "Administrator,拥有用户权限修改、菜单修改,定序器修改等权限。",
  296. AccountStatus = true,
  297. Md5Pwd = Md5Helper.GetMd5Hash("admin")
  298. };
  299. CreateAccount(adminAccount);
  300. ret.ActSuccess = true;
  301. ret.AccountInfo = adminAccount;
  302. ret.Description = string.Format("成功获取账号信息{0}", accountId);
  303. }
  304. else
  305. {
  306. ret.Description = string.Format("账号{0}不存在", accountId);
  307. ret.ActSuccess = false;
  308. }
  309. }
  310. else
  311. {
  312. ret.AccountInfo = new Account
  313. {
  314. Role = userNode.SelectSingleNode("Role").InnerText,
  315. Permission = GetSingleRolePermission(accountId == "admin" ? "Admin" : userNode.SelectSingleNode("Role").InnerText),
  316. AccountId = accountId,
  317. RealName = userNode.SelectSingleNode("RealName").InnerText,
  318. Email = userNode.SelectSingleNode("Email").InnerText,
  319. Telephone = userNode.SelectSingleNode("Telephone").InnerText,
  320. Touxian = userNode.SelectSingleNode("Touxian").InnerText,
  321. Company = userNode.SelectSingleNode("Company").InnerText,
  322. Department = userNode.SelectSingleNode("Department").InnerText,
  323. Description = userNode.SelectSingleNode("Description").InnerText,
  324. AccountStatus = (0 == String.Compare(userNode.SelectSingleNode("AccountState").InnerText, "Enable", true)),
  325. AccountCreationTime = userNode.SelectSingleNode("CreationTime").InnerText,
  326. LastAccountUpdateTime = userNode.SelectSingleNode("LastUpdateTime").InnerText,
  327. LastLoginTime = userNode.SelectSingleNode("LastLoginTime").InnerText,
  328. Md5Pwd = userNode.SelectSingleNode("Password").InnerText,
  329. };
  330. ret.Description = string.Format("获取账号{0}成功", accountId);
  331. ret.ActSuccess = true;
  332. }
  333. }
  334. return ret;
  335. }
  336. catch
  337. {
  338. string msg = string.Format("获取账号{0}发生异常", accountId);
  339. //LOG.Write(ex, msg);
  340. return new GetAccountInfoResult() { ActSuccess = false, Description = msg };
  341. }
  342. }
  343. /// <summary>
  344. /// change account password
  345. /// </summary>
  346. /// <param name="accountId"></param>
  347. /// <param name="newPassword"></param>
  348. public static ChangePwdResult ChangePassword(string accountId, string newPassword)
  349. {
  350. try
  351. {
  352. //LOG.Write(string.Format("修改账号{0}的密码", accountId));
  353. accountId = accountId.ToLower();
  354. ChangePwdResult ret = new ChangePwdResult();
  355. if (!FileSigner.IsValid(_accountPath)) //检查账号文件的数字签名
  356. {
  357. ret.Description = "修改密码失败,账号文件数字签名损坏!";
  358. ret.ActSucc = false;
  359. }
  360. else
  361. {
  362. XmlElement userNode = GetAccountNode(accountId);
  363. if (userNode == null)
  364. {
  365. ret.Description = string.Format("账号{0}不存在", accountId);
  366. ret.ActSucc = false;
  367. }
  368. else
  369. {
  370. userNode.SelectSingleNode("Password").InnerText = Md5Helper.GetMd5Hash(newPassword);
  371. Save(_accountXml, _accountPath);
  372. ret.Description = "修改密码成功!";
  373. ret.ActSucc = true;
  374. EV.PostMessage(Module, EventEnum.PasswordChanged, accountId);
  375. }
  376. }
  377. return ret;
  378. }
  379. catch (Exception ex)
  380. {
  381. var msg = string.Format("修改账号{0}的密码失败", accountId);
  382. //LOG.Write(ex, msg);
  383. LOG.Write(eEvent.ERR_UPDATE_USER_ERROR, MECF.Framework.Common.Equipment.ModuleName.System, accountId);
  384. return new ChangePwdResult() { ActSucc = false, Description = msg };
  385. }
  386. }
  387. /// <summary>
  388. /// create account
  389. /// </summary>
  390. /// <param name="newAccount"></param>
  391. /// <returns></returns>
  392. public static CreateAccountResult CreateAccount(Account newAccount)
  393. {
  394. try
  395. {
  396. //LOG.Write(string.Format("创建账号{0}", newAccount.AccountId));
  397. CreateAccountResult ret = new CreateAccountResult();
  398. if (newAccount == null)
  399. {
  400. ret.Description = "账号有误";
  401. ret.ActSucc = false;
  402. }
  403. else if (!FileSigner.IsValid(_accountPath)) //account xml file signer verify
  404. {
  405. ret.Description = string.Format("创建账号失败,数字签名损坏!");
  406. ret.ActSucc = false;
  407. }
  408. else
  409. {
  410. if (GetAccountNode(newAccount.AccountId) != null) //account has been existed
  411. {
  412. ret.Description = string.Format("创建账号失败,账号 {0} 已存在!", newAccount.AccountId);
  413. ret.ActSucc = false;
  414. }
  415. else
  416. {
  417. XmlElement userNode = _accountXml.CreateElement("Account");
  418. userNode.SetAttribute("AccountId", newAccount.AccountId.ToLower());
  419. _accountXml.DocumentElement.AppendChild(userNode);
  420. XmlElement subNode = _accountXml.CreateElement("RealName");
  421. subNode.InnerText = newAccount.RealName;
  422. userNode.AppendChild(subNode);
  423. subNode = _accountXml.CreateElement("Role");
  424. subNode.InnerText = newAccount.Role.ToString();
  425. userNode.AppendChild(subNode);
  426. subNode = _accountXml.CreateElement("Password");
  427. subNode.InnerText = Md5Helper.GetMd5Hash(newAccount.AccountId);//default new create account's password same as accountId
  428. userNode.AppendChild(subNode);
  429. subNode = _accountXml.CreateElement("AccountState");//defualt new create account's state "Enable"
  430. subNode.InnerText = newAccount.AccountStatus ? "Enable" : "Disable";
  431. userNode.AppendChild(subNode);
  432. subNode = _accountXml.CreateElement("Email");
  433. subNode.InnerText = newAccount.Email;
  434. userNode.AppendChild(subNode);
  435. subNode = _accountXml.CreateElement("Telephone");
  436. subNode.InnerText = newAccount.Telephone;
  437. userNode.AppendChild(subNode);
  438. subNode = _accountXml.CreateElement("Touxian");
  439. subNode.InnerText = newAccount.Touxian;
  440. userNode.AppendChild(subNode);
  441. subNode = _accountXml.CreateElement("Company");
  442. subNode.InnerText = newAccount.Company;
  443. userNode.AppendChild(subNode);
  444. subNode = _accountXml.CreateElement("Department");
  445. subNode.InnerText = newAccount.Department;
  446. userNode.AppendChild(subNode);
  447. subNode = _accountXml.CreateElement("Description");
  448. subNode.InnerText = newAccount.Description;
  449. userNode.AppendChild(subNode);
  450. subNode = _accountXml.CreateElement("CreationTime");
  451. subNode.InnerText = DateTime.Now.ToString();
  452. userNode.AppendChild(subNode);
  453. subNode = _accountXml.CreateElement("LastLoginTime");
  454. subNode.InnerText = string.Empty;
  455. userNode.AppendChild(subNode);
  456. subNode = _accountXml.CreateElement("LastUpdateTime");
  457. subNode.InnerText = string.Empty;
  458. userNode.AppendChild(subNode);
  459. Save(_accountXml, _accountPath);//save to xml file
  460. ret.Description = string.Format("创建新账号{0}成功", newAccount.AccountId);
  461. ret.ActSucc = true;
  462. EV.PostMessage(Module, EventEnum.AccountCreated, newAccount.AccountId);
  463. }
  464. }
  465. return ret;
  466. }
  467. catch
  468. {
  469. var msg = string.Format("创建账号{0}失败", newAccount.AccountId);
  470. //LOG.Write(ex, msg);
  471. return new CreateAccountResult() { ActSucc = false, Description = msg };
  472. }
  473. }
  474. /// <summary>
  475. /// Administrator user calls this method to delete an account.
  476. /// </summary>
  477. /// <param name="account"></param>
  478. /// <returns></returns>
  479. public static DeleteAccountResult DeleteAccount(string accountId)
  480. {
  481. try
  482. {
  483. //LOG.Write(string.Format("删除账号{0}", accountId));
  484. accountId = accountId.ToLower();
  485. DeleteAccountResult ret = new DeleteAccountResult();
  486. if (accountId == "admin")
  487. {
  488. ret.Description = "Admin\'admin\'账号不能删除";
  489. ret.ActSucc = false;
  490. }
  491. else if (!FileSigner.IsValid(_accountPath))//account xml file signer verify
  492. {
  493. ret.Description = "删除账号失败,账号文件数字签名损坏!";
  494. ret.ActSucc = false;
  495. }
  496. else
  497. {
  498. XmlElement accountNode = GetAccountNode(accountId);
  499. if (accountNode == null)//account has been existed
  500. {
  501. ret.Description = string.Format("删除账号 {0} 失败,账号不存在!", accountId);
  502. ret.ActSucc = false;
  503. }
  504. else
  505. {
  506. _accountXml.DocumentElement.RemoveChild(accountNode);//remove account node
  507. Save(_accountXml, _accountPath);//save to xml file
  508. ret.Description = string.Format("删除账号 {0} 成功!", accountId);
  509. ret.ActSucc = true;
  510. EV.PostMessage(Module, EventEnum.AccountDeleted, accountId);
  511. }
  512. }
  513. return ret;
  514. }
  515. catch (Exception ex)
  516. {
  517. var msg = string.Format("删除账号{0}发生异常", accountId);
  518. LOG.Write(eEvent.ERR_DELETE_USER_ERROR, MECF.Framework.Common.Equipment.ModuleName.System, accountId);
  519. //LOG.Write(ex, msg);
  520. return new DeleteAccountResult() { ActSucc = false, Description = msg };
  521. }
  522. }
  523. /// <summary>
  524. /// Update account information
  525. /// </summary>
  526. /// <param name="accountList"></param>
  527. /// <returns></returns>
  528. public static UpdateAccountResult UpdateAccount(Account account)
  529. {
  530. try
  531. {
  532. UpdateAccountResult ret = new UpdateAccountResult();
  533. if (account == null)
  534. {
  535. ret.Description = "账号有误";
  536. ret.ActSucc = false;
  537. }
  538. else if (!FileSigner.IsValid(_accountPath)) //account xml file signer verify
  539. {
  540. ret.Description = string.Format("更新账号资料失败,账号文件数字签名损坏!");
  541. ret.ActSucc = false;
  542. }
  543. else
  544. {
  545. XmlElement userNode = GetAccountNode(account.AccountId.ToLower());
  546. if (userNode == null)
  547. {
  548. ret.Description = string.Format("更新账号 {0} 失败,账号不存在!", account.AccountId);
  549. ret.ActSucc = false;
  550. }
  551. else
  552. {
  553. userNode.SelectSingleNode("RealName").InnerText = account.RealName;
  554. userNode.SelectSingleNode("Role").InnerText = account.AccountId.ToLower() == "admin" ? "Admin" : account.Role.ToString();
  555. userNode.SelectSingleNode("AccountState").InnerText = account.AccountStatus ? "Enable" : "Disable";
  556. userNode.SelectSingleNode("Email").InnerText = account.Email;
  557. userNode.SelectSingleNode("Telephone").InnerText = account.Telephone;
  558. userNode.SelectSingleNode("Touxian").InnerText = account.Touxian;
  559. userNode.SelectSingleNode("Company").InnerText = account.Company;
  560. userNode.SelectSingleNode("Department").InnerText = account.Department;
  561. userNode.SelectSingleNode("Description").InnerText = account.Description;
  562. userNode.SelectSingleNode("CreationTime").InnerText = account.AccountCreationTime;
  563. userNode.SelectSingleNode("LastLoginTime").InnerText = account.LastLoginTime;
  564. userNode.SelectSingleNode("LastUpdateTime").InnerText = account.LastAccountUpdateTime;
  565. Save(_accountXml, _accountPath);//save to xml file
  566. ret.Description = string.Format("成功更新 {0} 的账号资料!", account.AccountId);
  567. ret.ActSucc = true;
  568. EV.PostMessage(Module, EventEnum.AccountChanged, account.AccountId);
  569. }
  570. }
  571. return ret;
  572. }
  573. catch
  574. {
  575. var msg = string.Format("更新账号{0}资料发生异常", account.AccountId);
  576. //LOG.Write(ex, msg);
  577. LOG.Write(eEvent.ERR_UPDATE_USER_ERROR, MECF.Framework.Common.Equipment.ModuleName.System, account.AccountId);
  578. return new UpdateAccountResult() { ActSucc = false, Description = msg };
  579. }
  580. }
  581. public static GetAccountListResult Accounts { get; private set; }
  582. /// <summary>
  583. /// get account list
  584. /// </summary>
  585. /// <returns></returns>
  586. public static GetAccountListResult GetAccountList()
  587. {
  588. try
  589. {
  590. //LOG.Write("获取所有的账号信息列表");
  591. GetAccountListResult ret = new GetAccountListResult();
  592. if (!FileSigner.IsValid(_accountPath)) //account xml file signer verify
  593. {
  594. ret.Description = "获取账号列表失败,账号文件数字签名文件损坏!";
  595. ret.ActSuccess = false;
  596. ret.AccountList = null;
  597. }
  598. else
  599. {
  600. XmlNodeList userNodeList = _accountXml.SelectNodes("AccountManagement/Account");
  601. List<Account> accountList = new List<Account>();
  602. foreach (XmlNode userNode in userNodeList)
  603. {
  604. accountList.Add(GetAccountInfo(userNode.Attributes["AccountId"].Value).AccountInfo);
  605. }
  606. ret.AccountList = accountList;
  607. ret.Description = "成功获取账号列表!";
  608. ret.ActSuccess = true;
  609. }
  610. Accounts = ret;
  611. return ret;
  612. }
  613. catch
  614. {
  615. var msg = "获取账号列表发生异常";
  616. //LOG.Write(ex, msg);
  617. return new GetAccountListResult() { AccountList = null, ActSuccess = false, Description = msg };
  618. }
  619. }
  620. /// <summary>
  621. /// 定期检查账号是否Active
  622. /// 如果当前账号已被Promaxy注销,那么Promaxy将发送KickOut事件给该客户端
  623. /// 如果当前账号连续超过1min没有消息响应,那么Promaxy将该用户自动退出
  624. /// </summary>
  625. /// <param name="accountId"></param>
  626. public static void CheckAlive(string accountId)
  627. {
  628. try
  629. {
  630. if (_userList.ContainsKey(accountId))
  631. {
  632. _userList[accountId] = new Tuple<Guid, DateTime, string>(_userList[accountId].Item1, DateTime.Now, _userList[accountId].Item3);
  633. }
  634. else
  635. {
  636. //当前用户已被注销,发送客户端注销通知
  637. EV.PostKickoutMessage( string.Format("账号{0}已在服务器上注销", accountId));
  638. }
  639. }
  640. catch (Exception ex)
  641. {
  642. //LOG.Write(ex);
  643. }
  644. }
  645. /// <summary>
  646. /// get specified account xml node
  647. /// </summary>
  648. /// <param name="accountId"></param>
  649. /// <returns></returns>
  650. private static XmlElement GetAccountNode(string accountId)
  651. {
  652. XmlNode ndl = _accountXml.SelectSingleNode(string.Format("/AccountManagement/Account[@AccountId='{0}']", accountId.ToLower()));
  653. return (XmlElement)ndl;
  654. }
  655. #region view permission
  656. /// <summary>
  657. /// 获取系统注册的所有视图列表
  658. /// </summary>
  659. /// <returns></returns>
  660. public static SerializableDictionary<string, string> GetAllViewList()
  661. {
  662. var viewList = new SerializableDictionary<string, string>();
  663. try
  664. {
  665. var xml = new XmlDocument();
  666. var xmlPath = Path.Combine(PathManager.GetAccountFilePath() , "Views.xml");
  667. xml.Load(xmlPath);
  668. var nodes = xml.SelectNodes("/root/Views/View");
  669. if (nodes != null)
  670. {
  671. foreach (XmlElement node in nodes)
  672. {
  673. viewList.Add(node.Attributes["Name"].Value, node.Attributes["Description"].Value);
  674. }
  675. }
  676. }
  677. catch (Exception ex)
  678. {
  679. //LOG.Write(ex);
  680. viewList = new SerializableDictionary<string, string>();
  681. }
  682. return viewList;
  683. }
  684. /// <summary>
  685. /// Save group definition
  686. /// </summary>
  687. /// <param name="data"></param>
  688. /// <returns></returns>
  689. public static bool SaveAllRolesPermission(Dictionary<string, Dictionary<string, ViewPermission>> data)
  690. {
  691. try
  692. {
  693. var rolesNode = _roleXml.SelectSingleNode("/Aitex/Roles") as XmlElement;
  694. rolesNode.RemoveAll();
  695. foreach (var item in data)
  696. {
  697. if (item.Key == "Admin") continue;
  698. var newRoleNode = _roleXml.CreateElement("Role");
  699. newRoleNode.SetAttribute("Name", item.Key);
  700. rolesNode.AppendChild(newRoleNode);
  701. foreach (var view in data[item.Key].Keys)
  702. {
  703. var newViewNode = _roleXml.CreateElement("View");
  704. newRoleNode.AppendChild(newViewNode);
  705. newViewNode.SetAttribute("Name", view);
  706. newViewNode.SetAttribute("Permission", data[item.Key][view].ToString());
  707. }
  708. }
  709. _roleXml.Save(_rolePath);
  710. }
  711. catch (Exception ex)
  712. {
  713. //LOG.Write(ex);
  714. return false;
  715. }
  716. return true;
  717. }
  718. /// <summary>
  719. /// 获取当前系统定义的分组
  720. /// </summary>
  721. /// <returns></returns>
  722. public static IEnumerable<string> GetAllRoles()
  723. {
  724. List<string> roles = new List<string>();
  725. try
  726. {
  727. var nodes = _roleXml.SelectNodes("/Aitex/Roles/Role");
  728. foreach (XmlElement node in nodes)
  729. {
  730. roles.Add(node.Attributes["Name"].Value);
  731. }
  732. //如果没有管理员组,默认添加管理员组
  733. if (!roles.Contains("Admin"))
  734. {
  735. roles.Add("Admin");
  736. }
  737. }
  738. catch (Exception ex)
  739. {
  740. //LOG.Write(ex);
  741. roles = new List<string>();
  742. }
  743. return roles;
  744. }
  745. /// <summary>
  746. /// 获取指定用户角色的权限设定
  747. /// </summary>
  748. /// <param name="roleName">指定用户的角色名</param>
  749. /// <returns></returns>
  750. public static SerializableDictionary<string, ViewPermission> GetSingleRolePermission(string roleName)
  751. {
  752. var rolePermission = new SerializableDictionary<string, ViewPermission>();
  753. try
  754. {
  755. var viewDic = GetAllViewList();
  756. if (roleName == "Admin")
  757. {
  758. foreach (var view in viewDic)
  759. {
  760. rolePermission.Add(view.Key, ViewPermission.FullyControl);
  761. }
  762. }
  763. else
  764. {
  765. /* RoleName, ViewName, ViewPermission */
  766. var nodes = _roleXml.SelectSingleNode(string.Format("/Aitex/Roles/Role[@Name='{0}']", roleName));
  767. if (nodes != null)
  768. {
  769. foreach (XmlElement viewNode in nodes)
  770. {
  771. var viewName = viewNode.Attributes["Name"].Value;
  772. var permission = viewNode.Attributes["Permission"].Value;
  773. if (viewDic.ContainsKey(viewName))
  774. {
  775. rolePermission.Add(viewName, (ViewPermission)Enum.Parse(typeof(ViewPermission), permission, true));
  776. }
  777. }
  778. }
  779. }
  780. }
  781. catch (Exception ex)
  782. {
  783. //LOG.Write(ex);
  784. rolePermission = new SerializableDictionary<string, ViewPermission>();
  785. }
  786. return rolePermission;
  787. }
  788. /// <summary>
  789. /// 获取所有用户角色的权限设定
  790. /// </summary>
  791. /// <returns></returns>
  792. public static SerializableDictionary<string, SerializableDictionary<string, ViewPermission>> GetAllRolesPermission()
  793. {
  794. try
  795. {
  796. var rolePermission = new SerializableDictionary<string, SerializableDictionary<string, ViewPermission>>();
  797. foreach (var role in GetAllRoles())
  798. rolePermission.Add(role, GetSingleRolePermission(role));
  799. return rolePermission;
  800. }
  801. catch (Exception ex)
  802. {
  803. //LOG.Write(ex);
  804. return new SerializableDictionary<string, SerializableDictionary<string, ViewPermission>>();
  805. }
  806. }
  807. #endregion
  808. }
  809. }