using System; using System.Collections.Generic; using System.Linq; using System.Text; using Common.TextUtil; using System.Data; using QMFrameWork.Data; namespace Common.Applications.Verification { public static class ClientVerification { private static List VerifyList = null; /// /// 初始化客户端验证模块 /// public static void Init() { string sql = ""; DataTable dt = null; //Data.SqlLite.SqlLiteHelper dal = new Data.SqlLite.SqlLiteHelper(); try { sql = "SELECT * FROM T_QM_HISLICENSE"; VerifyList = new List(); using (IDataSession session = DataFactory.CreateSession()) { dt = session.GetTable(sql, new List().ToArray()); } foreach (DataRow row in dt.Rows) { ClientVerificationInformation info = new ClientVerificationInformation(); info.ClientSN = row["CLIENTSN"].ToString(); info.UserAuthority = (Data.UserAuthority)(int.Parse(row["USERAUTHORITY"].ToString())); info.FlagDel = (Data.FlagDel)(int.Parse(row["FLAGDEL"].ToString())); info.CreateTime = row["CREATETIME"].ToString(); VerifyList.Add(info); } } catch (Exception ex) { throw new Exceptions.CannotInitConfigException(); } } /// /// 验证客户端权限 /// /// 客户端授权码 /// public static Data.UserAuthority VerifyClient(string clientSN) { if (!clientSN.IsEmptyOrNull()) { var n = VerifyList.Find(p => p.ClientSN == clientSN); if (n != null) { if (n.FlagDel == Data.FlagDel.Deleted) { throw new Exceptions.LicenseRevokedException(); } else { return n.UserAuthority; } } } throw new Exceptions.NoneLicenseException(); } /// /// 获取客户端的权限,不符合权限要求的返回普通用户权限 /// /// 客户端授权码 /// public static Data.UserAuthority GetVerification(string clientSN) { if (!clientSN.IsEmptyOrNull()) { var n = VerifyList.Find(p => p.ClientSN == clientSN); if (n != null) { if (n.FlagDel != Data.FlagDel.Deleted) { return n.UserAuthority; } } } return Data.UserAuthority.User; } /// /// 创建新的客户端授权信息 /// /// 授权人的授权码 /// 要创建的授权码 /// 要创建的权限级别 public static void AddVerification(string clientSN, string addSN, Data.UserAuthority ua) { Data.UserAuthority auth = VerifyClient(clientSN); if (auth == Data.UserAuthority.User || auth == Data.UserAuthority.PowerUser) { throw new Exceptions.NotEnoughPermissionException(); } else { if (ua.GetHashCode() > auth.GetHashCode()) { throw new Exceptions.NotEnoughPermissionException(); } try { using (IDataSession session = DataFactory.CreateSession()) { DataTable table = session.GetTable("SELECT * FROM T_LICENSE WHERE CLIENTSN='" + addSN + "'",new List().ToArray()); if (table.Rows.Count > 0) { throw new Exceptions.DataExistException(); } session.ExecuteSql("INSERT INTO T_LICENSE(CLIENTSN,USERAUTHORITY,FLAGDEL,CREATETIME) VALUES('" + addSN + "','" + ua.GetHashCode().ToString() + "','0',DATETIME())"); } VerifyList.Add(new ClientVerificationInformation() { ClientSN = addSN, CreateTime = DateTime.Now.ToString(TextUtil.DateTimeUtil.DATETIME_YYYYMMDD_HHMMSS), FlagDel = Data.FlagDel.Normal, UserAuthority = ua }); } catch (Exceptions.DataExistException ex) { throw ex; } catch (Exception ex) { throw new Exceptions.ExcuteFailsException(); } } } /// /// 修改指定客户端的授权 /// /// 授权人的授权码 /// 要修改的授权码 /// 要赋予的权限 public static void ModifyAuthority(string clientSN, string targetSN, Data.UserAuthority ua) { Data.UserAuthority clientAuth = VerifyClient(clientSN); Data.UserAuthority targetAuth = VerifyClient(targetSN); if (clientAuth == Data.UserAuthority.User || clientAuth == Data.UserAuthority.PowerUser) { throw new Exceptions.NotEnoughPermissionException(); } else { if (targetAuth.GetHashCode() > clientAuth.GetHashCode()) { throw new Exceptions.NotEnoughPermissionException(); } if (ua.GetHashCode() > clientAuth.GetHashCode()) { throw new Exceptions.NotEnoughPermissionException(); } try { using (IDataSession session = DataFactory.CreateSession()) { session.ExecuteSql("UPDATE T_LICENSE SET USERAUTHORITY='" + ua.GetHashCode().ToString() + "' WHERE CLIENTSN='" + targetSN + "'"); } var n = VerifyList.Find(p => p.ClientSN == targetSN); n.UserAuthority = ua; } catch (Exceptions.DataExistException ex) { throw ex; } catch (Exception ex) { throw new Exceptions.ExcuteFailsException(); } } } /// /// 吊销指定客户端的授权 /// /// 授权人的授权码 /// 要修改的授权码 public static void DeleteAuthority(string clientSN, string targetSN) { Data.UserAuthority clientAuth = VerifyClient(clientSN); Data.UserAuthority targetAuth = VerifyClient(targetSN); if (clientAuth == Data.UserAuthority.User || clientAuth == Data.UserAuthority.PowerUser) { throw new Exceptions.NotEnoughPermissionException(); } else { if (targetAuth.GetHashCode() > clientAuth.GetHashCode()) { throw new Exceptions.NotEnoughPermissionException(); } try { using (IDataSession session = DataFactory.CreateSession()) { session.ExecuteSql("UPDATE T_LICENSE SET FLAGDEL='1' WHERE CLIENTSN='" + targetSN + "'"); } var n = VerifyList.Find(p => p.ClientSN == targetSN); n.FlagDel = Data.FlagDel.Deleted; } catch (Exceptions.DataExistException ex) { throw ex; } catch (Exception ex) { throw new Exceptions.ExcuteFailsException(); } } } /// /// 获取授权列表 /// /// 操作者的授权码 /// public static System.Data.DataTable SearchAuthority(string clientSN) { Data.UserAuthority clientAuth = VerifyClient(clientSN); if (clientAuth == Data.UserAuthority.User || clientAuth == Data.UserAuthority.PowerUser) { throw new Exceptions.NotEnoughPermissionException(); } else { try { using (IDataSession session = DataFactory.CreateSession()) { return session.GetTable("SELECT * FROM T_LICENSE WHERE USERAUTHORITY<'" + clientAuth.GetHashCode().ToString() + "'",new List().ToArray()); } } catch (Exception ex) { throw new Exceptions.ExcuteFailsException(); } } } } }