AuditingStore 复制.cs 8.9 KB

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