mirror of https://github.com/abpframework/abp.git
12 changed files with 260 additions and 16 deletions
@ -0,0 +1,2 @@ |
|||||
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> |
||||
|
<s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp71</s:String></wpf:ResourceDictionary> |
||||
@ -0,0 +1,111 @@ |
|||||
|
using Volo.Abp.DependencyInjection; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
using Volo.Abp.Timing; |
||||
|
using Volo.Abp.Users; |
||||
|
|
||||
|
namespace Volo.Abp.Auditing |
||||
|
{ |
||||
|
public class AuditPropertySetter : IAuditPropertySetter, ITransientDependency |
||||
|
{ |
||||
|
protected ICurrentUser CurrentUser { get; } |
||||
|
|
||||
|
protected ICurrentTenant CurrentTenant { get; } |
||||
|
|
||||
|
protected IClock Clock { get; } |
||||
|
|
||||
|
public AuditPropertySetter( |
||||
|
ICurrentUser currentUser, |
||||
|
ICurrentTenant currentTenant, |
||||
|
IClock clock) |
||||
|
{ |
||||
|
CurrentUser = currentUser; |
||||
|
CurrentTenant = currentTenant; |
||||
|
Clock = clock; |
||||
|
} |
||||
|
|
||||
|
public void SetCreationAuditProperties(object targetObject) |
||||
|
{ |
||||
|
if (!(targetObject is IHasCreationTime objectWithCreationTime)) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (objectWithCreationTime.CreationTime == default) |
||||
|
{ |
||||
|
objectWithCreationTime.CreationTime = Clock.Now; |
||||
|
} |
||||
|
|
||||
|
if (!(targetObject is ICreationAudited creationAuditedObject)) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (creationAuditedObject.CreatorId != null) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (!CurrentUser.Id.HasValue) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (targetObject is IMultiTenant multiTenantEntity) |
||||
|
{ |
||||
|
if (multiTenantEntity.TenantId != CurrentUser.TenantId) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/* TODO: The code below is from old ABP, not implemented yet |
||||
|
if (tenantId.HasValue && MultiTenancyHelper.IsHostEntity(entity)) |
||||
|
{ |
||||
|
//Tenant user created a host entity
|
||||
|
return; |
||||
|
} |
||||
|
*/ |
||||
|
|
||||
|
creationAuditedObject.CreatorId = CurrentUser.Id; |
||||
|
} |
||||
|
|
||||
|
public void SetModificationAuditProperties(object auditedObject) |
||||
|
{ |
||||
|
if (auditedObject is IHasModificationTime objectWithModificationTime) |
||||
|
{ |
||||
|
objectWithModificationTime.LastModificationTime = Clock.Now; |
||||
|
} |
||||
|
|
||||
|
if (!(auditedObject is IModificationAudited modificationAuditedObject)) |
||||
|
{ |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (!CurrentUser.Id.HasValue) |
||||
|
{ |
||||
|
modificationAuditedObject.LastModifierId = null; |
||||
|
return; |
||||
|
} |
||||
|
|
||||
|
if (modificationAuditedObject is IMultiTenant multiTenantEntity) |
||||
|
{ |
||||
|
if (multiTenantEntity.TenantId != CurrentUser.TenantId) |
||||
|
{ |
||||
|
modificationAuditedObject.LastModifierId = null; |
||||
|
return; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/* TODO: The code below is from old ABP, not implemented yet |
||||
|
if (tenantId.HasValue && MultiTenancyHelper.IsHostEntity(entity)) |
||||
|
{ |
||||
|
//Tenant user modified a host entity
|
||||
|
modificationAuditedObject.LastModifierId = null; |
||||
|
return; |
||||
|
} |
||||
|
*/ |
||||
|
|
||||
|
modificationAuditedObject.LastModifierId = CurrentUser.Id; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,9 @@ |
|||||
|
namespace Volo.Abp.Auditing |
||||
|
{ |
||||
|
public interface IAuditPropertySetter |
||||
|
{ |
||||
|
void SetCreationAuditProperties(object targetObject); |
||||
|
|
||||
|
void SetModificationAuditProperties(object auditedObject); |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,24 @@ |
|||||
|
<Project Sdk="Microsoft.NET.Sdk"> |
||||
|
|
||||
|
<PropertyGroup> |
||||
|
<TargetFramework>netcoreapp2.0</TargetFramework> |
||||
|
<LangVersion>latest</LangVersion> |
||||
|
<AssemblyName>Volo.Abp.Auditing.Tests</AssemblyName> |
||||
|
<PackageId>Volo.Abp.Auditing.Tests</PackageId> |
||||
|
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles> |
||||
|
<GenerateAssemblyConfigurationAttribute>false</GenerateAssemblyConfigurationAttribute> |
||||
|
<GenerateAssemblyCompanyAttribute>false</GenerateAssemblyCompanyAttribute> |
||||
|
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute> |
||||
|
<RootNamespace /> |
||||
|
</PropertyGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<ProjectReference Include="..\..\src\Volo.Abp.Auditing\Volo.Abp.Auditing.csproj" /> |
||||
|
<ProjectReference Include="..\AbpTestBase\AbpTestBase.csproj" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
<ItemGroup> |
||||
|
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" /> |
||||
|
</ItemGroup> |
||||
|
|
||||
|
</Project> |
||||
@ -0,0 +1,2 @@ |
|||||
|
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> |
||||
|
<s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp71</s:String></wpf:ResourceDictionary> |
||||
@ -0,0 +1,57 @@ |
|||||
|
using System; |
||||
|
using NSubstitute; |
||||
|
using Volo.Abp.MultiTenancy; |
||||
|
using Volo.Abp.Timing; |
||||
|
using Volo.Abp.Users; |
||||
|
|
||||
|
namespace Volo.Abp.Auditing |
||||
|
{ |
||||
|
public class AuditPropertySetterTestBase |
||||
|
{ |
||||
|
protected Guid? CurrentUserId = null; |
||||
|
protected Guid? CurrentUserTenantId = null; |
||||
|
protected Guid? CurrentTenantId = null; |
||||
|
|
||||
|
protected DateTime Now = DateTime.Now; |
||||
|
|
||||
|
protected MyAuditedObject TargetObject; |
||||
|
|
||||
|
protected readonly AuditPropertySetter AuditPropertySetter; |
||||
|
|
||||
|
public AuditPropertySetterTestBase() |
||||
|
{ |
||||
|
AuditPropertySetter = CreateAuditPropertySetter(); |
||||
|
TargetObject = new MyAuditedObject(); |
||||
|
} |
||||
|
|
||||
|
private AuditPropertySetter CreateAuditPropertySetter() |
||||
|
{ |
||||
|
var currentUser = Substitute.For<ICurrentUser>(); |
||||
|
currentUser.Id.Returns(ci => CurrentUserId); |
||||
|
currentUser.TenantId.Returns(ci => CurrentUserTenantId); |
||||
|
|
||||
|
var currentTenant = Substitute.For<ICurrentTenant>(); |
||||
|
currentTenant.Id.Returns(ci => CurrentTenantId); |
||||
|
|
||||
|
var clock = Substitute.For<IClock>(); |
||||
|
clock.Now.Returns(Now); |
||||
|
|
||||
|
return new AuditPropertySetter( |
||||
|
currentUser, |
||||
|
currentTenant, |
||||
|
clock |
||||
|
); |
||||
|
} |
||||
|
|
||||
|
public class MyAuditedObject : IFullAudited |
||||
|
{ |
||||
|
public DateTime CreationTime { get; set; } |
||||
|
public Guid? CreatorId { get; set; } |
||||
|
public DateTime? LastModificationTime { get; set; } |
||||
|
public Guid? LastModifierId { get; set; } |
||||
|
public bool IsDeleted { get; set; } |
||||
|
public DateTime? DeletionTime { get; set; } |
||||
|
public Guid? DeleterId { get; set; } |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,28 @@ |
|||||
|
using System; |
||||
|
using Shouldly; |
||||
|
using Xunit; |
||||
|
|
||||
|
namespace Volo.Abp.Auditing |
||||
|
{ |
||||
|
public class AuditPropertySetter_CreationAudit_Tests : AuditPropertySetterTestBase |
||||
|
{ |
||||
|
[Fact] |
||||
|
public void Should_Set_CreationTime() |
||||
|
{ |
||||
|
AuditPropertySetter.SetCreationAuditProperties(TargetObject); |
||||
|
|
||||
|
TargetObject.CreationTime.ShouldNotBe(default); |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Should_Set_CreatorId() |
||||
|
{ |
||||
|
CurrentUserId = Guid.NewGuid(); |
||||
|
|
||||
|
AuditPropertySetter.SetCreationAuditProperties(TargetObject); |
||||
|
|
||||
|
TargetObject.CreationTime.ShouldNotBe(default); |
||||
|
TargetObject.CreatorId.ShouldBe(CurrentUserId); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue