You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
207 lines
6.3 KiB
207 lines
6.3 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Volo.Abp;
|
|
using Volo.Abp.Auditing;
|
|
using Volo.Abp.DependencyInjection;
|
|
using Volo.Abp.MultiTenancy;
|
|
using Volo.Abp.Timing;
|
|
using Volo.Abp.Users;
|
|
|
|
namespace Shentun.Peis.Data
|
|
{
|
|
|
|
//public class CustomerAuditPropertySetter
|
|
//{
|
|
|
|
//}
|
|
/// <summary>
|
|
/// 生成默认ID
|
|
/// </summary>
|
|
public class CustomerAuditPropertySetter : IAuditPropertySetter, ITransientDependency
|
|
{
|
|
protected ICurrentUser CurrentUser { get; }
|
|
protected ICurrentTenant CurrentTenant { get; }
|
|
protected IClock Clock { get; }
|
|
|
|
public CustomerAuditPropertySetter(
|
|
ICurrentUser currentUser,
|
|
ICurrentTenant currentTenant,
|
|
IClock clock)
|
|
{
|
|
CurrentUser = currentUser;
|
|
CurrentTenant = currentTenant;
|
|
Clock = clock;
|
|
}
|
|
|
|
public void SetCreationProperties(object targetObject)
|
|
{
|
|
SetCreationTime(targetObject);
|
|
SetCreatorId(targetObject);
|
|
}
|
|
|
|
public void SetModificationProperties(object targetObject)
|
|
{
|
|
SetLastModificationTime(targetObject);
|
|
SetLastModifierId(targetObject);
|
|
}
|
|
|
|
public void SetDeletionProperties(object targetObject)
|
|
{
|
|
SetDeletionTime(targetObject);
|
|
SetDeleterId(targetObject);
|
|
}
|
|
|
|
protected virtual void SetCreationTime(object targetObject)
|
|
{
|
|
if (!(targetObject is IHasCreationTime objectWithCreationTime))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (objectWithCreationTime.CreationTime == default)
|
|
{
|
|
ObjectHelper.TrySetProperty(objectWithCreationTime, x => x.CreationTime, () => Clock.Now);
|
|
}
|
|
}
|
|
|
|
protected virtual void SetCreatorId(object targetObject)
|
|
{
|
|
if (!CurrentUser.Id.HasValue)
|
|
{
|
|
#region 无登录操作
|
|
if (targetObject is IMayHaveCreator mayHaveCreatorObjectNoLogin)
|
|
{
|
|
ObjectHelper.TrySetProperty(mayHaveCreatorObjectNoLogin, x => x.CreatorId, () => Guid.Parse("3a0c4180-107c-0c89-b25b-0bd34666dcec"));
|
|
}
|
|
#endregion
|
|
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;
|
|
}
|
|
*/
|
|
|
|
if (targetObject is IMayHaveCreator mayHaveCreatorObject)
|
|
{
|
|
if (mayHaveCreatorObject.CreatorId.HasValue && mayHaveCreatorObject.CreatorId.Value != default)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ObjectHelper.TrySetProperty(mayHaveCreatorObject, x => x.CreatorId, () => CurrentUser.Id);
|
|
}
|
|
else if (targetObject is IMustHaveCreator mustHaveCreatorObject)
|
|
{
|
|
if (mustHaveCreatorObject.CreatorId != default)
|
|
{
|
|
return;
|
|
}
|
|
|
|
ObjectHelper.TrySetProperty(mustHaveCreatorObject, x => x.CreatorId, () => CurrentUser.Id.Value);
|
|
}
|
|
}
|
|
|
|
protected virtual void SetLastModificationTime(object targetObject)
|
|
{
|
|
if (targetObject is IHasModificationTime objectWithModificationTime)
|
|
{
|
|
objectWithModificationTime.LastModificationTime = Clock.Now;
|
|
}
|
|
}
|
|
|
|
protected virtual void SetLastModifierId(object targetObject)
|
|
{
|
|
if (!(targetObject is IModificationAuditedObject modificationAuditedObject))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!CurrentUser.Id.HasValue)
|
|
{
|
|
//modificationAuditedObject.LastModifierId = null;
|
|
//return;
|
|
|
|
#region 无登录操作
|
|
modificationAuditedObject.LastModifierId = Guid.Parse("3a0c4180-107c-0c89-b25b-0bd34666dcec");
|
|
return;
|
|
#endregion
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
protected virtual void SetDeletionTime(object targetObject)
|
|
{
|
|
if (targetObject is IHasDeletionTime objectWithDeletionTime)
|
|
{
|
|
if (objectWithDeletionTime.DeletionTime == null)
|
|
{
|
|
objectWithDeletionTime.DeletionTime = Clock.Now;
|
|
}
|
|
}
|
|
}
|
|
|
|
protected virtual void SetDeleterId(object targetObject)
|
|
{
|
|
if (!(targetObject is IDeletionAuditedObject deletionAuditedObject))
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (deletionAuditedObject.DeleterId != null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!CurrentUser.Id.HasValue)
|
|
{
|
|
deletionAuditedObject.DeleterId = null;
|
|
return;
|
|
}
|
|
|
|
if (deletionAuditedObject is IMultiTenant multiTenantEntity)
|
|
{
|
|
if (multiTenantEntity.TenantId != CurrentUser.TenantId)
|
|
{
|
|
deletionAuditedObject.DeleterId = null;
|
|
return;
|
|
}
|
|
}
|
|
|
|
deletionAuditedObject.DeleterId = CurrentUser.Id;
|
|
}
|
|
}
|
|
}
|