AccountManager.cs 37 KB

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