AccountManager.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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. accountId = accountId.ToLower();
  224. if (_userList.ContainsKey(accountId))
  225. {
  226. _userList.Remove(accountId);
  227. }
  228. EV.PostMessage("System", EventEnum.UserLoggedOff, accountId);
  229. }
  230. catch (Exception ex)
  231. {
  232. LOG.Write(ex, string.Format("注销用户{0}发生异常", accountId));
  233. }
  234. }
  235. /// <summary>
  236. /// 用户被强制注销的理由
  237. /// </summary>
  238. /// <param name="accountId"></param>
  239. /// <param name="kickOutReason"></param>
  240. public static void Kickout(string accountId, string kickOutReason)
  241. {
  242. try
  243. {
  244. LOG.Write(string.Format("用户{0}强制注销登录", accountId));
  245. accountId = accountId.ToLower();
  246. if (_userList.ContainsKey(accountId))
  247. {
  248. EV.PostKickoutMessage(string.Format("用户{0}强制注销登录,{1}", accountId,kickOutReason));
  249. _userList.Remove(accountId);
  250. }
  251. EV.PostMessage(Module, EventEnum.UserLoggedOff, accountId);
  252. }
  253. catch (Exception ex)
  254. {
  255. LOG.Write(ex, string.Format("强制注销用户{0}发生异常", accountId));
  256. }
  257. }
  258. /// <summary>
  259. /// 返回指定用户的账号信息
  260. /// </summary>
  261. /// <param name="accountId"></param>
  262. /// <returns></returns>
  263. public static GetAccountInfoResult GetAccountInfo(string accountId)
  264. {
  265. try
  266. {
  267. //LOG.Write(string.Format("获取账号信息{0}", accountId));
  268. accountId = accountId.ToLower(); //账号转小写
  269. GetAccountInfoResult ret = new GetAccountInfoResult();
  270. if (!FileSigner.IsValid(_accountPath)) //检查账号文件的数字签名
  271. {
  272. ret.Description = "账号文件数字签名校验失败";
  273. ret.ActSuccess = false;
  274. }
  275. else
  276. {
  277. XmlElement userNode = GetAccountNode(accountId);
  278. if (userNode == null)
  279. {
  280. if (accountId == "admin") //如果没有admin账号,则创建默认的admin账号
  281. {
  282. Account adminAccount = new Account()
  283. {
  284. Role = "Admin",
  285. Permission = GetSingleRolePermission("Admin"),
  286. AccountId = "admin",
  287. RealName = "admin",
  288. Email = "admin@admin.com",
  289. Telephone = "86-21-88886666",
  290. Touxian = "Admin",
  291. Company = "MY Tech",
  292. Department = "IT",
  293. Description = "Administrator,拥有用户权限修改、菜单修改,定序器修改等权限。",
  294. AccountStatus = true,
  295. Md5Pwd = Md5Helper.GetMd5Hash("admin")
  296. };
  297. CreateAccount(adminAccount);
  298. ret.ActSuccess = true;
  299. ret.AccountInfo = adminAccount;
  300. ret.Description = string.Format("成功获取账号信息{0}", accountId);
  301. }
  302. else
  303. {
  304. ret.Description = string.Format("账号{0}不存在", accountId);
  305. ret.ActSuccess = false;
  306. }
  307. }
  308. else
  309. {
  310. ret.AccountInfo = new Account
  311. {
  312. Role = userNode.SelectSingleNode("Role").InnerText,
  313. Permission = GetSingleRolePermission(accountId == "admin" ? "Admin" : userNode.SelectSingleNode("Role").InnerText),
  314. AccountId = accountId,
  315. RealName = userNode.SelectSingleNode("RealName").InnerText,
  316. Email = userNode.SelectSingleNode("Email").InnerText,
  317. Telephone = userNode.SelectSingleNode("Telephone").InnerText,
  318. Touxian = userNode.SelectSingleNode("Touxian").InnerText,
  319. Company = userNode.SelectSingleNode("Company").InnerText,
  320. Department = userNode.SelectSingleNode("Department").InnerText,
  321. Description = userNode.SelectSingleNode("Description").InnerText,
  322. AccountStatus = (0 == String.Compare(userNode.SelectSingleNode("AccountState").InnerText, "Enable", true)),
  323. AccountCreationTime = userNode.SelectSingleNode("CreationTime").InnerText,
  324. LastAccountUpdateTime = userNode.SelectSingleNode("LastUpdateTime").InnerText,
  325. LastLoginTime = userNode.SelectSingleNode("LastLoginTime").InnerText,
  326. Md5Pwd = userNode.SelectSingleNode("Password").InnerText,
  327. };
  328. ret.Description = string.Format("获取账号{0}成功", accountId);
  329. ret.ActSuccess = true;
  330. }
  331. }
  332. return ret;
  333. }
  334. catch (Exception ex)
  335. {
  336. string msg = string.Format("获取账号{0}发生异常", accountId);
  337. LOG.Write(ex, msg);
  338. return new GetAccountInfoResult() { ActSuccess = false, Description = msg };
  339. }
  340. }
  341. /// <summary>
  342. /// change account password
  343. /// </summary>
  344. /// <param name="accountId"></param>
  345. /// <param name="newPassword"></param>
  346. public static ChangePwdResult ChangePassword(string accountId, string newPassword)
  347. {
  348. try
  349. {
  350. LOG.Write(string.Format("修改账号{0}的密码", accountId));
  351. accountId = accountId.ToLower();
  352. ChangePwdResult ret = new ChangePwdResult();
  353. if (!FileSigner.IsValid(_accountPath)) //检查账号文件的数字签名
  354. {
  355. ret.Description = "修改密码失败,账号文件数字签名损坏!";
  356. ret.ActSucc = false;
  357. }
  358. else
  359. {
  360. XmlElement userNode = GetAccountNode(accountId);
  361. if (userNode == null)
  362. {
  363. ret.Description = string.Format("账号{0}不存在", accountId);
  364. ret.ActSucc = false;
  365. }
  366. else
  367. {
  368. userNode.SelectSingleNode("Password").InnerText = Md5Helper.GetMd5Hash(newPassword);
  369. Save(_accountXml, _accountPath);
  370. ret.Description = "修改密码成功!";
  371. ret.ActSucc = true;
  372. EV.PostMessage(Module, EventEnum.PasswordChanged, accountId);
  373. }
  374. }
  375. return ret;
  376. }
  377. catch (Exception ex)
  378. {
  379. var msg = string.Format("修改账号{0}的密码失败", accountId);
  380. LOG.Write(ex, msg);
  381. return new ChangePwdResult() { ActSucc = false, Description = msg };
  382. }
  383. }
  384. /// <summary>
  385. /// create account
  386. /// </summary>
  387. /// <param name="newAccount"></param>
  388. /// <returns></returns>
  389. public static CreateAccountResult CreateAccount(Account newAccount)
  390. {
  391. try
  392. {
  393. LOG.Write(string.Format("创建账号{0}", newAccount.AccountId));
  394. CreateAccountResult ret = new CreateAccountResult();
  395. if (newAccount == null)
  396. {
  397. ret.Description = "账号有误";
  398. ret.ActSucc = false;
  399. }
  400. else if (!FileSigner.IsValid(_accountPath)) //account xml file signer verify
  401. {
  402. ret.Description = string.Format("创建账号失败,数字签名损坏!");
  403. ret.ActSucc = false;
  404. }
  405. else
  406. {
  407. if (GetAccountNode(newAccount.AccountId) != null) //account has been existed
  408. {
  409. ret.Description = string.Format("创建账号失败,账号 {0} 已存在!", newAccount.AccountId);
  410. ret.ActSucc = false;
  411. }
  412. else
  413. {
  414. XmlElement userNode = _accountXml.CreateElement("Account");
  415. userNode.SetAttribute("AccountId", newAccount.AccountId.ToLower());
  416. _accountXml.DocumentElement.AppendChild(userNode);
  417. XmlElement subNode = _accountXml.CreateElement("RealName");
  418. subNode.InnerText = newAccount.RealName;
  419. userNode.AppendChild(subNode);
  420. subNode = _accountXml.CreateElement("Role");
  421. subNode.InnerText = newAccount.Role.ToString();
  422. userNode.AppendChild(subNode);
  423. subNode = _accountXml.CreateElement("Password");
  424. subNode.InnerText = Md5Helper.GetMd5Hash(newAccount.AccountId);//default new create account's password same as accountId
  425. userNode.AppendChild(subNode);
  426. subNode = _accountXml.CreateElement("AccountState");//defualt new create account's state "Enable"
  427. subNode.InnerText = newAccount.AccountStatus ? "Enable" : "Disable";
  428. userNode.AppendChild(subNode);
  429. subNode = _accountXml.CreateElement("Email");
  430. subNode.InnerText = newAccount.Email;
  431. userNode.AppendChild(subNode);
  432. subNode = _accountXml.CreateElement("Telephone");
  433. subNode.InnerText = newAccount.Telephone;
  434. userNode.AppendChild(subNode);
  435. subNode = _accountXml.CreateElement("Touxian");
  436. subNode.InnerText = newAccount.Touxian;
  437. userNode.AppendChild(subNode);
  438. subNode = _accountXml.CreateElement("Company");
  439. subNode.InnerText = newAccount.Company;
  440. userNode.AppendChild(subNode);
  441. subNode = _accountXml.CreateElement("Department");
  442. subNode.InnerText = newAccount.Department;
  443. userNode.AppendChild(subNode);
  444. subNode = _accountXml.CreateElement("Description");
  445. subNode.InnerText = newAccount.Description;
  446. userNode.AppendChild(subNode);
  447. subNode = _accountXml.CreateElement("CreationTime");
  448. subNode.InnerText = DateTime.Now.ToString();
  449. userNode.AppendChild(subNode);
  450. subNode = _accountXml.CreateElement("LastLoginTime");
  451. subNode.InnerText = string.Empty;
  452. userNode.AppendChild(subNode);
  453. subNode = _accountXml.CreateElement("LastUpdateTime");
  454. subNode.InnerText = string.Empty;
  455. userNode.AppendChild(subNode);
  456. Save(_accountXml, _accountPath);//save to xml file
  457. ret.Description = string.Format("创建新账号{0}成功", newAccount.AccountId);
  458. ret.ActSucc = true;
  459. EV.PostMessage(Module, EventEnum.AccountCreated, newAccount.AccountId);
  460. }
  461. }
  462. return ret;
  463. }
  464. catch (Exception ex)
  465. {
  466. var msg = string.Format("创建账号{0}失败", newAccount.AccountId);
  467. LOG.Write(ex, msg);
  468. return new CreateAccountResult() { ActSucc = false, Description = msg };
  469. }
  470. }
  471. /// <summary>
  472. /// Administrator user calls this method to delete an account.
  473. /// </summary>
  474. /// <param name="account"></param>
  475. /// <returns></returns>
  476. public static DeleteAccountResult DeleteAccount(string accountId)
  477. {
  478. try
  479. {
  480. LOG.Write(string.Format("删除账号{0}", accountId));
  481. accountId = accountId.ToLower();
  482. DeleteAccountResult ret = new DeleteAccountResult();
  483. if (accountId == "admin")
  484. {
  485. ret.Description = "Admin\'admin\'账号不能删除";
  486. ret.ActSucc = false;
  487. }
  488. else if (!FileSigner.IsValid(_accountPath))//account xml file signer verify
  489. {
  490. ret.Description = "删除账号失败,账号文件数字签名损坏!";
  491. ret.ActSucc = false;
  492. }
  493. else
  494. {
  495. XmlElement accountNode = GetAccountNode(accountId);
  496. if (accountNode == null)//account has been existed
  497. {
  498. ret.Description = string.Format("删除账号 {0} 失败,账号不存在!", accountId);
  499. ret.ActSucc = false;
  500. }
  501. else
  502. {
  503. _accountXml.DocumentElement.RemoveChild(accountNode);//remove account node
  504. Save(_accountXml, _accountPath);//save to xml file
  505. ret.Description = string.Format("删除账号 {0} 成功!", accountId);
  506. ret.ActSucc = true;
  507. EV.PostMessage(Module, EventEnum.AccountDeleted, accountId);
  508. }
  509. }
  510. return ret;
  511. }
  512. catch (Exception ex)
  513. {
  514. var msg = string.Format("删除账号{0}发生异常", accountId);
  515. LOG.Write(ex, msg);
  516. return new DeleteAccountResult() { ActSucc = false, Description = msg };
  517. }
  518. }
  519. /// <summary>
  520. /// Update account information
  521. /// </summary>
  522. /// <param name="accountList"></param>
  523. /// <returns></returns>
  524. public static UpdateAccountResult UpdateAccount(Account account)
  525. {
  526. try
  527. {
  528. UpdateAccountResult ret = new UpdateAccountResult();
  529. if (account == null)
  530. {
  531. ret.Description = "账号有误";
  532. ret.ActSucc = false;
  533. }
  534. else if (!FileSigner.IsValid(_accountPath)) //account xml file signer verify
  535. {
  536. ret.Description = string.Format("更新账号资料失败,账号文件数字签名损坏!");
  537. ret.ActSucc = false;
  538. }
  539. else
  540. {
  541. XmlElement userNode = GetAccountNode(account.AccountId.ToLower());
  542. if (userNode == null)
  543. {
  544. ret.Description = string.Format("更新账号 {0} 失败,账号不存在!", account.AccountId);
  545. ret.ActSucc = false;
  546. }
  547. else
  548. {
  549. userNode.SelectSingleNode("RealName").InnerText = account.RealName;
  550. userNode.SelectSingleNode("Role").InnerText = account.AccountId.ToLower() == "admin" ? "Admin" : account.Role.ToString();
  551. userNode.SelectSingleNode("AccountState").InnerText = account.AccountStatus ? "Enable" : "Disable";
  552. userNode.SelectSingleNode("Email").InnerText = account.Email;
  553. userNode.SelectSingleNode("Telephone").InnerText = account.Telephone;
  554. userNode.SelectSingleNode("Touxian").InnerText = account.Touxian;
  555. userNode.SelectSingleNode("Company").InnerText = account.Company;
  556. userNode.SelectSingleNode("Department").InnerText = account.Department;
  557. userNode.SelectSingleNode("Description").InnerText = account.Description;
  558. userNode.SelectSingleNode("CreationTime").InnerText = account.AccountCreationTime;
  559. userNode.SelectSingleNode("LastLoginTime").InnerText = account.LastLoginTime;
  560. userNode.SelectSingleNode("LastUpdateTime").InnerText = account.LastAccountUpdateTime;
  561. Save(_accountXml, _accountPath);//save to xml file
  562. ret.Description = string.Format("成功更新 {0} 的账号资料!", account.AccountId);
  563. ret.ActSucc = true;
  564. EV.PostMessage(Module, EventEnum.AccountChanged, account.AccountId);
  565. }
  566. }
  567. return ret;
  568. }
  569. catch (Exception ex)
  570. {
  571. var msg = string.Format("更新账号{0}资料发生异常", account.AccountId);
  572. LOG.Write(ex, msg);
  573. return new UpdateAccountResult() { ActSucc = false, Description = msg };
  574. }
  575. }
  576. public static GetAccountListResult Accounts { get; private set; }
  577. /// <summary>
  578. /// get account list
  579. /// </summary>
  580. /// <returns></returns>
  581. public static GetAccountListResult GetAccountList()
  582. {
  583. try
  584. {
  585. LOG.Write("获取所有的账号信息列表");
  586. GetAccountListResult ret = new GetAccountListResult();
  587. if (!FileSigner.IsValid(_accountPath)) //account xml file signer verify
  588. {
  589. ret.Description = "获取账号列表失败,账号文件数字签名文件损坏!";
  590. ret.ActSuccess = false;
  591. ret.AccountList = null;
  592. }
  593. else
  594. {
  595. XmlNodeList userNodeList = _accountXml.SelectNodes("AccountManagement/Account");
  596. List<Account> accountList = new List<Account>();
  597. foreach (XmlNode userNode in userNodeList)
  598. {
  599. accountList.Add(GetAccountInfo(userNode.Attributes["AccountId"].Value).AccountInfo);
  600. }
  601. ret.AccountList = accountList;
  602. ret.Description = "成功获取账号列表!";
  603. ret.ActSuccess = true;
  604. }
  605. Accounts = ret;
  606. return ret;
  607. }
  608. catch (Exception ex)
  609. {
  610. var msg = "获取账号列表发生异常";
  611. LOG.Write(ex, msg);
  612. return new GetAccountListResult() { AccountList = null, ActSuccess = false, Description = msg };
  613. }
  614. }
  615. /// <summary>
  616. /// 定期检查账号是否Active
  617. /// 如果当前账号已被Promaxy注销,那么Promaxy将发送KickOut事件给该客户端
  618. /// 如果当前账号连续超过1min没有消息响应,那么Promaxy将该用户自动退出
  619. /// </summary>
  620. /// <param name="accountId"></param>
  621. public static void CheckAlive(string accountId)
  622. {
  623. try
  624. {
  625. if (_userList.ContainsKey(accountId))
  626. {
  627. _userList[accountId] = new Tuple<Guid, DateTime, string>(_userList[accountId].Item1, DateTime.Now, _userList[accountId].Item3);
  628. }
  629. else
  630. {
  631. //当前用户已被注销,发送客户端注销通知
  632. EV.PostKickoutMessage( string.Format("账号{0}已在服务器上注销", accountId));
  633. }
  634. }
  635. catch (Exception ex)
  636. {
  637. LOG.Write(ex);
  638. }
  639. }
  640. /// <summary>
  641. /// get specified account xml node
  642. /// </summary>
  643. /// <param name="accountId"></param>
  644. /// <returns></returns>
  645. private static XmlElement GetAccountNode(string accountId)
  646. {
  647. XmlNode ndl = _accountXml.SelectSingleNode(string.Format("/AccountManagement/Account[@AccountId='{0}']", accountId.ToLower()));
  648. return (XmlElement)ndl;
  649. }
  650. #region view permission
  651. /// <summary>
  652. /// 获取系统注册的所有视图列表
  653. /// </summary>
  654. /// <returns></returns>
  655. public static SerializableDictionary<string, string> GetAllViewList()
  656. {
  657. var viewList = new SerializableDictionary<string, string>();
  658. try
  659. {
  660. var xml = new XmlDocument();
  661. var xmlPath = Path.Combine(PathManager.GetAccountFilePath() , "Views.xml");
  662. xml.Load(xmlPath);
  663. var nodes = xml.SelectNodes("/root/Views/View");
  664. if (nodes != null)
  665. {
  666. foreach (XmlElement node in nodes)
  667. {
  668. viewList.Add(node.Attributes["Name"].Value, node.Attributes["Description"].Value);
  669. }
  670. }
  671. }
  672. catch (Exception ex)
  673. {
  674. LOG.Write(ex);
  675. viewList = new SerializableDictionary<string, string>();
  676. }
  677. return viewList;
  678. }
  679. /// <summary>
  680. /// Save group definition
  681. /// </summary>
  682. /// <param name="data"></param>
  683. /// <returns></returns>
  684. public static bool SaveAllRolesPermission(Dictionary<string, Dictionary<string, ViewPermission>> data)
  685. {
  686. try
  687. {
  688. var rolesNode = _roleXml.SelectSingleNode("/Aitex/Roles") as XmlElement;
  689. rolesNode.RemoveAll();
  690. foreach (var item in data)
  691. {
  692. if (item.Key == "Admin") continue;
  693. var newRoleNode = _roleXml.CreateElement("Role");
  694. newRoleNode.SetAttribute("Name", item.Key);
  695. rolesNode.AppendChild(newRoleNode);
  696. foreach (var view in data[item.Key].Keys)
  697. {
  698. var newViewNode = _roleXml.CreateElement("View");
  699. newRoleNode.AppendChild(newViewNode);
  700. newViewNode.SetAttribute("Name", view);
  701. newViewNode.SetAttribute("Permission", data[item.Key][view].ToString());
  702. }
  703. }
  704. _roleXml.Save(_rolePath);
  705. }
  706. catch (Exception ex)
  707. {
  708. LOG.Write(ex);
  709. return false;
  710. }
  711. return true;
  712. }
  713. /// <summary>
  714. /// 获取当前系统定义的分组
  715. /// </summary>
  716. /// <returns></returns>
  717. public static IEnumerable<string> GetAllRoles()
  718. {
  719. List<string> roles = new List<string>();
  720. try
  721. {
  722. var nodes = _roleXml.SelectNodes("/Aitex/Roles/Role");
  723. foreach (XmlElement node in nodes)
  724. {
  725. roles.Add(node.Attributes["Name"].Value);
  726. }
  727. //如果没有管理员组,默认添加管理员组
  728. if (!roles.Contains("Admin"))
  729. {
  730. roles.Add("Admin");
  731. }
  732. }
  733. catch (Exception ex)
  734. {
  735. LOG.Write(ex);
  736. roles = new List<string>();
  737. }
  738. return roles;
  739. }
  740. /// <summary>
  741. /// 获取指定用户角色的权限设定
  742. /// </summary>
  743. /// <param name="roleName">指定用户的角色名</param>
  744. /// <returns></returns>
  745. public static SerializableDictionary<string, ViewPermission> GetSingleRolePermission(string roleName)
  746. {
  747. var rolePermission = new SerializableDictionary<string, ViewPermission>();
  748. try
  749. {
  750. var viewDic = GetAllViewList();
  751. if (roleName == "Admin")
  752. {
  753. foreach (var view in viewDic)
  754. {
  755. rolePermission.Add(view.Key, ViewPermission.FullyControl);
  756. }
  757. }
  758. else
  759. {
  760. /* RoleName, ViewName, ViewPermission */
  761. var nodes = _roleXml.SelectSingleNode(string.Format("/Aitex/Roles/Role[@Name='{0}']", roleName));
  762. if (nodes != null)
  763. {
  764. foreach (XmlElement viewNode in nodes)
  765. {
  766. var viewName = viewNode.Attributes["Name"].Value;
  767. var permission = viewNode.Attributes["Permission"].Value;
  768. if (viewDic.ContainsKey(viewName))
  769. {
  770. rolePermission.Add(viewName, (ViewPermission)Enum.Parse(typeof(ViewPermission), permission, true));
  771. }
  772. }
  773. }
  774. }
  775. }
  776. catch (Exception ex)
  777. {
  778. LOG.Write(ex);
  779. rolePermission = new SerializableDictionary<string, ViewPermission>();
  780. }
  781. return rolePermission;
  782. }
  783. /// <summary>
  784. /// 获取所有用户角色的权限设定
  785. /// </summary>
  786. /// <returns></returns>
  787. public static SerializableDictionary<string, SerializableDictionary<string, ViewPermission>> GetAllRolesPermission()
  788. {
  789. try
  790. {
  791. var rolePermission = new SerializableDictionary<string, SerializableDictionary<string, ViewPermission>>();
  792. foreach (var role in GetAllRoles())
  793. rolePermission.Add(role, GetSingleRolePermission(role));
  794. return rolePermission;
  795. }
  796. catch (Exception ex)
  797. {
  798. LOG.Write(ex);
  799. return new SerializableDictionary<string, SerializableDictionary<string, ViewPermission>>();
  800. }
  801. }
  802. #endregion
  803. }
  804. }