//public virtual async Task<IList<CustomUserMenuItem>> LoadUserMenu(Abp.UserIdentifier userIdentifier)
        //{
        //    var menus = LoadSettingMenus(userIdentifier.UserId);
        //    var userMenus = new List<CustomUserMenuItem>();

        //    await FillUserMenuItems(userIdentifier, menus, userMenus);

        //    return userMenus;
        //}

        public async Task <int> FillUserMenuItems(UserIdentifier user, IList <MenuItemDefinition> menuItemDefinitions, IList <CustomUserMenuItem> userMenuItems)
        {
            //TODO: Can be optimized by re-using FeatureDependencyContext.

            var addedMenuItemCount = 0;

            using (var scope = _iocResolver.CreateScope())
            {
                var permissionDependencyContext = scope.Resolve <PermissionDependencyContext>();
                permissionDependencyContext.User = user;

                var featureDependencyContext = scope.Resolve <FeatureDependencyContext>();
                featureDependencyContext.TenantId = user == null ? null : user.TenantId;

                foreach (var menuItemDefinition in menuItemDefinitions)
                {
                    if (user == null)
                    {
                        continue;
                    }
                    if (menuItemDefinition.IsLeaf)
                    {
                        var permissionName       = $"Menu.{menuItemDefinition.Name}";
                        var permissionDependency = new SimplePermissionDependency(permissionName);
                        if (!(await permissionDependency.IsSatisfiedAsync(permissionDependencyContext)))
                        {
                            continue;
                        }
                    }
                    //if (menuItemDefinition.RequiresAuthentication && user == null)
                    //{
                    //    continue;
                    //}

                    //if (!string.IsNullOrEmpty(menuItemDefinition.RequiredPermissionName))
                    //{
                    //    var permissionDependency = new SimplePermissionDependency(menuItemDefinition.RequiredPermissionName);
                    //    if (user == null || !(await permissionDependency.IsSatisfiedAsync(permissionDependencyContext)))
                    //    {
                    //        continue;
                    //    }
                    //}

                    //if (menuItemDefinition.PermissionDependency != null &&
                    //    (user == null || !(await menuItemDefinition.PermissionDependency.IsSatisfiedAsync(permissionDependencyContext))))
                    //{
                    //    continue;
                    //}

                    //if (menuItemDefinition.FeatureDependency != null &&
                    //    (AbpSession.MultiTenancySide == MultiTenancySides.Tenant || (user != null && user.TenantId != null)) &&
                    //    !(await menuItemDefinition.FeatureDependency.IsSatisfiedAsync(featureDependencyContext)))
                    //{
                    //    continue;
                    //}

                    var userMenuItem = new CustomUserMenuItem(menuItemDefinition, _localizationContext);
                    if (menuItemDefinition.IsLeaf || (await FillUserMenuItems(user, menuItemDefinition.Items, userMenuItem.Items)) > 0)
                    {
                        userMenuItems.Add(userMenuItem);
                        ++addedMenuItemCount;
                    }
                }
            }

            return(addedMenuItemCount);
        }
Beispiel #2
0
        //public virtual async Task<IList<CustomUserMenuItem>> LoadUserMenu(Abp.UserIdentifier userIdentifier)
        //{
        //    var menus = LoadSettingMenus(userIdentifier.UserId);
        //    var userMenus = new List<CustomUserMenuItem>();

        //    await FillUserMenuItems(userIdentifier, menus, userMenus);

        //    return userMenus;
        //}

        public async Task <int> FillUserMenuItems(UserIdentifier user, IList <MenuItemDefinition> menuItemDefinitions, IList <CustomUserMenuItem> userMenuItems, bool filterUserPermission = true)
        {
            //TODO: Can be optimized by re-using FeatureDependencyContext.

            var addedMenuItemCount = 0;

            using (var scope = _iocResolver.CreateScope())
            {
                var permissionDependencyContext = scope.Resolve <PermissionDependencyContext>();
                permissionDependencyContext.User = user;

                var featureDependencyContext = scope.Resolve <FeatureDependencyContext>();
                featureDependencyContext.TenantId = user == null ? null : user.TenantId;

                foreach (var menuItemDefinition in menuItemDefinitions.OrderBy(o => o.Order))
                {
                    if (user == null)
                    {
                        continue;
                    }
                    //判断特性
                    if (AbpSession.MultiTenancySide == MultiTenancySides.Tenant && menuItemDefinition.FeatureDependency != null && !await menuItemDefinition.FeatureDependency.IsSatisfiedAsync(featureDependencyContext))
                    {
                        continue;
                    }
                    //只有叶节点需要权限验证
                    if (menuItemDefinition.IsLeaf)
                    {
                        //判断是Host还是Tenant
                        if (AbpSession.MultiTenancySide.HasFlag(MultiTenancySides.Host) && menuItemDefinition.Name.Contains("Tenancy"))
                        {
                            continue;
                        }
                        if (AbpSession.MultiTenancySide.HasFlag(MultiTenancySides.Tenant) && menuItemDefinition.Name.Contains("Host"))
                        {
                            continue;
                        }
                        //判断权限modi 20190530  Host登录不判断权限
                        if (!AbpSession.MultiTenancySide.HasFlag(MultiTenancySides.Host) && filterUserPermission && !(await menuItemDefinition.PermissionDependency.IsSatisfiedAsync(permissionDependencyContext)))
                        {
                            continue;
                        }

                        //todo:此处需要优化
                        //var permissionName = $"Menu.{menuItemDefinition.Name}";
                        //var permissionDependency = new SimplePermissionDependency(permissionName);
                        //if (!(await permissionDependency.IsSatisfiedAsync(permissionDependencyContext)))
                        //{
                        //    continue;
                        //}
                    }
                    //if (menuItemDefinition.RequiresAuthentication && user == null)
                    //{
                    //    continue;
                    //}

                    //if (!string.IsNullOrEmpty(menuItemDefinition.RequiredPermissionName))
                    //{
                    //    var permissionDependency = new SimplePermissionDependency(menuItemDefinition.RequiredPermissionName);
                    //    if (user == null || !(await permissionDependency.IsSatisfiedAsync(permissionDependencyContext)))
                    //    {
                    //        continue;
                    //    }
                    //}

                    //if (menuItemDefinition.PermissionDependency != null &&
                    //    (user == null || !(await menuItemDefinition.PermissionDependency.IsSatisfiedAsync(permissionDependencyContext))))
                    //{
                    //    continue;
                    //}

                    //if (menuItemDefinition.FeatureDependency != null &&
                    //    (AbpSession.MultiTenancySide == MultiTenancySides.Tenant || (user != null && user.TenantId != null)) &&
                    //    !(await menuItemDefinition.FeatureDependency.IsSatisfiedAsync(featureDependencyContext)))
                    //{
                    //    continue;
                    //}

                    var userMenuItem = new CustomUserMenuItem(menuItemDefinition, _localizationContext);
                    if (menuItemDefinition.IsLeaf || (await FillUserMenuItems(user, menuItemDefinition.Items, userMenuItem.Items, filterUserPermission)) > 0)
                    {
                        userMenuItems.Add(userMenuItem);
                        ++addedMenuItemCount;
                    }
                }
            }

            return(addedMenuItemCount);
        }