FalconClaim初步

This commit is contained in:
falcon 2023-03-02 17:17:16 +08:00
parent a9a29f2383
commit b0717c4522
8 changed files with 119 additions and 6 deletions

View File

@ -23,6 +23,7 @@ namespace Falcon.SugarApi.FalconClaim
this.Scheme = null; this.Scheme = null;
this.Context = null; this.Context = null;
TokenBuilter = tokenBuilter; TokenBuilter = tokenBuilter;
this.FalconAuthenticationKey=FalconClaimOption.FalconAuthenticationKey;
} }
/// <summary> /// <summary>
@ -30,6 +31,11 @@ namespace Falcon.SugarApi.FalconClaim
/// </summary> /// </summary>
public AuthenticationScheme? Scheme { get; set; } public AuthenticationScheme? Scheme { get; set; }
/// <summary>
/// 用于提供验证token的键
/// </summary>
public virtual string FalconAuthenticationKey { get; set; }
/// <summary> /// <summary>
/// HttpContext 上下文 /// HttpContext 上下文
/// </summary> /// </summary>
@ -37,7 +43,7 @@ namespace Falcon.SugarApi.FalconClaim
/// <inheritdoc /> /// <inheritdoc />
public Task<AuthenticateResult> AuthenticateAsync() { public Task<AuthenticateResult> 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; return UnLoginResultTask;
} }
var token = val.ToString(); var token = val.ToString();

View File

@ -0,0 +1,20 @@
using System.Linq;
using System.Security.Claims;
namespace Falcon.SugarApi.FalconClaim
{
/// <summary>
/// ITokenBuilter扩展
/// </summary>
public static class ITokenBuilterExtend
{
/// <summary>
/// 根据声称组生成token
/// </summary>
/// <param name="tokenBuilter">TokenBuilder</param>
/// <param name="claims">声称组</param>
/// <returns>token</returns>
public static string? GetToken(this ITokenBuilter tokenBuilter,params Claim[] claims)
=> tokenBuilter.GetToken(claims.ToList());
}
}

View File

@ -0,0 +1,16 @@
namespace Falcon.SugarApi.FalconClaim.Tables
{
/// <summary>
/// 权利
/// </summary>
public class FalconClaim_Right {
/// <summary>
/// 权利名称
/// </summary>
public string RightName { get; set; }
/// <summary>
/// 权利说明
/// </summary>
public string RightDescription { get; set; }
}
}

View File

@ -0,0 +1,18 @@
namespace Falcon.SugarApi.FalconClaim.Tables
{
/// <summary>
/// 权利和角色对应关系
/// </summary>
public class FalconClaim_RightInRoles
{
/// <summary>
/// 权力名称
/// </summary>
public string RightName { get; set; }
/// <summary>
/// 角色名称
/// </summary>
public string RoleName { get; set; }
}
}

View File

@ -14,4 +14,5 @@
/// </summary> /// </summary>
public string RoleName { get; set; } public string RoleName { get; set; }
} }
} }

View File

@ -1,4 +1,5 @@
using Falcon.SugarApi.DatabaseDefinitions; using Falcon.SugarApi.DatabaseDefinitions;
using System;
namespace Falcon.SugarApi.FalconClaim.Tables namespace Falcon.SugarApi.FalconClaim.Tables
{ {
@ -11,11 +12,57 @@ namespace Falcon.SugarApi.FalconClaim.Tables
/// 初始化FalconClaim角色相关表 /// 初始化FalconClaim角色相关表
/// </summary> /// </summary>
/// <param name="dbContext">数据库上下文</param> /// <param name="dbContext">数据库上下文</param>
/// <param name="InitDataFunc">默认超管数据</param>
/// <returns>数据库上下文</returns> /// <returns>数据库上下文</returns>
public static SugarDbContext InitFalconClaimRoleDbTables(this SugarDbContext dbContext) { public static SugarDbContext InitFalconClaimRoleDbTables(this SugarDbContext dbContext,Func<InitAdministratorData>? InitDataFunc) {
dbContext.CodeFirst.InitTables<FalconClaim_Roles>(); dbContext.CodeFirst.InitTables<FalconClaim_Roles>();
dbContext.CodeFirst.InitTables<FalconClaim_UserInRoles>(); dbContext.CodeFirst.InitTables<FalconClaim_UserInRoles>();
dbContext.CodeFirst.InitTables<FalconClaim_Right>();
dbContext.CodeFirst.InitTables<FalconClaim_RightInRoles>();
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<FalconClaim_Roles>().Where(m => m.RoleName==roleName);
if(!roleQu.Any()) {
dbContext.Insertable(role).ExecuteCommand();
}
}
if(rightName.IsNotNullOrEmpty()) {
var rightQu = dbContext.Queryable<FalconClaim_Right>().Where(m => m.RightName==rightName);
if(!rightQu.Any()) {
dbContext.Insertable(right).ExecuteCommand();
}
}
if(roleName.IsNotNullOrEmpty()&&rightName.IsNotNullOrEmpty()) {
var map = dbContext.Queryable<FalconClaim_RightInRoles>().Where(m => m.RoleName==roleName&&m.RightName==rightName);
if(!map.Any()) {
dbContext.Insertable(new FalconClaim_RightInRoles {
RoleName=roleName,RightName=rightName,
}).ExecuteCommand();
}
}
}
return dbContext; return dbContext;
} }
} }
/// <summary>
/// 默认初始化超管数据
/// </summary>
public class InitAdministratorData
{
/// <summary>
/// 默认超管角色
/// </summary>
public FalconClaim_Roles Role { get; set; } = new FalconClaim_Roles();
/// <summary>
/// 默认超管权利
/// </summary>
public FalconClaim_Right Right { get; set; } = new FalconClaim_Right();
}
} }

View File

@ -25,6 +25,11 @@ namespace Falcon.SugarApi.FalconClaim
/// </summary> /// </summary>
public DateTime? LoginTime { get; set; } = DateTime.Now; public DateTime? LoginTime { get; set; } = DateTime.Now;
/// <summary>
/// 需要随请求提供的请求头Key
/// </summary>
public string AuthKey { get; set; } = FalconClaimOption.FalconAuthenticationKey;
/// <summary> /// <summary>
/// 获取一条成功的登录信息 /// 获取一条成功的登录信息
/// </summary> /// </summary>

View File

@ -25,7 +25,7 @@ namespace Falcon.SugarApi
join t in target.GetType().GetProperties() on s.Name equals t.Name join t in target.GetType().GetProperties() on s.Name equals t.Name
select new { s,t }; select new { s,t };
foreach(var item in all) { 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)); item.t.SetValue(target,item.s.GetValue(source).ChangeType(item.t.PropertyType));
} }
return source; return source;
@ -109,13 +109,13 @@ namespace Falcon.SugarApi
/// 为对象属性设置属性值 /// 为对象属性设置属性值
/// </summary> /// </summary>
/// <param name="obj">要设置的对象</param> /// <param name="obj">要设置的对象</param>
/// <param name="GetValue">通过属性和原始值获取新值的方法委托</param> /// <param name="getValue">通过属性和原始值获取新值的方法委托</param>
/// <exception cref="ObjectSetValueException">设置属性值引发的异常</exception> /// <exception cref="ObjectSetValueException">设置属性值引发的异常</exception>
public static T SetPropertyValue<T>(this T obj,Func<PropertyInfo,object?,object?> GetValue) where T : class { public static T SetPropertyValue<T>(this T obj,Func<PropertyInfo,object?,object?> getValue) where T : class {
foreach(PropertyInfo info in obj.GetType().GetProperties()) { foreach(PropertyInfo info in obj.GetType().GetProperties()) {
if(info.CanWrite&&info.CanRead) { if(info.CanWrite&&info.CanRead) {
object? originalVal = info.GetValue(obj); object? originalVal = info.GetValue(obj);
var val = GetValue(info,originalVal); var val = getValue(info,originalVal);
try { try {
info.SetValue(obj,val); info.SetValue(obj,val);
} }