AuditingStore.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. using System;
  2. using System.Linq;
  3. using System.Reflection;
  4. using System.Threading.Tasks;
  5. using Abp.Auditing;
  6. using Abp.Dependency;
  7. using Abp.Domain.Repositories;
  8. using Abp.Extensions;
  9. using Abp.Runtime.Caching;
  10. using ShwasherSys.Authorization.Users;
  11. using ShwasherSys.BaseSysInfo;
  12. using IwbZero;
  13. using IwbZero.Auditing;
  14. namespace ShwasherSys.Auditing
  15. {
  16. /// <summary>
  17. /// Implements <see cref="IAuditingStore"/> to save auditing informations to database.
  18. /// </summary>
  19. public class ShwasherAuditingStore : IAuditingStore, ITransientDependency
  20. {
  21. private readonly IRepository<SysLog, long> _auditLogRepository;
  22. private readonly IRepository<SysUser, long> _userRepository;
  23. private readonly IRepository<SysFunction, int> _funRepository;
  24. private readonly ICacheManager _cacheManager;
  25. private SysFunction SysFunction { get; set; }
  26. private Type Type { get; set; }
  27. private string MethondNameSuffix { get; set; }
  28. /// <summary>
  29. /// Creates a new <see cref="IwbAuditingStore"/>.
  30. /// </summary>
  31. public ShwasherAuditingStore(IRepository<SysLog, long> auditLogRepository, IRepository<SysUser, long> userRepository, IRepository<SysFunction, int> funRepository, ICacheManager cacheManager)
  32. {
  33. SysFunction = null;
  34. Type = null;
  35. _auditLogRepository = auditLogRepository;
  36. _userRepository = userRepository;
  37. _funRepository = funRepository;
  38. _cacheManager = cacheManager;
  39. }
  40. public virtual Task SaveAsync(AuditInfo auditInfo)
  41. {
  42. var user = _cacheManager.GetCache(IwbZeroConsts.SystemUserCache).Get(auditInfo.UserId, () =>
  43. _userRepository.FirstOrDefault(a => a.Id == auditInfo.UserId));
  44. string userName = user?.UserName ?? "";
  45. if (auditInfo.MethodName.ToLower() == "login" || auditInfo.MethodName.ToLower().Contains("password"))
  46. {
  47. auditInfo.Parameters = "";
  48. }
  49. SysFunction = null;
  50. Type = null;
  51. MethondNameSuffix = "";
  52. var methodName = auditInfo.MethodName;
  53. var serviceName = _cacheManager.GetCache(IwbZeroConsts.AuditLogDescCache).Get(
  54. auditInfo.ServiceName, () => GetServiceName(auditInfo.ServiceName));
  55. if (string.IsNullOrEmpty(serviceName))
  56. {
  57. serviceName = GetServiceName(auditInfo.ServiceName);
  58. if (!string.IsNullOrEmpty(serviceName))
  59. _cacheManager.GetCache(IwbZeroConsts.AuditLogDescCache).Set(auditInfo.ServiceName, serviceName);
  60. }
  61. int logType = auditInfo.ServiceName == serviceName ? 0 : 1;
  62. if (logType != 0)
  63. {
  64. methodName = _cacheManager.GetCache(IwbZeroConsts.AuditLogDescCache).Get(
  65. auditInfo.ServiceName + "." + auditInfo.MethodName,
  66. () => GetMethodName(auditInfo.MethodName, auditInfo.ServiceName));
  67. if (string.IsNullOrEmpty(methodName))
  68. {
  69. methodName = GetMethodName(auditInfo.MethodName, auditInfo.ServiceName);
  70. if (!string.IsNullOrEmpty(methodName))
  71. _cacheManager.GetCache(IwbZeroConsts.AuditLogDescCache).Set(auditInfo.ServiceName + "." + auditInfo.MethodName, methodName);
  72. }
  73. logType = auditInfo.MethodName == methodName ? 0 : 1;
  74. }
  75. auditInfo.ServiceName = serviceName;
  76. auditInfo.MethodName = methodName;
  77. return _auditLogRepository.InsertAsync(SysLog.CreateFromAuditInfo(auditInfo, userName, logType));
  78. }
  79. private string GetServiceName(string serviceName)
  80. {
  81. if (string.IsNullOrEmpty(serviceName))
  82. {
  83. return null;
  84. }
  85. try
  86. {
  87. var funNoArray = serviceName.Split(".", StringSplitOptions.RemoveEmptyEntries);
  88. var funNo = funNoArray[funNoArray.Length - 1]?.Replace("sAppService", "").Replace("AppService", "").Replace("Controller", "");
  89. SysFunction = _funRepository.FirstOrDefault(a => a.FunctionNo == funNo);
  90. if (SysFunction == null)
  91. {
  92. return GetServiceNameByAttr(serviceName);
  93. }
  94. return SysFunction.FunctionName;
  95. }
  96. catch (Exception e)
  97. {
  98. this.LogError(e);
  99. return null;
  100. }
  101. }
  102. private string GetServiceNameByAttr(string serviceName)
  103. {
  104. Type = Typen(serviceName);
  105. var attr = Type.GetSingleAttribute<AuditLogAttribute>();
  106. if (attr != null)
  107. {
  108. serviceName = attr.Name;
  109. MethondNameSuffix = attr.MethondNameSuffix ?? "";
  110. }
  111. return serviceName;
  112. }
  113. private string GetMethodName(string methodName, string serviceName)
  114. {
  115. if (methodName.IsNullOrEmpty())
  116. {
  117. return null;
  118. }
  119. try
  120. {
  121. if (SysFunction == null)
  122. {
  123. return GetMethodNameByAttr(methodName, serviceName);
  124. }
  125. var funNoArray = methodName.Split(".", StringSplitOptions.RemoveEmptyEntries);
  126. var funNo = funNoArray[funNoArray.Length - 1]?.Replace("AppService", "AppService").Replace("sAppService", "");
  127. var fun = _funRepository.FirstOrDefault(a =>
  128. a.FunctionNo == funNo && a.ParentNo == SysFunction.FunctionNo);
  129. if (fun == null)
  130. {
  131. return GetMethodNameByAttr(methodName, serviceName);
  132. }
  133. return fun.FunctionName;
  134. }
  135. catch (Exception e)
  136. {
  137. typeof(SysLog).LogError(e);
  138. return methodName;
  139. }
  140. }
  141. private string GetMethodNameByAttr(string methodName, string serviceName)
  142. {
  143. Type = Type ?? Typen(serviceName);
  144. var member = Type?.GetMember(methodName).FirstOrDefault();
  145. if (member != null)
  146. {
  147. var attr = member.GetMemberSingleAttribute<AuditLogAttribute>();
  148. if (attr != null)
  149. {
  150. methodName = attr.Name;
  151. }
  152. else
  153. {
  154. switch (methodName.ToLower())
  155. {
  156. case "get":
  157. methodName = "查询";
  158. break;
  159. case "getall":
  160. methodName = "查询";
  161. break;
  162. case "create":
  163. methodName = "创建";
  164. break;
  165. case "update":
  166. methodName = "修改";
  167. break;
  168. case "delete":
  169. methodName = "删除";
  170. break;
  171. case "auth":
  172. methodName = "授权";
  173. break;
  174. case "refresh":
  175. methodName = "刷新";
  176. break;
  177. case "moveup":
  178. methodName = "上移";
  179. break;
  180. case "movedown":
  181. methodName = "下移";
  182. break;
  183. }
  184. if (MethondNameSuffix.IsNullOrEmpty())
  185. {
  186. MethondNameSuffix = GetMethondNameSuffixAttr(serviceName);
  187. }
  188. return methodName + MethondNameSuffix;
  189. }
  190. }
  191. return methodName;
  192. }
  193. private string GetMethondNameSuffixAttr(string serviceName)
  194. {
  195. Type = Typen(serviceName);
  196. var attr = Type.GetSingleAttribute<AuditLogAttribute>();
  197. if (attr != null)
  198. {
  199. return attr.MethondNameSuffix ?? "";
  200. }
  201. return "";
  202. }
  203. private Type Typen(string typeName)
  204. {
  205. var applicationAssemblyName = System.Configuration.ConfigurationManager.AppSettings["ApplicationAssemblyName"];
  206. var path = AppDomain.CurrentDomain.BaseDirectory + "bin/";
  207. Assembly assembly = Assembly.LoadFrom(path + applicationAssemblyName);
  208. var type = assembly.TypenFromAssembly(typeName);
  209. if (type == null)
  210. {
  211. var webAssemblyName = System.Configuration.ConfigurationManager.AppSettings["WebAssemblyName"];
  212. assembly = Assembly.LoadFrom(path + webAssemblyName);
  213. type = assembly.TypenFromAssembly(typeName);
  214. }
  215. return type;
  216. }
  217. }
  218. }