using Hangfire.Dashboard;
using System.IdentityModel.Tokens.Jwt;
using System;
using System.Linq;
using IRaCIS.Core.Domain.Share;

namespace IRaCIS.Core.API.Filter
{

    //从cookie 中取值
    public class hangfireAuthorizationFilter : IDashboardAuthorizationFilter
    {
        public bool Authorize(DashboardContext context)
        {
            var httpContext = context.GetHttpContext();

            // Allow all authenticated users to see the Dashboard (potentially dangerous).
            //return httpContext.User.Identity.IsAuthenticated;

            var jwtToken = httpContext.Request.Cookies["access_token"]?.ToString();

            var handler = new JwtSecurityTokenHandler();

            if (handler.CanReadToken(jwtToken))
            {

                var jwtSecurityToken = handler.ReadJwtToken(jwtToken);

                return jwtSecurityToken.Claims.Any(t => t.Type == JwtIRaCISClaimType.UserTypeEnum && (t.Value == UserTypeEnum.Admin.ToString()||  t.Value== UserTypeEnum.SuperAdmin.ToString()));
            }
            else
            {
                return false;
            }


        }
    }
}
