| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- using System;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Threading.Tasks;
- using Abp.Auditing;
- using Abp.Dependency;
- using Abp.Domain.Repositories;
- using Abp.Extensions;
- using Abp.Runtime.Caching;
- using WeApp.Authorization.Users;
- using WeApp.BaseInfo;
- using IwbZero;
- using IwbZero.Auditing;
- using IwbZero.Authorization.Base.SystemInfo;
- using IwbZero.ToolCommon.LogHelpers;
- namespace WeApp.Auditing
- {
- /// <summary>
- /// Implements <see cref="IAuditingStore"/> to save auditing informations to database.
- /// </summary>
- public class IwbAuditingStore : IAuditingStore, ITransientDependency
- {
- private readonly IRepository<SysLog, long> _auditLogRepository;
- private readonly IRepository<User, long> _userRepository;
- private readonly IRepository<SysFunction, int> _funRepository;
- private readonly ICacheManager _cacheManager;
- private SysFunction SysFunction { get; set; }
- private Type Type { get; set; }
- private string MethondNameSuffix { get; set; }
- /// <summary>
- /// Creates a new <see cref="IwbAuditingStore"/>.
- /// </summary>
- public IwbAuditingStore(IRepository<SysLog, long> auditLogRepository, IRepository<User, long> userRepository, IRepository<SysFunction, int> funRepository, ICacheManager cacheManager)
- {
- SysFunction = null;
- Type = null;
- _auditLogRepository = auditLogRepository;
- _userRepository = userRepository;
- _funRepository = funRepository;
- _cacheManager = cacheManager;
- }
- public virtual Task SaveAsync(AuditInfo auditInfo)
- {
- var user = _cacheManager.GetCache(IwbZeroConsts.SystemUserCache).Get(auditInfo.UserId, () =>
- _userRepository.FirstOrDefault(a => a.Id == auditInfo.UserId));
- string userName = user?.UserName ?? "";
- if (auditInfo.MethodName.ToLower() == "login" || auditInfo.MethodName.ToLower().Contains("password"))
- {
- auditInfo.Parameters = "";
- }
- var sysLog = new SysLog().CreateFromAuditInfo(auditInfo, userName);
- SysFunction = null;
- Type = null;
- MethondNameSuffix = "";
- sysLog = GetServiceName(sysLog);
- int logType = auditInfo.ServiceName == sysLog.ServiceName ? 0 : 1;
- if (logType != 0)
- {
- sysLog = GetMethodName(sysLog, auditInfo.ServiceName);
- logType = auditInfo.MethodName == sysLog.MethodName ? 0 : 1;
- }
- sysLog.LogType = logType;
- return _auditLogRepository.InsertAsync(sysLog);
- }
- public SysLog GetServiceName(SysLog log)
- {
- if (!log.ServiceName.IsNullOrEmpty())
- {
- try
- {
- var array = log.ServiceName.Split(".", StringSplitOptions.RemoveEmptyEntries);
- var funNo = array[array.Length - 1]?.Replace("sAppService", "").Replace("AppService", "").Replace("Controller", "");
- log.ServiceNameLang = $"Log_{funNo}";
- log.ServiceName = _cacheManager.GetCache(IwbZeroConsts.AuditLogDescCache).Get(
- log.ServiceName, () => GetServiceName(funNo, log.ServiceName));
- }
- catch (Exception e)
- {
- this.LogError(e);
- }
- }
- return log;
- }
- private string GetServiceName(string funNo, string serviceName)
- {
- if (serviceName.IsNullOrEmpty())
- {
- return serviceName;
- }
- SysFunction = _funRepository.FirstOrDefault(a => a.FunctionNo == funNo);
- return SysFunction == null ? GetServiceNameByAttr(serviceName) : SysFunction.FunctionName;
- }
- private string GetServiceNameByAttr(string serviceName)
- {
- Type = Typen(serviceName);
- if (Type != null)
- {
- var attr = GetSingleAttribute<AuditLogAttribute>(Type);
- if (attr != null)
- {
- serviceName = attr.Name;
- MethondNameSuffix = attr.MethondNameSuffix ?? "";
- }
- }
- return serviceName;
- }
- public SysLog GetMethodName(SysLog log, string serviceName)
- {
- if (!log.MethodName.IsNullOrEmpty())
- {
- try
- {
- var array = log.MethodName.Split(".", StringSplitOptions.RemoveEmptyEntries);
- var funNo = array[array.Length - 1]?.Replace("sAppService", "").Replace("AppService", "").Replace("Controller", "");
- log.MethodNameLang = $"Log_{funNo}";
- log.MethodName = _cacheManager.GetCache(IwbZeroConsts.AuditLogDescCache).Get(
- log.MethodName, () => GetMethodName(funNo, log.MethodName, serviceName));
- }
- catch (Exception e)
- {
- this.LogError(e);
- }
- }
- return log;
- }
- private string GetMethodName(string funNo, string methodName, string serviceName)
- {
- if (methodName.IsNullOrEmpty())
- {
- return methodName;
- }
- if (SysFunction == null)
- {
- return GetMethodNameByAttr(methodName, serviceName);
- }
- var fun = _funRepository.FirstOrDefault(a =>
- a.FunctionNo == funNo && a.ParentNo == SysFunction.FunctionNo);
- return fun == null ? GetMethodNameByAttr(methodName, serviceName) : fun.FunctionName;
- }
- private string GetMethodNameByAttr(string methodName, string serviceName)
- {
- Type = Type ?? Typen(serviceName);
- var member = Type?.GetMember(methodName).FirstOrDefault();
- if (member != null)
- {
- var attr = GetMemberSingleAttribute<AuditLogAttribute>(member);
- if (attr != null)
- {
- methodName = attr.Name;
- }
- else
- {
- switch (methodName.ToLower())
- {
- case "get":
- methodName = "查询";
- break;
- case "getall":
- methodName = "查询";
- break;
- case "create":
- methodName = "创建";
- break;
- case "update":
- methodName = "修改";
- break;
- case "delete":
- methodName = "删除";
- break;
- case "auth":
- methodName = "授权";
- break;
- case "refresh":
- methodName = "刷新";
- break;
- case "moveup":
- methodName = "上移";
- break;
- case "movedown":
- methodName = "下移";
- break;
- }
- if (MethondNameSuffix.IsNullOrEmpty())
- {
- MethondNameSuffix = GetMethondNameSuffixAttr(serviceName);
- }
- return methodName + MethondNameSuffix;
- }
- }
- return methodName;
- }
- private string GetMethondNameSuffixAttr(string serviceName)
- {
- Type = Typen(serviceName);
- var attr = GetSingleAttribute<AuditLogAttribute>(Type);
- if (attr != null)
- {
- return attr.MethondNameSuffix ?? "";
- }
- return "";
- }
- private Type Typen(string typeName)
- {
- string applicationAssemblyName = "";
- try
- {
- applicationAssemblyName = System.Configuration.ConfigurationManager.AppSettings["ApplicationAssemblyName"] ?? "WeApp.Application.dll";
- }
- catch
- {
- //
- }
- applicationAssemblyName = applicationAssemblyName.IsNullOrEmpty()
- ? "WeApp.Application.dll"
- : applicationAssemblyName;
- var path = AppDomain.CurrentDomain.BaseDirectory;
- Assembly assembly = Assembly.LoadFrom(Path.Combine(path, "bin/", applicationAssemblyName));
- var type = TypenFromAssembly(assembly, typeName);
- if (type == null)
- {
- string webAssemblyName = "";
- try
- {
- webAssemblyName = System.Configuration.ConfigurationManager.AppSettings["WebAssemblyName"] ?? "WeApp.Web.Mvc.dll";
- }
- catch
- {
- //
- }
- webAssemblyName = webAssemblyName.IsNullOrEmpty() ? "WeApp.Web.dll" : webAssemblyName;
- assembly = Assembly.LoadFrom(Path.Combine(path, "bin/", webAssemblyName));
- type = TypenFromAssembly(assembly, typeName);
- }
- return type;
- }
- public static TAttribute GetSingleAttribute<TAttribute>(Type type, TAttribute defaultValue = default(TAttribute), bool inherit = true)
- where TAttribute : Attribute
- {
- return type.GetCustomAttributes(inherit).OfType<TAttribute>().FirstOrDefault()
- ?? type.ReflectedType?.GetTypeInfo().GetCustomAttributes(inherit).OfType<TAttribute>().FirstOrDefault()
- ?? defaultValue;
- }
- public static TAttribute GetMemberSingleAttribute<TAttribute>(MemberInfo memberInfo, TAttribute defaultValue = default(TAttribute), bool inherit = true)
- where TAttribute : Attribute
- {
- //Get attribute on the member
- if (memberInfo.IsDefined(typeof(TAttribute), inherit))
- {
- return memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>().First();
- }
- return defaultValue;
- }
- public static Type TypenFromAssembly(Assembly assembly, string typeName)
- {
- Type[] typeArray = assembly.GetTypes();
- foreach (var type in typeArray)
- {
- if ((type.FullName != null && type.FullName.Equals(typeName)) || type.Name.Equals(typeName))
- {
- return type;
- }
- }
- return null;
- }
- }
- }
|