using System.Collections.Generic;
using System.Linq;
using Model;
using BLL;
using System.Web.Mvc;
namespace Lib
{
public class LoginFilter : BaseAuthorizeAttribute
{
private bool ValidateLogin = true;
private bool ValidateRequest = true;
public LoginFilter()
: base()
{
}
public LoginFilter(bool _validateLogin)
: base()
{
this.ValidateLogin = _validateLogin;
}
public LoginFilter(bool _validateLogin, bool _validateRequest)
: base()
{
this.ValidateLogin = _validateLogin;
this.ValidateRequest = _validateRequest;
}
public override void OnAuthorization(AuthorizationContext filterContext)
{
if (this.ValidateLogin)
{
UserInfo LoginUser = filterContext.HttpContext.Session["wms-session-userinfo"] as UserInfo;
string path = filterContext.HttpContext.Request.Path;
if (LoginUser == null)
{
string url = "/Login/Login";
if (!string.IsNullOrEmpty(path))
{
path = filterContext.HttpContext.Server.UrlEncode(path);
url = url + "?returnurl=" + path;
}
filterContext.Result = new RedirectResult(url);
}
else
{
if (ValidateRequest && path != "/")
{
if (!HasPower(path, LoginUser.UserCode))
{
string url = "/Home/Error";
filterContext.Result = new RedirectResult(url);
}
}
}
}
}
///
/// 判断是否有权限操作
///
///
///
///
public bool HasPower(string ResNum, string RoleNum)
{
IDALGerUserMenu provider = new DALGetUserMenu();
List ListResult = provider.GetList(RoleNum).ToList();
ListResult = ListResult == null? new List() : ListResult;
bool hasPower = ListResult.Exists(a => a.ResNum.ToLower() == ResNum.ToLower() || (a.Url != null && a.Url.ToLower() == ResNum.ToLower()));
//超级管理员权限
//if (RoleNum == "SuperUser" || RoleNum=="D001")//ResourceManager.GetSettingEntity("Super_AdminRole").Value
//{
hasPower = true;
//}
return hasPower;
}
}
}