69 lines
2.1 KiB
C#
69 lines
2.1 KiB
C#
using System.Security.Claims;
|
|
using Domain.Admin.Admin;
|
|
using Domain.Admin.Admin.ENum;
|
|
using Microsoft.AspNetCore.Http;
|
|
|
|
namespace AdminService.Admin
|
|
{
|
|
public static class OperationLogHelper
|
|
{
|
|
public static AdminOperationLog CreateLog(
|
|
EnumOperationModule module,
|
|
string actionType,
|
|
string description,
|
|
string httpMethod,
|
|
string requestPath,
|
|
string? requestParams = null,
|
|
string? response = null,
|
|
int status = 1)
|
|
{
|
|
return new AdminOperationLog
|
|
{
|
|
Module = (int)module,
|
|
ActionType = actionType,
|
|
Description = description,
|
|
HttpMethod = httpMethod,
|
|
RequestPath = requestPath,
|
|
RequestParams = requestParams,
|
|
Response = response,
|
|
Status = status,
|
|
OperateTime = DateTime.Now
|
|
};
|
|
}
|
|
|
|
public static (string? adminId, string? adminName) GetAdminInfo(ClaimsPrincipal? user)
|
|
{
|
|
if (user == null) return (null, null);
|
|
|
|
var adminId = user.FindFirst("uid")?.Value;
|
|
var adminName = user.FindFirst("dname")?.Value;
|
|
|
|
if (string.IsNullOrEmpty(adminName))
|
|
{
|
|
adminName = user.FindFirst(ClaimTypes.Name)?.Value;
|
|
}
|
|
|
|
return (adminId, adminName);
|
|
}
|
|
|
|
public static void FillAdminInfo(AdminOperationLog log, ClaimsPrincipal? user)
|
|
{
|
|
var (adminId, adminName) = GetAdminInfo(user);
|
|
log.AdminId = adminId;
|
|
log.AdminName = adminName;
|
|
}
|
|
|
|
public static string? GetClientIp(HttpContext? context)
|
|
{
|
|
if (context == null) return null;
|
|
|
|
var ip = context.Request.Headers["X-Forwarded-For"].FirstOrDefault();
|
|
if (string.IsNullOrEmpty(ip))
|
|
{
|
|
ip = context.Connection.RemoteIpAddress?.ToString();
|
|
}
|
|
return ip;
|
|
}
|
|
}
|
|
}
|