Skip to content

Commit

Permalink
🐛 (auth) 修复授权模块缺失OsharpPolicy策略的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
gmf520 committed Feb 29, 2020
1 parent c8451b3 commit 5ec0113
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ public abstract class FunctionAuthorizationPackBase<TFunctionAuthorizationManage
/// <returns></returns>
public override IServiceCollection AddServices(IServiceCollection services)
{
services.AddFunctionAuthorizationHandler();

services.AddSingleton(typeof(IFunctionAuthorization), typeof(TFunctionAuthorization));
services.AddSingleton(typeof(IFunctionAuthCache), typeof(TFunctionAuthCache));
services.AddSingleton(typeof(IModuleHandler), typeof(TModuleHandler));
Expand All @@ -105,17 +107,8 @@ public override IServiceCollection AddServices(IServiceCollection services)
/// <param name="app">Asp应用程序构建器</param>
public override void UsePack(IApplicationBuilder app)
{
app.UseAuthorization();
app.UseCookiePolicy();

IServiceProvider provider = app.ApplicationServices;

IModuleHandler moduleHandler = provider.GetService<IModuleHandler>();
moduleHandler.Initialize();

IFunctionHandler functionHandler = provider.GetService<IFunctionHandler>();
functionHandler.RefreshCache();

app.UseFunctionAuthorization();
IsEnabled = true;
}
}
Expand Down
56 changes: 56 additions & 0 deletions src/OSharp.Authorization.Functions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// -----------------------------------------------------------------------
// <copyright file="ServiceCollectionExtensions.cs" company="OSharp开源团队">
// Copyright (c) 2014-2020 OSharp. All rights reserved.
// </copyright>
// <site>http://www.osharp.org</site>
// <last-editor>郭明锋</last-editor>
// <last-date>2020-02-27 23:41</last-date>
// -----------------------------------------------------------------------

using System;

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

using OSharp.Authorization.Functions;
using OSharp.Authorization.Modules;


namespace OSharp.Authorization
{
public static class ServiceCollectionExtensions
{
/// <summary>
/// 添加名称为 OsharpPolicy 的授权策略
/// </summary>
public static IServiceCollection AddFunctionAuthorizationHandler(this IServiceCollection services)
{
services.AddAuthorization(opts =>
{
opts.AddPolicy(FunctionRequirement.OsharpPolicy, policy => policy.Requirements.Add(new FunctionRequirement()));
});
services.AddSingleton<IAuthorizationHandler, FunctionAuthorizationHandler>();

return services;
}

/// <summary>
/// 应用功能权限授权
/// </summary>
public static IApplicationBuilder UseFunctionAuthorization(this IApplicationBuilder app)
{
app.UseAuthorization();

IServiceProvider provider = app.ApplicationServices;

IModuleHandler moduleHandler = provider.GetService<IModuleHandler>();
moduleHandler.Initialize();

IFunctionHandler functionHandler = provider.GetService<IFunctionHandler>();
functionHandler.RefreshCache();

return app;
}
}
}
2 changes: 1 addition & 1 deletion src/OSharp/Dependency/ScopedDictionary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace OSharp.Dependency
/// 基于Scoped生命周期的数据字典,可用于在Scoped环境中传递数据
/// </summary>
[Dependency(ServiceLifetime.Scoped, AddSelf = true)]
public class ScopedDictionary : ConcurrentDictionary<string, object>, IDisposable
public sealed class ScopedDictionary : ConcurrentDictionary<string, object>, IDisposable
{
/// <summary>
/// 获取或设置 当前执行的功能
Expand Down

0 comments on commit 5ec0113

Please sign in to comment.