增加后台功能
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using NewLife.Log;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using System.Net;
|
||||
|
||||
namespace COMCMS.Web.Common
|
||||
{
|
||||
public class HttpGlobalExceptionFilter : IExceptionFilter
|
||||
{
|
||||
readonly IHostingEnvironment _env;
|
||||
public HttpGlobalExceptionFilter(IHostingEnvironment env)
|
||||
{
|
||||
_env = env;
|
||||
}
|
||||
|
||||
public void OnException(ExceptionContext context)
|
||||
{
|
||||
|
||||
//ILog log = LogManager.GetLogger(Startup.repository.Name, typeof(ExceptionContext));
|
||||
XTrace.WriteException(context.Exception);
|
||||
//log.Error(context.Exception.Message, context.Exception);
|
||||
|
||||
//var logger = _loggerFactory.CreateLogger(context.Exception.TargetSite.ReflectedType);
|
||||
|
||||
// logger.LogError(new EventId(context.Exception.HResult),
|
||||
// context.Exception,
|
||||
// context.Exception.Message);
|
||||
|
||||
//var json = new ErrorResponse("未知错误,请重试");
|
||||
|
||||
//if (_env.IsDevelopment()) json.DeveloperMessage = context.Exception;
|
||||
|
||||
//context.Result = new ApplicationErrorResult(json);
|
||||
//context.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
||||
|
||||
//context.ExceptionHandled = true;
|
||||
}
|
||||
|
||||
|
||||
public class ApplicationErrorResult : ObjectResult
|
||||
{
|
||||
public ApplicationErrorResult(object value) : base(value)
|
||||
{
|
||||
StatusCode = (int)HttpStatusCode.InternalServerError;
|
||||
}
|
||||
}
|
||||
|
||||
public class ErrorResponse
|
||||
{
|
||||
public ErrorResponse(string msg)
|
||||
{
|
||||
Message = msg;
|
||||
}
|
||||
public string Message { get; set; }
|
||||
public object DeveloperMessage { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using COMCMS.Core;
|
||||
using XCode;
|
||||
using NewLife.Log;
|
||||
using Newtonsoft.Json;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace COMCMS.Web.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// 后台授权过滤器
|
||||
/// </summary>
|
||||
public class MyAuthorizeAttribute : ResultFilterAttribute
|
||||
{
|
||||
/// <summary>
|
||||
/// 菜单key
|
||||
/// </summary>
|
||||
private readonly string _menuKey;
|
||||
/// <summary>
|
||||
/// 操作key
|
||||
/// </summary>
|
||||
private readonly string _eventKey;
|
||||
/// <summary>
|
||||
/// 返回类型 HTML 和 JSON
|
||||
/// </summary>
|
||||
private readonly string _returnType;
|
||||
public MyAuthorizeAttribute(string EventKey, string MenuKey, string returnType = "HTML")
|
||||
{
|
||||
_menuKey = MenuKey;
|
||||
_eventKey = EventKey;
|
||||
_returnType = !string.IsNullOrWhiteSpace(returnType) && returnType.ToLower() == "json" ? "JSON" : "HTML";
|
||||
}
|
||||
|
||||
public override void OnResultExecuting(ResultExecutingContext context)
|
||||
{
|
||||
bool isOK = false;
|
||||
if (Admin.IsAdminLogin())
|
||||
{
|
||||
Admin my = Core.Admin.GetMyInfo();
|
||||
if (my.Roles.IsSuperAdmin == 1)
|
||||
{
|
||||
isOK = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!string.IsNullOrEmpty(my.Roles.Powers))
|
||||
{
|
||||
IList<AdminMenuEvent> listevents = JsonConvert.DeserializeObject<IList<AdminMenuEvent>>(my.Roles.Powers);
|
||||
if (listevents != null && listevents.Count <= 0)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_menuKey) && !string.IsNullOrEmpty(_eventKey))
|
||||
{
|
||||
|
||||
if (listevents.FirstOrDefault(s => s.EventKey == _eventKey && s.MenuKey == _menuKey) != null)
|
||||
{
|
||||
isOK = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
XTrace.WriteLine($"没有找到菜单!EventKey:{_eventKey};MenuKey:{_menuKey}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//没有权限
|
||||
if (!isOK)
|
||||
{
|
||||
XTrace.WriteLine($"没有权限菜单!EventKey:{_eventKey};MenuKey:{_menuKey}");
|
||||
if (_returnType == "JSON")
|
||||
{
|
||||
JsonTip tip = new JsonTip()
|
||||
{
|
||||
Id = 0,
|
||||
Message = "您没有权限执行此操作!",
|
||||
ReturnUrl = "/AdminCP/Login"
|
||||
};
|
||||
context.Result = new JsonResult(tip);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
context.Result = new RedirectResult("/AdminCP/Error/NotAuthorize");
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
base.OnResultExecuting(context);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user