feat:添加登录事件
This commit is contained in:
@@ -16,60 +16,21 @@ namespace Yi.Furion.Application.Rbac.Event
|
|||||||
{
|
{
|
||||||
_loginLogRepository = loginLogRepository;
|
_loginLogRepository = loginLogRepository;
|
||||||
}
|
}
|
||||||
//[EventSubscribe(nameof(LoginEventSource))]
|
[EventSubscribe(nameof(LoginEventSource))]
|
||||||
public Task HandlerAsync(EventHandlerExecutingContext context)
|
public Task HandlerAsync(EventHandlerExecutingContext context)
|
||||||
{
|
{
|
||||||
var eventData = (LoginEventArgs)context.Source.Payload;
|
var eventData = (LoginEventArgs)context.Source.Payload;
|
||||||
var loginLogEntity = GetLoginLogInfo(eventData.httpContext);
|
var loginLogEntity = eventData.LoginLogEntity;
|
||||||
loginLogEntity.Id = SnowflakeHelper.NextId;
|
loginLogEntity.Id = SnowflakeHelper.NextId;
|
||||||
loginLogEntity.LogMsg = eventData.UserName + "登录系统";
|
loginLogEntity.LogMsg = eventData.UserName + "登录系统";
|
||||||
loginLogEntity.LoginUser = eventData.UserName;
|
loginLogEntity.LoginUser = eventData.UserName;
|
||||||
loginLogEntity.LoginIp = eventData.httpContext.GetClientIp();
|
|
||||||
|
|
||||||
_loginLogRepository.InsertAsync(loginLogEntity);
|
_loginLogRepository.InsertAsync(loginLogEntity);
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 获取客户端信息
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="context"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private static ClientInfo GetClientInfo(HttpContext context)
|
|
||||||
{
|
|
||||||
var str = context.GetUserAgent();
|
|
||||||
var uaParser = Parser.GetDefault();
|
|
||||||
ClientInfo c = uaParser.Parse(str);
|
|
||||||
return c;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// 记录用户登陆信息
|
|
||||||
/// </summary>
|
|
||||||
/// <param name="context"></param>
|
|
||||||
/// <returns></returns>
|
|
||||||
private static LoginLogEntity GetLoginLogInfo(HttpContext context)
|
|
||||||
{
|
|
||||||
var ipAddr = context.GetClientIp();
|
|
||||||
IpInfo location;
|
|
||||||
if (ipAddr == "127.0.0.1")
|
|
||||||
{
|
|
||||||
location = new IpInfo() { Province = "本地", City = "本机" };
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
location = IpTool.Search(ipAddr);
|
|
||||||
}
|
|
||||||
ClientInfo clientInfo = GetClientInfo(context);
|
|
||||||
LoginLogEntity entity = new()
|
|
||||||
{
|
|
||||||
Browser = clientInfo.Device.Family,
|
|
||||||
Os = clientInfo.OS.ToString(),
|
|
||||||
LoginIp = ipAddr,
|
|
||||||
LoginLocation = location.Province + "-" + location.City
|
|
||||||
};
|
|
||||||
|
|
||||||
return entity;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,8 +1,12 @@
|
|||||||
using System.Text.RegularExpressions;
|
using System.Text.RegularExpressions;
|
||||||
using Furion.EventBus;
|
using Furion.EventBus;
|
||||||
|
using IPTools.Core;
|
||||||
|
using Microsoft.AspNetCore.Http;
|
||||||
using Microsoft.Extensions.Caching.Memory;
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using Microsoft.Extensions.Options;
|
using Microsoft.Extensions.Options;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
|
using UAParser;
|
||||||
|
using Yi.Framework.Infrastructure.AspNetCore;
|
||||||
using Yi.Framework.Infrastructure.CurrentUsers;
|
using Yi.Framework.Infrastructure.CurrentUsers;
|
||||||
using Yi.Framework.Infrastructure.Ddd.Repositories;
|
using Yi.Framework.Infrastructure.Ddd.Repositories;
|
||||||
using Yi.Framework.Infrastructure.Ddd.Services;
|
using Yi.Framework.Infrastructure.Ddd.Services;
|
||||||
@@ -22,7 +26,7 @@ namespace Yi.Furion.Application.Rbac.Services.Impl
|
|||||||
public class AccountService : ApplicationService, ITransient, IDynamicApiController
|
public class AccountService : ApplicationService, ITransient, IDynamicApiController
|
||||||
{
|
{
|
||||||
|
|
||||||
public AccountService(IUserRepository userRepository, ICurrentUser currentUser, AccountManager accountManager, IRepository<MenuEntity> menuRepository, SmsAliyunManager smsAliyunManager, IOptions<SmsAliyunOptions> smsAliyunManagerOptions, SecurityCodeHelper securityCode, IMemoryCache memoryCache, IEventPublisher eventPublisher,IHttpContextAccessor httpContextAccessor) =>
|
public AccountService(IUserRepository userRepository, ICurrentUser currentUser, AccountManager accountManager, IRepository<MenuEntity> menuRepository, SmsAliyunManager smsAliyunManager, IOptions<SmsAliyunOptions> smsAliyunManagerOptions, SecurityCodeHelper securityCode, IMemoryCache memoryCache, IEventPublisher eventPublisher, IHttpContextAccessor httpContextAccessor) =>
|
||||||
(_userRepository, _currentUser, _accountManager, _menuRepository, _smsAliyunManager, _smsAliyunManagerOptions, _securityCode, _memoryCache, _eventPublisher, _httpContextAccessor) =
|
(_userRepository, _currentUser, _accountManager, _menuRepository, _smsAliyunManager, _smsAliyunManagerOptions, _securityCode, _memoryCache, _eventPublisher, _httpContextAccessor) =
|
||||||
(userRepository, currentUser, accountManager, menuRepository, smsAliyunManager, smsAliyunManagerOptions, securityCode, memoryCache, eventPublisher, httpContextAccessor);
|
(userRepository, currentUser, accountManager, menuRepository, smsAliyunManager, smsAliyunManagerOptions, securityCode, memoryCache, eventPublisher, httpContextAccessor);
|
||||||
|
|
||||||
@@ -123,19 +127,14 @@ namespace Yi.Furion.Application.Rbac.Services.Impl
|
|||||||
throw new UserFriendlyException(UserConst.用户无角色分配);
|
throw new UserFriendlyException(UserConst.用户无角色分配);
|
||||||
}
|
}
|
||||||
//这里抛出一个登录的事件
|
//这里抛出一个登录的事件
|
||||||
|
var loginLogEntity = _httpContextAccessor.HttpContext.GetLoginLogInfo();
|
||||||
//不阻碍执行,无需等待
|
await _eventPublisher.PublishAsync(new LoginEventSource(new LoginEventArgs
|
||||||
|
|
||||||
|
|
||||||
await _eventPublisher.PublishAsync(new LoginEventSource(new LoginEventArgs
|
|
||||||
{
|
{
|
||||||
|
|
||||||
UserId = userInfo.User.Id,
|
UserId = userInfo.User.Id,
|
||||||
UserName = user.UserName,
|
UserName = user.UserName,
|
||||||
httpContext= _httpContextAccessor.HttpContext
|
LoginLogEntity = loginLogEntity
|
||||||
|
})
|
||||||
})
|
);
|
||||||
);;
|
|
||||||
|
|
||||||
|
|
||||||
//创建token
|
//创建token
|
||||||
|
|||||||
@@ -264,20 +264,6 @@
|
|||||||
<param name="postIds"></param>
|
<param name="postIds"></param>
|
||||||
<returns></returns>
|
<returns></returns>
|
||||||
</member>
|
</member>
|
||||||
<member name="M:Yi.Furion.Application.Rbac.Event.LoginEventHandler.GetClientInfo(Microsoft.AspNetCore.Http.HttpContext)">
|
|
||||||
<summary>
|
|
||||||
获取客户端信息
|
|
||||||
</summary>
|
|
||||||
<param name="context"></param>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="M:Yi.Furion.Application.Rbac.Event.LoginEventHandler.GetLoginLogInfo(Microsoft.AspNetCore.Http.HttpContext)">
|
|
||||||
<summary>
|
|
||||||
记录用户登陆信息
|
|
||||||
</summary>
|
|
||||||
<param name="context"></param>
|
|
||||||
<returns></returns>
|
|
||||||
</member>
|
|
||||||
<member name="T:Yi.Furion.Application.Rbac.Services.IConfigService">
|
<member name="T:Yi.Furion.Application.Rbac.Services.IConfigService">
|
||||||
<summary>
|
<summary>
|
||||||
Config服务抽象
|
Config服务抽象
|
||||||
|
|||||||
@@ -1,5 +1,8 @@
|
|||||||
using System;
|
using System;
|
||||||
|
using IPTools.Core;
|
||||||
using SqlSugar;
|
using SqlSugar;
|
||||||
|
using UAParser;
|
||||||
|
using Yi.Framework.Infrastructure.AspNetCore;
|
||||||
using Yi.Framework.Infrastructure.Data.Auditing;
|
using Yi.Framework.Infrastructure.Data.Auditing;
|
||||||
using Yi.Framework.Infrastructure.Ddd.Entities;
|
using Yi.Framework.Infrastructure.Ddd.Entities;
|
||||||
|
|
||||||
@@ -45,4 +48,45 @@ namespace Yi.Furion.Core.Rbac.Entities
|
|||||||
|
|
||||||
public long? CreatorId { get; set; }
|
public long? CreatorId { get; set; }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class LoginLogEntityExtensions {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 记录用户登陆信息
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="context"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public static LoginLogEntity GetLoginLogInfo(this HttpContext context)
|
||||||
|
{
|
||||||
|
ClientInfo GetClientInfo(HttpContext context)
|
||||||
|
{
|
||||||
|
var str = context.GetUserAgent();
|
||||||
|
var uaParser = Parser.GetDefault();
|
||||||
|
ClientInfo c = uaParser.Parse(str);
|
||||||
|
return c;
|
||||||
|
}
|
||||||
|
var ipAddr = context.GetClientIp();
|
||||||
|
IpInfo location;
|
||||||
|
if (ipAddr == "127.0.0.1")
|
||||||
|
{
|
||||||
|
location = new IpInfo() { Province = "本地", City = "本机" };
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
location = IpTool.Search(ipAddr);
|
||||||
|
}
|
||||||
|
ClientInfo clientInfo = GetClientInfo(context);
|
||||||
|
LoginLogEntity entity = new()
|
||||||
|
{
|
||||||
|
Browser = clientInfo.Device.Family,
|
||||||
|
Os = clientInfo.OS.ToString(),
|
||||||
|
LoginIp = ipAddr,
|
||||||
|
LoginLocation = location.Province + "-" + location.City
|
||||||
|
};
|
||||||
|
|
||||||
|
return entity;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System.Text;
|
|||||||
using System.Threading;
|
using System.Threading;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
using Furion.EventBus;
|
using Furion.EventBus;
|
||||||
|
using Yi.Furion.Core.Rbac.Entities;
|
||||||
|
|
||||||
namespace Yi.Furion.Core.Rbac.Etos
|
namespace Yi.Furion.Core.Rbac.Etos
|
||||||
{
|
{
|
||||||
@@ -28,6 +29,6 @@ namespace Yi.Furion.Core.Rbac.Etos
|
|||||||
public long UserId { get; set; }
|
public long UserId { get; set; }
|
||||||
public string UserName { get; set; }
|
public string UserName { get; set; }
|
||||||
|
|
||||||
public HttpContext httpContext { get; set; }
|
public LoginLogEntity LoginLogEntity { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -506,6 +506,13 @@
|
|||||||
登录信息
|
登录信息
|
||||||
</summary>
|
</summary>
|
||||||
</member>
|
</member>
|
||||||
|
<member name="M:Yi.Furion.Core.Rbac.Entities.LoginLogEntityExtensions.GetLoginLogInfo(Microsoft.AspNetCore.Http.HttpContext)">
|
||||||
|
<summary>
|
||||||
|
记录用户登陆信息
|
||||||
|
</summary>
|
||||||
|
<param name="context"></param>
|
||||||
|
<returns></returns>
|
||||||
|
</member>
|
||||||
<member name="T:Yi.Furion.Core.Rbac.Entities.MenuEntity">
|
<member name="T:Yi.Furion.Core.Rbac.Entities.MenuEntity">
|
||||||
<summary>
|
<summary>
|
||||||
菜单表
|
菜单表
|
||||||
|
|||||||
Reference in New Issue
Block a user