コード例 #1
0
 public async Task ApplyRulesAfterSave(Tenant tenant, TenantModel input)
 {
     using (var scope = _iocResolver.CreateScope())
     {
         var assemblies      = _assembly.GetAssembliesByType(typeof(IRuleAfterSaveTenant));
         var allScopeResolve = assemblies.Select(x => (IRuleAfterSaveTenant)scope.Resolve(x));
         var allTask         = allScopeResolve
                               .Select(x => x.ApplyRules(tenant, input))
                               .ToList();
         await Task.WhenAll(allTask);
     }
 }
コード例 #2
0
 /// <summary>
 /// This method starts a scope to resolve and release all objects automatically.
 /// You can use the <c>scope</c> in <see cref="action"/>.
 /// </summary>
 /// <param name="iocResolver">IIocResolver object</param>
 /// <param name="action">An action that can use the resolved object</param>
 public static void UsingScope(this IIocResolver iocResolver, Action <IScopedIocResolver> action)
 {
     using (var scope = iocResolver.CreateScope())
     {
         action(scope);
     }
 }
コード例 #3
0
        protected virtual async Task <AbpUserSettingConfigDto> GetUserSettingConfig()
        {
            var config = new AbpUserSettingConfigDto
            {
                Values = new Dictionary <string, string>()
            };

            var settingDefinitions = SettingDefinitionManager
                                     .GetAllSettingDefinitions();

            using (var scope = _iocResolver.CreateScope())
            {
                foreach (var settingDefinition in settingDefinitions)
                {
                    if (!await settingDefinition.ClientVisibilityProvider.CheckVisible(scope))
                    {
                        continue;
                    }

                    var settingValue = await SettingManager.GetSettingValueAsync(settingDefinition.Name);

                    config.Values.Add(settingDefinition.Name, settingValue);
                }
            }

            return(config);
        }
コード例 #4
0
        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);
        }
コード例 #5
0
        private static int?GetCurrentTenantIdOrNull(IIocResolver iocResolver)
        {
            using (var scope = iocResolver.CreateScope())
            {
                var currentUnitOfWorkProvider = scope.Resolve <ICurrentUnitOfWorkProvider>();
                if (currentUnitOfWorkProvider?.Current != null)
                {
                    return(currentUnitOfWorkProvider.Current.GetTenantId());
                }
            }

            return(iocResolver.Resolve <IAbpSession>().TenantId);
        }
        private static long? GetCurrentBranchIdOrNull(IIocResolver iocResolver)
        {
            using (var scope = iocResolver.CreateScope())
            {
                var currentUnitOfWorkProvider = scope.Resolve<ICurrentUnitOfWorkProvider>();

                if (currentUnitOfWorkProvider?.Current != null)
                {
                    return currentUnitOfWorkProvider.Current.GetBranchId();
                }

                return iocResolver.Resolve<IAbpSession>().BranchId;
            }
        }
コード例 #7
0
        public async Task <string> GetScriptAsync()
        {
            var script = new StringBuilder();

            script.AppendLine("(function(){");
            script.AppendLine("    CodeZero.setting = CodeZero.setting || {};");
            script.AppendLine("    CodeZero.setting.values = {");

            var settingDefinitions = _settingDefinitionManager
                                     .GetAllSettingDefinitions();

            var added = 0;

            using (var scope = _iocResolver.CreateScope())
            {
                foreach (var settingDefinition in settingDefinitions)
                {
                    if (!await settingDefinition.ClientVisibilityProvider.CheckVisible(scope))
                    {
                        continue;
                    }

                    if (added > 0)
                    {
                        script.AppendLine(",");
                    }
                    else
                    {
                        script.AppendLine();
                    }

                    var settingValue = await _settingManager.GetSettingValueAsync(settingDefinition.Name);

                    script.Append("        '" +
                                  settingDefinition.Name.Replace("'", @"\'") + "': " +
                                  (settingValue == null ? "null" : "'" + HttpEncode.JavaScriptStringEncode(settingValue) + "'"));

                    ++added;
                }
            }

            script.AppendLine();
            script.AppendLine("    };");

            script.AppendLine();
            script.Append("})();");

            return(script.ToString());
        }
        private static Expression <Func <TEntity, bool> > GetFilterExpressionOrNull <TEntity, TPrimaryKey>(IIocResolver iocResolver)
            where TEntity : Entity <TPrimaryKey>
        {
            Expression <Func <TEntity, bool> > expression = null;

            using (var scope = iocResolver.CreateScope())
            {
                var currentUnitOfWorkProvider = scope.Resolve <ICurrentUnitOfWorkProvider>();

                if (typeof(ISoftDelete).IsAssignableFrom(typeof(TEntity)))
                {
                    var isSoftDeleteFilterEnabled = currentUnitOfWorkProvider.Current?.IsFilterEnabled(AbpDataFilters.SoftDelete) == true;
                    if (isSoftDeleteFilterEnabled)
                    {
                        Expression <Func <TEntity, bool> > softDeleteFilter = e => !((ISoftDelete)e).IsDeleted;
                        expression = softDeleteFilter;
                    }
                }

                if (typeof(IMayHaveTenant).IsAssignableFrom(typeof(TEntity)))
                {
                    var isMayHaveTenantFilterEnabled = currentUnitOfWorkProvider.Current?.IsFilterEnabled(AbpDataFilters.MayHaveTenant) == true;
                    var currentTenantId = GetCurrentTenantIdOrNull(iocResolver);

                    if (isMayHaveTenantFilterEnabled)
                    {
                        Expression <Func <TEntity, bool> > mayHaveTenantFilter = e => ((IMayHaveTenant)e).TenantId == currentTenantId;
                        expression = expression == null ? mayHaveTenantFilter : ExpressionCombiner.Combine(expression, mayHaveTenantFilter);
                    }
                }

                if (typeof(IMustHaveTenant).IsAssignableFrom(typeof(TEntity)))
                {
                    var isMustHaveTenantFilterEnabled = currentUnitOfWorkProvider.Current?.IsFilterEnabled(AbpDataFilters.MustHaveTenant) == true;
                    var currentTenantId = GetCurrentTenantIdOrNull(iocResolver);

                    if (isMustHaveTenantFilterEnabled)
                    {
                        Expression <Func <TEntity, bool> > mustHaveTenantFilter = e => ((IMustHaveTenant)e).TenantId == currentTenantId;
                        expression = expression == null ? mustHaveTenantFilter : ExpressionCombiner.Combine(expression, mustHaveTenantFilter);
                    }
                }
            }

            return(expression);
        }
コード例 #9
0
        /// <inheritdoc cref="IUserConfigAppService.GetUserSettings"/>
        public async Task <UserSettingsConfigDto> GetUserSettings()
        {
            var config = new UserSettingsConfigDto
            {
                Values = new Dictionary <string, string>(),
            };

            var settings = await SettingManager.GetAllSettingValuesAsync(SettingScopes.All);

            using var scope = _iocResolver.CreateScope();
            foreach (var settingValue in settings)
            {
                if (!await _settingDefinitionManager.GetSettingDefinition(settingValue.Name)
                    .ClientVisibilityProvider
                    .CheckVisible(scope))
                {
                    continue;
                }

                config.Values.Add(settingValue.Name, settingValue.Value);
            }

            return(config);
        }
コード例 #10
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);
        }
コード例 #11
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));
            //}
        }