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 WeOnlineApp.Authorization.Users; using WeOnlineApp.BaseInfo; using IwbZero; using IwbZero.Auditing; using IwbZero.Authorization.Base.SystemInfo; using IwbZero.ToolCommon.LogHelpers; namespace WeOnlineApp.Auditing { /// /// Implements to save auditing informations to database. /// public class IwbAuditingStore : IAuditingStore, ITransientDependency { private readonly IRepository _auditLogRepository; private readonly IRepository _userRepository; private readonly IRepository _funRepository; private readonly ICacheManager _cacheManager; private SysFunction SysFunction { get; set; } private Type Type { get; set; } private string MethondNameSuffix { get; set; } /// /// Creates a new . /// public IwbAuditingStore(IRepository auditLogRepository, IRepository userRepository, IRepository 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(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(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(Type); if (attr != null) { return attr.MethondNameSuffix ?? ""; } return ""; } private Type Typen(string typeName) { string applicationAssemblyName = ""; try { applicationAssemblyName = System.Configuration.ConfigurationManager.AppSettings["ApplicationAssemblyName"] ?? "WeOnlineApp.Application.dll"; } catch { // } applicationAssemblyName = applicationAssemblyName.IsNullOrEmpty() ? "WeOnlineApp.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"] ?? "WeOnlineApp.Web.Mvc.dll"; } catch { // } webAssemblyName = webAssemblyName.IsNullOrEmpty() ? "WeOnlineApp.Web.dll" : webAssemblyName; assembly = Assembly.LoadFrom(Path.Combine(path, "bin/", webAssemblyName)); type = TypenFromAssembly(assembly, typeName); } return type; } public static TAttribute GetSingleAttribute(Type type, TAttribute defaultValue = default(TAttribute), bool inherit = true) where TAttribute : Attribute { return type.GetCustomAttributes(inherit).OfType().FirstOrDefault() ?? type.ReflectedType?.GetTypeInfo().GetCustomAttributes(inherit).OfType().FirstOrDefault() ?? defaultValue; } public static TAttribute GetMemberSingleAttribute(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().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; } } }