Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add security provider injector #524

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ public interface ISecurityRuleExpander : ISecurityModeExpander,
{
DomainSecurityRule.ExpandedRolesSecurityRule FullRoleExpand(DomainSecurityRule.RoleBaseSecurityRule securityRule);

DomainSecurityRule FullDomainExpand(DomainSecurityRule securityRule);
DomainSecurityRule FullDomainExpand(DomainSecurityRule securityRule, SecurityRuleExpandSettings? settings = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ public static DomainSecurityRule Expand(this ISecurityModeExpander expander, Dom
nameof(securityRule),
$"{nameof(SecurityRule)} with mode '{securityRule}' not found for type '{securityRule.DomainType.Name}'");
}

public static DomainSecurityRule? TryExpand<TDomainObject>(this ISecurityModeExpander expander, SecurityRule.ModeSecurityRule securityRule)
{
return expander.TryExpand(securityRule.ToDomain<TDomainObject>());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Framework.Core;

namespace Framework.SecuritySystem.Expanders;

public record SecurityRuleExpandSettings(DeepEqualsCollection<Type> IgnoredTypes)
{
public SecurityRuleExpandSettings(IEnumerable<Type> ignoredTypes)
: this(DeepEqualsCollection.Create(ignoredTypes))
{
}

public static SecurityRuleExpandSettings Disabled { get; } = new(new[] { typeof(SecurityRule) });
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,24 @@

using Framework.Core;
using Framework.Persistent;
using Framework.SecuritySystem.ProviderFactories;
using Framework.SecuritySystem.SecurityRuleInfo;

using Microsoft.Extensions.DependencyInjection;

namespace Framework.SecuritySystem.DependencyInjection.DomainSecurityServiceBuilder;

public abstract class DomainSecurityServiceBuilder : IDomainSecurityServiceBuilder
public abstract class DomainSecurityServiceBuilder
{
public abstract Type DomainType { get; }

public abstract void Register(IServiceCollection services);
public abstract void Register(IServiceCollection services, bool addSelfRelativePath);
}

internal class DomainSecurityServiceBuilder<TDomainObject> : DomainSecurityServiceBuilder, IDomainSecurityServiceBuilder<TDomainObject>
where TDomainObject : IIdentityObject<Guid>
{
private readonly List<Type> functorTypes = [];
private readonly List<Type> injectorTypes = [];

private readonly Dictionary<SecurityRule.ModeSecurityRule, DomainSecurityRule> domainObjectSecurityDict = [];

Expand All @@ -28,11 +29,11 @@ internal class DomainSecurityServiceBuilder<TDomainObject> : DomainSecurityServi

private Type? customServiceType;

private Type? dependencyServiceType;
private Type? dependencyInjectorType;

public override Type DomainType { get; } = typeof(TDomainObject);

public override void Register(IServiceCollection services)
public override void Register(IServiceCollection services, bool addSelfRelativePath)
{
foreach (var (modeSecurityRule, implementedSecurityRule) in this.domainObjectSecurityDict)
{
Expand All @@ -49,50 +50,12 @@ public override void Register(IServiceCollection services)
services.AddSingleton(pair.Type, pair.Instance);
}

foreach (var (decl, impl) in this.GetRegisterDomainSecurityService())
if (addSelfRelativePath)
{
if (decl == null)
{
services.AddScoped(impl);
}
else
{
services.AddScoped(decl, impl);
}
services.AddSingleton<IRelativeDomainPathInfo<TDomainObject, TDomainObject>, SelfRelativeDomainPathInfo<TDomainObject>>();
}
}

private IEnumerable<(Type? Decl, Type Impl)> GetRegisterDomainSecurityService()
{
var baseServiceType = typeof(IDomainSecurityService<TDomainObject>);

var actualCustomServiceType = this.customServiceType ?? this.dependencyServiceType;

var functorTypeDecl = typeof(IOverrideSecurityProviderFunctor<TDomainObject>);

var realFunctorTypes = this.functorTypes.Where(f => f.HasInterfaceMethodOverride(functorTypeDecl)).ToList();

if (realFunctorTypes.Any())
{
foreach (var functorType in realFunctorTypes)
{
yield return (functorTypeDecl, functorType);
}

var withFunctorActualCustomServiceType = actualCustomServiceType ?? typeof(ContextDomainSecurityService<TDomainObject>);

yield return (null, withFunctorActualCustomServiceType);

var withWrappedFunctorServiceType = typeof(DomainSecurityServiceWithFunctor<,>).MakeGenericType(
withFunctorActualCustomServiceType,
typeof(TDomainObject));

yield return (baseServiceType, withWrappedFunctorServiceType);
}
else if (actualCustomServiceType != null)
{
yield return (baseServiceType, actualCustomServiceType);
}
services.AddScoped(typeof(IDomainSecurityService<TDomainObject>), this.customServiceType ?? typeof(DomainSecurityService<TDomainObject>));
}

public IDomainSecurityServiceBuilder<TDomainObject> SetMode(SecurityRule.ModeSecurityRule modeSecurityRule, DomainSecurityRule implementedSecurityRule)
Expand All @@ -111,6 +74,7 @@ public IDomainSecurityServiceBuilder<TDomainObject> SetPath(SecurityPath<TDomain

public IDomainSecurityServiceBuilder<TDomainObject> SetDependency<TSource>()
{
this.dependencyInjectorType =
this.dependencyServiceType = typeof(DependencyDomainSecurityService<TDomainObject, TSource>);

return this;
Expand Down Expand Up @@ -142,8 +106,8 @@ public IDomainSecurityServiceBuilder<TDomainObject> SetCustomService<TDomainSecu
return this;
}

public IDomainSecurityServiceBuilder<TDomainObject> Override<TSecurityFunctor>()
where TSecurityFunctor : IOverrideSecurityProviderFunctor<TDomainObject>
public IDomainSecurityServiceBuilder<TDomainObject> AddInjector<TSecurityFunctor>()
where TSecurityFunctor : ISecurityProviderInjector<TDomainObject>
{
this.functorTypes.Add(typeof(TSecurityFunctor));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ private IDomainSecurityServiceRootBuilder AddMetadataInternal<TMetadata, TDomain
where TMetadata : IDomainSecurityServiceMetadata<TDomainObject>
where TDomainObject : IIdentityObject<Guid>
{
return this.AddInternal<TDomainObject>(b => b.Override<TMetadata>().Pipe(TMetadata.Setup));
return this.AddInternal<TDomainObject>(b => b.AddInjector<TMetadata>().Pipe(TMetadata.Setup));
}

private IDomainSecurityServiceRootBuilder AddInternal<TDomainObject>(Action<IDomainSecurityServiceBuilder<TDomainObject>> setup)
Expand All @@ -51,14 +51,7 @@ public void Register(IServiceCollection services)
{
foreach (var domainBuilder in this.domainBuilders)
{
domainBuilder.Register(services);

if (this.AutoAddSelfRelativePath)
{
services.AddSingleton(
typeof(IRelativeDomainPathInfo<,>).MakeGenericType(domainBuilder.DomainType, domainBuilder.DomainType),
typeof(SelfRelativeDomainPathInfo<>).MakeGenericType(domainBuilder.DomainType));
}
domainBuilder.Register(services, this.AutoAddSelfRelativePath);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Linq.Expressions;

using Framework.SecuritySystem.ProviderFactories;

using Microsoft.Extensions.DependencyInjection;

namespace Framework.SecuritySystem.DependencyInjection.DomainSecurityServiceBuilder;
Expand Down Expand Up @@ -39,11 +41,6 @@ public interface IDomainSecurityServiceBuilder<TDomainObject>
IDomainSecurityServiceBuilder<TDomainObject> SetCustomService<TDomainSecurityService>()
where TDomainSecurityService : IDomainSecurityService<TDomainObject>;

IDomainSecurityServiceBuilder<TDomainObject> Override<TSecurityFunctor>()
where TSecurityFunctor : IOverrideSecurityProviderFunctor<TDomainObject>;
}

public interface IDomainSecurityServiceBuilder
{
void Register(IServiceCollection services);
IDomainSecurityServiceBuilder<TDomainObject> AddInjector<TSecurityFunctor>()
where TSecurityFunctor : ISecurityProviderInjector<TDomainObject>;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
namespace Framework.SecuritySystem.DependencyInjection.DomainSecurityServiceBuilder;
using Framework.SecuritySystem.ProviderFactories;

public interface IDomainSecurityServiceMetadata<TDomainObject> : IDomainSecurityServiceMetadata, IOverrideSecurityProviderFunctor<TDomainObject>
namespace Framework.SecuritySystem.DependencyInjection.DomainSecurityServiceBuilder;

public interface IDomainSecurityServiceMetadata<TDomainObject> :
IDomainSecurityServiceMetadata,
ISecurityProviderInjector<TDomainObject, SecurityRule.ModeSecurityRule>,
ISecurityProviderInjector<TDomainObject, DomainSecurityRule.OperationSecurityRule>,
ISecurityProviderInjector<TDomainObject, DomainSecurityRule.NonExpandedRolesSecurityRule>,
ISecurityProviderInjector<TDomainObject, DomainSecurityRule.ExpandedRolesSecurityRule>
{
static Type IDomainSecurityServiceMetadata.DomainType { get; } = typeof(TDomainObject);

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,60 +1,15 @@
using Framework.SecuritySystem.Expanders;

using static Framework.SecuritySystem.DomainSecurityRule;
using Framework.SecuritySystem.Services;

namespace Framework.SecuritySystem;

public abstract class DomainSecurityService<TDomainObject>(ISecurityRuleExpander securityRuleExpander) : DomainSecurityServiceBase<TDomainObject>
public class DomainSecurityService<TDomainObject>(IDomainSecurityProviderFactory<TDomainObject> domainSecurityProviderFactory)

Check failure on line 5 in src/Framework.SecuritySystem/DomainServices/DomainSecurityService.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IDomainSecurityProviderFactory<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 5 in src/Framework.SecuritySystem/DomainServices/DomainSecurityService.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IDomainSecurityProviderFactory<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 5 in src/Framework.SecuritySystem/DomainServices/DomainSecurityService.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IDomainSecurityProviderFactory<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 5 in src/Framework.SecuritySystem/DomainServices/DomainSecurityService.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IDomainSecurityProviderFactory<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 5 in src/Framework.SecuritySystem/DomainServices/DomainSecurityService.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IDomainSecurityProviderFactory<>' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 5 in src/Framework.SecuritySystem/DomainServices/DomainSecurityService.cs

View workflow job for this annotation

GitHub Actions / build

The type or namespace name 'IDomainSecurityProviderFactory<>' could not be found (are you missing a using directive or an assembly reference?)
: DomainSecurityServiceBase<TDomainObject>
{
protected sealed override ISecurityProvider<TDomainObject> CreateSecurityProvider(SecurityRule baseSecurityRule)
{
switch (baseSecurityRule)
{
case SecurityRule.ModeSecurityRule securityRule:
return this.CreateSecurityProvider(securityRule);

case DomainModeSecurityRule securityRule:
return this.CreateSecurityProvider(securityRuleExpander.Expand(securityRule));

case ClientSecurityRule securityRule:
return this.CreateSecurityProvider(securityRuleExpander.Expand(securityRule));

case OperationSecurityRule securityRule:
return this.CreateSecurityProvider(securityRule);

case NonExpandedRolesSecurityRule securityRule:
return this.CreateSecurityProvider(securityRule);

case ExpandedRolesSecurityRule securityRule:
return this.CreateSecurityProvider(securityRule);

case DomainSecurityRule securityRule:
return this.CreateFinalSecurityProvider(securityRule);

default:
throw new ArgumentOutOfRangeException(nameof(baseSecurityRule));
}
}

protected virtual ISecurityProvider<TDomainObject> CreateSecurityProvider(SecurityRule.ModeSecurityRule securityRule)
{
return this.GetSecurityProvider(securityRule.ToDomain<TDomainObject>());
}

protected virtual ISecurityProvider<TDomainObject> CreateSecurityProvider(OperationSecurityRule securityRule)
{
return this.GetSecurityProvider(securityRuleExpander.Expand(securityRule));
}

protected virtual ISecurityProvider<TDomainObject> CreateSecurityProvider(NonExpandedRolesSecurityRule securityRule)
{
return this.GetSecurityProvider(securityRuleExpander.Expand(securityRule));
}

protected virtual ISecurityProvider<TDomainObject> CreateSecurityProvider(ExpandedRolesSecurityRule securityRule)
{
return this.CreateFinalSecurityProvider(securityRule);
}
protected virtual ISecurityProvider<TDomainObject> CreateSecurityProvider(
SecurityRule securityRule,
SecurityPath<TDomainObject>? customSecurityPath) =>
domainSecurityProviderFactory.Create(securityRule, customSecurityPath);

protected abstract ISecurityProvider<TDomainObject> CreateFinalSecurityProvider(DomainSecurityRule securityRule);
protected override ISecurityProvider<TDomainObject> CreateSecurityProvider(SecurityRule securityRule) =>
this.CreateSecurityProvider(securityRule, null);
}
Loading
Loading