You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
214 lines
9.8 KiB
214 lines
9.8 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Claims;
|
|
using Yitter.IdGenerator;
|
|
using Znyc.Cloudcar.Admin.Commons.Cache;
|
|
using Znyc.Cloudcar.Admin.Commons.Helpers;
|
|
using Znyc.Cloudcar.Admin.Commons.Json;
|
|
using Znyc.Cloudcar.Admin.Commons.Log;
|
|
using Znyc.Cloudcar.Admin.Commons.Services;
|
|
using Znyc.Cloudcar.Admin.Security.Dtos;
|
|
using Znyc.Cloudcar.Admin.Security.Entitys;
|
|
using Znyc.Cloudcar.Admin.Security.IRepositories;
|
|
using Znyc.Cloudcar.Admin.Security.IServices;
|
|
|
|
namespace Znyc.Cloudcar.Admin.Security.Services
|
|
{
|
|
/// <summary>
|
|
/// </summary>
|
|
public class ExceptionsLogsService : BaseService<ExceptionsLogsEntity, ExceptionsLogsOutputDto, long>,
|
|
IExceptionsLogsService
|
|
{
|
|
private readonly IExceptionsLogsRepository _iLogRepository;
|
|
private readonly IUserRepository _iuserRepository;
|
|
|
|
/// <summary>
|
|
/// </summary>
|
|
/// <param name="repository"></param>
|
|
/// <param name="userRepository"></param>
|
|
public ExceptionsLogsService(IExceptionsLogsRepository repository, IUserRepository userRepository) :
|
|
base(repository)
|
|
{
|
|
_iLogRepository = repository;
|
|
_iuserRepository = userRepository;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据条件查询数据库,并返回对象集合(用于分页数据显示)
|
|
/// </summary>
|
|
/// <param name="search">查询的条件</param>
|
|
/// <returns>指定对象的集合</returns>
|
|
//public async Task<PageResult<ExceptionsLogsOutputDto>> FindWithPagerSearchAsync(SearchExceptionsLogsModel search)
|
|
//{
|
|
// bool order = search.Order == "asc" ? false : true;
|
|
// string where = GetDataPrivilege(false);
|
|
// if (!string.IsNullOrEmpty(search.CreatedTime1))
|
|
// {
|
|
// @where += " and CreatedTime >='" + search.CreatedTime1.ToDateTime() + "'";
|
|
// }
|
|
|
|
// if (!string.IsNullOrEmpty(search.CreatedTime2))
|
|
// {
|
|
// @where += " and CreatedTime <='" + search.CreatedTime2.ToDateTime() + "'";
|
|
// }
|
|
|
|
// if (!string.IsNullOrEmpty(search.Filter.IPAddress))
|
|
// {
|
|
// @where += string.Format(" and IPAddress = '{0}'", search.Filter.IPAddress);
|
|
// };
|
|
// if (!string.IsNullOrEmpty(search.Filter.Account))
|
|
// {
|
|
// @where += string.Format(" and Account = '{0}'", search.Filter.Account);
|
|
// };
|
|
// PagerInfo pagerInfo = new PagerInfo
|
|
// {
|
|
// CurrenetPageIndex = search.CurrenetPageIndex,
|
|
// PageSize = search.PageSize
|
|
// };
|
|
|
|
// //Expression<Func<Log, bool>> filter = log => true;
|
|
// //if (!string.IsNullOrEmpty(search.Keywords))
|
|
// //{
|
|
// // filter = filter.And(log => log.Account.StartsWith(search.Keywords) || log.ModuleName.StartsWith(search.Keywords) || log.IPAddress.StartsWith(search.Keywords)
|
|
// // || log.IPAddressName.StartsWith(search.Keywords) || log.Description.StartsWith(search.Keywords));
|
|
// //}
|
|
// //if (!string.IsNullOrEmpty(search.EnCode))
|
|
// //{
|
|
// // filter = filter.And(log=>search.EnCode.Contains(log.Type));
|
|
// //}
|
|
// List<ExceptionsLogsEntity> list = await _iLogRepository.FindWithPagerAsync(where, pagerInfo, search.Sort, order);
|
|
// PageResult<ExceptionsLogsOutputDto> pageResult = new PageResult<ExceptionsLogsOutputDto>
|
|
// {
|
|
// CurrentPage = pagerInfo.CurrenetPageIndex,
|
|
// Items = list.MapTo<ExceptionsLogsOutputDto>(),
|
|
// ItemsPerPage = pagerInfo.PageSize,
|
|
// TotalItems = pagerInfo.RecordCount
|
|
// };
|
|
// return pageResult;
|
|
//}
|
|
|
|
/// <summary>
|
|
/// 根据相关信息,写入用户的操作日志记录
|
|
/// </summary>
|
|
/// <param name="tableName">操作表名称</param>
|
|
/// <param name="operationType">操作类型</param>
|
|
/// <param name="note">操作详细表述</param>
|
|
/// <returns></returns>
|
|
public bool OnOperationLog(string tableName, string operationType, string note)
|
|
{
|
|
try
|
|
{
|
|
//虽然实现了这个事件,但是我们还需要判断该表是否在配置表里面,如果不在,则不记录操作日志。
|
|
//var identities = _httpContextAccessor.HttpContext.User.Identities;
|
|
if (HttpContextHelper.HttpContext == null)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
IEnumerable<ClaimsIdentity> identities = HttpContextHelper.HttpContext.User.Identities;
|
|
ClaimsIdentity claimsIdentity = identities.First();
|
|
List<Claim> claimlist = claimsIdentity.Claims as List<Claim>;
|
|
string userId = claimlist[0].Value;
|
|
CacheHelper cacheHelper = new CacheHelper();
|
|
AdminCurrentUser AdminCurrentUser = new AdminCurrentUser();
|
|
AdminCurrentUser user = cacheHelper.Get("login_user_" + userId).ToJson().ToObject<AdminCurrentUser>();
|
|
if (user != null)
|
|
{
|
|
AdminCurrentUser = user;
|
|
bool insert = operationType == DbLogType.Create.ToString();
|
|
; //&& settingInfo.InsertLog;
|
|
bool update = operationType == DbLogType.Update.ToString(); // && settingInfo.UpdateLog;
|
|
bool delete = operationType == DbLogType.Delete.ToString(); // && settingInfo.DeleteLog;
|
|
bool deletesoft = operationType == DbLogType.DeleteSoft.ToString(); // && settingInfo.DeleteLog;
|
|
bool exception = operationType == DbLogType.Exception.ToString(); // && settingInfo.DeleteLog;
|
|
bool sql = operationType == DbLogType.SQL.ToString(); // && settingInfo.DeleteLog;
|
|
|
|
if (insert || update || delete || deletesoft || exception || sql)
|
|
{
|
|
ExceptionsLogsEntity info = new ExceptionsLogsEntity
|
|
{
|
|
Id = YitIdHelper.NextId()
|
|
};
|
|
//info.ModuleName = tableName;
|
|
//info.Type = operationType;
|
|
//info.Description = note;
|
|
//info.Date = info.CreatedTime = DateTime.Now;
|
|
//info.CreatedUserId = CurrentUser.UserId;
|
|
//info.Account = AdminCurrentUser.Account;
|
|
//info.UserName = AdminCurrentUser.UserName;
|
|
//info.OrganizeId = AdminCurrentUser.OrganizeId;
|
|
//info.IPAddress = AdminCurrentUser.CurrentLoginIP;
|
|
//info.IPAddressName = AdminCurrentUser.IPAddressName;
|
|
//info.Result = true;
|
|
long lg = _iLogRepository.Insert(info);
|
|
if (lg > 0)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log4NetHelper.Error("", ex);
|
|
return false;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 根据相关信息,写入用户的操作日志记录
|
|
/// 主要用于写操作模块日志
|
|
/// </summary>
|
|
/// <param name="module">操作模块名称</param>
|
|
/// <param name="operationType">操作类型</param>
|
|
/// <param name="note">操作详细表述</param>
|
|
/// <param name="AdminCurrentUser">操作用户</param>
|
|
/// <returns></returns>
|
|
public bool OnOperationLog(string module, string operationType, string note, AdminCurrentUser AdminCurrentUser)
|
|
{
|
|
//虽然实现了这个事件,但是我们还需要判断该表是否在配置表里面,如果不在,则不记录操作日志。
|
|
//OperationLogSettingInfo settingInfo = BLLFactory<OperationLogSetting>.Instance.FindByTableName(tableName, trans);
|
|
|
|
if (AdminCurrentUser != null)
|
|
{
|
|
bool login = operationType == DbLogType.Login.ToString();
|
|
bool visit = operationType == DbLogType.Visit.ToString();
|
|
bool exit = operationType == DbLogType.Exit.ToString();
|
|
bool other = operationType == DbLogType.Other.ToString();
|
|
bool insert = operationType == DbLogType.Create.ToString();
|
|
bool update = operationType == DbLogType.Update.ToString();
|
|
bool delete = operationType == DbLogType.Delete.ToString();
|
|
bool deletesoft = operationType == DbLogType.DeleteSoft.ToString();
|
|
bool exception = operationType == DbLogType.Exception.ToString();
|
|
if (login || visit || exit || other || insert || update || delete || deletesoft || exception)
|
|
{
|
|
ExceptionsLogsEntity info = new ExceptionsLogsEntity
|
|
{
|
|
Id = YitIdHelper.NextId()
|
|
};
|
|
//info.ModuleName = module;
|
|
//info.Type = operationType;
|
|
//info.Description = note;
|
|
//info.Date = info.CreatedTime = DateTime.Now;
|
|
//info.CreatedUserId = CurrentUser.UserId;
|
|
//info.Account = AdminCurrentUser.Account;
|
|
//info.UserName = AdminCurrentUser.UserName;
|
|
//info.OrganizeId = AdminCurrentUser.OrganizeId;
|
|
//info.IPAddress = AdminCurrentUser.CurrentLoginIP;
|
|
//info.IPAddressName = IpAddressUtil.GetCityByIp(AdminCurrentUser.CurrentLoginIP);
|
|
//info.Result = true;
|
|
long lg = _iLogRepository.Insert(info);
|
|
if (lg > 0)
|
|
{
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
}
|