chore: 构建稳定版本
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.Users;
|
||||
using Yi.Framework.Core.Extensions;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Consts;
|
||||
|
||||
namespace Yi.Framework.Rbac.Domain.Authorization
|
||||
{
|
||||
public class DefaultPermissionHandler : IPermissionHandler, ITransientDependency
|
||||
{
|
||||
private ICurrentUser _currentUser { get; set; }
|
||||
private IHttpContextAccessor _httpContextAccessor;
|
||||
|
||||
public DefaultPermissionHandler(ICurrentUser currentUser, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_currentUser = currentUser;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
public bool IsPass(string permission)
|
||||
{
|
||||
var permissions = _httpContextAccessor.HttpContext.GetUserPermissions(TokenTypeConst.Permission);
|
||||
if (permissions is not null)
|
||||
{
|
||||
if (permissions.Contains("*:*:*"))
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
return permissions.Contains(permission);
|
||||
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Yi.Framework.Rbac.Domain.Authorization
|
||||
{
|
||||
public interface IPermissionHandler
|
||||
{
|
||||
bool IsPass(string permission);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace Yi.Framework.Rbac.Domain.Authorization
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
|
||||
public class PermissionAttribute : Attribute
|
||||
{
|
||||
internal string Code { get; set; }
|
||||
|
||||
public PermissionAttribute(string code)
|
||||
{
|
||||
Code = code;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
using Microsoft.AspNetCore.Http.HttpResults;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Controllers;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.DependencyInjection;
|
||||
using Volo.Abp.Http;
|
||||
using Yi.Framework.Core.Helper;
|
||||
using static System.Runtime.InteropServices.JavaScript.JSType;
|
||||
|
||||
namespace Yi.Framework.Rbac.Domain.Authorization
|
||||
{
|
||||
internal class PermissionGlobalAttribute : ActionFilterAttribute, ITransientDependency
|
||||
{
|
||||
private readonly IPermissionHandler _permissionHandler;
|
||||
public PermissionGlobalAttribute(IPermissionHandler permissionHandler)
|
||||
{
|
||||
_permissionHandler = permissionHandler;
|
||||
}
|
||||
public override void OnActionExecuting(ActionExecutingContext context)
|
||||
{
|
||||
if (context.ActionDescriptor is not ControllerActionDescriptor controllerActionDescriptor) return;
|
||||
PermissionAttribute? perAttribute = controllerActionDescriptor.MethodInfo.GetCustomAttributes(inherit: true)
|
||||
.FirstOrDefault(a => a.GetType().Equals(typeof(PermissionAttribute))) as PermissionAttribute;
|
||||
//空对象直接返回
|
||||
if (perAttribute is null) return;
|
||||
|
||||
var result = _permissionHandler.IsPass(perAttribute.Code);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
var model = new RemoteServiceErrorInfo()
|
||||
{
|
||||
Code = "403",
|
||||
Message = $"您无权限访问,请联系管理员申请",
|
||||
Details = $"您无权限访问该接口-{context.HttpContext.Request.Path.Value}",
|
||||
};
|
||||
|
||||
var content = new ObjectResult(new { error = model })
|
||||
{
|
||||
StatusCode = 403
|
||||
};
|
||||
context.Result = content;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user