diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditPropertySetter.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditPropertySetter.cs
index ef79af2b05..c273799c6b 100644
--- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditPropertySetter.cs
+++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/AuditPropertySetter.cs
@@ -12,8 +12,8 @@ namespace Volo.Abp.Auditing
protected IClock Clock { get; }
public AuditPropertySetter(
- ICurrentUser currentUser,
- ICurrentTenant currentTenant,
+ ICurrentUser currentUser,
+ ICurrentTenant currentTenant,
IClock clock)
{
CurrentUser = currentUser;
@@ -54,16 +54,6 @@ namespace Volo.Abp.Auditing
private void SetCreatorId(object targetObject)
{
- if (!(targetObject is ICreationAudited creationAuditedObject))
- {
- return;
- }
-
- if (creationAuditedObject.CreatorId != null)
- {
- return;
- }
-
if (!CurrentUser.Id.HasValue)
{
return;
@@ -78,16 +68,33 @@ namespace Volo.Abp.Auditing
}
/* TODO: The code below is from old ABP, not implemented yet
- if (tenantId.HasValue && MultiTenancyHelper.IsHostEntity(entity))
+ if (tenantId.HasValue && MultiTenancyHelper.IsHostEntity(entity))
+ {
+ //Tenant user created a host entity
+ return;
+ }
+ */
+
+ if (targetObject is IMayHaveCreator mayHaveCreatorObject)
{
- //Tenant user created a host entity
- return;
+ if (mayHaveCreatorObject.CreatorId.HasValue && mayHaveCreatorObject.CreatorId.Value != default)
+ {
+ return;
+ }
+
+ mayHaveCreatorObject.CreatorId = CurrentUser.Id;
}
- */
+ else if (targetObject is IMustHaveCreator mustHaveCreatorObject)
+ {
+ if (mustHaveCreatorObject.CreatorId != default)
+ {
+ return;
+ }
- creationAuditedObject.CreatorId = CurrentUser.Id;
+ mustHaveCreatorObject.CreatorId = CurrentUser.Id.Value;
+ }
}
-
+
private void SetLastModificationTime(object targetObject)
{
if (targetObject is IHasModificationTime objectWithModificationTime)
diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/ICreationAudited.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/ICreationAudited.cs
index ce9be3d8a4..14d53e4fd9 100644
--- a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/ICreationAudited.cs
+++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/ICreationAudited.cs
@@ -1,27 +1,19 @@
-using System;
-
namespace Volo.Abp.Auditing
{
///
/// This interface can be implemented to store creation information (who and when created).
///
- public interface ICreationAudited : IHasCreationTime
+ public interface ICreationAudited : IHasCreationTime, IMayHaveCreator
{
- ///
- /// Id of the creator user.
- ///
- Guid? CreatorId { get; set; }
+
}
///
- /// Adds navigation properties to interface for user.
+ /// Adds navigation property (object reference) to interface.
///
- /// Type of the user
- public interface ICreationAudited : ICreationAudited
+ /// Type of the user
+ public interface ICreationAudited : ICreationAudited, IMayHaveCreator
{
- ///
- /// Reference to the creator user.
- ///
- TUser Creator { get; set; }
+
}
}
\ No newline at end of file
diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IMayHaveCreator.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IMayHaveCreator.cs
new file mode 100644
index 0000000000..7960827d77
--- /dev/null
+++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IMayHaveCreator.cs
@@ -0,0 +1,25 @@
+using System;
+using JetBrains.Annotations;
+
+namespace Volo.Abp.Auditing
+{
+ public interface IMayHaveCreator
+ {
+ ///
+ /// Reference to the creator.
+ ///
+ [CanBeNull]
+ TCreator Creator { get; set; }
+ }
+
+ ///
+ /// Standard interface for an entity that MAY have a creator.
+ ///
+ public interface IMayHaveCreator
+ {
+ ///
+ /// Id of the creator.
+ ///
+ Guid? CreatorId { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IMustHaveCreator.cs b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IMustHaveCreator.cs
new file mode 100644
index 0000000000..87671f97d6
--- /dev/null
+++ b/framework/src/Volo.Abp.Auditing/Volo/Abp/Auditing/IMustHaveCreator.cs
@@ -0,0 +1,28 @@
+using System;
+using JetBrains.Annotations;
+
+namespace Volo.Abp.Auditing
+{
+ ///
+ /// Standard interface for an entity that MUST have a creator of type .
+ ///
+ public interface IMustHaveCreator : IMustHaveCreator
+ {
+ ///
+ /// Reference to the creator.
+ ///
+ [NotNull]
+ TCreator Creator { get; set; }
+ }
+
+ ///
+ /// Standard interface for an entity that MUST have a creator.
+ ///
+ public interface IMustHaveCreator
+ {
+ ///
+ /// Id of the creator.
+ ///
+ Guid CreatorId { get; set; }
+ }
+}
\ No newline at end of file
diff --git a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs
index 0db2a5c5da..1d11b04cc9 100644
--- a/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs
+++ b/framework/src/Volo.Abp.EntityFrameworkCore/Volo/Abp/EntityFrameworkCore/Modeling/AbpEntityTypeBuilderExtensions.cs
@@ -33,6 +33,18 @@ namespace Volo.Abp.EntityFrameworkCore.Modeling
b.Property(x => x.DeletionTime).IsRequired(false).HasColumnName(nameof(IHasDeletionTime.DeletionTime));
}
+ public static void ConfigureMayHaveCreator(this EntityTypeBuilder b)
+ where T : class, IMayHaveCreator
+ {
+ b.Property(x => x.CreatorId).IsRequired(false).HasColumnName(nameof(IMayHaveCreator.CreatorId));
+ }
+
+ public static void ConfigureMustHaveCreator(this EntityTypeBuilder b)
+ where T : class, IMustHaveCreator
+ {
+ b.Property(x => x.CreatorId).IsRequired().HasColumnName(nameof(IMustHaveCreator.CreatorId));
+ }
+
public static void ConfigureDeletionAudited(this EntityTypeBuilder b)
where T : class, IDeletionAudited
{