AuditingStore.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using System.Reflection;
  5. using System.Threading.Tasks;
  6. using Abp.Auditing;
  7. using Abp.Dependency;
  8. using Abp.Domain.Repositories;
  9. using Abp.Extensions;
  10. using Abp.Runtime.Caching;
  11. using WeApp.Authorization.Users;
  12. using WeApp.BaseInfo;
  13. using IwbZero;
  14. using IwbZero.Auditing;
  15. using IwbZero.Authorization.Base.SystemInfo;
  16. using IwbZero.ToolCommon.LogHelpers;
  17. namespace WeApp.Auditing
  18. {
  19. /// <summary>
  20. /// Implements <see cref="IAuditingStore"/> to save auditing informations to database.
  21. /// </summary>
  22. public class IwbAuditingStore : IAuditingStore, ITransientDependency
  23. {
  24. private readonly IRepository<SysLog, long> _auditLogRepository;
  25. private readonly IRepository<User, long> _userRepository;
  26. private readonly IRepository<SysFunction, int> _funRepository;
  27. private readonly ICacheManager _cacheManager;
  28. private SysFunction SysFunction { get; set; }
  29. private Type Type { get; set; }
  30. private string MethondNameSuffix { get; set; }
  31. /// <summary>
  32. /// Creates a new <see cref="IwbAuditingStore"/>.
  33. /// </summary>
  34. public IwbAuditingStore(IRepository<SysLog, long> auditLogRepository, IRepository<User, long> userRepository, IRepository<SysFunction, int> funRepository, ICacheManager cacheManager)
  35. {
  36. SysFunction = null;
  37. Type = null;
  38. _auditLogRepository = auditLogRepository;
  39. _userRepository = userRepository;
  40. _funRepository = funRepository;
  41. _cacheManager = cacheManager;
  42. }
  43. public virtual Task SaveAsync(AuditInfo auditInfo)
  44. {
  45. var user = _cacheManager.GetCache(IwbZeroConsts.SystemUserCache).Get(auditInfo.UserId, () =>
  46. _userRepository.FirstOrDefault(a => a.Id == auditInfo.UserId));
  47. string userName = user?.UserName ?? "";
  48. if (auditInfo.MethodName.ToLower() == "login" || auditInfo.MethodName.ToLower().Contains("password"))
  49. {
  50. auditInfo.Parameters = "";
  51. }
  52. var sysLog = new SysLog().CreateFromAuditInfo(auditInfo, userName);
  53. SysFunction = null;
  54. Type = null;
  55. MethondNameSuffix = "";
  56. sysLog = GetServiceName(sysLog);
  57. int logType = auditInfo.ServiceName == sysLog.ServiceName ? 0 : 1;
  58. if (logType != 0)
  59. {
  60. sysLog = GetMethodName(sysLog, auditInfo.ServiceName);
  61. logType = auditInfo.MethodName == sysLog.MethodName ? 0 : 1;
  62. }
  63. sysLog.LogType = logType;
  64. return _auditLogRepository.InsertAsync(sysLog);
  65. }
  66. public SysLog GetServiceName(SysLog log)
  67. {
  68. if (!log.ServiceName.IsNullOrEmpty())
  69. {
  70. try
  71. {
  72. var array = log.ServiceName.Split(".", StringSplitOptions.RemoveEmptyEntries);
  73. var funNo = array[array.Length - 1]?.Replace("sAppService", "").Replace("AppService", "").Replace("Controller", "");
  74. log.ServiceNameLang = $"Log_{funNo}";
  75. log.ServiceName = _cacheManager.GetCache(IwbZeroConsts.AuditLogDescCache).Get(
  76. log.ServiceName, () => GetServiceName(funNo, log.ServiceName));
  77. }
  78. catch (Exception e)
  79. {
  80. this.LogError(e);
  81. }
  82. }
  83. return log;
  84. }
  85. private string GetServiceName(string funNo, string serviceName)
  86. {
  87. if (serviceName.IsNullOrEmpty())
  88. {
  89. return serviceName;
  90. }
  91. SysFunction = _funRepository.FirstOrDefault(a => a.FunctionNo == funNo);
  92. return SysFunction == null ? GetServiceNameByAttr(serviceName) : SysFunction.FunctionName;
  93. }
  94. private string GetServiceNameByAttr(string serviceName)
  95. {
  96. Type = Typen(serviceName);
  97. if (Type != null)
  98. {
  99. var attr = GetSingleAttribute<AuditLogAttribute>(Type);
  100. if (attr != null)
  101. {
  102. serviceName = attr.Name;
  103. MethondNameSuffix = attr.MethondNameSuffix ?? "";
  104. }
  105. }
  106. return serviceName;
  107. }
  108. public SysLog GetMethodName(SysLog log, string serviceName)
  109. {
  110. if (!log.MethodName.IsNullOrEmpty())
  111. {
  112. try
  113. {
  114. var array = log.MethodName.Split(".", StringSplitOptions.RemoveEmptyEntries);
  115. var funNo = array[array.Length - 1]?.Replace("sAppService", "").Replace("AppService", "").Replace("Controller", "");
  116. log.MethodNameLang = $"Log_{funNo}";
  117. log.MethodName = _cacheManager.GetCache(IwbZeroConsts.AuditLogDescCache).Get(
  118. log.MethodName, () => GetMethodName(funNo, log.MethodName, serviceName));
  119. }
  120. catch (Exception e)
  121. {
  122. this.LogError(e);
  123. }
  124. }
  125. return log;
  126. }
  127. private string GetMethodName(string funNo, string methodName, string serviceName)
  128. {
  129. if (methodName.IsNullOrEmpty())
  130. {
  131. return methodName;
  132. }
  133. if (SysFunction == null)
  134. {
  135. return GetMethodNameByAttr(methodName, serviceName);
  136. }
  137. var fun = _funRepository.FirstOrDefault(a =>
  138. a.FunctionNo == funNo && a.ParentNo == SysFunction.FunctionNo);
  139. return fun == null ? GetMethodNameByAttr(methodName, serviceName) : fun.FunctionName;
  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 = GetMemberSingleAttribute<AuditLogAttribute>(member);
  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 = GetSingleAttribute<AuditLogAttribute>(Type);
  197. if (attr != null)
  198. {
  199. return attr.MethondNameSuffix ?? "";
  200. }
  201. return "";
  202. }
  203. private Type Typen(string typeName)
  204. {
  205. string applicationAssemblyName = "";
  206. try
  207. {
  208. applicationAssemblyName = System.Configuration.ConfigurationManager.AppSettings["ApplicationAssemblyName"] ?? "WeApp.Application.dll";
  209. }
  210. catch
  211. {
  212. //
  213. }
  214. applicationAssemblyName = applicationAssemblyName.IsNullOrEmpty()
  215. ? "WeApp.Application.dll"
  216. : applicationAssemblyName;
  217. var path = AppDomain.CurrentDomain.BaseDirectory;
  218. Assembly assembly = Assembly.LoadFrom(Path.Combine(path, "bin/", applicationAssemblyName));
  219. var type = TypenFromAssembly(assembly, typeName);
  220. if (type == null)
  221. {
  222. string webAssemblyName = "";
  223. try
  224. {
  225. webAssemblyName = System.Configuration.ConfigurationManager.AppSettings["WebAssemblyName"] ?? "WeApp.Web.Mvc.dll";
  226. }
  227. catch
  228. {
  229. //
  230. }
  231. webAssemblyName = webAssemblyName.IsNullOrEmpty() ? "WeApp.Web.dll" : webAssemblyName;
  232. assembly = Assembly.LoadFrom(Path.Combine(path, "bin/", webAssemblyName));
  233. type = TypenFromAssembly(assembly, typeName);
  234. }
  235. return type;
  236. }
  237. public static TAttribute GetSingleAttribute<TAttribute>(Type type, TAttribute defaultValue = default(TAttribute), bool inherit = true)
  238. where TAttribute : Attribute
  239. {
  240. return type.GetCustomAttributes(inherit).OfType<TAttribute>().FirstOrDefault()
  241. ?? type.ReflectedType?.GetTypeInfo().GetCustomAttributes(inherit).OfType<TAttribute>().FirstOrDefault()
  242. ?? defaultValue;
  243. }
  244. public static TAttribute GetMemberSingleAttribute<TAttribute>(MemberInfo memberInfo, TAttribute defaultValue = default(TAttribute), bool inherit = true)
  245. where TAttribute : Attribute
  246. {
  247. //Get attribute on the member
  248. if (memberInfo.IsDefined(typeof(TAttribute), inherit))
  249. {
  250. return memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>().First();
  251. }
  252. return defaultValue;
  253. }
  254. public static Type TypenFromAssembly(Assembly assembly, string typeName)
  255. {
  256. Type[] typeArray = assembly.GetTypes();
  257. foreach (var type in typeArray)
  258. {
  259. if ((type.FullName != null && type.FullName.Equals(typeName)) || type.Name.Equals(typeName))
  260. {
  261. return type;
  262. }
  263. }
  264. return null;
  265. }
  266. }
  267. }