天津投入产出系统后端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

265 lines
9.7 KiB

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<ClientVerificationInformation> VerifyList = null;
/// <summary>
/// 初始化客户端验证模块
/// </summary>
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<ClientVerificationInformation>();
using (IDataSession session = DataFactory.CreateSession())
{
dt = session.GetTable(sql, new List<DataParameter>().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();
}
}
/// <summary>
/// 验证客户端权限
/// </summary>
/// <param name="clientSN">客户端授权码</param>
/// <returns></returns>
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();
}
/// <summary>
/// 获取客户端的权限,不符合权限要求的返回普通用户权限
/// </summary>
/// <param name="clientSN">客户端授权码</param>
/// <returns></returns>
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;
}
/// <summary>
/// 创建新的客户端授权信息
/// </summary>
/// <param name="clientSN">授权人的授权码</param>
/// <param name="addSN">要创建的授权码</param>
/// <param name="ua">要创建的权限级别</param>
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<DataParameter>().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();
}
}
}
/// <summary>
/// 修改指定客户端的授权
/// </summary>
/// <param name="clientSN">授权人的授权码</param>
/// <param name="targetSN">要修改的授权码</param>
/// <param name="ua">要赋予的权限</param>
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();
}
}
}
/// <summary>
/// 吊销指定客户端的授权
/// </summary>
/// <param name="clientSN">授权人的授权码</param>
/// <param name="targetSN">要修改的授权码</param>
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();
}
}
}
/// <summary>
/// 获取授权列表
/// </summary>
/// <param name="clientSN">操作者的授权码</param>
/// <returns></returns>
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<DataParameter>().ToArray());
}
}
catch (Exception ex)
{
throw new Exceptions.ExcuteFailsException();
}
}
}
}
}