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 client security rule #508

Merged
merged 5 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions src/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
<PackageVersion Include="NUnit3TestAdapter" Version="4.6.0" />
<PackageVersion Include="nuSpec.Abstraction" Version="1.3.0" />
<PackageVersion Include="nuSpec.NHibernate" Version="1.3.0" />
<PackageVersion Include="Swashbuckle.AspNetCore.SwaggerGen" Version="6.6.2" />
<PackageVersion Include="System.CodeDom" Version="8.0.0" />
<PackageVersion Include="System.Reflection.Emit" Version="4.7.0" />
<PackageVersion Include="System.Runtime.Loader" Version="4.3.0" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Framework.SecuritySystem.AvailableSecurity;

public interface IAvailableClientSecurityRuleSource
{
Task<List<ClientSecurityRuleHeader>> GetAvailableSecurityRules(CancellationToken cancellationToken = default);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Framework.SecuritySystem;
namespace Framework.SecuritySystem.AvailableSecurity;

public interface IAvailableSecurityOperationSource
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Framework.SecuritySystem;
namespace Framework.SecuritySystem.AvailableSecurity;

public interface IAvailableSecurityRoleSource
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Framework.SecuritySystem.Expanders;

public interface IRoleFactorySecurityRuleExpander
{
DomainSecurityRule.RoleBaseSecurityRule Expand(DomainSecurityRule.RoleFactorySecurityRule securityRule);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Framework.SecuritySystem.Expanders;

public interface ISecurityModeExpander
{
DomainSecurityRule? TryExpand(DomainSecurityRule.DomainModeSecurityRule securityRule);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Framework.SecuritySystem.Expanders;

public interface ISecurityOperationExpander
{
DomainSecurityRule.NonExpandedRolesSecurityRule Expand(DomainSecurityRule.OperationSecurityRule securityRule);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Framework.SecuritySystem.Expanders;

public interface ISecurityRoleExpander
{
DomainSecurityRule.ExpandedRolesSecurityRule Expand(DomainSecurityRule.NonExpandedRolesSecurityRule securityRule);
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
namespace Framework.SecuritySystem.Expanders;

public interface ISecurityRuleExpander
public interface ISecurityRuleExpander : ISecurityModeExpander, ISecurityOperationExpander, ISecurityRoleExpander, IRoleFactorySecurityRuleExpander
{
DomainSecurityRule? TryExpand<TDomainObject>(SecurityRule.ModeSecurityRule securityRule);

DomainSecurityRule.NonExpandedRolesSecurityRule Expand(DomainSecurityRule.OperationSecurityRule securityRule);

DomainSecurityRule.ExpandedRolesSecurityRule Expand(DomainSecurityRule.NonExpandedRolesSecurityRule securityRule);

DomainSecurityRule.RoleBaseSecurityRule Expand(DomainSecurityRule.RoleFactorySecurityRule securityRule);

DomainSecurityRule.ExpandedRolesSecurityRule FullExpand(DomainSecurityRule.RoleBaseSecurityRule securityRule);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Framework.SecuritySystem.Expanders;

public static class SecurityModeExpanderExtensions
{
public static DomainSecurityRule Expand(this ISecurityModeExpander expander, DomainSecurityRule.DomainModeSecurityRule securityRule)
{
return expander.TryExpand(securityRule)
?? throw new ArgumentOutOfRangeException(
nameof(securityRule),
$"{nameof(SecurityRule)} with mode '{securityRule}' not found for type '{securityRule.DomainType.Name}'");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Framework.SecuritySystem;

public record ClientSecurityRuleHeader(string Name)
{
public override string ToString() => this.Name;
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ public abstract record DomainSecurityRule : SecurityRule

public static implicit operator DomainSecurityRule(SecurityRole[] securityRoles) => securityRoles.ToSecurityRule();

public record DomainModeSecurityRule(Type DomainType, ModeSecurityRule Mode) : DomainSecurityRule
{
public override string ToString() => $"{this.Mode} ({this.DomainType.Name})";
}

public record CurrentUserSecurityRule(string? RelativePathKey = null) : DomainSecurityRule
{
public override string ToString() => this.RelativePathKey ?? nameof(CurrentUser);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Framework.SecuritySystem.SecurityRuleInfo;

public record ClientSecurityRuleInfo(ClientSecurityRuleHeader Header, DomainSecurityRule Implementation)
{
public ClientSecurityRuleInfo(string name, DomainSecurityRule implementation)
: this(new ClientSecurityRuleHeader(name), implementation)
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace Framework.SecuritySystem.SecurityRuleInfo;

public record DomainModeSecurityRuleInfo(DomainSecurityRule.DomainModeSecurityRule SecurityRule, DomainSecurityRule Implementation);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Framework.SecuritySystem.SecurityRuleInfo;

public interface IClientDomainModeSecurityRuleSource
{
IEnumerable<DomainSecurityRule.DomainModeSecurityRule> GetRules();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Framework.SecuritySystem.SecurityRuleInfo;

public interface IClientSecurityRuleInfoSource
{
IEnumerable<ClientSecurityRuleInfo> GetInfos();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System.Reflection;

namespace Framework.SecuritySystem.SecurityRuleInfo;

public interface IClientSecurityRuleNameExtractor
{
string ExtractName(PropertyInfo propertyInfo);

string ExtractName(DomainSecurityRule.DomainModeSecurityRule securityRule);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Framework.SecuritySystem.SecurityRuleInfo;

public interface IClientSecurityRuleResolver
{
IEnumerable<ClientSecurityRuleHeader> Resolve(SecurityRole securityRole);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Framework.SecuritySystem.SecurityRuleInfo;

public interface IDomainModeSecurityRuleResolver
{
IEnumerable<DomainSecurityRule.DomainModeSecurityRule> Resolve(SecurityRole securityRole);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace Framework.SecuritySystem.SecurityRuleInfo;

public interface IDomainSecurityRoleExtractor
{
IEnumerable<SecurityRole> Extract(DomainSecurityRule securityRule);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using Framework.SecuritySystem;

namespace Framework.DomainDriven;
namespace Framework.SecuritySystem.SecurityRuleInfo;

public interface ISecurityRuleParser
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void AdministratorRole_ShouldNotContains_SystemIntegrationRole()
public void SecurityRoleExpander_ExpandDeepChild_AllRolesExpanded()
{
// Arrange
var expander = this.rootServiceProvider.GetRequiredService<SecurityRoleExpander>();
var expander = this.rootServiceProvider.GetRequiredService<ISecurityRoleExpander>();

// Act
var expandResult = expander.Expand(ExampleSecurityRole.TestRole3);
Expand All @@ -44,7 +44,7 @@ public void SecurityRoleExpander_ExpandDeepChild_AllRolesExpanded()
public void SecurityRoleExpander_ExpandWithDefaultExpandType_RoleResolved()
{
// Arrange
var expander = this.rootServiceProvider.GetRequiredService<SecurityOperationExpander>();
var expander = this.rootServiceProvider.GetRequiredService<ISecurityOperationExpander>();

// Act
var expandResult = expander.Expand(new DomainSecurityRule.OperationSecurityRule(ExampleSecurityOperation.EmployeeView));
Expand All @@ -57,7 +57,7 @@ public void SecurityRoleExpander_ExpandWithDefaultExpandType_RoleResolved()
public void SecurityRoleExpander_ExpandWithCustomExpandType_SecurityRuleCorrected()
{
// Arrange
var expander = this.rootServiceProvider.GetRequiredService<SecurityOperationExpander>();
var expander = this.rootServiceProvider.GetRequiredService<ISecurityOperationExpander>();

// Act
var expandResult = expander.Expand(ExampleSecurityOperation.EmployeeView.ToSecurityRule(HierarchicalExpandType.None));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Framework.SecuritySystem.SecurityRuleInfo;

namespace Framework.SecuritySystem.AvailableSecurity;

public class AvailableClientSecurityRuleSource(
IAvailableSecurityRoleSource availableSecurityRoleSource,
IClientSecurityRuleResolver clientSecurityRuleResolver) : IAvailableClientSecurityRuleSource
{
public async Task<List<ClientSecurityRuleHeader>> GetAvailableSecurityRules(CancellationToken cancellationToken = default)
{
var roles = await availableSecurityRoleSource.GetAvailableSecurityRoles(true, cancellationToken);

return roles.SelectMany(clientSecurityRuleResolver.Resolve)
.Distinct()
.ToList();
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Framework.SecuritySystem;
namespace Framework.SecuritySystem.AvailableSecurity;

public class AvailableSecurityOperationSource(IAvailableSecurityRoleSource availableSecurityRoleSource)
: IAvailableSecurityOperationSource
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Framework.Core;
using Framework.SecuritySystem.ExternalSystem;

namespace Framework.SecuritySystem;
namespace Framework.SecuritySystem.AvailableSecurity;

public class AvailableSecurityRoleSource(IEnumerable<IPermissionSystem> permissionSystems, ISecurityRoleSource securityRoleSource)
: IAvailableSecurityRoleSource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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

using Microsoft.Extensions.DependencyInjection;

Expand All @@ -18,36 +18,33 @@ public abstract class DomainSecurityServiceBuilder : IDomainSecurityServiceBuild
internal class DomainSecurityServiceBuilder<TDomainObject> : DomainSecurityServiceBuilder, IDomainSecurityServiceBuilder<TDomainObject>
where TDomainObject : IIdentityObject<Guid>
{
private readonly List<Type> functorTypes = new();
private readonly List<Type> functorTypes = [];

public DomainSecurityRule? ViewRule { get; private set; }
private readonly Dictionary<SecurityRule.ModeSecurityRule, DomainSecurityRule> domainObjectSecurityDict = [];

public DomainSecurityRule? EditRule { get; private set; }
private SecurityPath<TDomainObject>? securityPath;

public SecurityPath<TDomainObject>? SecurityPath { get; private set; }
private (Type Type, object Instance)? relativePathData;

public (Type Type, object Instance)? RelativePathData { get; private set; }
private Type? customServiceType;

public Type? CustomServiceType { get; private set; }

public Type? DependencyServiceType { get; private set; }
private Type? dependencyServiceType;

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

public override void Register(IServiceCollection services)
{
if (this.ViewRule != null || this.EditRule != null)
foreach (var domainObjectSecurityPair in this.domainObjectSecurityDict)
{
services.AddSingleton(
new DomainObjectSecurityModeInfo(typeof(TDomainObject), this.ViewRule, this.EditRule));
services.AddSingleton(new DomainModeSecurityRuleInfo(new DomainSecurityRule.DomainModeSecurityRule(this.DomainType, domainObjectSecurityPair.Key), domainObjectSecurityPair.Value));
}

if (this.SecurityPath != null)
if (this.securityPath != null)
{
services.AddSingleton(this.SecurityPath);
services.AddSingleton(this.securityPath);
}

if (this.RelativePathData is { } pair)
if (this.relativePathData is { } pair)
{
services.AddSingleton(pair.Type, pair.Instance);
}
Expand All @@ -69,7 +66,7 @@ public override void Register(IServiceCollection services)
{
var baseServiceType = typeof(IDomainSecurityService<TDomainObject>);

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

var functorTypeDecl = typeof(IOverrideSecurityProviderFunctor<TDomainObject>);

Expand Down Expand Up @@ -100,28 +97,28 @@ public override void Register(IServiceCollection services)

public IDomainSecurityServiceBuilder<TDomainObject> SetView(DomainSecurityRule securityRule)
{
this.ViewRule = securityRule;
this.domainObjectSecurityDict[SecurityRule.View] = securityRule;

return this;
}

public IDomainSecurityServiceBuilder<TDomainObject> SetEdit(DomainSecurityRule securityRule)
{
this.EditRule = securityRule;
this.domainObjectSecurityDict[SecurityRule.Edit] = securityRule;

return this;
}

public IDomainSecurityServiceBuilder<TDomainObject> SetPath(SecurityPath<TDomainObject> securityPath)
public IDomainSecurityServiceBuilder<TDomainObject> SetPath(SecurityPath<TDomainObject> newSecurityPath)
{
this.SecurityPath = securityPath;
this.securityPath = newSecurityPath;

return this;
}

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

return this;
}
Expand All @@ -130,7 +127,7 @@ public IDomainSecurityServiceBuilder<TDomainObject> SetDependency<TSource>(Expre
{
this.SetDependency<TSource>();

this.RelativePathData =
this.relativePathData =
(typeof(IRelativeDomainPathInfo<TDomainObject, TSource>),
new RelativeDomainPathInfo<TDomainObject, TSource>(relativeDomainPath));

Expand All @@ -139,15 +136,15 @@ public IDomainSecurityServiceBuilder<TDomainObject> SetDependency<TSource>(Expre

public IDomainSecurityServiceBuilder<TDomainObject> SetUntypedDependency<TSource>()
{
this.DependencyServiceType = typeof(UntypedDependencyDomainSecurityService<,>).MakeGenericType(typeof(TDomainObject), typeof(TSource));
this.dependencyServiceType = typeof(UntypedDependencyDomainSecurityService<,>).MakeGenericType(typeof(TDomainObject), typeof(TSource));

return this;
}

public IDomainSecurityServiceBuilder<TDomainObject> SetCustomService<TDomainSecurityService>()
where TDomainSecurityService : IDomainSecurityService<TDomainObject>
{
this.CustomServiceType = typeof(TDomainSecurityService);
this.customServiceType = typeof(TDomainSecurityService);

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Framework.SecuritySystem.DependencyInjection.DomainSecurityServiceBuil

internal class DomainSecurityServiceRootBuilder : IDomainSecurityServiceRootBuilder
{
private readonly List<DomainSecurityServiceBuilder> domainBuilders = new();
private readonly List<DomainSecurityServiceBuilder> domainBuilders = [];

public bool AutoAddSelfRelativePath { get; set; } = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class DomainSecurityServiceWithFunctor<TOriginalDomainSecurityService, TD
{
protected override ISecurityProvider<TDomainObject> CreateSecurityProvider(SecurityRule.ModeSecurityRule securityRule)
{
var actualSecurityRule = (SecurityRule?)securityRuleExpander.TryExpand<TDomainObject>(securityRule) ?? securityRule;
var actualSecurityRule = (SecurityRule?)securityRuleExpander.TryExpand(new DomainSecurityRule.DomainModeSecurityRule(typeof(TDomainObject), securityRule)) ?? securityRule;

var originalSecurityProvider = originalDomainSecurityService.GetSecurityProvider(actualSecurityRule);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Framework.SecuritySystem.DependencyInjection.DomainSecurityServiceBuilder;
using Framework.SecuritySystem.ExternalSystem;
using Framework.SecuritySystem.SecurityAccessor;
using Framework.SecuritySystem.SecurityRuleInfo;
using Framework.SecuritySystem.Services;
using Microsoft.Extensions.DependencyInjection;

Expand Down Expand Up @@ -53,4 +54,11 @@ ISecuritySystemSettings SetSecurityAccessorInfinityStorage<TStorage>()

ISecuritySystemSettings SetDefaultSecurityRuleCredential(SecurityRuleCredential securityRuleCredential);

ISecuritySystemSettings AddClientSecurityRuleInfoSource<TClientSecurityRuleInfoSource>()
where TClientSecurityRuleInfoSource : class, IClientSecurityRuleInfoSource;

ISecuritySystemSettings AddClientSecurityRuleInfoSource(Type sourceType);

ISecuritySystemSettings SetSecurityRuleParser<TSecurityRuleParser>()
where TSecurityRuleParser : class, ISecurityRuleParser;
}
Loading
Loading