Mini.Crm/Mini.Web/WebHelper/AuthorizeToolBar.cs

80 lines
2.7 KiB
C#

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Filters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
namespace Mini.Web.WebHelper
{
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public class AuthorizeToolBar : ActionFilterAttribute
{
private const string IS_AUTHORIZED = "isAuthorized";
public string RedirectUrl = "~/Base/Account/UnAuthorized";
private string _rightId;
private int _toolBarId;
private string[] m_roles;
public AuthorizeToolBar()
{
//this.m_roles = UserRightsHelper.getUserRights();
}
public AuthorizeToolBar(string rightId, int toolBarId)
{
this._rightId = rightId;
this._toolBarId = toolBarId;
}
private bool isAuthorizeCore()
{
if (!string.IsNullOrEmpty(this._rightId) && this._toolBarId > 0 && !this.IsRoleToolBar(this._rightId, this._toolBarId))
{
return false;
}
return true;
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
this.m_roles = UserRightsHelper.getUserRights(filterContext.HttpContext);
if (isAuthorizeCore())
{
return;
}
var account = filterContext.HttpContext.Request.Query["weixincrmaccount"];
if (!string.IsNullOrEmpty(account))
{
return;
}
HttpRequest httpRequest = filterContext.HttpContext.Request;
RedirectResult redirectResult = new RedirectResult(RedirectUrl + "?type=button");
filterContext.Result = redirectResult;
}
private bool IsRoleToolBar(string role, int toobar)
{
int outInt = 0;
if ((role != null) && (this.m_roles != null))
{
string[] rolesRights = this.m_roles.Where(p => p.Contains(role)).ToArray();
for (int i = 0; i < rolesRights.Length; i++)
{
if ((rolesRights[i] != null) && (rolesRights[i].Length > 4) && (string.Compare(rolesRights[i], 0, role, 0, 4, StringComparison.OrdinalIgnoreCase) == 0) && int.TryParse(rolesRights[i].Substring(4), out outInt) && (outInt & toobar) > 0)
{
return true;
}
}
}
return false;
}
}
}