AccountExManager.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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, Role role)
  59. {
  60. LoginResult result = new LoginResult() {ActSucc = true, SessionId = Guid.NewGuid().ToString() };
  61. try
  62. {
  63. string SpecilAccount = System.Configuration.ConfigurationManager.AppSettings["SpecialAccount"];
  64. var tempAccoutList = RoleLoader.AccountList;
  65. var user = tempAccoutList.FirstOrDefault(f => f.LoginName == userid);
  66. if (user == null)
  67. {
  68. result.ActSucc = false;
  69. result.Description = AuthorizeResult.NoMatchUser.ToString();
  70. }
  71. else
  72. {
  73. foreach (AccountEx ac in tempAccoutList)
  74. {
  75. if (ac.LoginName == userid && ac.Password == password)
  76. {
  77. foreach (string r in ac.RoleIDs)
  78. {
  79. if (r == role.RoleID)
  80. {
  81. if (SpecilAccount == ac.LoginName && role.RoleID=="2")
  82. result.IsSpecialAccount = true;
  83. else
  84. result.IsSpecialAccount = false;
  85. //role.RoleID = r;
  86. //role.MenuPermission = roles[0].MenuPermission;
  87. result.ActSucc = true;
  88. result.Description = AuthorizeResult.None.ToString();
  89. foreach (var accountEx in _loginUserList)
  90. {
  91. if (SC.ContainsItem("System.AllowMultiClientLogin")
  92. && SC.GetValue<bool>("System.AllowMultiClientLogin")
  93. && (accountEx.Value.LoginName!= userid))
  94. {
  95. continue;
  96. }
  97. EV.PostKickoutMessage(accountEx.Value.LoginId);
  98. }
  99. _loginUserList[userid] = ac;
  100. _loginUserList[userid].LoginId = result.SessionId;
  101. EV.PostInfoLog("System", string.Format("{0} login as {1}", userid, role.RoleName));
  102. return result;
  103. }
  104. else
  105. {
  106. result.ActSucc = false;
  107. result.Description = AuthorizeResult.NoMatchRole.ToString();
  108. }
  109. }
  110. return result;
  111. }
  112. else
  113. {
  114. result.ActSucc = false;
  115. result.Description = AuthorizeResult.WrongPwd.ToString();
  116. }
  117. }
  118. }
  119. }
  120. catch (Exception ex)
  121. {
  122. LOG.Error(ex.Message, ex);
  123. }
  124. return result;
  125. }
  126. internal void Logout(string accountId, string loginId)
  127. {
  128. foreach (var key in _loginUserList.Keys)
  129. {
  130. if (accountId == key)
  131. {
  132. if (_loginUserList[key].LoginId == loginId)
  133. {
  134. _loginUserList.Remove(key);
  135. break;
  136. }
  137. }
  138. }
  139. }
  140. }
  141. }