AccountManager.cs 35 KB

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