private async Task <int> FillUserMenuItems(UserIdentifier user, IList <MenuItemDefinition> menuItemDefinitions,
                                                   IList <UserMenuItem> 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 (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 &&
                        (StudioXSession.MultiTenancySide == MultiTenancySides.Tenant || (user != null && user.TenantId != null)) &&
                        !(await menuItemDefinition.FeatureDependency.IsSatisfiedAsync(featureDependencyContext)))
                    {
                        continue;
                    }

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

            return(addedMenuItemCount);
        }
Example #2
0
        private async Task CreateChild(UserIdentifier user, UserMenuItem currentUserMenuIte, IEnumerable <MenuItem> childs)
        {
            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 (MenuItem menuItem in childs)
                {
                    MenuItemDefinition menuItemDefinition = new MenuItemDefinition(
                        menuItem.Name,
                        L(menuItem.Name),
                        url: menuItem.Route,
                        icon: menuItem.Icon,
                        target: "tab_" + menuItem.Id,
                        requiredPermissionName: menuItem.PermissionName,
                        order: menuItem.Sort
                        );
                    if (menuItemDefinition.RequiresAuthentication && user == null)
                    {
                        continue;
                    }
                    if (!string.IsNullOrEmpty(menuItemDefinition.RequiredPermissionName))
                    {
                        var permissionDependency = new SimplePermissionDependency(menuItemDefinition.RequiredPermissionName);
                        try
                        {
                            if (user == null ||
                                !(await permissionDependency.IsSatisfiedAsync(permissionDependencyContext)))
                            {
                                continue;
                            }
                        }
                        catch (Exception e)
                        {
                            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;
                    }
                    currentUserMenuIte.Items.Add(new UserMenuItem(menuItemDefinition, _localizationContext));
                }
            }

            //foreach (MenuItem menuItem in childs)
            //{
            //    MenuItemDefinition menuItemDefinition = new MenuItemDefinition(
            //        menuItem.Name,
            //        L(menuItem.Name),
            //        url: menuItem.Route,
            //        icon: menuItem.Icon,
            //        target: "tab_" + menuItem.Id,
            //        requiredPermissionName: menuItem.PermissionName,
            //        order: menuItem.Sort
            //    );
            //    var permissionDependency = new SimplePermissionDependency(menuItemDefinition.RequiredPermissionName);
            //    if (user == null || !(await permissionDependency.IsSatisfiedAsync(permissionDependencyContext)))
            //    {
            //        continue;
            //    }
            //    currentUserMenuIte.Items.Add(new UserMenuItem(menuItemDefinition, _localizationContext));
            //}
        }