Skip to content

Commit

Permalink
fix: updated for latest authorizatio support
Browse files Browse the repository at this point in the history
  • Loading branch information
pksorensen committed Dec 3, 2024
1 parent c516c5f commit 4416b33
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Import Condition="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../')) != ''" Project="$([MSBuild]::GetPathOfFileAbove('Directory.Build.props', '$(MSBuildThisFileDirectory)../'))" />
<PropertyGroup>
<LangVersion>12.0</LangVersion>
<EAVFrameworkVersion Condition="'$(EAVFrameworkVersion)' == ''">5.0.0-dev.1</EAVFrameworkVersion>
<EAVFrameworkVersion Condition="'$(EAVFrameworkVersion)' == ''">5.0.0-dev.6</EAVFrameworkVersion>
<UseEAVFromNuget Condition="'$(UseEAVFromNuget)' == ''">true</UseEAVFromNuget>
<LocalEAVFrameworkPath Condition="'$(LocalEAVFrameworkPath)' == ''">$(MSBuildThisFileDirectory)/external/EAVFramework</LocalEAVFrameworkPath>
<LocalExternalpath Condition="'$(LocalExternalpath)' == ''">$(MSBuildThisFileDirectory)/external</LocalExternalpath>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using EAVFramework.Endpoints;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EAVFW.Extensions.SecurityModel
{
public class EAVAuthorizationHandlerProvider : IAuthorizationHandlerProvider
{
private readonly IServiceProvider _serviceProvider;

public EAVAuthorizationHandlerProvider(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
public Task<IEnumerable<IAuthorizationHandler>> GetHandlersAsync(AuthorizationHandlerContext context)
{
var handlers = new List<IAuthorizationHandler>(_serviceProvider.GetService<IEnumerable<IAuthorizationHandler>>());

foreach(var ctxgroup in context.PendingRequirements.OfType<CreateRecordRequirement>().GroupBy(c=>c.Context))
{
handlers.Add(_serviceProvider.GetRequiredService(typeof(PermissionBasedCreateRecordRequirementHandler<>).MakeGenericType(ctxgroup.Key)) as IAuthorizationHandler);
}
foreach (var ctxgroup in context.PendingRequirements.OfType<UpdateRecordRequirement>().GroupBy(c => c.Context))
{
handlers.Add(_serviceProvider.GetRequiredService(typeof(PermissionBasedUpdateRecordRequirementHandler<>).MakeGenericType(ctxgroup.Key)) as IAuthorizationHandler);
}

// builder.Services.AddDynamicScoped<TContext,IAuthorizationHandler>(typeof( PermissionBasedCreateRecordRequirementHandler<>));
// builder.Services.AddDynamicScoped<TContext,IAuthorizationHandler>(typeof( PermissionBasedUpdateRecordRequirementHandler<>));

return Task.FromResult< IEnumerable< IAuthorizationHandler>>(handlers);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using EAVFramework;
using EAVFramework;
using EAVFramework.Endpoints;
using EAVFramework.Shared.V2;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EAVFW.Extensions.SecurityModel
Expand All @@ -10,19 +15,21 @@ public class PermissionBasedCreateRecordRequirementHandler<TContext> :
AuthorizationHandler<CreateRecordRequirement, EAVResource>
where TContext : DynamicContext
{
private readonly IPermissionStore<TContext> _permissionStore;

private readonly IServiceProvider _serviceProvider;

public PermissionBasedCreateRecordRequirementHandler(IPermissionStore<TContext> permissionStore)
public PermissionBasedCreateRecordRequirementHandler(IServiceProvider serviceProvider)
{
_permissionStore = permissionStore;

_serviceProvider = serviceProvider;
}


protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, CreateRecordRequirement requirement, EAVResource resource)
{
var entitySchemaName = resource.EntityCollectionSchemaName;

var hasPemission = await _permissionStore.GetPermissions(context.User, resource).AnyAsync(permision => permision == $"{entitySchemaName}CreateGlobal" || (permision == $"{entitySchemaName}Create") || (permision == $"{entitySchemaName}CreateBU"));
var permissionStore = _serviceProvider.GetRequiredService<IPermissionStore<TContext>>();
var hasPemission = await permissionStore.GetPermissions(context.User, resource).AnyAsync(permision => permision == $"{entitySchemaName}CreateGlobal" || (permision == $"{entitySchemaName}Create") || (permision == $"{entitySchemaName}CreateBU"));

if (hasPemission)
context.Succeed(requirement);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using EAVFramework;
using EAVFramework;
using EAVFramework.Endpoints;
using Microsoft.AspNetCore.Authorization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Threading.Tasks;

namespace EAVFW.Extensions.SecurityModel
Expand All @@ -10,19 +12,20 @@ public class PermissionBasedUpdateRecordRequirementHandler<TContext> :
AuthorizationHandler<UpdateRecordRequirement, EAVResource>
where TContext : DynamicContext
{
private readonly IPermissionStore<TContext> _permissionStore;
private readonly IServiceProvider _serviceProvider;

public PermissionBasedUpdateRecordRequirementHandler(IPermissionStore<TContext> permissionStore)
public PermissionBasedUpdateRecordRequirementHandler(IServiceProvider serviceProvider)
{
_permissionStore = permissionStore;

_serviceProvider = serviceProvider;
}


protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context, UpdateRecordRequirement requirement, EAVResource resource)
{
var entitySchemaName = resource.EntityCollectionSchemaName;

var hasPemission = await _permissionStore.GetPermissions(context.User, resource).AnyAsync(permision => permision == $"{entitySchemaName}UpdateGlobal" || (permision == $"{entitySchemaName}Update") || (permision == $"{entitySchemaName}UpdateBU"));
var permissionStore = _serviceProvider.GetRequiredService<IPermissionStore<TContext>>();
var hasPemission = await permissionStore.GetPermissions(context.User, resource).AnyAsync(permision => permision == $"{entitySchemaName}UpdateGlobal" || (permision == $"{entitySchemaName}Update") || (permision == $"{entitySchemaName}UpdateBU"));


if (hasPemission)
Expand Down
9 changes: 6 additions & 3 deletions src/EAVFW.Extensions.SecurityModel/SecurityModelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ public static IEAVFrameworkBuilder WithPermissionBasedAuthorization<TContext>(th
builder.Services.AddDynamicInterface<ISecurityGroup>();
builder.Services.AddDynamicInterface<ISecurityGroupMember>();
builder.Services.AddDynamicInterface<ITRecordShare>();

builder.Services.AddDynamicScoped<TContext,IQueryExtender<TContext>>( typeof(OwnerBasedAuthorizationQueryExtender<,,,,,,,,>));
builder.Services.AddDynamicScoped<TContext,IPermissionStore<TContext>>(typeof(PermissionStore<,,,,,,,,>));
builder.Services.AddDynamicScoped<TContext,IAuthorizationHandler>(typeof( PermissionBasedCreateRecordRequirementHandler<>));
builder.Services.AddDynamicScoped<TContext,IAuthorizationHandler>(typeof( PermissionBasedUpdateRecordRequirementHandler<>));

builder.Services.AddScoped(typeof(PermissionBasedCreateRecordRequirementHandler<>), typeof(PermissionBasedCreateRecordRequirementHandler<>));
builder.Services.AddScoped(typeof(PermissionBasedUpdateRecordRequirementHandler<>), typeof(PermissionBasedUpdateRecordRequirementHandler<>));

builder.Services.AddScoped<IAuthorizationHandlerProvider, EAVAuthorizationHandlerProvider>();
return builder;
}
}
Expand Down

0 comments on commit 4416b33

Please sign in to comment.