123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Net;
- using System.Timers;
- using Aitex.Core.RT.Event;
- namespace Aitex.Core.Account
- {
- public enum AuthorizationStatusEnum
- {
- NoAuthorization,
- Authorizing,
- Granted,
- Rejected,
- }
-
- public static class Authorization
- {
- public static string Module { get; set; }
- static Authorization()
- {
- Module = "System";
- AuthorizedAccount = string.Empty;
- AuthorizedIP = string.Empty;
- AuthorizingAccount = string.Empty;
- AuthorizingIP = string.Empty;
- // Update();
- _timer = new Timer(3 * 60*1000);//3 minutes
- _timer.AutoReset = false;
- _timer.Elapsed += new ElapsedEventHandler(Timer_Elapsed);
- }
- static void Timer_Elapsed(object sender, ElapsedEventArgs e)
- {
- if (_status == AuthorizationStatusEnum.Authorizing)
- {
- Grant(true);
- }
- }
- public static AuthorizationStatusEnum Status
- {
- get
- {
- return _status;
- }
- }
- public static bool IsAuthorizedAccount(string accountId, string ip)
- {
- if (string.IsNullOrWhiteSpace(AuthorizedAccount))
- {
- EV.PostMessage(Module, EventEnum.AccountWithoutAuthorization, accountId);
- return false;
- }
- if (ip == Aitex.Core.Utilities.Network.LocalIP && AuthorizedIP == Aitex.Core.Utilities.Network.LocalIP) return true;
- if (AuthorizedAccount != accountId)
- {
- EV.PostMessage(Module, EventEnum.AccountWithoutAuthorization, accountId);
- return false;
- }
- return true;
- }
- static bool CanAutoAuthorize(string accountId, string ip)
- {
- return string.IsNullOrWhiteSpace(AuthorizedAccount) //未有授权用户
- || (ip == Aitex.Core.Utilities.Network.LocalIP) //申请IP来自服务器同一地址
- || (accountId == AuthorizedAccount); //已授权账户
- //|| (AuthorizedIP == ServerIP && ip == ServerIP) //本地IP已授权,且本地账户申请权限
- }
- public static string AuthorizedAccount
- {
- get;
- private set;
- }
- public static string AuthorizedIP
- {
- get;
- private set;
- }
- public static string AuthorizingAccount
- {
- get;
- private set;
- }
- public static string AuthorizingIP
- {
- get;
- private set;
- }
- /// <summary>
- /// User applies operation & control authorization
- /// </summary>
- /// <param name="accountId">client user</param>
- /// <param name="ip">client ip</param>
- public static void Request(string accountId, string ip)
- {
- EV.PostMessage(Module, EventEnum.OperationAuthorization, string.Format("{0} 在申请操控权", accountId));
- if (CanAutoAuthorize(accountId, ip))
- {
- AuthorizedAccount = accountId;
- AuthorizedIP = ip;
- EV.PostMessage(Module, EventEnum.OperationAuthorization, string.Format("{0} 获得操控权", AuthorizedAccount));
- _status = AuthorizationStatusEnum.Granted;
- Update();
- return;
- }
- AuthorizingAccount = accountId;
- AuthorizingIP = ip;
- _status = AuthorizationStatusEnum.Authorizing;
- Update();
- _timer.Start();
- }
- /// <summary>
- /// Authorizing user aborts this authorization operation.
- /// </summary>
- public static void Abort()
- {
- _timer.Stop();
- _status = AuthorizationStatusEnum.NoAuthorization;
- AuthorizingAccount = string.Empty;
- AuthorizingIP = string.Empty;
- Update();
- }
- /// <summary>
- /// Authorized user grants or rejects the authorization
- /// </summary>
- /// <param name="isGranted">True: granted False: Rejected</param>
- public static void Grant(bool isGranted)
- {
- _timer.Stop();
- if (_status == AuthorizationStatusEnum.Granted) return;
- if (isGranted)
- {
- AuthorizedAccount = AuthorizingAccount;
- AuthorizedIP = AuthorizingIP;
- _status = AuthorizationStatusEnum.Granted;
- EV.PostMessage(Module, EventEnum.OperationAuthorization, string.Format("{0} 获得操控权", AuthorizedAccount));
- }
- else
- {
- _status = AuthorizationStatusEnum.Rejected;
- EV.PostMessage(Module, EventEnum.OperationAuthorization, string.Format("{0} 拒绝转交操控权", AuthorizedAccount));
- }
- Update();
- }
- public static void Exit(string accountId)
- {
- if (accountId == AuthorizedAccount)
- {
- AuthorizedAccount = string.Empty;
- AuthorizedIP = string.Empty;
-
- Abort();
- }
- else if (accountId == AuthorizingAccount)
- {
- Abort();
- }
- }
- #region private
- static Timer _timer = new Timer();
- static AuthorizationStatusEnum _status = AuthorizationStatusEnum.NoAuthorization;
- static void Update()
- {
- //ViewModelManager.UpdateAttribute(ChamberSet.Transfer, "Authorization", "AuthorizedAccount", AuthorizedAccount);
- //ViewModelManager.UpdateAttribute(ChamberSet.Transfer, "Authorization", "AuthorizedIP", AuthorizedIP);
- //ViewModelManager.UpdateAttribute(ChamberSet.Transfer, "Authorization", "AuthorizingAccount", AuthorizingAccount);
- //ViewModelManager.UpdateAttribute(ChamberSet.Transfer, "Authorization", "AuthorizingIP", AuthorizingIP);
- //ViewModelManager.UpdateAttribute(ChamberSet.Transfer, "Authorization", "AuthorizationStatus", Status.ToString());
- //ViewModelManager.UpdateAttribute(ChamberSet.Transfer, "Authorization", "ServerIP", Aitex.Core.Utilities.Network.Network.LocalIP);
- }
- #endregion
- }
- }
|