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

Fluent mapping auth and configuration schemas #529

Open
wants to merge 4 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
@@ -1,6 +1,7 @@
using Framework.Core;
using Framework.DomainDriven.Serialization;
using Framework.Persistent;
using Framework.Persistent.Mapping;
using Framework.Restriction;

namespace Framework.Authorization.Domain;
Expand All @@ -9,6 +10,7 @@ namespace Framework.Authorization.Domain;
/// Набор секьюрных операций, который выдается принципалу вместе с контекcтом их применения
/// </summary>
[UniqueGroup]
[IgnoreHbmMapping]
public class BusinessRole : BaseDirectory
{
private readonly ICollection<Permission> permissions = new List<Permission>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace Framework.Authorization.Domain;
/// Пермиссии могут выдаваться в рамках контекстов
/// </remarks>
/// <seealso cref="SecurityContextType"/>
[IgnoreHbmMapping]
[System.Diagnostics.DebuggerDisplay("Principal={Principal.Name}, Role={Role.Name}")]
public class Permission : AuditPersistentDomainObjectBase,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
using Framework.DomainDriven.Serialization;
using Framework.DomainDriven.Tracking.LegacyValidators;
using Framework.Persistent;
using Framework.Persistent.Mapping;
using Framework.Restriction;

namespace Framework.Authorization.Domain;

/// <summary>
/// Связь между пермиссией и контекстом
/// </summary>
[IgnoreHbmMapping]
public class PermissionRestriction : AuditPersistentDomainObjectBase, IDetail<Permission>
{
private readonly Permission permission;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;

using Framework.Persistent;
using Framework.Persistent.Mapping;
using Framework.Restriction;

namespace Framework.Authorization.Domain;
Expand All @@ -10,6 +11,7 @@ namespace Framework.Authorization.Domain;
/// </summary>
[DebuggerDisplay("{Name}, RunAs={RunAs}")]
[UniqueGroup]
[IgnoreHbmMapping]
public class Principal : BaseDirectory, IMaster<Permission>
{
private readonly ICollection<Permission> permissions = new List<Permission>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Framework.DomainDriven.Serialization;
using Framework.Persistent.Mapping;

namespace Framework.Authorization.Domain;

Expand All @@ -11,6 +12,7 @@ namespace Framework.Authorization.Domain;
/// <seealso cref="PermissionRestriction"/>
/// Типы, в контексте которых выдаются права пользователю, записываются вручную на уровне SQL в базу конкретной системы
/// </remarks>
[IgnoreHbmMapping]
public class SecurityContextType : BaseDirectory
{
/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
<Compile Include="..\..\__SolutionItems\CommonAssemblyInfo.cs" Link="Properties\CommonAssemblyInfo.cs" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="Mapping\Generated.Framework.Authorization.Domain.hbm.xml" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Framework.Authorization.Domain\Framework.Authorization.Domain.csproj" />
<ProjectReference Include="..\..\_DomainDriven\Framework.DomainDriven.NHibernate\Framework.DomainDriven.NHibernate.csproj" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using FluentNHibernate.Mapping;

using Framework.Authorization.Domain;

namespace Framework.Authorization.Generated.DAL.NHibernate.Mapping.Base;

public abstract class AuthBaseMap<TEntity> : ClassMap<TEntity>
where TEntity : AuditPersistentDomainObjectBase
{
protected AuthBaseMap()
{
this.Schema("auth");

this.DynamicUpdate();

this.Id(x => x.Id).GeneratedBy.GuidComb();

this.Map(x => x.CreatedBy);
this.Map(x => x.CreateDate);
this.Map(x => x.ModifiedBy);
this.Map(x => x.ModifyDate);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Framework.Authorization.Domain;
using Framework.Authorization.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Authorization.Generated.DAL.NHibernate.Mapping;

public class BusinessRoleMap : AuthBaseMap<BusinessRole>
{
public BusinessRoleMap()
{
this.Map(x => x.Name).Unique().Not.Nullable();
this.Map(x => x.Description);
this.HasMany(x => x.Permissions).AsSet().Inverse().Cascade.None();
}
}
Original file line number Diff line number Diff line change
@@ -1,80 +1,2 @@
<?xml version="1.0" encoding="utf-8"?>
<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" />
</id>
<property name="CreateDate" column="createDate" type="timestamp" access="field.camelcase" />
<property name="CreatedBy" column="createdBy" access="field.camelcase" />
<property name="Description" column="description" access="field.camelcase" />
<property name="ModifiedBy" column="modifiedBy" access="field.camelcase" />
<property name="ModifyDate" column="modifyDate" type="timestamp" access="field.camelcase" />
<property name="Name" column="name" unique-key="UIX_nameBusinessRole" access="field.camelcase" />
<set name="Permissions" inverse="true" access="field.camelcase" cascade="None">
<key column="roleId" />
<one-to-many class="Framework.Authorization.Domain.Permission" />
</set>
</class>
<class name="Framework.Authorization.Domain.Permission" table="Permission">
<id name="Id" column="id" type="Guid" access="field.camelcase">
<generator class="guid.comb" />
</id>
<property name="Comment" column="comment" length="2147483647" access="field.camelcase" />
<property name="CreateDate" column="createDate" type="timestamp" access="field.camelcase" />
<property name="CreatedBy" column="createdBy" access="field.camelcase" />
<property name="ModifiedBy" column="modifiedBy" access="field.camelcase" />
<property name="ModifyDate" column="modifyDate" type="timestamp" access="field.camelcase" />
<many-to-one name="DelegatedFrom" column="delegatedFromId" class="Framework.Authorization.Domain.Permission" access="field.camelcase" />
<many-to-one name="Principal" column="principalId" class="Framework.Authorization.Domain.Principal" access="field.camelcase" />
<many-to-one name="Role" column="roleId" class="Framework.Authorization.Domain.BusinessRole" access="field.camelcase" />
<set name="DelegatedTo" inverse="true" access="field.camelcase" cascade="all-delete-orphan">
<key column="delegatedFromId" />
<one-to-many class="Framework.Authorization.Domain.Permission" />
</set>
<set name="Restrictions" inverse="true" access="field.camelcase" cascade="all-delete-orphan">
<key column="permissionId" />
<one-to-many class="Framework.Authorization.Domain.PermissionRestriction" />
</set>
<component name="Period" class="Framework.Core.Period, Framework.Core" access="field.camelcase">
<property name="EndDate" column="periodendDate" access="field.camelcase" type="timestamp" />
<property name="StartDate" column="periodstartDate" access="field.camelcase" type="timestamp" />
</component>
</class>
<class name="Framework.Authorization.Domain.PermissionRestriction" table="PermissionRestriction">
<id name="Id" column="id" type="Guid" access="field.camelcase">
<generator class="guid.comb" />
</id>
<property name="CreateDate" column="createDate" type="timestamp" access="field.camelcase" />
<property name="CreatedBy" column="createdBy" access="field.camelcase" />
<property name="ModifiedBy" column="modifiedBy" access="field.camelcase" />
<property name="ModifyDate" column="modifyDate" type="timestamp" access="field.camelcase" />
<property name="SecurityContextId" column="securityContextId" unique-key="UIX_permission_securityContextId_securityContextTypePermissionRestriction" access="field.camelcase" />
<many-to-one name="Permission" column="permissionId" class="Framework.Authorization.Domain.Permission" unique-key="UIX_permission_securityContextId_securityContextTypePermissionRestriction" access="field.camelcase" />
<many-to-one name="SecurityContextType" column="securityContextTypeId" class="Framework.Authorization.Domain.SecurityContextType" unique-key="UIX_permission_securityContextId_securityContextTypePermissionRestriction" access="field.camelcase" />
</class>
<class name="Framework.Authorization.Domain.Principal" table="Principal">
<id name="Id" column="id" type="Guid" access="field.camelcase">
<generator class="guid.comb" />
</id>
<property name="CreateDate" column="createDate" type="timestamp" access="field.camelcase" />
<property name="CreatedBy" column="createdBy" access="field.camelcase" />
<property name="ModifiedBy" column="modifiedBy" access="field.camelcase" />
<property name="ModifyDate" column="modifyDate" type="timestamp" access="field.camelcase" />
<property name="Name" column="name" unique-key="UIX_namePrincipal" access="field.camelcase" />
<many-to-one name="RunAs" column="runAsId" class="Framework.Authorization.Domain.Principal" access="field.camelcase" />
<set name="Permissions" inverse="true" access="field.camelcase" cascade="all-delete-orphan">
<key column="principalId" />
<one-to-many class="Framework.Authorization.Domain.Permission" />
</set>
</class>
<class name="Framework.Authorization.Domain.SecurityContextType" table="SecurityContextType">
<id name="Id" column="id" type="Guid" access="field.camelcase">
<generator class="guid.comb" />
</id>
<property name="CreateDate" column="createDate" type="timestamp" access="field.camelcase" />
<property name="CreatedBy" column="createdBy" access="field.camelcase" />
<property name="ModifiedBy" column="modifiedBy" access="field.camelcase" />
<property name="ModifyDate" column="modifyDate" type="timestamp" access="field.camelcase" />
<property name="Name" column="name" access="field.camelcase" />
</class>
</hibernate-mapping>
<hibernate-mapping assembly="Framework.Authorization.Domain" auto-import="false" schema="auth" xmlns="urn:nhibernate-mapping-2.2" />
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using Framework.Authorization.Domain;
using Framework.Authorization.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Authorization.Generated.DAL.NHibernate.Mapping;

public class PermissionMap : AuthBaseMap<Permission>
{
public PermissionMap()
{
this.Map(x => x.Comment).Length(int.MaxValue);
this.References(x => x.DelegatedFrom).Column($"{nameof(Permission.DelegatedFrom)}Id");
this.References(x => x.Principal).Column($"{nameof(Permission.Principal)}Id").Not.Nullable();
this.References(x => x.Role).Column($"{nameof(Permission.Role)}Id").Not.Nullable();
this.Component(
x => x.Period,
part =>
{
part.Map(x => x.EndDate).Column("periodendDate");
part.Map(x => x.StartDate).Column("periodstartDate");
});

this.HasMany(x => x.DelegatedTo).AsSet().Inverse().Cascade.AllDeleteOrphan();
this.HasMany(x => x.Restrictions).AsSet().Inverse().Cascade.AllDeleteOrphan();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Framework.Authorization.Domain;
using Framework.Authorization.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Authorization.Generated.DAL.NHibernate.Mapping;

public class PermissionRestrictionMap : AuthBaseMap<PermissionRestriction>
{
public PermissionRestrictionMap()
{
this.Map(x => x.SecurityContextId).Not.Nullable()
.UniqueKey("UIX_permission_securityContextId_securityContextTypePermissionRestriction");
this.References(x => x.Permission).Column($"{nameof(PermissionRestriction.Permission)}Id").Not.Nullable()
.UniqueKey("UIX_permission_securityContextId_securityContextTypePermissionRestriction");
this.References(x => x.SecurityContextType).Column($"{nameof(PermissionRestriction.SecurityContextType)}Id").Not.Nullable()
.UniqueKey("UIX_permission_securityContextId_securityContextTypePermissionRestriction");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Framework.Authorization.Domain;
using Framework.Authorization.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Authorization.Generated.DAL.NHibernate.Mapping;

public class PrincipalMap : AuthBaseMap<Principal>
{
public PrincipalMap()
{
this.Map(x => x.Name).Not.Nullable().UniqueKey("UIX_namePrincipal");
this.References(x => x.RunAs).Column($"{nameof(Principal.RunAs)}Id");
this.HasMany(x => x.Permissions).AsSet().Inverse().Cascade.AllDeleteOrphan();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Framework.Authorization.Domain;
using Framework.Authorization.Generated.DAL.NHibernate.Mapping.Base;

namespace Framework.Authorization.Generated.DAL.NHibernate.Mapping;

public class SecurityContextTypeMap : AuthBaseMap<SecurityContextType>
{
public SecurityContextTypeMap() => this.Map(x => x.Name).Not.Nullable();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Framework.DomainDriven;
using Framework.DomainDriven.DBGenerator;
using Framework.DomainDriven.NHibernate;

namespace Framework.Authorization.TestGenerate;

Expand Down Expand Up @@ -50,4 +51,28 @@ public string GenerateDB(
var lines = result.ToNewLinesCombined();
return lines;
}

public string GenerateDB(
MappingSettings mappingSettings,
string serverName,
DatabaseScriptGeneratorMode generatorMode = DatabaseScriptGeneratorMode.AutoGenerateUpdateChangeTypeScript,
DBGenerateScriptMode mode = DBGenerateScriptMode.AppliedOnTargetDatabase,
IEnumerable<string> migrationScriptFolderPaths = null,
IEnumerable<string> auditMigrationScriptFolderPaths = null,
bool preserveSchemaDatabase = false,
DbUserCredential credentials = null)
{
var generator = new DBGenerator(mappingSettings);
var result = generator.Generate(
serverName,
mode: mode,
generatorMode: generatorMode,
migrationScriptFolderPaths: migrationScriptFolderPaths,
auditMigrationScriptFolderPaths: auditMigrationScriptFolderPaths,
preserveSchemaDatabase: preserveSchemaDatabase,
credentials: credentials);

var lines = result.ToNewLinesCombined();
return lines;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Framework.Configuration.Domain;
/// <seealso cref="DomainObjectBase" />
[UniqueGroup]
[NotAuditedClass]
[IgnoreHbmMapping]
public class CodeFirstSubscription : AuditPersistentDomainObjectBase, ICodeObject<string>
{
private readonly string code;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace Framework.Configuration.Domain;

[NotAuditedClass]
[IgnoreHbmMapping]
public class ControlSettings : BaseDirectory,
IMaster<ControlSettings>,
IMaster<ControlSettingsParam>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace Framework.Configuration.Domain;

[NotAuditedClass]
[IgnoreHbmMapping]
public class ControlSettingsParam : AuditPersistentDomainObjectBase, IMaster<ControlSettingsParamValue>, IDetail<ControlSettings>, ITypeObject<ControlSettingParamType>
{
private readonly ICollection<ControlSettingsParamValue> controlSettingsParamValues = new List<ControlSettingsParamValue>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace Framework.Configuration.Domain;

[NotAuditedClass]
[IgnoreHbmMapping]
public class ControlSettingsParamValue : AuditPersistentDomainObjectBase, IDetail<ControlSettingsParam>
{
private readonly ControlSettingsParam controlSettingsParam;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Framework.Configuration.Domain;
/// Событие над объектом (Save, Remove etc), сохраняемое в базу.
/// </summary>
[NotAuditedClass]
[IgnoreHbmMapping]
public class DomainObjectEvent : AuditPersistentDomainObjectBase
{
private readonly QueueProgressStatus status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Framework.Configuration.Domain;
/// </summary>
[UniqueGroup]
[NotAuditedClass]
[IgnoreHbmMapping]
public class DomainObjectModification : AuditPersistentDomainObjectBase, ITypeObject<ModificationType>, IVersionObject<long>
{
private Guid domainObjectId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Framework.Configuration.Domain;
/// Нотификация хранимая в бд
/// </summary>
[NotAuditedClass]
[IgnoreHbmMapping]
public class DomainObjectNotification : AuditPersistentDomainObjectBase
{
private readonly QueueProgressStatus status;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ namespace Framework.Configuration.Domain;
/// Механизм Framework-а сохраняет все ошибки по всем системам, записывает их в базу и высылает на почту
/// </remarks>
[NotAuditedClass]
[IgnoreHbmMapping]
public class ExceptionMessage : AuditPersistentDomainObjectBase
{
private ExceptionMessage innerException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ namespace Framework.Configuration.Domain;
/// </summary>
[UniqueGroup]
[NotAuditedClass]
[IgnoreHbmMapping]
public class GenericNamedLock : BaseDirectory;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace Framework.Configuration.Domain;
/// Сообщение, отправленное пользователю
/// </summary>
[NotAuditedClass]
[IgnoreHbmMapping]
public class SentMessage : AuditPersistentDomainObjectBase
{
private readonly string from;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ namespace Framework.Configuration.Domain;
/// </summary>
[UniqueGroup]
[NotAuditedClass]
[IgnoreHbmMapping]
public class Sequence : BaseDirectory, INumberObject<long>
{
private long number;
Expand Down
Loading
Loading