| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- using System.Reflection;
- using Abp.Auditing;
- using Abp.Dependency;
- using Abp.Domain.Repositories;
- using Abp.Runtime.Caching;
- using Castle.Core.Logging;
- using VberZero.BaseSystem;
- using VberZero.Configuration;
- using VberZero.Tools.StringModel;
- using ILogger = Castle.Core.Logging.ILogger;
- namespace VberZero.Auditing;
- /// <summary>
- /// 实现 <see cref="IAuditingStore"/> 将审计信息保存到数据库。
- /// </summary>
- public class VzAuditingStore : IAuditingStore, ITransientDependency
- {
- private readonly IRepository<AuditLog, long> _auditLogRepository;
- private readonly IRepository<SysFunction, int?> _funRepository;
- private readonly ICacheManager _cacheManager;
- private readonly IConfigurationRoot _appConfiguration;
- public ILogger Logger { get; set; }
- private SysFunction SysFunction { get; set; }
- private Type Type { get; set; }
- private string MethodNameSuffix { get; set; }
- public VzAuditingStore(IRepository<AuditLog, long> auditLogRepository, IRepository<SysFunction, int?> funRepository, ICacheManager cacheManager, IWebHostEnvironment env)
- {
- _auditLogRepository = auditLogRepository;
- _funRepository = funRepository;
- _cacheManager = cacheManager;
- _appConfiguration = env.GetAppConfiguration();
- Logger = NullLogger.Instance;
- }
- public virtual Task SaveAsync(AuditInfo auditInfo)
- {
- return _auditLogRepository.InsertAsync(CreateFromAuditInfo(auditInfo));
- }
- public virtual void Save(AuditInfo auditInfo)
- {
- _auditLogRepository.Insert(CreateFromAuditInfo(auditInfo));
- }
- protected virtual AuditLog CreateFromAuditInfo(AuditInfo auditInfo)
- {
- var log = AuditLog.CreateFromAuditInfo(auditInfo);
- SysFunction = null;
- Type = null;
- MethodNameSuffix = "";
- log = ConvertServiceName(log);
- int logType = auditInfo.ServiceName == log.ServiceName ? 0 : 1;
- if (logType != 0)
- {
- log = GetMethodName(log, auditInfo.ServiceName);
- logType = auditInfo.MethodName == log.MethodName ? 0 : 1;
- }
- log.LogType = logType;
- return log;
- }
- public AuditLog ConvertServiceName(AuditLog log)
- {
- if (log.ServiceName.NotEmpty())
- {
- try
- {
- var sName = log.ServiceName.Substring(log.ServiceName.LastIndexOf(".", StringComparison.Ordinal) + 1).Replace("sAppService", "").Replace("AppService", "").Replace("Controller", "");
- log.ServiceNameLang = $"Log_{sName}";
- log.ServiceName = _cacheManager.GetCache<string, string>(VzConsts.CacheAuditLogDesc).Get(log.ServiceName, () => GetServiceName(sName, log.ServiceName));
- }
- catch (Exception e)
- {
- Logger.Error(e.Message, e);
- }
- }
- return log;
- }
- private string GetServiceName(string funNo, string serviceName)
- {
- if (serviceName.Empty())
- {
- return serviceName;
- }
- SysFunction = _funRepository.FirstOrDefault(a => a.Name == funNo);
- return SysFunction == null ? GetServiceNameByAttr(serviceName) : SysFunction.DisplayName;
- }
- private string GetServiceNameByAttr(string serviceName)
- {
- Type = GetType(serviceName);
- if (Type != null)
- {
- var attr = GetSingleAttribute<AuditLogAttribute>(Type);
- if (attr != null)
- {
- serviceName = attr.Name;
- MethodNameSuffix = attr.MethodNameSuffix ?? "";
- }
- }
- return serviceName;
- }
- public AuditLog GetMethodName(AuditLog log, string serviceName)
- {
- if (log.MethodName.NotEmpty())
- {
- try
- {
- log.MethodNameLang = $"Log_{log.MethodName}";
- log.MethodName = _cacheManager.GetCache<string, string>(VzConsts.CacheAuditLogDesc).Get(
- serviceName + "_" + log.MethodName, () => GetMethodName(log.MethodName, log.MethodName, serviceName));
- }
- catch (Exception e)
- {
- Logger.Error(e.Message, e);
- }
- }
- return log;
- }
- private string GetMethodName(string funNo, string methodName, string serviceName)
- {
- if (methodName.Empty())
- {
- return methodName;
- }
- if (SysFunction == null)
- {
- return GetMethodNameByAttr(methodName, serviceName);
- }
- var fun = _funRepository.FirstOrDefault(a =>
- a.Name == funNo && a.ParentNo == SysFunction.Id);
- return fun == null ? GetMethodNameByAttr(methodName, serviceName) : fun.DisplayName;
- }
- private string GetMethodNameByAttr(string methodName, string serviceName)
- {
- Type ??= GetType(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 (MethodNameSuffix.Empty())
- {
- MethodNameSuffix = GetMethodNameSuffixAttr(serviceName);
- }
- return methodName + MethodNameSuffix;
- }
- }
- return methodName;
- }
- private string GetMethodNameSuffixAttr(string serviceName)
- {
- Type = GetType(serviceName);
- var attr = GetSingleAttribute<AuditLogAttribute>(Type);
- if (attr != null)
- {
- return attr.MethodNameSuffix ?? "";
- }
- return "";
- }
- private Type GetType(string typeName)
- {
- string appAssemblyName = _appConfiguration["Auditing:AppAssembly"];
- var path = AppDomain.CurrentDomain.BaseDirectory;
- Assembly assembly = Assembly.LoadFrom(Path.Combine(path, appAssemblyName));
- var type = GetTypeFromAssembly(assembly, typeName);
- if (type == null)
- {
- string webAssemblyName = _appConfiguration["Auditing:WebAssembly"];
- assembly = Assembly.LoadFrom(Path.Combine(path, webAssemblyName));
- type = GetTypeFromAssembly(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
- {
- if (memberInfo.IsDefined(typeof(TAttribute), inherit))
- {
- return memberInfo.GetCustomAttributes(typeof(TAttribute), inherit).Cast<TAttribute>().First();
- }
- return defaultValue;
- }
- public static Type GetTypeFromAssembly(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;
- }
- }
|