AccountExManager.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using Aitex.Common.Util;
  9. using Aitex.Core.Account;
  10. using Aitex.Core.RT.Event;
  11. using Aitex.Core.RT.Log;
  12. using Aitex.Core.RT.SCCore;
  13. using Aitex.Core.Util;
  14. using Aitex.Core.WCF;
  15. using MECF.Framework.Common.Account.Extends;
  16. using MECF.Framework.UI.Core.Accounts;
  17. namespace MECF.Framework.Common.Account
  18. {
  19. public enum AuthorizeResult
  20. {
  21. None = -1,
  22. WrongPwd = 1,
  23. HasLogin = 2,
  24. NoMatchRole = 3,
  25. NoSession = 4,
  26. NoMatchUser = 5
  27. }
  28. public class AccountExManager : Singleton<AccountExManager>
  29. {
  30. public RoleLoader RoleLoader { get; private set; }
  31. Dictionary<string /*account id , login name*/, AccountEx> _loginUserList = new Dictionary<string, AccountEx>();
  32. private string _scAccountFile = PathManager.GetCfgDir() + "Account//Account.xml";
  33. private string _scAccountLocalFile = PathManager.GetCfgDir() + "Account//_Account.xml";
  34. public AccountExManager()
  35. {
  36. }
  37. public void Initialize(bool enableService)
  38. {
  39. if (!File.Exists(_scAccountLocalFile))
  40. {
  41. if (!File.Exists(_scAccountFile))
  42. {
  43. throw new ApplicationException($"Can not initialize account configuration file, {_scAccountFile}");
  44. }
  45. File.Copy(_scAccountFile, _scAccountLocalFile);
  46. Thread.Sleep(10);
  47. }
  48. this.RoleLoader = new RoleLoader(_scAccountLocalFile);
  49. this.RoleLoader.Load();
  50. if (enableService)
  51. {
  52. Singleton<WcfServiceManager>.Instance.Initialize(new Type[]
  53. {
  54. typeof(AccountService)
  55. });
  56. }
  57. }
  58. public LoginResult AuthLogin(string userid, string password, string role)
  59. {
  60. LoginResult result = new LoginResult() {ActSucc = true, SessionId = Guid.NewGuid().ToString() };
  61. try
  62. {
  63. var tempAccoutList = RoleLoader.AccountList;
  64. var user = tempAccoutList.FirstOrDefault(f => f.LoginName == userid);
  65. if (user == null)
  66. {
  67. result.ActSucc = false;
  68. result.Description = AuthorizeResult.NoMatchUser.ToString();
  69. }
  70. else
  71. {
  72. foreach (AccountEx ac in tempAccoutList)
  73. {
  74. if (ac.LoginName == userid && ac.Password == password)
  75. {
  76. foreach (string r in ac.RoleIDs)
  77. {
  78. if (r == role || userid == "AMTEadmin")
  79. {
  80. result.ActSucc = true;
  81. result.Description = AuthorizeResult.None.ToString();
  82. foreach (var accountEx in _loginUserList)
  83. {
  84. if (SC.ContainsItem("System.AllowMultiClientLogin")
  85. && SC.GetValue<bool>("System.AllowMultiClientLogin")
  86. && (accountEx.Value.LoginName!= userid))
  87. {
  88. continue;
  89. }
  90. EV.PostKickoutMessage(accountEx.Value.LoginId);
  91. }
  92. _loginUserList[userid] = ac;
  93. _loginUserList[userid].LoginId = result.SessionId;
  94. return result;
  95. }
  96. else
  97. {
  98. result.ActSucc = false;
  99. result.Description = AuthorizeResult.NoMatchRole.ToString();
  100. }
  101. }
  102. return result;
  103. }
  104. else
  105. {
  106. result.ActSucc = false;
  107. result.Description = AuthorizeResult.WrongPwd.ToString();
  108. }
  109. }
  110. }
  111. }
  112. catch (Exception ex)
  113. {
  114. LOG.Error(ex.Message, ex);
  115. }
  116. return result;
  117. }
  118. internal void Logout(string accountId, string loginId)
  119. {
  120. foreach (var key in _loginUserList.Keys)
  121. {
  122. if (accountId == key)
  123. {
  124. if (_loginUserList[key].LoginId == loginId)
  125. {
  126. _loginUserList.Remove(key);
  127. break;
  128. }
  129. }
  130. }
  131. }
  132. }
  133. }