Authorization.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Net;
  7. using System.Timers;
  8. using Aitex.Core.RT.Event;
  9. namespace Aitex.Core.Account
  10. {
  11. public enum AuthorizationStatusEnum
  12. {
  13. NoAuthorization,
  14. Authorizing,
  15. Granted,
  16. Rejected,
  17. }
  18. public static class Authorization
  19. {
  20. public static string Module { get; set; }
  21. static Authorization()
  22. {
  23. Module = "System";
  24. AuthorizedAccount = string.Empty;
  25. AuthorizedIP = string.Empty;
  26. AuthorizingAccount = string.Empty;
  27. AuthorizingIP = string.Empty;
  28. // Update();
  29. _timer = new Timer(3 * 60*1000);//3 minutes
  30. _timer.AutoReset = false;
  31. _timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
  32. }
  33. static void Timer_Elapsed(object sender, ElapsedEventArgs e)
  34. {
  35. if (_status == AuthorizationStatusEnum.Authorizing)
  36. {
  37. Grant(true);
  38. }
  39. }
  40. public static AuthorizationStatusEnum Status
  41. {
  42. get
  43. {
  44. return _status;
  45. }
  46. }
  47. public static bool IsAuthorizedAccount(string accountId, string ip)
  48. {
  49. if (string.IsNullOrWhiteSpace(AuthorizedAccount))
  50. {
  51. EV.PostMessage(Module, EventEnum.AccountWithoutAuthorization, accountId);
  52. return false;
  53. }
  54. if (ip == Aitex.Core.Utilities.Network.LocalIP && AuthorizedIP == Aitex.Core.Utilities.Network.LocalIP) return true;
  55. if (AuthorizedAccount != accountId)
  56. {
  57. EV.PostMessage(Module, EventEnum.AccountWithoutAuthorization, accountId);
  58. return false;
  59. }
  60. return true;
  61. }
  62. static bool CanAutoAuthorize(string accountId, string ip)
  63. {
  64. return string.IsNullOrWhiteSpace(AuthorizedAccount) //未有授权用户
  65. || (ip == Aitex.Core.Utilities.Network.LocalIP) //申请IP来自服务器同一地址
  66. || (accountId == AuthorizedAccount); //已授权账户
  67. //|| (AuthorizedIP == ServerIP && ip == ServerIP) //本地IP已授权,且本地账户申请权限
  68. }
  69. public static string AuthorizedAccount
  70. {
  71. get;
  72. private set;
  73. }
  74. public static string AuthorizedIP
  75. {
  76. get;
  77. private set;
  78. }
  79. public static string AuthorizingAccount
  80. {
  81. get;
  82. private set;
  83. }
  84. public static string AuthorizingIP
  85. {
  86. get;
  87. private set;
  88. }
  89. /// <summary>
  90. /// User applies operation & control authorization
  91. /// </summary>
  92. /// <param name="accountId">client user</param>
  93. /// <param name="ip">client ip</param>
  94. public static void Request(string accountId, string ip)
  95. {
  96. EV.PostMessage(Module, EventEnum.OperationAuthorization, string.Format("{0} 在申请操控权", accountId));
  97. if (CanAutoAuthorize(accountId, ip))
  98. {
  99. AuthorizedAccount = accountId;
  100. AuthorizedIP = ip;
  101. EV.PostMessage(Module, EventEnum.OperationAuthorization, string.Format("{0} 获得操控权", AuthorizedAccount));
  102. _status = AuthorizationStatusEnum.Granted;
  103. Update();
  104. return;
  105. }
  106. AuthorizingAccount = accountId;
  107. AuthorizingIP = ip;
  108. _status = AuthorizationStatusEnum.Authorizing;
  109. Update();
  110. _timer.Start();
  111. }
  112. /// <summary>
  113. /// Authorizing user aborts this authorization operation.
  114. /// </summary>
  115. public static void Abort()
  116. {
  117. _timer.Stop();
  118. _status = AuthorizationStatusEnum.NoAuthorization;
  119. AuthorizingAccount = string.Empty;
  120. AuthorizingIP = string.Empty;
  121. Update();
  122. }
  123. /// <summary>
  124. /// Authorized user grants or rejects the authorization
  125. /// </summary>
  126. /// <param name="isGranted">True: granted False: Rejected</param>
  127. public static void Grant(bool isGranted)
  128. {
  129. _timer.Stop();
  130. if (_status == AuthorizationStatusEnum.Granted) return;
  131. if (isGranted)
  132. {
  133. AuthorizedAccount = AuthorizingAccount;
  134. AuthorizedIP = AuthorizingIP;
  135. _status = AuthorizationStatusEnum.Granted;
  136. EV.PostMessage(Module, EventEnum.OperationAuthorization, string.Format("{0} 获得操控权", AuthorizedAccount));
  137. }
  138. else
  139. {
  140. _status = AuthorizationStatusEnum.Rejected;
  141. EV.PostMessage(Module, EventEnum.OperationAuthorization, string.Format("{0} 拒绝转交操控权", AuthorizedAccount));
  142. }
  143. Update();
  144. }
  145. public static void Exit(string accountId)
  146. {
  147. if (accountId == AuthorizedAccount)
  148. {
  149. AuthorizedAccount = string.Empty;
  150. AuthorizedIP = string.Empty;
  151. Abort();
  152. }
  153. else if (accountId == AuthorizingAccount)
  154. {
  155. Abort();
  156. }
  157. }
  158. #region private
  159. static Timer _timer = new Timer();
  160. static AuthorizationStatusEnum _status = AuthorizationStatusEnum.NoAuthorization;
  161. static void Update()
  162. {
  163. //ViewModelManager.UpdateAttribute(ChamberSet.Transfer, "Authorization", "AuthorizedAccount", AuthorizedAccount);
  164. //ViewModelManager.UpdateAttribute(ChamberSet.Transfer, "Authorization", "AuthorizedIP", AuthorizedIP);
  165. //ViewModelManager.UpdateAttribute(ChamberSet.Transfer, "Authorization", "AuthorizingAccount", AuthorizingAccount);
  166. //ViewModelManager.UpdateAttribute(ChamberSet.Transfer, "Authorization", "AuthorizingIP", AuthorizingIP);
  167. //ViewModelManager.UpdateAttribute(ChamberSet.Transfer, "Authorization", "AuthorizationStatus", Status.ToString());
  168. //ViewModelManager.UpdateAttribute(ChamberSet.Transfer, "Authorization", "ServerIP", Aitex.Core.Utilities.Network.Network.LocalIP);
  169. }
  170. #endregion
  171. }
  172. }