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

2021-02-07 03:16

阅读:522

标签:reac   throw   ssi   启用   ilo   alt   退出   地方   ref   

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

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

技术图片

这个/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

 

技术图片

 

 

 

 好了,现在基本算是找到菜单和权限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 IDictionarystring, 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 Dictionarystring, 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 Dictionarystring, 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项目中去全局搜索一下

技术图片

 

 

看来是同一个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,这样便于区分和查询

 

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

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

标签:reac   throw   ssi   启用   ilo   alt   退出   地方   ref   

原文地址:https://www.cnblogs.com/wangpengzong/p/13089690.html


评论


亲,登录后才可以留言!