如何配置一个用户具有只读访问权限,而另一用户具有对Hangfire仪表板的完全访问权限?

时间:2020-06-06 17:28:38

标签: hangfire

如何配置一个用户具有只读权限,而另一个用户具有对仪表板的完全访问权限?

1 个答案:

答案 0 :(得分:0)

您可以使用DashboardOptions和AuthorizationFilter来设置“只读”和“编辑”访问权限。 See Documentation from Hangfire

public class HangFireAuthorizationFilter : IDashboardAuthorizationFilter
{
    public bool Authorize([NotNull] DashboardContext context)
    {
        string user = HttpContext.Current.User.Identity.Name;
        var adminAuthz = InternalMethod.lookup_db_for_user_access(user, "View");

        return adminAuthz != null;
    }

    public bool IsUserAuthorizedToEditHangfireDashboard([NotNull] DashboardContext context)
    {
        string user = HttpContext.Current.User.Identity.Name;
        var adminAuthz = InternalMethod.lookup_db_for_user_access(user, "Edit");

        return adminAuthz != null;
    }
}

在Hangfire仪表板初始化中使用上述过滤器

public void Configuration(IAppBuilder app)
{
    var hangfireAuthz = new HangFireAuthorizationFilter();
    var dashboardOptions = new DashboardOptions
    {
        Authorization = new[] { hangfireAuthz },
        IsReadOnlyFunc = (DashboardContext context) => !hangfireAuthz.IsUserAuthorizedToEditHangfireDashboard(context)
    };

    app.UseHangfireDashboard("/hangfire", dashboardOptions);
}