コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="isShowPage"></param>
        /// <param name="routeName"></param>
        /// <returns></returns>
        public async Task <IViewComponentResult> InvokeAsync(bool isShowPage = false)
        {
            string    area       = this.RouteData.Values["area"].ToString();
            string    controller = this.RouteData.Values["controller"].ToString();
            string    action     = this.RouteData.Values["action"].ToString();
            RouteName routeName  = new RouteName()
            {
                Action     = action,
                Areas      = area,
                Controller = controller
            };

            string url = string.IsNullOrEmpty(routeName.Areas)
                ? "/" + routeName.Controller + "/" + routeName.Action
                : "/" + routeName.Areas + "/" + routeName.Controller + "/" + routeName.Action;

            url = url.Trim().ToLower();
            var permission = await _permissionStorage.GetPermissionAsync();

            var menu = permission.Menus.FirstOrDefault(m => m.MenuUrl != null && m.MenuUrl.Trim().ToLower() == url);

            if (menu != null && isShowPage == false)
            {
                var list = CreateBtn(routeName, menu);
                return(View(list));
            }
            else
            {
                if (isShowPage == true)
                {
                    List <string> list = new List <string>()
                    {
                        "<div class=\"btn-group btn-group-sm\">",
                        "<button type=\"button\" id=\"formSave\" class=\"btn btn-primary btn-sm btn-permission btn-permission-save\"><i class=\"fa fa-save\"></i><span class=\"ml5\">保存</span></button>",
                        "<button type=\"button\" id=\"formReturn\" class=\"btn btn-default btn-sm btn-permission\"><i class=\"fa fa-mail-reply\"></i><span class=\"ml5\">返回</span></button>",
                        "</div>"
                    };
                    return(View(list));
                }
                else
                {
                    return(View(new List <string>()));
                }
            }
        }
コード例 #2
0
            public async Task OnResourceExecutionAsync(ResourceExecutingContext context, ResourceExecutionDelegate next)
            {
                string menuUrl = _requiredPermissions.UrlAndButtonType.Url;

                //判断用户权限
                if (string.IsNullOrEmpty(menuUrl))
                {
                    //区域判断
                    string area = context.RouteData.Values["area"].ToString();
                    if (string.IsNullOrEmpty(area))
                    {
                        menuUrl = "/" + context.RouteData.Values["controller"] + "/" + context.RouteData.Values["action"];
                    }
                    else
                    {
                        menuUrl = "/" + area + "/" + context.RouteData.Values["controller"] + "/" + context.RouteData.Values["action"];
                    }
                }
                menuUrl = menuUrl.Trim().ToLower();
                var dbpermission = await _permissionStorage.GetPermissionAsync();

                var menu = dbpermission.Menus.FirstOrDefault(m => m.MenuUrl != null && m.MenuUrl.Trim().ToLower() == menuUrl);

                if (menu != null)//地址存在
                {
                    if (_requiredPermissions.UrlAndButtonType.ButtonType == default(byte))
                    {
                        await next();
                    }
                    else
                    {
                        byte buttonType = (byte)_requiredPermissions.UrlAndButtonType.ButtonType;
                        if (menu.MenuButton.Select(m => m.ButtonType).Contains(buttonType))//拥有操作权限
                        {
                            await next();
                        }
                        else
                        {
                            //没有操作权限
                            if (_requiredPermissions.UrlAndButtonType.IsPage)
                            {
                                context.Result = new RedirectResult("/error/noauth");
                            }
                            else
                            {
                                context.Result = new ContentResult()
                                {
                                    Content = PermissionStatusCodes.Status2Unauthorized.ToString()
                                };
                            }
                            await context.Result.ExecuteResultAsync(context);
                        }
                    }
                }
                else
                {
                    //没有操作权限
                    if (_requiredPermissions.UrlAndButtonType.IsPage)
                    {
                        context.Result = new RedirectResult("/error/noauth");
                    }
                    else
                    {
                        context.Result = new ContentResult()
                        {
                            Content = PermissionStatusCodes.Status2Unauthorized.ToString()
                        };
                    }
                    await context.Result.ExecuteResultAsync(context);
                }
            }