using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;

public class CheckPermissionFilter : IAuthorizationFilter
{
    /// <summary>
    /// 权限判断
    /// </summary>
    /// <param name="filterContext"></param>
    public void OnAuthorization(AuthorizationContext filterContext)
    {
        if (filterContext == null)
        {
            throw new ArgumentNullException("filterContext");
        }
        if (filterContext.HttpContext.Request.Url == null)
        {
            throw new ArgumentNullException("filterContext");
        }
        string pageUrl = filterContext.HttpContext.Request.Url.AbsolutePath.ToLower(); //OperateContext.GetThisPageUrl(false);
                                                                             // 是否是Ajax请求
        var bAjax = filterContext.HttpContext.Request.IsAjaxRequest();
        // 1、允许匿名访问 用于标记在授权期间要跳过 AuthorizeAttribute 的控制器和操作的特性 
        var actionAnonymous = filterContext.ActionDescriptor.GetCustomAttributes(typeof(AllowAnonymousAttribute), true) as IEnumerable<AllowAnonymousAttribute>;
        var controllerAnonymous = filterContext.Controller.GetType().GetCustomAttributes(typeof(AllowAnonymousAttribute), true) as IEnumerable<AllowAnonymousAttribute>;
        if ((actionAnonymous != null && actionAnonymous.Any()) || (controllerAnonymous != null && controllerAnonymous.Any()))
        {
            return;
        }
        // 判断是否登录或登录已超时 需要重新登录

        bool islocal = false;//是否需要走单点登录  true 不需要  false 需要
        string LoginName = filterContext.HttpContext.Request.Headers.Get("OAM_REMOTE_USER");
        
        HttpCookie loginInfoCookies = filterContext.HttpContext.Request.Cookies["BaseCallCenter_T_User"];//用户信息
        if (loginInfoCookies != null)
        {
            islocal = true;
        }
        else
        {
            HttpCookie cookies = filterContext.HttpContext.Request.Cookies["platform"];
            if (cookies != null && cookies.HasKeys)
            {
                if (!string.IsNullOrEmpty(cookies["islocal"]))
                {
                    if (cookies["islocal"] == "1")
                    {
                        //有值，说明是本地登录
                        islocal = true;
                    }
                }
            }
        }
        if (islocal)
        {
            //本地登录过滤器不做任何处理  已经登录 过滤器不做任何处理OtherPage
        }
        else if (pageUrl.Contains("default") || pageUrl == "/"|| pageUrl.Contains("otherpage") || pageUrl.Contains("api") || pageUrl.Contains("report") || pageUrl.Contains("workordertypefromdatanew"))
        {
            //通用路径过滤器不做任何处理
        }
        else  if (string.IsNullOrEmpty(LoginName)|| LoginName.Equals("Anonymous"))
        {
            //filterContext.Result = new RedirectResult("https://portal.zzmetro.cn:4443/ZZMSsoLogin/zzmteroLogin.jsp");
            filterContext.Result = new RedirectResult(ConfigurationManager.AppSettings["sso"].ToMyString());
        }

    }
}