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

2 years ago
2 years ago
2 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Volo.Abp;
  7. using Volo.Abp.Auditing;
  8. using Volo.Abp.DependencyInjection;
  9. using Volo.Abp.MultiTenancy;
  10. using Volo.Abp.Timing;
  11. using Volo.Abp.Users;
  12. namespace Shentun.Peis.Data
  13. {
  14. //public class CustomerAuditPropertySetter
  15. //{
  16. //}
  17. /// <summary>
  18. /// 生成默认ID
  19. /// </summary>
  20. public class CustomerAuditPropertySetter : IAuditPropertySetter, ITransientDependency
  21. {
  22. protected ICurrentUser CurrentUser { get; }
  23. protected ICurrentTenant CurrentTenant { get; }
  24. protected IClock Clock { get; }
  25. public CustomerAuditPropertySetter(
  26. ICurrentUser currentUser,
  27. ICurrentTenant currentTenant,
  28. IClock clock)
  29. {
  30. CurrentUser = currentUser;
  31. CurrentTenant = currentTenant;
  32. Clock = clock;
  33. }
  34. public void SetCreationProperties(object targetObject)
  35. {
  36. SetCreationTime(targetObject);
  37. SetCreatorId(targetObject);
  38. }
  39. public void SetModificationProperties(object targetObject)
  40. {
  41. SetLastModificationTime(targetObject);
  42. SetLastModifierId(targetObject);
  43. }
  44. public void SetDeletionProperties(object targetObject)
  45. {
  46. SetDeletionTime(targetObject);
  47. SetDeleterId(targetObject);
  48. }
  49. protected virtual void SetCreationTime(object targetObject)
  50. {
  51. if (!(targetObject is IHasCreationTime objectWithCreationTime))
  52. {
  53. return;
  54. }
  55. if (objectWithCreationTime.CreationTime == default)
  56. {
  57. ObjectHelper.TrySetProperty(objectWithCreationTime, x => x.CreationTime, () => Clock.Now);
  58. }
  59. }
  60. protected virtual void SetCreatorId(object targetObject)
  61. {
  62. if (!CurrentUser.Id.HasValue)
  63. {
  64. #region 无登录操作
  65. if (targetObject is IMayHaveCreator mayHaveCreatorObjectNoLogin)
  66. {
  67. ObjectHelper.TrySetProperty(mayHaveCreatorObjectNoLogin, x => x.CreatorId, () => Guid.Parse("3a0c4180-107c-0c89-b25b-0bd34666dcec"));
  68. }
  69. #endregion
  70. return;
  71. }
  72. if (targetObject is IMultiTenant multiTenantEntity)
  73. {
  74. if (multiTenantEntity.TenantId != CurrentUser.TenantId)
  75. {
  76. return;
  77. }
  78. }
  79. /* TODO: The code below is from old ABP, not implemented yet
  80. if (tenantId.HasValue && MultiTenancyHelper.IsHostEntity(entity))
  81. {
  82. //Tenant user created a host entity
  83. return;
  84. }
  85. */
  86. if (targetObject is IMayHaveCreator mayHaveCreatorObject)
  87. {
  88. if (mayHaveCreatorObject.CreatorId.HasValue && mayHaveCreatorObject.CreatorId.Value != default)
  89. {
  90. return;
  91. }
  92. ObjectHelper.TrySetProperty(mayHaveCreatorObject, x => x.CreatorId, () => CurrentUser.Id);
  93. }
  94. else if (targetObject is IMustHaveCreator mustHaveCreatorObject)
  95. {
  96. if (mustHaveCreatorObject.CreatorId != default)
  97. {
  98. return;
  99. }
  100. ObjectHelper.TrySetProperty(mustHaveCreatorObject, x => x.CreatorId, () => CurrentUser.Id.Value);
  101. }
  102. }
  103. protected virtual void SetLastModificationTime(object targetObject)
  104. {
  105. if (targetObject is IHasModificationTime objectWithModificationTime)
  106. {
  107. objectWithModificationTime.LastModificationTime = Clock.Now;
  108. }
  109. }
  110. protected virtual void SetLastModifierId(object targetObject)
  111. {
  112. if (!(targetObject is IModificationAuditedObject modificationAuditedObject))
  113. {
  114. return;
  115. }
  116. if (!CurrentUser.Id.HasValue)
  117. {
  118. //modificationAuditedObject.LastModifierId = null;
  119. //return;
  120. #region 无登录操作
  121. modificationAuditedObject.LastModifierId = Guid.Parse("3a0c4180-107c-0c89-b25b-0bd34666dcec");
  122. return;
  123. #endregion
  124. }
  125. if (modificationAuditedObject is IMultiTenant multiTenantEntity)
  126. {
  127. if (multiTenantEntity.TenantId != CurrentUser.TenantId)
  128. {
  129. modificationAuditedObject.LastModifierId = null;
  130. return;
  131. }
  132. }
  133. /* TODO: The code below is from old ABP, not implemented yet
  134. if (tenantId.HasValue && MultiTenancyHelper.IsHostEntity(entity))
  135. {
  136. //Tenant user modified a host entity
  137. modificationAuditedObject.LastModifierId = null;
  138. return;
  139. }
  140. */
  141. modificationAuditedObject.LastModifierId = CurrentUser.Id;
  142. }
  143. protected virtual void SetDeletionTime(object targetObject)
  144. {
  145. if (targetObject is IHasDeletionTime objectWithDeletionTime)
  146. {
  147. if (objectWithDeletionTime.DeletionTime == null)
  148. {
  149. objectWithDeletionTime.DeletionTime = Clock.Now;
  150. }
  151. }
  152. }
  153. protected virtual void SetDeleterId(object targetObject)
  154. {
  155. if (!(targetObject is IDeletionAuditedObject deletionAuditedObject))
  156. {
  157. return;
  158. }
  159. if (deletionAuditedObject.DeleterId != null)
  160. {
  161. return;
  162. }
  163. if (!CurrentUser.Id.HasValue)
  164. {
  165. deletionAuditedObject.DeleterId = null;
  166. return;
  167. }
  168. if (deletionAuditedObject is IMultiTenant multiTenantEntity)
  169. {
  170. if (multiTenantEntity.TenantId != CurrentUser.TenantId)
  171. {
  172. deletionAuditedObject.DeleterId = null;
  173. return;
  174. }
  175. }
  176. deletionAuditedObject.DeleterId = CurrentUser.Id;
  177. }
  178. }
  179. }