Использование внедрения зависимостей Чтобы получить доступ к EF, используйте службу уровня в HtmlHelper stati c class - PullRequest
0 голосов
/ 11 июля 2020

Чтобы получить доступ к объекту через уровень обслуживания, как его можно использовать в классах c stati? В моем примере кода возникает следующая ошибка!

Операция не может быть завершена, потому что DbContext удален.

Пример кода:

    public static class ActionLinkPermissionHelper
{
    private static readonly IUserService _userService;
  
    static ActionLinkPermissionHelper()
    {
        _userService = DependencyResolver.Current.GetService<IUserService>();
    }
    public static bool CheckActionPermission(string controllerName, string actionName, string requestType, string controllerNamespace = null)
    {
        if (HttpContext.Current.User.Identity.IsAuthenticated)
        {
           
            string username = HttpContext.Current.User.Identity.Name;
            var users = _userService.GetUser(username);

         
            // If User Is SuperAdmin, Do Nothing!
            var user = _userService.GetUser(users.UserID);
            if (user.IsSuperAdmin)
            {
                return true;
            }

            List<PermissionListItem> allPermissions = PermissionsHelper.GetAllPermissions();

            // Now Check If Permission Requires Authorization And User Has This Permission
            string currentActionFullName = controllerName + actionName;// + requestType;
            var requestedAction = allPermissions.FirstOrDefault(pr => pr.ActionFullName == currentActionFullName);
            if (requestedAction != null && (requestedAction.PermissiongRequiresAuthorization || requestedAction.PermissiongGroupRequiresAuthorization) && !requestedAction.PermissionAllowAnonymous)
            {
                List<PermissionListItem> userPermissionItems = PermissionsHelper.GetUserPermissions();

                if (!userPermissionItems.Any(upr =>
                        upr.ActionFullName == currentActionFullName
                        && upr.PermissiongGroupName == controllerName
                        && (controllerNamespace == null || upr.PermissiongGroupNamespace == controllerNamespace)))
                {
                    return false;
                }
            }
        }
        return true;
    }

}

  public static MvcHtmlString ActionLinkPermission(this HtmlHelper htmlHelper, string linkText, string actionName)
        {
            string controllerNamespace = htmlHelper.ViewContext.Controller.GetType().Namespace;
            string controllername = (string)htmlHelper.ViewContext.RouteData.GetRequiredString("controller");
            string requestMethod = (string)htmlHelper.ViewContext.HttpContext.Request.HttpMethod;
            if (!CheckActionPermission(controllername, actionName, requestMethod, controllerNamespace))
            {
                return null;
            }
            return htmlHelper.ActionLink(linkText, actionName);
        }
...