AccountManager.cs 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  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.WriteExeption(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.WriteExeption(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.WriteExeption(msg, ex);
  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.WriteExeption(string.Format("强制注销用户{0}发生异常", accountId), ex);
  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.WriteExeption(msg, ex);
  384. LOG.Write(eEvent.ERR_UPDATE_USER_ERROR, MECF.Framework.Common.Equipment.ModuleName.System, accountId);
  385. return new ChangePwdResult() { ActSucc = false, Description = msg };
  386. }
  387. }
  388. /// <summary>
  389. /// create account
  390. /// </summary>
  391. /// <param name="newAccount"></param>
  392. /// <returns></returns>
  393. public static CreateAccountResult CreateAccount(Account newAccount)
  394. {
  395. try
  396. {
  397. //LOG.Write(string.Format("创建账号{0}", newAccount.AccountId));
  398. CreateAccountResult ret = new CreateAccountResult();
  399. if (newAccount == null)
  400. {
  401. ret.Description = "账号有误";
  402. ret.ActSucc = false;
  403. }
  404. else if (!FileSigner.IsValid(_accountPath)) //account xml file signer verify
  405. {
  406. ret.Description = string.Format("创建账号失败,数字签名损坏!");
  407. ret.ActSucc = false;
  408. }
  409. else
  410. {
  411. if (GetAccountNode(newAccount.AccountId) != null) //account has been existed
  412. {
  413. ret.Description = string.Format("创建账号失败,账号 {0} 已存在!", newAccount.AccountId);
  414. ret.ActSucc = false;
  415. }
  416. else
  417. {
  418. XmlElement userNode = _accountXml.CreateElement("Account");
  419. userNode.SetAttribute("AccountId", newAccount.AccountId.ToLower());
  420. _accountXml.DocumentElement.AppendChild(userNode);
  421. XmlElement subNode = _accountXml.CreateElement("RealName");
  422. subNode.InnerText = newAccount.RealName;
  423. userNode.AppendChild(subNode);
  424. subNode = _accountXml.CreateElement("Role");
  425. subNode.InnerText = newAccount.Role.ToString();
  426. userNode.AppendChild(subNode);
  427. subNode = _accountXml.CreateElement("Password");
  428. subNode.InnerText = Md5Helper.GetMd5Hash(newAccount.AccountId);//default new create account's password same as accountId
  429. userNode.AppendChild(subNode);
  430. subNode = _accountXml.CreateElement("AccountState");//defualt new create account's state "Enable"
  431. subNode.InnerText = newAccount.AccountStatus ? "Enable" : "Disable";
  432. userNode.AppendChild(subNode);
  433. subNode = _accountXml.CreateElement("Email");
  434. subNode.InnerText = newAccount.Email;
  435. userNode.AppendChild(subNode);
  436. subNode = _accountXml.CreateElement("Telephone");
  437. subNode.InnerText = newAccount.Telephone;
  438. userNode.AppendChild(subNode);
  439. subNode = _accountXml.CreateElement("Touxian");
  440. subNode.InnerText = newAccount.Touxian;
  441. userNode.AppendChild(subNode);
  442. subNode = _accountXml.CreateElement("Company");
  443. subNode.InnerText = newAccount.Company;
  444. userNode.AppendChild(subNode);
  445. subNode = _accountXml.CreateElement("Department");
  446. subNode.InnerText = newAccount.Department;
  447. userNode.AppendChild(subNode);
  448. subNode = _accountXml.CreateElement("Description");
  449. subNode.InnerText = newAccount.Description;
  450. userNode.AppendChild(subNode);
  451. subNode = _accountXml.CreateElement("CreationTime");
  452. subNode.InnerText = DateTime.Now.ToString();
  453. userNode.AppendChild(subNode);
  454. subNode = _accountXml.CreateElement("LastLoginTime");
  455. subNode.InnerText = string.Empty;
  456. userNode.AppendChild(subNode);
  457. subNode = _accountXml.CreateElement("LastUpdateTime");
  458. subNode.InnerText = string.Empty;
  459. userNode.AppendChild(subNode);
  460. Save(_accountXml, _accountPath);//save to xml file
  461. ret.Description = string.Format("创建新账号{0}成功", newAccount.AccountId);
  462. ret.ActSucc = true;
  463. EV.PostMessage(Module, EventEnum.AccountCreated, newAccount.AccountId);
  464. }
  465. }
  466. return ret;
  467. }
  468. catch
  469. {
  470. var msg = string.Format("创建账号{0}失败", newAccount.AccountId);
  471. //LOG.Write(ex, msg);
  472. return new CreateAccountResult() { ActSucc = false, Description = msg };
  473. }
  474. }
  475. /// <summary>
  476. /// Administrator user calls this method to delete an account.
  477. /// </summary>
  478. /// <param name="account"></param>
  479. /// <returns></returns>
  480. public static DeleteAccountResult DeleteAccount(string accountId)
  481. {
  482. try
  483. {
  484. //LOG.Write(string.Format("删除账号{0}", accountId));
  485. accountId = accountId.ToLower();
  486. DeleteAccountResult ret = new DeleteAccountResult();
  487. if (accountId == "admin")
  488. {
  489. ret.Description = "Admin\'admin\'账号不能删除";
  490. ret.ActSucc = false;
  491. }
  492. else if (!FileSigner.IsValid(_accountPath))//account xml file signer verify
  493. {
  494. ret.Description = "删除账号失败,账号文件数字签名损坏!";
  495. ret.ActSucc = false;
  496. }
  497. else
  498. {
  499. XmlElement accountNode = GetAccountNode(accountId);
  500. if (accountNode == null)//account has been existed
  501. {
  502. ret.Description = string.Format("删除账号 {0} 失败,账号不存在!", accountId);
  503. ret.ActSucc = false;
  504. }
  505. else
  506. {
  507. _accountXml.DocumentElement.RemoveChild(accountNode);//remove account node
  508. Save(_accountXml, _accountPath);//save to xml file
  509. ret.Description = string.Format("删除账号 {0} 成功!", accountId);
  510. ret.ActSucc = true;
  511. EV.PostMessage(Module, EventEnum.AccountDeleted, accountId);
  512. }
  513. }
  514. return ret;
  515. }
  516. catch (Exception ex)
  517. {
  518. var msg = string.Format("删除账号{0}发生异常", accountId);
  519. LOG.Write(eEvent.ERR_DELETE_USER_ERROR, MECF.Framework.Common.Equipment.ModuleName.System, accountId);
  520. LOG.WriteExeption(msg, ex);
  521. return new DeleteAccountResult() { ActSucc = false, Description = msg };
  522. }
  523. }
  524. /// <summary>
  525. /// Update account information
  526. /// </summary>
  527. /// <param name="accountList"></param>
  528. /// <returns></returns>
  529. public static UpdateAccountResult UpdateAccount(Account account)
  530. {
  531. try
  532. {
  533. UpdateAccountResult ret = new UpdateAccountResult();
  534. if (account == null)
  535. {
  536. ret.Description = "账号有误";
  537. ret.ActSucc = false;
  538. }
  539. else if (!FileSigner.IsValid(_accountPath)) //account xml file signer verify
  540. {
  541. ret.Description = string.Format("更新账号资料失败,账号文件数字签名损坏!");
  542. ret.ActSucc = false;
  543. }
  544. else
  545. {
  546. XmlElement userNode = GetAccountNode(account.AccountId.ToLower());
  547. if (userNode == null)
  548. {
  549. ret.Description = string.Format("更新账号 {0} 失败,账号不存在!", account.AccountId);
  550. ret.ActSucc = false;
  551. }
  552. else
  553. {
  554. userNode.SelectSingleNode("RealName").InnerText = account.RealName;
  555. userNode.SelectSingleNode("Role").InnerText = account.AccountId.ToLower() == "admin" ? "Admin" : account.Role.ToString();
  556. userNode.SelectSingleNode("AccountState").InnerText = account.AccountStatus ? "Enable" : "Disable";
  557. userNode.SelectSingleNode("Email").InnerText = account.Email;
  558. userNode.SelectSingleNode("Telephone").InnerText = account.Telephone;
  559. userNode.SelectSingleNode("Touxian").InnerText = account.Touxian;
  560. userNode.SelectSingleNode("Company").InnerText = account.Company;
  561. userNode.SelectSingleNode("Department").InnerText = account.Department;
  562. userNode.SelectSingleNode("Description").InnerText = account.Description;
  563. userNode.SelectSingleNode("CreationTime").InnerText = account.AccountCreationTime;
  564. userNode.SelectSingleNode("LastLoginTime").InnerText = account.LastLoginTime;
  565. userNode.SelectSingleNode("LastUpdateTime").InnerText = account.LastAccountUpdateTime;
  566. Save(_accountXml, _accountPath);//save to xml file
  567. ret.Description = string.Format("成功更新 {0} 的账号资料!", account.AccountId);
  568. ret.ActSucc = true;
  569. EV.PostMessage(Module, EventEnum.AccountChanged, account.AccountId);
  570. }
  571. }
  572. return ret;
  573. }
  574. catch
  575. {
  576. var msg = string.Format("更新账号{0}资料发生异常", account.AccountId);
  577. //LOG.Write(ex, msg);
  578. LOG.Write(eEvent.ERR_UPDATE_USER_ERROR, MECF.Framework.Common.Equipment.ModuleName.System, account.AccountId);
  579. return new UpdateAccountResult() { ActSucc = false, Description = msg };
  580. }
  581. }
  582. public static GetAccountListResult Accounts { get; private set; }
  583. /// <summary>
  584. /// get account list
  585. /// </summary>
  586. /// <returns></returns>
  587. public static GetAccountListResult GetAccountList()
  588. {
  589. try
  590. {
  591. //LOG.Write("获取所有的账号信息列表");
  592. GetAccountListResult ret = new GetAccountListResult();
  593. if (!FileSigner.IsValid(_accountPath)) //account xml file signer verify
  594. {
  595. ret.Description = "获取账号列表失败,账号文件数字签名文件损坏!";
  596. ret.ActSuccess = false;
  597. ret.AccountList = null;
  598. }
  599. else
  600. {
  601. XmlNodeList userNodeList = _accountXml.SelectNodes("AccountManagement/Account");
  602. List<Account> accountList = new List<Account>();
  603. foreach (XmlNode userNode in userNodeList)
  604. {
  605. accountList.Add(GetAccountInfo(userNode.Attributes["AccountId"].Value).AccountInfo);
  606. }
  607. ret.AccountList = accountList;
  608. ret.Description = "成功获取账号列表!";
  609. ret.ActSuccess = true;
  610. }
  611. Accounts = ret;
  612. return ret;
  613. }
  614. catch
  615. {
  616. var msg = "获取账号列表发生异常";
  617. //LOG.Write(ex, msg);
  618. return new GetAccountListResult() { AccountList = null, ActSuccess = false, Description = msg };
  619. }
  620. }
  621. /// <summary>
  622. /// 定期检查账号是否Active
  623. /// 如果当前账号已被Promaxy注销,那么Promaxy将发送KickOut事件给该客户端
  624. /// 如果当前账号连续超过1min没有消息响应,那么Promaxy将该用户自动退出
  625. /// </summary>
  626. /// <param name="accountId"></param>
  627. public static void CheckAlive(string accountId)
  628. {
  629. try
  630. {
  631. if (_userList.ContainsKey(accountId))
  632. {
  633. _userList[accountId] = new Tuple<Guid, DateTime, string>(_userList[accountId].Item1, DateTime.Now, _userList[accountId].Item3);
  634. }
  635. else
  636. {
  637. //当前用户已被注销,发送客户端注销通知
  638. EV.PostKickoutMessage( string.Format("账号{0}已在服务器上注销", accountId));
  639. }
  640. }
  641. catch (Exception ex)
  642. {
  643. LOG.WriteExeption(ex);
  644. }
  645. }
  646. /// <summary>
  647. /// get specified account xml node
  648. /// </summary>
  649. /// <param name="accountId"></param>
  650. /// <returns></returns>
  651. private static XmlElement GetAccountNode(string accountId)
  652. {
  653. XmlNode ndl = _accountXml.SelectSingleNode(string.Format("/AccountManagement/Account[@AccountId='{0}']", accountId.ToLower()));
  654. return (XmlElement)ndl;
  655. }
  656. #region view permission
  657. /// <summary>
  658. /// 获取系统注册的所有视图列表
  659. /// </summary>
  660. /// <returns></returns>
  661. public static SerializableDictionary<string, string> GetAllViewList()
  662. {
  663. var viewList = new SerializableDictionary<string, string>();
  664. try
  665. {
  666. var xml = new XmlDocument();
  667. var xmlPath = Path.Combine(PathManager.GetAccountFilePath() , "Views.xml");
  668. xml.Load(xmlPath);
  669. var nodes = xml.SelectNodes("/root/Views/View");
  670. if (nodes != null)
  671. {
  672. foreach (XmlElement node in nodes)
  673. {
  674. viewList.Add(node.Attributes["Name"].Value, node.Attributes["Description"].Value);
  675. }
  676. }
  677. }
  678. catch (Exception ex)
  679. {
  680. LOG.WriteExeption(ex);
  681. viewList = new SerializableDictionary<string, string>();
  682. }
  683. return viewList;
  684. }
  685. /// <summary>
  686. /// Save group definition
  687. /// </summary>
  688. /// <param name="data"></param>
  689. /// <returns></returns>
  690. public static bool SaveAllRolesPermission(Dictionary<string, Dictionary<string, ViewPermission>> data)
  691. {
  692. try
  693. {
  694. var rolesNode = _roleXml.SelectSingleNode("/Aitex/Roles") as XmlElement;
  695. rolesNode.RemoveAll();
  696. foreach (var item in data)
  697. {
  698. if (item.Key == "Admin") continue;
  699. var newRoleNode = _roleXml.CreateElement("Role");
  700. newRoleNode.SetAttribute("Name", item.Key);
  701. rolesNode.AppendChild(newRoleNode);
  702. foreach (var view in data[item.Key].Keys)
  703. {
  704. var newViewNode = _roleXml.CreateElement("View");
  705. newRoleNode.AppendChild(newViewNode);
  706. newViewNode.SetAttribute("Name", view);
  707. newViewNode.SetAttribute("Permission", data[item.Key][view].ToString());
  708. }
  709. }
  710. _roleXml.Save(_rolePath);
  711. }
  712. catch (Exception ex)
  713. {
  714. LOG.WriteExeption(ex);
  715. return false;
  716. }
  717. return true;
  718. }
  719. /// <summary>
  720. /// 获取当前系统定义的分组
  721. /// </summary>
  722. /// <returns></returns>
  723. public static IEnumerable<string> GetAllRoles()
  724. {
  725. List<string> roles = new List<string>();
  726. try
  727. {
  728. var nodes = _roleXml.SelectNodes("/Aitex/Roles/Role");
  729. foreach (XmlElement node in nodes)
  730. {
  731. roles.Add(node.Attributes["Name"].Value);
  732. }
  733. //如果没有管理员组,默认添加管理员组
  734. if (!roles.Contains("Admin"))
  735. {
  736. roles.Add("Admin");
  737. }
  738. }
  739. catch (Exception ex)
  740. {
  741. LOG.WriteExeption(ex);
  742. roles = new List<string>();
  743. }
  744. return roles;
  745. }
  746. /// <summary>
  747. /// 获取指定用户角色的权限设定
  748. /// </summary>
  749. /// <param name="roleName">指定用户的角色名</param>
  750. /// <returns></returns>
  751. public static SerializableDictionary<string, ViewPermission> GetSingleRolePermission(string roleName)
  752. {
  753. var rolePermission = new SerializableDictionary<string, ViewPermission>();
  754. try
  755. {
  756. var viewDic = GetAllViewList();
  757. if (roleName == "Admin")
  758. {
  759. foreach (var view in viewDic)
  760. {
  761. rolePermission.Add(view.Key, ViewPermission.FullyControl);
  762. }
  763. }
  764. else
  765. {
  766. /* RoleName, ViewName, ViewPermission */
  767. var nodes = _roleXml.SelectSingleNode(string.Format("/Aitex/Roles/Role[@Name='{0}']", roleName));
  768. if (nodes != null)
  769. {
  770. foreach (XmlElement viewNode in nodes)
  771. {
  772. var viewName = viewNode.Attributes["Name"].Value;
  773. var permission = viewNode.Attributes["Permission"].Value;
  774. if (viewDic.ContainsKey(viewName))
  775. {
  776. rolePermission.Add(viewName, (ViewPermission)Enum.Parse(typeof(ViewPermission), permission, true));
  777. }
  778. }
  779. }
  780. }
  781. }
  782. catch (Exception ex)
  783. {
  784. LOG.WriteExeption(ex);
  785. rolePermission = new SerializableDictionary<string, ViewPermission>();
  786. }
  787. return rolePermission;
  788. }
  789. /// <summary>
  790. /// 获取所有用户角色的权限设定
  791. /// </summary>
  792. /// <returns></returns>
  793. public static SerializableDictionary<string, SerializableDictionary<string, ViewPermission>> GetAllRolesPermission()
  794. {
  795. try
  796. {
  797. var rolePermission = new SerializableDictionary<string, SerializableDictionary<string, ViewPermission>>();
  798. foreach (var role in GetAllRoles())
  799. rolePermission.Add(role, GetSingleRolePermission(role));
  800. return rolePermission;
  801. }
  802. catch (Exception ex)
  803. {
  804. LOG.WriteExeption(ex);
  805. return new SerializableDictionary<string, SerializableDictionary<string, ViewPermission>>();
  806. }
  807. }
  808. #endregion
  809. }
  810. }