EntityAuditingHelper.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using Abp.Timing;
  2. using System;
  3. using Abp.Configuration.Startup;
  4. using Abp.MultiTenancy;
  5. using Abp.Extensions;
  6. namespace Abp.Domain.Entities.Auditing
  7. {
  8. public static class EntityAuditingHelper
  9. {
  10. public static void SetCreationAuditProperties(
  11. IMultiTenancyConfig multiTenancyConfig,
  12. object entityAsObj,
  13. int? tenantId,
  14. long? userId)
  15. {
  16. var entityWithCreationTime = entityAsObj as IHasCreationTime;
  17. if (entityWithCreationTime == null)
  18. {
  19. //Object does not implement IHasCreationTime
  20. return;
  21. }
  22. if (entityWithCreationTime.CreationTime == default(DateTime))
  23. {
  24. entityWithCreationTime.CreationTime = Clock.Now;
  25. }
  26. if (!(entityAsObj is ICreationAudited))
  27. {
  28. //Object does not implement ICreationAudited
  29. return;
  30. }
  31. if (!userId.HasValue)
  32. {
  33. //Unknown user
  34. return;
  35. }
  36. var entity = entityAsObj as ICreationAudited;
  37. if (entity.CreatorUserId != null)
  38. {
  39. //CreatorUserId is already set
  40. return;
  41. }
  42. if (multiTenancyConfig?.IsEnabled == true)
  43. {
  44. if (MultiTenancyHelper.IsMultiTenantEntity(entity) &&
  45. !MultiTenancyHelper.IsTenantEntity(entity, tenantId))
  46. {
  47. //A tenant entitiy is created by host or a different tenant
  48. return;
  49. }
  50. if (tenantId.HasValue && MultiTenancyHelper.IsHostEntity(entity))
  51. {
  52. //Tenant user created a host entity
  53. return;
  54. }
  55. }
  56. //Finally, set CreatorUserId!
  57. entity.CreatorUserId = userId;
  58. }
  59. public static void SetModificationAuditProperties(
  60. IMultiTenancyConfig multiTenancyConfig,
  61. object entityAsObj,
  62. int? tenantId,
  63. long? userId)
  64. {
  65. if (entityAsObj is IHasModificationTime)
  66. {
  67. entityAsObj.As<IHasModificationTime>().LastModificationTime = Clock.Now;
  68. }
  69. if (!(entityAsObj is IModificationAudited))
  70. {
  71. //Entity does not implement IModificationAudited
  72. return;
  73. }
  74. var entity = entityAsObj.As<IModificationAudited>();
  75. if (userId == null)
  76. {
  77. //Unknown user
  78. entity.LastModifierUserId = null;
  79. return;
  80. }
  81. if (multiTenancyConfig?.IsEnabled == true)
  82. {
  83. if (MultiTenancyHelper.IsMultiTenantEntity(entity) &&
  84. !MultiTenancyHelper.IsTenantEntity(entity, tenantId))
  85. {
  86. //A tenant entitiy is modified by host or a different tenant
  87. entity.LastModifierUserId = null;
  88. return;
  89. }
  90. if (tenantId.HasValue && MultiTenancyHelper.IsHostEntity(entity))
  91. {
  92. //Tenant user modified a host entity
  93. entity.LastModifierUserId = null;
  94. return;
  95. }
  96. }
  97. //Finally, set LastModifierUserId!
  98. entity.LastModifierUserId = userId;
  99. }
  100. }
  101. }