Skip to content

Commit

Permalink
add component convention (#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
iatsuta authored Sep 18, 2024
1 parent c8e098a commit a85ff0b
Show file tree
Hide file tree
Showing 49 changed files with 667 additions and 290 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,21 +14,19 @@ namespace Framework.SecuritySystem.DependencyInjection;

public class SecuritySystemSettings : ISecuritySystemSettings
{
public List<Action<IServiceCollection>> RegisterActions { get; } = new();
private readonly List<Action<IServiceCollection>> registerActions = new();

public Action<IServiceCollection> RegisterUserSourceAction { get; private set; } = _ => { };
private Action<IServiceCollection> registerUserSourceAction = _ => { };

public Action<IServiceCollection> RegisterRunAsManagerAction { get; private set; } = _ => { };
private Action<IServiceCollection> registerRunAsManagerAction= _ => { };

public bool InitializeAdministratorRole { get; set; } = true;
private SecurityRuleCredential defaultSecurityRuleCredential = SecurityRuleCredential.CurrentUserWithRunAs;

public SecurityRuleCredential DefaultSecurityRuleCredential { get; private set; } = SecurityRuleCredential.CurrentUserWithRunAs;
private Type accessDeniedExceptionServiceType = typeof(AccessDeniedExceptionService);

public Type AccessDeniedExceptionServiceType { get; private set; } = typeof(AccessDeniedExceptionService);
private Type? securityAccessorInfinityStorageType;

public Type CurrentUserType { get; private set; } = typeof(CurrentUser);

public Type? SecurityAccessorInfinityStorageType { get; private set; }
public bool InitializeAdministratorRole { get; set; } = true;

public ISecuritySystemSettings AddSecurityContext<TSecurityContext>(
Guid ident,
Expand All @@ -41,65 +39,65 @@ public ISecuritySystemSettings AddSecurityContext<TSecurityContext>(

public ISecuritySystemSettings AddSecurityContext(Action<ISecurityContextInfoBuilder> setup)
{
this.RegisterActions.Add(sc => sc.RegisterSecurityContextSource(setup));
this.registerActions.Add(sc => sc.RegisterSecurityContextSource(setup));

return this;
}

public ISecuritySystemSettings AddDomainSecurityServices(Action<IDomainSecurityServiceRootBuilder> setup)
{
this.RegisterActions.Add(sc => sc.RegisterDomainSecurityServices(setup));
this.registerActions.Add(sc => sc.RegisterDomainSecurityServices(setup));

return this;
}

public ISecuritySystemSettings AddSecurityRole(SecurityRole securityRole, SecurityRoleInfo info)
{
this.RegisterActions.Add(sc => this.AddSecurityRole(sc, new FullSecurityRole(securityRole.Name, info)));
this.registerActions.Add(sc => this.AddSecurityRole(sc, new FullSecurityRole(securityRole.Name, info)));

return this;
}

public ISecuritySystemSettings AddSecurityRule(DomainSecurityRule.SecurityRuleHeader header, DomainSecurityRule implementation)
{
this.RegisterActions.Add(sc => sc.AddSingleton(new SecurityRuleFullInfo(header, implementation)));
this.registerActions.Add(sc => sc.AddSingleton(new SecurityRuleFullInfo(header, implementation)));

return this;
}

public ISecuritySystemSettings AddSecurityOperation(SecurityOperation securityOperation, SecurityOperationInfo info)
{
this.RegisterActions.Add(sc => sc.AddSingleton(new FullSecurityOperation(securityOperation, info)));
this.registerActions.Add(sc => sc.AddSingleton(new FullSecurityOperation(securityOperation, info)));

return this;
}

public ISecuritySystemSettings AddPermissionSystem<TPermissionSystemFactory>()
where TPermissionSystemFactory : class, IPermissionSystemFactory
{
this.RegisterActions.Add(sc => sc.AddScoped<IPermissionSystemFactory, TPermissionSystemFactory>());
this.registerActions.Add(sc => sc.AddScoped<IPermissionSystemFactory, TPermissionSystemFactory>());

return this;
}

public ISecuritySystemSettings AddPermissionSystem(Func<IServiceProvider, IPermissionSystemFactory> getFactory)
{
this.RegisterActions.Add(sc => sc.AddScoped(getFactory));
this.registerActions.Add(sc => sc.AddScoped(getFactory));

return this;
}

public ISecuritySystemSettings AddExtensions(ISecuritySystemExtension extensions)
{
this.RegisterActions.Add(extensions.AddServices);
this.registerActions.Add(extensions.AddServices);

return this;
}

public ISecuritySystemSettings SetAccessDeniedExceptionService<TAccessDeniedExceptionService>()
where TAccessDeniedExceptionService : class, IAccessDeniedExceptionService
{
this.AccessDeniedExceptionServiceType = typeof(TAccessDeniedExceptionService);
this.accessDeniedExceptionServiceType = typeof(TAccessDeniedExceptionService);

return this;
}
Expand All @@ -109,7 +107,7 @@ public ISecuritySystemSettings SetUserSource<TUserDomainObject>(
Expression<Func<TUserDomainObject, string>> namePath,
Expression<Func<TUserDomainObject, bool>> filter)
{
this.RegisterUserSourceAction = sc =>
this.registerUserSourceAction = sc =>
{
var info = new UserPathInfo<TUserDomainObject>(idPath, namePath, filter);
sc.AddSingleton(info);
Expand All @@ -129,26 +127,54 @@ public ISecuritySystemSettings SetUserSource<TUserDomainObject>(
public ISecuritySystemSettings SetRunAsManager<TRunAsManager>()
where TRunAsManager : class, IRunAsManager
{
this.RegisterRunAsManagerAction = sc => sc.AddScoped<IRunAsManager, TRunAsManager>();
this.registerRunAsManagerAction = sc => sc.AddScoped<IRunAsManager, TRunAsManager>();

return this;
}

public ISecuritySystemSettings SetSecurityAccessorInfinityStorage<TStorage>()
where TStorage : class, ISecurityAccessorInfinityStorage
{
this.SecurityAccessorInfinityStorageType = typeof(TStorage);
this.securityAccessorInfinityStorageType = typeof(TStorage);

return this;
}

public ISecuritySystemSettings SetDefaultSecurityRuleCredential(SecurityRuleCredential securityRuleCredential)
{
this.DefaultSecurityRuleCredential = securityRuleCredential;
this.defaultSecurityRuleCredential = securityRuleCredential;

return this;
}

public void Initialize(IServiceCollection services)
{
this.registerActions.ForEach(v => v(services));

this.registerUserSourceAction(services);
this.registerRunAsManagerAction(services);

if (this.InitializeAdministratorRole)
{
services.AddSingleton<IInitializedSecurityRoleSource, InitializedSecurityRoleSource>();
services.AddSingletonFrom((IInitializedSecurityRoleSource source) => source.GetSecurityRoles());
}

services.AddSingleton(typeof(IAccessDeniedExceptionService), this.accessDeniedExceptionServiceType);

if (this.securityAccessorInfinityStorageType == null)
{
services.AddNotImplemented<ISecurityAccessorInfinityStorage>(
"Use 'SetSecurityAccessorInfinityStorage' for initialize infinity storage");
}
else
{
services.AddScoped(typeof(ISecurityAccessorInfinityStorage), this.securityAccessorInfinityStorageType);
}

services.AddSingleton(this.defaultSecurityRuleCredential);
}

private void AddSecurityRole(IServiceCollection serviceCollection, FullSecurityRole fullSecurityRole)
{
if (this.InitializeAdministratorRole)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Linq.Expressions;

using Framework.DependencyInjection;
using Framework.SecuritySystem.Builders._Factory;

using Framework.SecuritySystem.Builders.AccessorsBuilder;
Expand Down Expand Up @@ -54,31 +53,7 @@ public static IServiceCollection AddSecuritySystem(this IServiceCollection servi

setupAction(settings);

settings.RegisterActions.ForEach(v => v(services));
settings.RegisterUserSourceAction(services);
settings.RegisterRunAsManagerAction(services);

if (settings.InitializeAdministratorRole)
{
services.AddSingleton<IInitializedSecurityRoleSource, InitializedSecurityRoleSource>();
services.AddSingletonFrom((IInitializedSecurityRoleSource source) => source.GetSecurityRoles());
}

services.AddSingleton(typeof(IAccessDeniedExceptionService), settings.AccessDeniedExceptionServiceType);

services.AddScoped(typeof(ICurrentUser), settings.CurrentUserType);

if (settings.SecurityAccessorInfinityStorageType == null)
{
services.AddNotImplemented<ISecurityAccessorInfinityStorage>(
"Use 'SetSecurityAccessorInfinityStorage' for initialize infinity storage");
}
else
{
services.AddScoped(typeof(ISecurityAccessorInfinityStorage), settings.SecurityAccessorInfinityStorageType);
}

services.AddSingleton(settings.DefaultSecurityRuleCredential);
settings.Initialize(services);

return services;
}
Expand Down Expand Up @@ -118,6 +93,7 @@ private static IServiceCollection RegisterGeneralSecuritySystem(this IServiceCol
.AddSingleton<ISecurityPathRestrictionService, SecurityPathRestrictionService>()
.AddScoped(typeof(ISecurityFilterFactory<>), typeof(SecurityFilterBuilderFactory<>))
.AddScoped(typeof(IAccessorsFilterFactory<>), typeof(AccessorsFilterBuilderFactory<>))
.AddScoped<ICurrentUser, CurrentUser>()
.AddKeyedScoped(
typeof(ISecurityProvider<>),
nameof(DomainSecurityRule.CurrentUser),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ namespace Framework.Authorization.Generated.DAL.NHibernate;
public record AuthorizationMappingSettings(DatabaseName DatabaseName, AuditDatabaseName? AuditDatabaseName)
: MappingSettings<PersistentDomainObjectBase>(typeof(AuthorizationMappingSettings).Assembly, DatabaseName, AuditDatabaseName)
{
public AuthorizationMappingSettings()
: this(new DatabaseName("", "auth"))
public AuthorizationMappingSettings(DatabaseName databaseName)
: this(databaseName, databaseName.ToDefaultAudit())
{
}

public AuthorizationMappingSettings(DatabaseName databaseName)
: this(databaseName, databaseName.ToDefaultAudit())
public AuthorizationMappingSettings()
: this(new DatabaseName("", "auth"))
{
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Framework.Authorization.Domain" auto-import="false" schema="Authorization.dbo" xmlns="urn:nhibernate-mapping-2.2">
<hibernate-mapping assembly="Framework.Authorization.Domain" auto-import="false" schema="auth" xmlns="urn:nhibernate-mapping-2.2">
<class name="Framework.Authorization.Domain.BusinessRole" table="BusinessRole">
<id name="Id" column="id" type="Guid" access="field.camelcase">
<generator class="guid.comb" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using Framework.DomainDriven.NHibernate.DALGenerator;
using Framework.DomainDriven;
using Framework.DomainDriven.NHibernate.DALGenerator;

namespace Framework.Authorization.TestGenerate;

public class DALGeneratorConfiguration : GeneratorConfigurationBase<ServerGenerationEnvironment>
public class DALGeneratorConfiguration(ServerGenerationEnvironment environment)
: GeneratorConfigurationBase<ServerGenerationEnvironment>(environment)
{
public DALGeneratorConfiguration(ServerGenerationEnvironment environment)
: base(environment)
{

}
public override DatabaseName DatabaseName => this.Environment.DatabaseName;
}

Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public partial class ServerGenerationEnvironment : GenerationEnvironmentBase
public readonly DALGeneratorConfiguration DAL;

public ServerGenerationEnvironment()
: this(new DatabaseName(nameof(Authorization)))
: this(new DatabaseName("", "auth"))
{
}

Expand All @@ -48,7 +48,7 @@ public ServerGenerationEnvironment(DatabaseName databaseName)
/// DatabaseName - Берётся из namespace'а сборки, которая сдержит тип PersistentDomainObjectBase (метод GetTargetSystemName);
/// Types - Список доменных объектов. Это все типы наследованные от PersistentDomainObjectBase той сборки, в которой содеержится PersistentDomainObjectBase.
/// </summary>
public MappingSettings MappingSettings => new MappingSettings<PersistentDomainObjectBase>(this.DAL.GetMappingGenerators().Select(mg => mg.Generate()), this.DatabaseName, true);
public MappingSettings MappingSettings => new MappingSettings<PersistentDomainObjectBase>(this.DAL.GetMappingGenerators().Select(mg => mg.Generate()), this.DatabaseName, this.DatabaseName.ToDefaultAudit());


public DatabaseName DatabaseName { get; }
Expand All @@ -61,7 +61,7 @@ public MappingSettings GetMappingSettings(DatabaseName dbName, AuditDatabaseName

public MappingSettings GetMappingSettingsWithoutAudit(DatabaseName dbName)
{
return new MappingSettings<PersistentDomainObjectBase>(this.DAL.GetMappingGenerators().Select(mg => mg.Generate()), dbName, false);
return new MappingSettings<PersistentDomainObjectBase>(this.DAL.GetMappingGenerators().Select(mg => mg.Generate()), dbName);
}

public override IDomainTypeRootExtendedMetadata ExtendedMetadata { get; } =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="Framework.Configuration.Domain" auto-import="false" schema="Configuration.dbo" xmlns="urn:nhibernate-mapping-2.2">
<hibernate-mapping assembly="Framework.Configuration.Domain" auto-import="false" schema="configuration" xmlns="urn:nhibernate-mapping-2.2">
<class name="Framework.Configuration.Domain.CodeFirstSubscription" table="CodeFirstSubscription">
<id name="Id" column="id" type="Guid" access="field.camelcase">
<generator class="guid.comb" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@

namespace Framework.Configuration.TestGenerate;

public class DALGeneratorConfiguration : GeneratorConfigurationBase<ServerGenerationEnvironment>
public class DALGeneratorConfiguration(ServerGenerationEnvironment environment)
: GeneratorConfigurationBase<ServerGenerationEnvironment>(environment)
{
public DALGeneratorConfiguration(ServerGenerationEnvironment environment)
: base(environment)
{
}


public override DatabaseName DatabaseName => this.Environment.DatabaseName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public partial class ServerGenerationEnvironment : GenerationEnvironmentBase
public readonly ServerDTOGeneratorConfiguration ServerDTO;

public ServerGenerationEnvironment()
:this(new DatabaseName(nameof(Configuration)))
:this(new DatabaseName("", "configuration"))
{
}

Expand All @@ -48,16 +48,16 @@ public ServerGenerationEnvironment(DatabaseName databaseName)

this.AuditDTO = new AuditDTOGeneratorConfiguration(this);

this.DatabaseName = databaseName ?? throw new ArgumentNullException(nameof(databaseName));
this.DatabaseName = databaseName;
}

public DatabaseName DatabaseName { get; }

public MappingSettings MappingSettings => new MappingSettings<PersistentDomainObjectBase>(this.DAL.GetMappingGenerators().Select(mg => mg.Generate()), this.DatabaseName, true);
public MappingSettings MappingSettings => this.GetMappingSettings(this.DatabaseName);

public MappingSettings GetMappingSettings(DatabaseName dbName, AuditDatabaseName dbAuditName)
public MappingSettings GetMappingSettings(DatabaseName dbName)
{
return new MappingSettings<PersistentDomainObjectBase>(this.DAL.GetMappingGenerators().Select(mg => mg.Generate()), dbName, dbAuditName);
return new MappingSettings<PersistentDomainObjectBase>(this.DAL.GetMappingGenerators().Select(mg => mg.Generate()), dbName);
}

public override IDomainTypeRootExtendedMetadata ExtendedMetadata { get; } =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ public string GenerateDB(
public string GenerateDB(
string serverName,
DatabaseName databaseName,
AuditDatabaseName auditDatabaseName,
DatabaseScriptGeneratorMode generatorMode = DatabaseScriptGeneratorMode.AutoGenerateUpdateChangeTypeScript,
DBGenerateScriptMode mode = DBGenerateScriptMode.AppliedOnTargetDatabase,
IEnumerable<string> migrationScriptFolderPaths = null,
IEnumerable<string> auditMigrationScriptFolderPaths = null,
bool preserveSchemaDatabase = false,
UserCredential credentials = null)
{
var generator = new DBGenerator(this.Environment.GetMappingSettings(databaseName, auditDatabaseName));
var generator = new DBGenerator(this.Environment.GetMappingSettings(databaseName));
var result = generator.Generate(
serverName,
mode: mode,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using Framework.Core;

namespace Framework.DomainDriven.ServiceModel.IAD;
namespace Framework.DomainDriven;

public class DefaultDBSessionEventListener(
IInitializeManager initializeManager,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Framework.DomainDriven.ServiceModel.IAD;
namespace Framework.DomainDriven;

public interface IInitializeManager
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace Framework.DomainDriven.ServiceModel.IAD;
namespace Framework.DomainDriven;

public class InitializeManager : IInitializeManager
{
Expand Down
Loading

0 comments on commit a85ff0b

Please sign in to comment.