示例#1
0
        /// <summary>
        /// 拼接本表的数据权限过滤
        /// </summary>
        /// <typeparam name="T">数据类</typeparam>
        /// <param name="query">源query</param>
        /// <param name="dps">数据权限列表</param>
        /// <returns>拼接好where条件的query</returns>
        private static IQueryable <T> AppendSelfDPWhere <T>(IQueryable <T> query, List <DataPrivilege> dps) where T : TopBasePoco
        {
            var dpsSetting           = GlobalServices.GetService <Configs>().DataPrivilegeSettings;
            ParameterExpression pe   = Expression.Parameter(typeof(T));
            Expression          peid = Expression.PropertyOrField(pe, "Id");

            //循环数据权限,加入到where条件中,达到自动过滤的效果
            if (dpsSetting?.Where(x => x.ModelName == query.ElementType.Name).SingleOrDefault() != null)
            {
                //如果dps参数是空,则生成 1!=1 这种错误的表达式,这样就查不到任何数据了
                if (dps == null)
                {
                    query = query.Where(Expression.Lambda <Func <T, bool> >(Expression.NotEqual(Expression.Constant(1), Expression.Constant(1)), pe));
                }
                else
                {
                    //在dps中找到和baseQuery源数据表名一样的关联id
                    var ids = dps.Where(x => x.TableName == query.ElementType.Name).Select(x => x.RelateId).ToList();
                    if (ids == null || ids.Count() == 0)
                    {
                        query = query.Where(Expression.Lambda <Func <T, bool> >(Expression.NotEqual(Expression.Constant(1), Expression.Constant(1)), pe));
                    }
                    else
                    {
                        if (!ids.Contains(null))
                        {
                            List <Guid> finalIds = new List <Guid>();
                            ids.ForEach(x => finalIds.Add(x.Value));
                            Expression dpleft      = Expression.Constant(finalIds, typeof(List <Guid>));
                            Expression dpcondition = Expression.Call(dpleft, "Contains", new Type[] { }, peid);
                            query = query.Where(Expression.Lambda <Func <T, bool> >(dpcondition, pe));
                        }
                    }
                }
            }
            return(query);
        }
示例#2
0
        public void when_retrieving_exports_then_reports_duplicate_services(Type serviceType)
        {
            var componentModel = GlobalServices.GetService <SComponentModel, IComponentModel>();
            var contractName   = AttributedModelServices.GetContractName(serviceType);
            var components     = componentModel.DefaultExportProvider
                                 .GetExports <object, IDictionary <string, object> >(contractName)
                                 .ToArray();

            if (components.Length != 1)
            {
                var info = new CompositionInfo(componentModel.DefaultCatalog, componentModel.DefaultExportProvider);
                var log  = Path.GetTempFileName();
                using (var writer = new StreamWriter(log))
                {
                    CompositionInfoTextFormatter.Write(info, writer);
                    writer.Flush();
                }

                output.WriteLine(log);
                // Process.Start(new ProcessStartInfo("notepad", log) { UseShellExecute = true });

                Assert.False(true, $"Expected only one component of {serviceType.Name}. Composition log at {log}");
            }
        }
        public void Load_shell_service()
        {
            var shellService = GlobalServices.GetService <SVsShell>() as IVsShell;

            Assert.NotNull(shellService);
        }
示例#4
0
        /// <summary>
        /// 为查询语句添加关联表的权限过滤
        /// </summary>
        /// <typeparam name="T">源数据类</typeparam>
        /// <param name="baseQuery">源Query</param>
        /// <param name="dps">数据权限</param>
        /// <param name="tableName">关联数据权限的表名,如果关联外键为自身,则参数第一个为自身</param>
        /// <param name="IdFields">关联表外键</param>
        /// <returns>修改后的查询语句</returns>
        public static IQueryable <T> DPWhere <T>(this IQueryable <T> baseQuery, List <DataPrivilege> dps, List <string> tableName, params Expression <Func <T, object> >[] IdFields)
        {
            // var dpsSetting = BaseVM.AllDPS;
            ParameterExpression pe       = Expression.Parameter(typeof(T));
            Expression          left1    = Expression.Constant(1);
            Expression          right1   = Expression.Constant(1);
            Expression          trueExp  = Expression.Equal(left1, right1);
            Expression          falseExp = Expression.NotEqual(left1, right1);
            Expression          finalExp = null;

            //循环所有关联外键
            foreach (var IdField in IdFields)
            {
                Expression exp = trueExp;
                //将外键Id用.分割,循环生成指向最终id的表达式,比如x=> x.a.b.Id
                var        fullname = IdField.GetPropertyName();
                string[]   splits   = fullname.Split('.');
                Expression peid     = Expression.PropertyOrField(pe, splits[0]);
                for (int i = 1; i < splits.Length; i++)
                {
                    peid = Expression.PropertyOrField(peid, splits[i]);
                }
                //如果dps为空,则拼接一个返回假的表达式,这样就查询不出任何数据
                if (dps == null)
                {
                    exp = falseExp;
                }
                else
                {
                    var fieldName = IdField.GetPropertyName(false);
                    //如果外键名称不是‘id’,则根据model层的命名规则,它应该是xxxId,所以抹掉最后的 Id 应该是关联的类名
                    if (fieldName.ToLower() != "id")
                    {
                        fieldName = fieldName.Remove(fieldName.Length - 2);
                        //var IsTableName = tableName?.Where(x => x == fieldName).FirstOrDefault();
                        var IsTableName = tableName?.Where(x => x.ToLower().Contains(fieldName.ToLower())).FirstOrDefault();
                        if (string.IsNullOrEmpty(IsTableName))
                        {
                            continue;
                        }
                        fieldName = IsTableName;
                        //typename = PropertyHelper.GetPropertyInfo(IdField).DeclaringType.GetProperty(fieldName).PropertyType.Name;
                    }
                    //如果是Id,则本身就是关联的类
                    else
                    {
                        fieldName = tableName[0];
                    }
                    var dpsSetting = GlobalServices.GetService <Configs>().DataPrivilegeSettings;

                    //循环系统设定的数据权限,如果没有和关联类一样的表,则跳过
                    if (dpsSetting.Where(x => x.ModelName == fieldName).SingleOrDefault() == null)
                    {
                        continue;
                    }
                    //获取dps中关联到关联类的id列表
                    var ids = dps.Where(x => x.TableName == fieldName).Select(x => x.RelateId).ToList();
                    //如果没有关联的id,则拼接一个返回假的where,是语句查询不到任何数据
                    if (ids == null || ids.Count() == 0)
                    {
                        if (peid.Type == typeof(Guid))
                        {
                            exp = Expression.Equal(peid, Expression.Constant(Guid.NewGuid()));
                        }
                        else
                        {
                            exp = Expression.Equal(peid, Expression.Constant(null));
                        }
                    }
                    //如果有关联 Id
                    else
                    {
                        //如果关联 Id 不包含null,则生成类似 x=> ids.Contains(x.a.b.Id) 这种条件
                        //如果关联 Id 包括null,则代表可以访问所有数据,就不需要再拼接where条件了
                        if (!ids.Contains(null))
                        {
                            exp = ids.GetContainIdExpression <T>(peid).Body;
                        }
                    }
                }
                //把所有where里的条件用And拼接在一起
                if (finalExp == null)
                {
                    finalExp = exp;
                }
                else
                {
                    finalExp = Expression.Or(finalExp, exp);
                }
            }
            //如果没有进行任何修改,则还返回baseQuery
            if (finalExp == null)
            {
                return(baseQuery);
            }
            else
            {
                //返回加入了where条件之后的baseQuery
                var query = baseQuery.Where(Expression.Lambda <Func <T, bool> >(finalExp, pe));
                return(query);
            }
        }
示例#5
0
 ICustomSolutionExplorerNodeFactory GetFactory()
 {
     return(GlobalServices.GetService <SComponentModel, IComponentModel>().DefaultExportProvider
            .GetExportedValues <ICustomSolutionExplorerNodeFactory>(ContractNames.FallbackNodeFactory)
            .First(factory => factory.GetType().Name == typeof(TNode).Name));
 }
示例#6
0
        /// <summary>
        /// Navigates to the location of the error in the source code
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="arguments"></param>
        private void NavigateTo(object sender, EventArgs arguments)
        {
            Microsoft.VisualStudio.Shell.Task task = sender as Microsoft.VisualStudio.Shell.Task;
            if (task == null)
            {
                throw new ArgumentException("Sender is not a Microsoft.VisualStudio.Shell.Task", "sender");
            }

            // Get the doc data for the task's document
            if (String.IsNullOrEmpty(task.Document))
            {
                return;
            }

            IVsUIShellOpenDocument openDoc = services.GetService <IVsUIShellOpenDocument>();

            if (openDoc == null)
            {
                return;
            }

            IVsWindowFrame frame;

            Microsoft.VisualStudio.OLE.Interop.IServiceProvider sp;
            IVsUIHierarchy hier;
            uint           itemid;
            Guid           logicalView = VSConstants.LOGVIEWID_Code;

            if (Microsoft.VisualStudio.ErrorHandler.Failed(openDoc.OpenDocumentViaProject(task.Document, ref logicalView, out sp, out hier, out itemid, out frame)) || frame == null)
            {
                return;
            }

            object docData;

            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(frame.GetProperty((int)__VSFPROPID.VSFPROPID_DocData, out docData));

            // Get the VsTextBuffer
            VsTextBuffer buffer = docData as VsTextBuffer;

            if (buffer == null)
            {
                IVsTextBufferProvider bufferProvider = docData as IVsTextBufferProvider;
                if (bufferProvider != null)
                {
                    IVsTextLines lines;
                    Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(bufferProvider.GetTextBuffer(out lines));
                    buffer = lines as VsTextBuffer;
                    System.Diagnostics.Debug.Assert(buffer != null, "IVsTextLines does not implement IVsTextBuffer");
                    if (buffer == null)
                    {
                        return;
                    }
                }
            }

            // Finally, perform the navigation.
            IVsTextManager mgr = services.GetService <IVsTextManager>(typeof(VsTextManagerClass));

            if (mgr == null)
            {
                return;
            }

            Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(mgr.NavigateToLineAndColumn(buffer, ref logicalView, task.Line, task.Column, task.Line, task.Column));
        }
示例#7
0
        public SolutionFixture(string solutionFile, bool useCopy = false)
        {
            if (!Path.IsPathRooted(solutionFile))
            {
                var rootedFile = Path.Combine(Path.GetDirectoryName(GetType().Assembly.ManifestModule.FullyQualifiedName), solutionFile);
                if (!File.Exists(rootedFile))
                {
                    rootedFile = Path.Combine(baseDirectory, solutionFile);
                    var currentDir = new DirectoryInfo(Directory.GetCurrentDirectory());
                    while (!File.Exists(rootedFile) && currentDir != null)
                    {
                        rootedFile = Path.Combine(currentDir.FullName, solutionFile);
                        currentDir = currentDir.Parent;
                    }
                }

                solutionFile = rootedFile;
            }

            this.solutionFile = solutionFile;

            if (!File.Exists(solutionFile))
            {
                throw new FileNotFoundException("Could not find solution file " + solutionFile, solutionFile);
            }

            if (useCopy)
            {
                tempDir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                if (!Directory.Exists(tempDir))
                {
                    Directory.CreateDirectory(tempDir);
                }

                DirectoryCopy(Path.GetDirectoryName(solutionFile), tempDir, true);
                solutionFile = Path.Combine(tempDir, Path.GetFileName(solutionFile));
            }

            solution = new Lazy <ISolutionNode>(() =>
            {
                try
                {
                    var dte = GlobalServices.GetService <DTE>();
                    if (!dte.Solution.IsOpen || !dte.Solution.FullName.Equals(this.solutionFile, StringComparison.OrdinalIgnoreCase))
                    {
                        // Ensure no .suo is loaded, since that would dirty the state across runs.
                        var suoFile = Path.ChangeExtension(this.solutionFile, ".suo");
                        if (File.Exists(suoFile))
                        {
                            Try(() => File.Delete(suoFile));
                        }

                        var sdfFile = Path.ChangeExtension(this.solutionFile, ".sdf");
                        if (File.Exists(sdfFile))
                        {
                            Try(() => File.Delete(sdfFile));
                        }

                        var vsDir = Path.Combine(Path.GetDirectoryName(this.solutionFile), ".vs");
                        if (Directory.Exists(vsDir))
                        {
                            Try(() => Directory.Delete(vsDir, true));
                        }

                        dte.Solution.Open(this.solutionFile);
                        GlobalServices.GetService <SVsSolution, IVsSolution4>()
                        .EnsureSolutionIsLoaded((uint)(__VSBSLFLAGS.VSBSLFLAGS_LoadAllPendingProjects | __VSBSLFLAGS.VSBSLFLAGS_LoadBuildDependencies));
                    }

                    return(GlobalServices.GetService <SComponentModel, IComponentModel>().GetService <ISolutionExplorer>().Solution);
                }
                catch (Exception ex)
                {
                    throw new ArgumentException("Failed to open and access solution: " + solutionFile, ex);
                }
            });

            // If the collection is being created inside the VS process,
            // instantiate the value right now to cause the solution to open.
            if (GlobalServices.GetService <DTE>() != null)
            {
                Assert.NotNull(solution.Value);
            }
        }
示例#8
0
        private static GlobalData GetGlobalData()
        {
            var gd = new GlobalData();

            //获取所有程序集
            gd.AllAssembly = Utils.GetAllAssembly();
            var admin = GetRuntimeAssembly("WalkingTec.Mvvm.Mvc.Admin");

            if (admin != null && gd.AllAssembly.Contains(admin) == false)
            {
                gd.AllAssembly.Add(admin);
            }
            var mvc = GetRuntimeAssembly("WalkingTec.Mvvm.Mvc");

            if (mvc != null && gd.AllAssembly.Contains(mvc) == false)
            {
                gd.AllAssembly.Add(mvc);
            }
            var core = GetRuntimeAssembly("WalkingTec.Mvvm.Core");

            if (core != null && gd.AllAssembly.Contains(core) == false)
            {
                gd.AllAssembly.Add(core);
            }
            var layui = GetRuntimeAssembly("WalkingTec.Mvvm.TagHelpers.LayUI");

            if (layui != null && gd.AllAssembly.Contains(layui) == false)
            {
                gd.AllAssembly.Add(layui);
            }
            gd.DataContextCI = GetDbContextCI(gd.AllAssembly);
            gd.AllModels     = GetAllModels(gd.DataContextCI);
            var controllers = GetAllControllers(gd.AllAssembly);

            gd.AllAccessUrls = GetAllAccessUrls(controllers);

            gd.SetModuleGetFunc(() =>
            {
                return(GetAllModules(controllers));
            });

            gd.SetMenuGetFunc(() =>
            {
                var menus        = new List <FrameworkMenu>();
                var cache        = GlobalServices.GetService <IDistributedCache>();
                var menuCacheKey = "FFMenus";
                if (cache.TryGetValue(menuCacheKey, out List <FrameworkMenu> rv) == false)
                {
                    var data = GetAllMenus(gd.AllModule, gd.DataContextCI);
                    cache.Add(menuCacheKey, data);
                    menus = data;
                }
                else
                {
                    menus = rv;
                }

                return(menus);
            });
            return(gd);
        }
示例#9
0
        public void when_getting_solution_explorer_then_succeeds()
        {
            var solutionExplorer = GlobalServices.GetService <SComponentModel, IComponentModel>().GetService <ISolutionExplorer>();

            Assert.NotNull(solutionExplorer);
        }
示例#10
0
        public void when_getting_solution_node_then_returns_non_null()
        {
            var solutionExplorer = GlobalServices.GetService <SComponentModel, IComponentModel>().GetService <ISolutionExplorer>();

            Assert.NotNull(solutionExplorer.Solution);
        }