Skip to content

Commit

Permalink
wip: (auth) 优化功能-角色相关的日志记录,使用功能名代替Id
Browse files Browse the repository at this point in the history
  • Loading branch information
gmf520 committed Oct 6, 2023
1 parent 8fdfbcf commit a18443c
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion samples/web/Liuliu.Demo.WebApi/Startup.cs
Expand Up @@ -42,7 +42,7 @@ public void ConfigureServices(IServiceCollection services)
.AddPack<FunctionAuthorizationPack>()
.AddPack<DataAuthorizationPack>()
.AddPack<SqlServerDefaultDbContextMigrationPack>()
.AddPack<HangfirePack>()
//.AddPack<HangfirePack>()
.AddPack<AuditPack>()
.AddPack<InfosPack>();
}
Expand Down
6 changes: 3 additions & 3 deletions samples/web/Liuliu.Demo.WebApi/appsettings.Development.json
@@ -1,9 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Debug",
"OSharp": "Debug",
"Liuliu": "Debug",
"Default": "Information",
"OSharp": "Information",
"Liuliu": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Warning"
}
Expand Down
8 changes: 6 additions & 2 deletions src/OSharp.Authorization.Functions/FunctionAuthCacheBase.cs
Expand Up @@ -7,6 +7,8 @@
// <last-date>2018-05-11 0:59</last-date>
// -----------------------------------------------------------------------

using System.Diagnostics.CodeAnalysis;


namespace OSharp.Authorization;

Expand Down Expand Up @@ -99,13 +101,15 @@ public virtual void RemoveUserCaches(params string[] userNames)
public string[] GetFunctionRoles(Guid functionId, IServiceProvider scopeProvider = null, bool forceRefresh = false)
{
string key = GetFunctionRolesKey(functionId);
IFunctionHandler functionHandler = _serviceProvider.GetRequiredService<IFunctionHandler>();
var function = functionHandler.GetFunction(functionId);
string[] roleNames;
if (!forceRefresh)
{
roleNames = _cache.Get<string[]>(key);
if (roleNames != null)
{
_logger.LogDebug($"从缓存中获取到功能“{functionId}”的“Function-Roles[]”缓存,角色数:{roleNames.Length}");
_logger.LogDebug($"从缓存中获取到功能“{function.Name}”的“Function-Roles[]”缓存,角色数:{roleNames.Length}");
return roleNames;
}
}
Expand Down Expand Up @@ -139,7 +143,7 @@ public string[] GetFunctionRoles(Guid functionId, IServiceProvider scopeProvider
// 有效期为 7 ± 1 天
int seconds = 7 * 24 * 3600 + _random.Next(-24 * 3600, 24 * 3600);
_cache.Set(key, roleNames, seconds);
_logger.LogDebug($"添加功能“{functionId}”的“Function-Roles[]”缓存,角色数:{roleNames.Length}");
_logger.LogDebug($"添加功能“{function.Name}”的“Function-Roles[]”缓存,角色数:{roleNames.Length}");

serviceScope?.Dispose();
return roleNames;
Expand Down
2 changes: 1 addition & 1 deletion src/OSharp.Authorization.Functions/ModuleHandlerBase.cs
Expand Up @@ -30,7 +30,7 @@ protected ModuleHandlerBase(IServiceProvider serviceProvider)
_serviceProvider = serviceProvider;
_moduleInfoPicker = serviceProvider.GetService<IModuleInfoPicker>();
Logger = serviceProvider.GetLogger(GetType());
ModuleInfos = new ModuleInfo[0];
ModuleInfos = Array.Empty<ModuleInfo>();
}

/// <summary>
Expand Down
17 changes: 16 additions & 1 deletion src/OSharp/Authorization/Functions/FunctionHandlerBase.cs
Expand Up @@ -62,6 +62,21 @@ public void Initialize()
/// <returns></returns>
public abstract MethodInfo[] GetMethodInfos(Type functionType);

/// <summary>
/// 查找指定编号的功能信息
/// </summary>
/// <param name="functionId">功能编号</param>
/// <returns></returns>
public IFunction GetFunction(Guid functionId)
{
if (_functions.Count == 0)
{
RefreshCache();
}

return _functions.FirstOrDefault(m => m.Id.Equals(functionId));
}

/// <summary>
/// 查找指定条件的功能信息
/// </summary>
Expand Down Expand Up @@ -362,4 +377,4 @@ protected virtual TFunction[] GetFromDatabase(IServiceProvider scopedProvider)
TFunction[] functions = repository.QueryAsNoTracking(null, false).ToArray();
return functions;
}
}
}
9 changes: 8 additions & 1 deletion src/OSharp/Authorization/Functions/IFunctionHandler.cs
Expand Up @@ -32,6 +32,13 @@ public interface IFunctionHandler
/// <returns></returns>
MethodInfo[] GetMethodInfos(Type functionType);

/// <summary>
/// 查找指定编号的功能信息
/// </summary>
/// <param name="functionId">功能编号</param>
/// <returns></returns>
IFunction GetFunction(Guid functionId);

/// <summary>
/// 查找指定条件的功能信息
/// </summary>
Expand All @@ -50,4 +57,4 @@ public interface IFunctionHandler
/// 清空功能信息缓存
/// </summary>
void ClearCache();
}
}

0 comments on commit a18443c

Please sign in to comment.