.net core3.1 abp动态菜单和动态权限(思路) (二)

ps:本文需要先把abp的源码下载一份来下,跟着一起找实现,更容易懂

在abp中,对于权限和菜单使用静态来管理,菜单的加载是在登陆页面的地方(具体是怎么知道的,浏览器按F12,然后去sources中去找)

.net core3.1 abp动态菜单和动态权限(思路)  (二)

这个/AbpScripts/GetScripts是获取需要初始化的script,源自AbpScriptsController,GetScripts方法包括

页面加载时的链接是:http://localhost:62114/AbpScripts/GetScripts?v=637274153555501055_multiTenancyScriptManager  //当前租户初始化  对应报文的 abp.multiTenancy_sessionScriptManager //当前session初始化 对应报文的 abp.session
_localizationScriptManager  //本地化的初始化 对应报文的 abp.localization
_featuresScriptManager  //对应报文的 abp.features
_authorizationScriptManager  //权限初始化  对应报文的 abp.auth
_navigationScriptManager  //导航菜单初始化  对应报文的 abp.nav
_settingScriptManager  //设置初始化  对应报文的 abp.setting
_timingScriptManager  //对应报文的 abp.clock
_customConfigScriptManager  //对应报文的 abp.custom

.net core3.1 abp动态菜单和动态权限(思路)  (二)

好了,现在基本算是找到菜单和权限js获取的地方了,一般系统里面,权限是依赖于菜单和菜单按钮的,所以我们先不管权限,先把菜单做成动态加载的

从await _navigationScriptManager.GetScriptAsync()开始,一路F12,大概流程是

(接口)INavigationScriptManager=>(接口实现)NavigationScriptManager=>(方法)GetScriptAsync=>(调用)await _userNavigationManager.GetMenusAsync=>
(接口)IUserNavigationManager=>(接口实现)UserNavigationManager=>(方法)GetMenuAsync=>(调用)navigationManager.Menus=>
(接口)INavigationManager=>(接口实现)NavigationManager=>(非静态构造函数为Menus属性赋值)NavigationManager

到这里之后基本就到底了,我们看看NavigationManager的内容

    internal class NavigationManager : INavigationManager, ISingletonDependency
    {
        public IDictionary<string, MenuDefinition> Menus { get; private set; }  //属性

        public MenuDefinition MainMenu //属性
        {
            get { return Menus["MainMenu"]; }
        }

        private readonly IIocResolver _iocResolver;
        private readonly INavigationConfiguration _configuration;

        public NavigationManager(IIocResolver iocResolver, INavigationConfiguration configuration) //非静态构造函数
        {
            _iocResolver = iocResolver;
            _configuration = configuration;

            Menus = new Dictionary<string, MenuDefinition>
                    {
                        {"MainMenu", new MenuDefinition("MainMenu", new LocalizableString("MainMenu", AbpConsts.LocalizationSourceName))}
                    };
        }

        public void Initialize()  //初始化方法
        {
            var context = new NavigationProviderContext(this);

            foreach (var providerType in _configuration.Providers)
            {
                using (var provider = _iocResolver.ResolveAsDisposable(providerType))
                {
                    provider.Object.SetNavigation(context);  //中式英语翻译一下,应该是设置导航
                }
            }
        }
    }

这个类里面就只有属性、需要注入的接口声明、非静态构造函数、初始化方法,我们到这里需要关注的是Menus这个属性,这个属性似乎将会包含我们需要生成的菜单内容

Menus = new Dictionary<string, MenuDefinition>
                    {
                        {"MainMenu", new MenuDefinition("MainMenu", new LocalizableString("MainMenu", AbpConsts.LocalizationSourceName))}
                    };

这里是对Menus的赋值,实例化了一个Dictionary,前面的不用看,主要是看标红的这句话,从new LocalizableString(“MainMenu”, AbpConsts.LocalizationSourceName)里面获取到值

好了现在基本找到地方了,我们不知道LocalizableString是什么意思,但是我们可以百度一波

ILocalizableString/LocalizableString:封装需要被本地化的string的信息,并提供Localize方法(调用ILocalizationManager的GetString方法)返回本地化的string. SourceName指定其从那个本地化资源读取本地化文本。

LocalizableString(“Questions”, “”) 如果本地找不到资源,会报300

大概的意思是通过new LocalizableString,我们可以在本地化来源为AbpConsts.LocalizationSourceName的string里面寻找到Key为MainMenu的value(理解不对请喷)

现在需要去找到那个地方对MainMenu进行了本地化操作,一般来说这个事情都是在程序加载的时候进行的,先对MainMenu进行读取,保存到本地,然后在 __navigationScriptManager读取,传输给前台_

似乎不好找了,但是我们发现有一个类型MenuDefinition,F12一下,可以发现宝藏

namespace Abp.Application.Navigation
{
    ///
    /// Represents a navigation menu for an application.  //表示应用程序的导航菜单
///
    public class MenuDefinition : IHasMenuItemDefinitions
    {
        ///
        /// Unique name of the menu in the application. Required.  //应用程序中菜单的唯一名称。 必须
        ///
        public string Name { get; private set; }

        ///
        /// Display name of the menu. Required.  //菜单显示名称 必须
///
        public ILocalizableString DisplayName { get; set; }

        ///
        /// Can be used to store a custom object related to this menu. Optional.  //可用于存储与此菜单相关的自定义对象
///
        public object CustomData { get; set; }

        ///
        /// Menu items (first level).   //菜单项(第一级)
///
        public List Items { get; set; }

        ///
        /// Creates a new  object.

        ///
        /// Unique name of the menu
        /// Display name of the menu
        /// Can be used to store a custom object related to this menu.

        public MenuDefinition(string name, ILocalizableString displayName, object customData = null)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentNullException("name", "Menu name can not be empty or null.");
            }

            if (displayName == null)
            {
                throw new ArgumentNullException("displayName", "Display name of the menu can not be null.");
            }

            Name = name;
            DisplayName = displayName;
            CustomData = customData;

            Items = new List();
        }

        ///
        /// Adds a  to .
        ///
        ///  to be added
        /// This  object
        public MenuDefinition AddItem(MenuItemDefinition menuItem)
        {
            Items.Add(menuItem);
            return this;
        }

        ///
        /// Remove menu item with given name
        ///
        ///
        public void RemoveItem(string name)
        {
            Items.RemoveAll(m => m.Name == name);
        }
    }
}

找到了菜单的类型了,那么我们去找保存的地方就好找了,我们其实可以根据AddItem这个方法去找,去查看哪个地方引用了

AddItem方法添加的是MenuItemDefinition类型的变量,那我们现在退出abp源码,去我们的AbpLearn项目中去全局搜索一下

.net core3.1 abp动态菜单和动态权限(思路)  (二)

看来是同一个AbpLearnNavigationProvider类里面,双击过去看一下

///
    /// This class defines menus for the application.

    ///
    public class AbpLearnNavigationProvider : NavigationProvider
    {
        public override void SetNavigation(INavigationProviderContext context)
        {
            context.Manager.MainMenu
                .AddItem(
                    new MenuItemDefinition(
                        PageNames.Home,
                        L("HomePage"),
                        url: "",
                        icon: "fas fa-home",
                        requiresAuthentication: true
                    )
                ).AddItem(
                    new MenuItemDefinition(
                        PageNames.Tenants,
                        L("Tenants"),
                        url: "Tenants",
                        icon: "fas fa-building",
                        permissionDependency: new SimplePermissionDependency(PermissionNames.Pages_Tenants)
                    )
                ).AddItem(
                    new MenuItemDefinition(
                        PageNames.Users,
                        L("Users"),
                        url: "Users",
                        icon: "fas fa-users",
                        permissionDependency: new SimplePermissionDependency(PermissionNames.Pages_Users)
                    )
                ).AddItem(
                    new MenuItemDefinition(
                        PageNames.Roles,
                        L("Roles"),
                        url: "Roles",
                        icon: "fas fa-theater-masks",
                        permissionDependency: new SimplePermissionDependency(PermissionNames.Pages_Roles)
                            )
                )
                .AddItem(
                    new MenuItemDefinition(
                        PageNames.About,
                        L("About"),
                        url: "About",
                        icon: "fas fa-info-circle"
                    )
                ).AddItem( // Menu items below is just for demonstration!

                    new MenuItemDefinition(
                        "MultiLevelMenu",
                        L("MultiLevelMenu"),
                        icon: "fas fa-circle"
                    ).AddItem(
                        new MenuItemDefinition(
                            "AspNetBoilerplate",
                            new FixedLocalizableString("ASP.NET Boilerplate"),
                            icon: "far fa-circle"
                        ).AddItem(
                            new MenuItemDefinition(
                                "AspNetBoilerplateHome",
                                new FixedLocalizableString("Home"),
                                url: "https://aspnetboilerplate.com?ref=abptmpl",
                                icon: "far fa-dot-circle"
                            )
                        ).AddItem(
                            new MenuItemDefinition(
                                "AspNetBoilerplateTemplates",
                                new FixedLocalizableString("Templates"),
                                url: "https://aspnetboilerplate.com/Templates?ref=abptmpl",
                                icon: "far fa-dot-circle"
                            )
                        ).AddItem(
                            new MenuItemDefinition(
                                "AspNetBoilerplateSamples",
                                new FixedLocalizableString("Samples"),
                                url: "https://aspnetboilerplate.com/Samples?ref=abptmpl",
                                icon: "far fa-dot-circle"
                            )
                        ).AddItem(
                            new MenuItemDefinition(
                                "AspNetBoilerplateDocuments",
                                new FixedLocalizableString("Documents"),
                                url: "https://aspnetboilerplate.com/Pages/Documents?ref=abptmpl",
                                icon: "far fa-dot-circle"
                            )
                        )
                    ).AddItem(
                        new MenuItemDefinition(
                            "AspNetZero",
                            new FixedLocalizableString("ASP.NET Zero"),
                            icon: "far fa-circle"
                        ).AddItem(
                            new MenuItemDefinition(
                                "AspNetZeroHome",
                                new FixedLocalizableString("Home"),
                                url: "https://aspnetzero.com?ref=abptmpl",
                                icon: "far fa-dot-circle"
                            )
                        ).AddItem(
                            new MenuItemDefinition(
                                "AspNetZeroFeatures",
                                new FixedLocalizableString("Features"),
                                url: "https://aspnetzero.com/Features?ref=abptmpl",
                                icon: "far fa-dot-circle"
                            )
                        ).AddItem(
                            new MenuItemDefinition(
                                "AspNetZeroPricing",
                                new FixedLocalizableString("Pricing"),
                                url: "https://aspnetzero.com/Pricing?ref=abptmpl#pricing",
                                icon: "far fa-dot-circle"
                            )
                        ).AddItem(
                            new MenuItemDefinition(
                                "AspNetZeroFaq",
                                new FixedLocalizableString("Faq"),
                                url: "https://aspnetzero.com/Faq?ref=abptmpl",
                                icon: "far fa-dot-circle"
                            )
                        ).AddItem(
                            new MenuItemDefinition(
                                "AspNetZeroDocuments",
                                new FixedLocalizableString("Documents"),
                                url: "https://aspnetzero.com/Documents?ref=abptmpl",
                                icon: "far fa-dot-circle"
                            )
                        )
                    )
                );
        }

        private static ILocalizableString L(string name)
        {
            return new LocalizableString(name, AbpLearnConsts.LocalizationSourceName);
        }
    }

好了,现在我们找到菜单定义的地方了,那么我们如何去做动态菜单哪?

首先我们想一下需要什么样的动态菜单?

1.从数据库加载,不从数据库加载怎么叫动态

2.可以根据不同Host(管理者)和Tenant(租户)加载不同的菜单,不可能管理者和租户看到的菜单全是一个样子的吧!

3.可以根据不同的角色或者用户加载不同的菜单(这个就牵扯到权限了,比如谁可以看到什么,不可以看到什么)

4.权限、按钮最好和菜单相绑定,这样便于控制

……

根据以上几点,我们可以确定

1.必须要在用户登录之后加载出来的菜单才能符合条件

2.菜单需要建一个表(因为abp默认没有单独的菜单表),来进行存放

3.字段需要包含:菜单名,菜单与权限对应的名称(用于动态权限),菜单对应的Url,Icon,级联父Id,是否启用,排序,租户Id

4.需要对菜单进行编辑时,因为牵扯到多租户,我们需要对多租户定义一个标准的菜单,在添加租户时,自动将标准菜单复制保存一份到新租户中,所以我们需要对于菜单的进行区分,一般来说Host对应的数据行TenantId(int)都为null,我们可以将标准菜单的TenantId标为-1,已经分配保存的菜单TenantId为当前租户Id,这样便于区分和查询

好了,让我们开始写动态菜单吧

Original: https://www.cnblogs.com/spatxos/p/13089690.html
Author: spatxos
Title: .net core3.1 abp动态菜单和动态权限(思路) (二)

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/582928/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 【实操填坑】在树莓派上编译 EtherCAT IgH Master 主站程序

    官网下载地址:https://etherlab.org/download/ethercat/ (可list查看文件列表)https://etherlab.org/download/…

    Linux 2023年6月7日
    0125
  • ESXI系列问题整理以及记录——使用Windows PowerShell中的SSH功能连接ESXI控制台

    首先进入ESXI管理页面,开启ESXI的SSH功能 接下来到位于同一局域网的Win主机上开启Powershell,如果ESXI主机的IP地址为192.168.1.77,则在Powe…

    Linux 2023年6月13日
    0124
  • JVM 配置参数 -D,-X,-XX 的区别

    转载请注明出处: 最近在安全护网行动,需要针对服务进行不断的安全加固,如 对服务的 log4j 的安全配置进行防护,对 fastjson 的漏洞进行安全加固等,最快的防护方法就是通…

    Linux 2023年6月14日
    0102
  • Python:给定一个不超过5位的正整数,判断有几位

    方法一:作比较 方法二:使用整除实现,除完后如果是个0或不是个0,这种方法引入了计算,效率会降低,所以能加就不要减,能乘就不要除,能不计算就不计算 方法三: 方法四:字符串处理实现…

    Linux 2023年6月7日
    093
  • 概率算法_二项分布和泊松分布

    本次函数有 1、阶乘 2、计算组合数C(n,x) 3、二项概率分布 4、泊松分布 以下是历史函数 继续概率,本次是二项分布和泊松分布,这个两个还是挺好玩的,可以作为预测函数用,因为…

    Linux 2023年6月6日
    095
  • c++模板类的使用,编译的问题

    前两天在写代码时,把模板类的声明和分开放在两个文件中了,类似于下面这样: stack.hpp: #ifndef _STACK_HPP #define _STACK_HPP temp…

    Linux 2023年6月14日
    095
  • redis好书推荐

    redis好书推荐原创amy_xing01 最后发布于2018-07-16 18:29:54 阅读数 10427 收藏展开Redis从入门到深入学习,推荐几本好书: 《Redis入…

    Linux 2023年5月28日
    092
  • 博客园装饰——(一)置顶菜单栏

    功能描述:当页面向下滚动到菜单栏上边沿触碰到浏览器窗口上边沿时,菜单栏会固定地显示在浏览器窗口上方(贴紧),即达到了置顶菜单栏的效果。而当页面向上滚动到原来的位置时,菜单栏又会自动…

    Linux 2023年6月14日
    0110
  • SQLI-LABS(Less-5)

    Less-5(GET-Double injection-Single Quotes-String) 打开 Less-5页面,可以看到页面中间有一句 Please input the…

    Linux 2023年6月6日
    084
  • Vim 编辑器|批量注释与批量取消注释

    添加注释 ctrl + v 进入块选泽模式。 上下键选中需要注释的行。 按大写 I 进入插入模式,输入注释符。 按两次 ESC 退出,即完成添加注释。 取消注释 ctrl + v …

    Linux 2023年5月27日
    0104
  • Linux 配置Git

    前言:请各大网友尊重本人原创知识分享,谨记本人博客: 南国以南i 一、用git –version命令检查是否已经安装 二、下载git源码并解压 wget https:/…

    Linux 2023年6月14日
    090
  • Linux 查看端口被占用

    端口被占用网上很多,这种频繁操作的命令容易忘记,写这边文章的目的主要是加深操作命令的印象, Liux 查看端口占用情况可以使用 lsof 和 netstat 命令。 lsof ls…

    Linux 2023年6月6日
    089
  • SSH的 Write failed: Broken pipe 问题

    问题现象: 表示连接管道已经断开 解决方法: 方法一:客户端配置在客户端的 ~/.ssh/ config文件(如不存在请自行创建)中添加下面内容:ServerAliveInterv…

    Linux 2023年6月8日
    092
  • Linux的OpenLava配置

    OpenLava OpenLava是基于LSF早期的开源版本发展而来,其 免费、 开源、 兼容IBM LSF的工作负载调度器。当你需要执行某项业务时候(比如跑渲染之类的),当有服务…

    Linux 2023年6月6日
    0101
  • spring-data-redis 2.0 的使用

    在使用Spring Boot2.x运行Redis时,发现百度不到顺手的文档,搞通后发现其实这个过程非常简单和简洁,觉得有必要拿出来分享一下。 Spring Boot2.x 不再使用…

    Linux 2023年5月28日
    0116
  • Linux 常用目录管理命令

    cp:复制文件或目录,直接复制,如,cp /root/install.sh /home cp -a:相当於 -pdr 的意思,至於 pdr 请参考下列说明;(常用),如 cp -a…

    Linux 2023年6月14日
    0108
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球