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.
141 lines
5.6 KiB
141 lines
5.6 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using System.Web.Mvc;
|
|
using System.Web.Routing;
|
|
using System.Web.Security;
|
|
using Warehouse.DAL.Permission;
|
|
|
|
namespace Warehouse.Code
|
|
{
|
|
public class Authorize
|
|
{
|
|
}
|
|
public class CustomAuthorizeAttribute : ActionFilterAttribute// AuthorizeAttribute
|
|
{
|
|
//表示是否检查登录
|
|
public bool IsCheck { get; set; }
|
|
public override void OnActionExecuting(ActionExecutingContext filterContext) //OnAuthorization AuthorizationContext
|
|
{
|
|
var actionDescriptor = filterContext.ActionDescriptor;
|
|
var controllerDescriptor = actionDescriptor.ControllerDescriptor;
|
|
var controller = controllerDescriptor.ControllerName;
|
|
var action = actionDescriptor.ActionName;
|
|
if (!IsCheck)
|
|
{
|
|
//base.OnAuthorization(filterContext);
|
|
base.OnActionExecuting(filterContext);
|
|
return;
|
|
}
|
|
if (!filterContext.RequestContext.HttpContext.Request.IsAuthenticated)
|
|
{
|
|
if (controller == "Account" || controller == "Error")
|
|
{
|
|
//base.OnAuthorization(filterContext);
|
|
//return;
|
|
}
|
|
if (controller == "Home")
|
|
{
|
|
//返回主页
|
|
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Account", action = "Login", returnUrl = filterContext.HttpContext.Request.Url, returnMessage = "没有登陆" }));
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Error", action = "NoPermission", returnUrl = filterContext.HttpContext.Request.Url, returnMessage = "您无权查看." }));
|
|
return;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (controller == "Home")
|
|
{
|
|
base.OnActionExecuting(filterContext);
|
|
return;
|
|
}
|
|
var HasPermission = false;
|
|
//判断是否有权限
|
|
var UserId = filterContext.HttpContext.User.Identity.Name;
|
|
// 多数据库判断 modify by xue lei on 2019-8-6
|
|
IEnumerable<dynamic> UserPermission = null;
|
|
if (Common.DBType == (int)DB.sqlserver)
|
|
{
|
|
UserPermission = new PermissionMSDal().GetUserPermission(Convert.ToInt32(UserId));
|
|
}
|
|
else
|
|
{
|
|
UserPermission = new PermissionDal().GetUserPermission(Convert.ToInt32(UserId));
|
|
}
|
|
|
|
if (action.Length > 5)
|
|
{
|
|
if (action.Substring(action.Length - 5, 5) == "Excel")
|
|
{
|
|
HasPermission = true;
|
|
}
|
|
}
|
|
|
|
//var UserPermission =
|
|
foreach (var item in UserPermission)
|
|
{
|
|
if (Convert.ToString(item.Remark) == Convert.ToString(action))
|
|
{
|
|
HasPermission = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!HasPermission)//没有权限
|
|
{
|
|
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Error", action = "NoPermission", returnUrl = filterContext.HttpContext.Request.Url, returnMessage = "您无权查看." }));
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
// base.OnAuthorization(filterContext);
|
|
base.OnActionExecuting(filterContext);
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public class RoleAuthorizeAttribute : AuthorizeAttribute
|
|
{
|
|
public override void OnAuthorization(AuthorizationContext filterContext)
|
|
{
|
|
var isAuth = true;//false
|
|
if (!filterContext.RequestContext.HttpContext.Request.IsAuthenticated)
|
|
{
|
|
isAuth = false;
|
|
}
|
|
else
|
|
{
|
|
if (filterContext.RequestContext.HttpContext.User.Identity != null)
|
|
{
|
|
//var roleService = new RoleService();
|
|
var actionDescriptor = filterContext.ActionDescriptor;
|
|
var controllerDescriptor = actionDescriptor.ControllerDescriptor;
|
|
var controller = controllerDescriptor.ControllerName;
|
|
var action = actionDescriptor.ActionName;
|
|
var ticket = (filterContext.RequestContext.HttpContext.User.Identity as FormsIdentity).Ticket;
|
|
var role = "";// roleService.GetById(ticket.Version);
|
|
if (role != null)
|
|
{
|
|
//isAuth = role.Permissions.Any(x => x.Permission.Controller.ToLower() == controller.ToLower() && x.Permission.Action.ToLower() == action.ToLower());
|
|
}
|
|
}
|
|
}
|
|
if (!isAuth)
|
|
{
|
|
filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary(new { controller = "Error", action = "NoPermission", returnUrl = filterContext.HttpContext.Request.Url, returnMessage = "您无权查看." }));
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
base.OnAuthorization(filterContext);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|