diff --git a/Falcon.SugarApi/FalconClaim/FalconAuthenticationHandler.cs b/Falcon.SugarApi/FalconClaim/FalconAuthenticationHandler.cs index 49ee73c..c33ce24 100644 --- a/Falcon.SugarApi/FalconClaim/FalconAuthenticationHandler.cs +++ b/Falcon.SugarApi/FalconClaim/FalconAuthenticationHandler.cs @@ -23,6 +23,7 @@ namespace Falcon.SugarApi.FalconClaim this.Scheme = null; this.Context = null; TokenBuilter = tokenBuilter; + this.FalconAuthenticationKey=FalconClaimOption.FalconAuthenticationKey; } /// @@ -30,6 +31,11 @@ namespace Falcon.SugarApi.FalconClaim /// public AuthenticationScheme? Scheme { get; set; } + /// + /// 用于提供验证token的键 + /// + public virtual string FalconAuthenticationKey { get; set; } + /// /// HttpContext 上下文 /// @@ -37,7 +43,7 @@ namespace Falcon.SugarApi.FalconClaim /// public Task AuthenticateAsync() { - if (!this.Context.Request.Headers.TryGetValue(FalconClaimOption.FalconAuthenticationKey, out var val)) { + if (!this.Context.Request.Headers.TryGetValue(this.FalconAuthenticationKey, out var val)) { return UnLoginResultTask; } var token = val.ToString(); diff --git a/Falcon.SugarApi/FalconClaim/ITokenBuilterExtend.cs b/Falcon.SugarApi/FalconClaim/ITokenBuilterExtend.cs new file mode 100644 index 0000000..1a82c9b --- /dev/null +++ b/Falcon.SugarApi/FalconClaim/ITokenBuilterExtend.cs @@ -0,0 +1,20 @@ +using System.Linq; +using System.Security.Claims; + +namespace Falcon.SugarApi.FalconClaim +{ + /// + /// ITokenBuilter扩展 + /// + public static class ITokenBuilterExtend + { + /// + /// 根据声称组生成token + /// + /// TokenBuilder + /// 声称组 + /// token + public static string? GetToken(this ITokenBuilter tokenBuilter,params Claim[] claims) + => tokenBuilter.GetToken(claims.ToList()); + } +} diff --git a/Falcon.SugarApi/FalconClaim/Tables/FalconClaim_Right.cs b/Falcon.SugarApi/FalconClaim/Tables/FalconClaim_Right.cs new file mode 100644 index 0000000..45282f9 --- /dev/null +++ b/Falcon.SugarApi/FalconClaim/Tables/FalconClaim_Right.cs @@ -0,0 +1,16 @@ +namespace Falcon.SugarApi.FalconClaim.Tables +{ + /// + /// 权利 + /// + public class FalconClaim_Right { + /// + /// 权利名称 + /// + public string RightName { get; set; } + /// + /// 权利说明 + /// + public string RightDescription { get; set; } + } +} diff --git a/Falcon.SugarApi/FalconClaim/Tables/FalconClaim_RightInRoles.cs b/Falcon.SugarApi/FalconClaim/Tables/FalconClaim_RightInRoles.cs new file mode 100644 index 0000000..69a824a --- /dev/null +++ b/Falcon.SugarApi/FalconClaim/Tables/FalconClaim_RightInRoles.cs @@ -0,0 +1,18 @@ +namespace Falcon.SugarApi.FalconClaim.Tables +{ + /// + /// 权利和角色对应关系 + /// + public class FalconClaim_RightInRoles + { + /// + /// 权力名称 + /// + public string RightName { get; set; } + /// + /// 角色名称 + /// + public string RoleName { get; set; } + } + +} diff --git a/Falcon.SugarApi/FalconClaim/Tables/FalconClaim_UserInRoles.cs b/Falcon.SugarApi/FalconClaim/Tables/FalconClaim_UserInRoles.cs index 7f5a918..08d919a 100644 --- a/Falcon.SugarApi/FalconClaim/Tables/FalconClaim_UserInRoles.cs +++ b/Falcon.SugarApi/FalconClaim/Tables/FalconClaim_UserInRoles.cs @@ -14,4 +14,5 @@ /// public string RoleName { get; set; } } + } diff --git a/Falcon.SugarApi/FalconClaim/Tables/SugarDbContextExtend.cs b/Falcon.SugarApi/FalconClaim/Tables/SugarDbContextExtend.cs index 238a3b9..0c67594 100644 --- a/Falcon.SugarApi/FalconClaim/Tables/SugarDbContextExtend.cs +++ b/Falcon.SugarApi/FalconClaim/Tables/SugarDbContextExtend.cs @@ -1,4 +1,5 @@ using Falcon.SugarApi.DatabaseDefinitions; +using System; namespace Falcon.SugarApi.FalconClaim.Tables { @@ -11,11 +12,57 @@ namespace Falcon.SugarApi.FalconClaim.Tables /// 初始化FalconClaim角色相关表 /// /// 数据库上下文 + /// 默认超管数据 /// 数据库上下文 - public static SugarDbContext InitFalconClaimRoleDbTables(this SugarDbContext dbContext) { + public static SugarDbContext InitFalconClaimRoleDbTables(this SugarDbContext dbContext,Func? InitDataFunc) { dbContext.CodeFirst.InitTables(); dbContext.CodeFirst.InitTables(); + dbContext.CodeFirst.InitTables(); + dbContext.CodeFirst.InitTables(); + if(InitDataFunc!=null) { + var initData = InitDataFunc(); + var role = initData?.Role; + var roleName = role?.RoleName; + var right = initData?.Right; + var rightName = right?.RightName; + + if(roleName.IsNotNullOrEmpty()) { + var roleQu = dbContext.Queryable().Where(m => m.RoleName==roleName); + if(!roleQu.Any()) { + dbContext.Insertable(role).ExecuteCommand(); + } + } + if(rightName.IsNotNullOrEmpty()) { + var rightQu = dbContext.Queryable().Where(m => m.RightName==rightName); + if(!rightQu.Any()) { + dbContext.Insertable(right).ExecuteCommand(); + } + } + if(roleName.IsNotNullOrEmpty()&&rightName.IsNotNullOrEmpty()) { + var map = dbContext.Queryable().Where(m => m.RoleName==roleName&&m.RightName==rightName); + if(!map.Any()) { + dbContext.Insertable(new FalconClaim_RightInRoles { + RoleName=roleName,RightName=rightName, + }).ExecuteCommand(); + } + } + } return dbContext; } } + + /// + /// 默认初始化超管数据 + /// + public class InitAdministratorData + { + /// + /// 默认超管角色 + /// + public FalconClaim_Roles Role { get; set; } = new FalconClaim_Roles(); + /// + /// 默认超管权利 + /// + public FalconClaim_Right Right { get; set; } = new FalconClaim_Right(); + } } diff --git a/Falcon.SugarApi/FalconClaim/UserLoginResult.cs b/Falcon.SugarApi/FalconClaim/UserLoginResult.cs index 5270f1e..072ca2f 100644 --- a/Falcon.SugarApi/FalconClaim/UserLoginResult.cs +++ b/Falcon.SugarApi/FalconClaim/UserLoginResult.cs @@ -25,6 +25,11 @@ namespace Falcon.SugarApi.FalconClaim /// public DateTime? LoginTime { get; set; } = DateTime.Now; + /// + /// 需要随请求提供的请求头Key + /// + public string AuthKey { get; set; } = FalconClaimOption.FalconAuthenticationKey; + /// /// 获取一条成功的登录信息 /// diff --git a/Falcon.SugarApi/ObjectExtend.cs b/Falcon.SugarApi/ObjectExtend.cs index a6053b9..197c49d 100644 --- a/Falcon.SugarApi/ObjectExtend.cs +++ b/Falcon.SugarApi/ObjectExtend.cs @@ -25,7 +25,7 @@ namespace Falcon.SugarApi join t in target.GetType().GetProperties() on s.Name equals t.Name select new { s,t }; foreach(var item in all) { - //item.t.SetValue(target, Convert.ChangeType(item.s.GetValue(source), item.t.Type)); + //item.t.SetValue(target, Convert.ChangeType(item.s.getValue(source), item.t.Type)); item.t.SetValue(target,item.s.GetValue(source).ChangeType(item.t.PropertyType)); } return source; @@ -109,13 +109,13 @@ namespace Falcon.SugarApi /// 为对象属性设置属性值 /// /// 要设置的对象 - /// 通过属性和原始值获取新值的方法委托 + /// 通过属性和原始值获取新值的方法委托 /// 设置属性值引发的异常 - public static T SetPropertyValue(this T obj,Func GetValue) where T : class { + public static T SetPropertyValue(this T obj,Func getValue) where T : class { foreach(PropertyInfo info in obj.GetType().GetProperties()) { if(info.CanWrite&&info.CanRead) { object? originalVal = info.GetValue(obj); - var val = GetValue(info,originalVal); + var val = getValue(info,originalVal); try { info.SetValue(obj,val); }