/// <summary>
        /// 根据程序集列表,反射生成系统功能列表
        /// </summary>
        /// <param name="allAsses">程序集列表</param>
        public void InitFunctions(IList <Assembly> allAsses)
        {
            var selfAss = this.GetType().Assembly;

            if (!allAsses.Any(ass => ass.FullName == selfAss.FullName))
            {
                allAsses.Insert(0, selfAss);
            }

            foreach (var ass in allAsses)
            {
                var         menu    = GetMenu(ass);
                AppFunction assFunc = new AppFunction()
                {
                    Name           = menu.Name,
                    ControllerName = ass.GetName().Name,
                    Ord            = menu.Ord,
                    Id             = menu.Id,
                    Visible        = menu.Visible,
                    Location       = GetAssUrl(ass),
                    AuthType       = menu.AuthType
                };
                var root = mFuncTrees.Add(assFunc);
                GetFunctions(root, ass);
                //对该程序集,如果一个功能都没有被加入
                if (root.Length == 0)
                {
                    mFuncTrees.Remove(root);
                }
            }

            mFunctionMgr.Clear();
            AddToMgr(mFuncTrees);
        }
        /// <summary>
        /// 根据程序集反射出所有的需权限的控制器和功能
        /// </summary>
        /// <param name="rootTree">程序集根结点</param>
        /// <param name="assembly">程序集</param>
        void GetFunctions(JTree <AppFunction> rootTree, Assembly assembly)
        {
            var functionInfos =
                (from t in assembly.GetTypes()
                 where !t.IsAbstract && t.Name.EndsWith("Controller")
                 from m in t.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)
                 where !m.IsSpecialName
                 select new MvcFuncInfo()
            {
                Controller = t,
                Action = m,
                ControllerName = t.Name.Substring(0, t.Name.Length - 10),
                ActionName = m.Name,
                Parameters = m.GetParameters()
            }).ToList();

            List <AppFunction> functionList = new List <AppFunction>();

            foreach (var groupInfo in functionInfos.GroupBy(f => f.Controller))
            {
                var menu = GetMenu(groupInfo.Key, rootTree.Node.AuthType, rootTree.Node.LogType);

                //生成Controller作为二级的功能项
                AppFunction parent = new AppFunction()
                {
                    Name           = menu.Name,
                    Location       = menu.AreaName + "/" + groupInfo.First().ControllerName,
                    ControllerName = groupInfo.Key.FullName,
                    Visible        = menu.Visible,
                    AuthType       = menu.AuthType,
                    LogType        = menu.LogType,
                    Ord            = menu.Ord,
                };

                if (parent.AuthType == JAuthType.Ignore)
                {
                    continue;
                }

                parent.LocationSamples = parent.Location;
                ReDefineId(menu, parent);
                var parentTree = rootTree.Add(parent);

                //生成Controller下面的Action作为三级的功能项
                foreach (var info in groupInfo)
                {
                    info.AuthType = parent.AuthType;
                    //提取Attributes
                    GetAttributes(info);

                    menu = GetMenu(info);
                    AppFunction child = new AppFunction()
                    {
                        Name     = menu.Name,
                        Location = menu.AreaName + info.ToString(),
                        ParentId = parent.Id,
                        Visible  = menu.Visible,

                        Method    = info.HasPost ? WebMethod.POST : WebMethod.GET,
                        Ord       = menu.Ord,
                        AuthType  = menu.AuthType,
                        Arguments = info.Parameters
                                    .ToDictionary(p => p.Name, p => p.ParameterType.Name.StartsWith("Int") ? @"\d+" : null),
                        LogType        = menu.LogType,
                        ControllerName = parent.ControllerName,
                        ActionName     = info.ActionName
                    };


                    if (child.AuthType == JAuthType.Ignore)
                    {
                        continue;
                    }

                    ReDefineId(menu, child);

                    if (child.Method == WebMethod.GET)
                    {
                        MakeRegTail(child, info);
                    }
                    else
                    {
                        MakePostRegTail(child, info);
                    }
                    parentTree.Add(child);
                }
            }
        }
Esempio n. 3
0
        object Init(Type catType)
        {
            int?   tParentId = null;
            object catDef;

            catDef = Activator.CreateInstance(catType);

            AssignNameValue(catDef);

            var parentTree = _catalogTree.FindTree(n => n.GetType() == catType.BaseType);

            //如果在树中找不到父结点,则它为根结点的一级子结点
            if (parentTree == null)
            {
                var tree = _catalogTree.Add(catDef);

                //判断有无其它先期加入的结点应该是它的子结点(根据类继承关系来判断)
                //判断过于复杂,先不做,这就要求初始化顺序一定要按父子关系来
                //foreach (object node in _catalogTree)
                //{
                //    if (node.GetType().IsAssignableFrom(catType))
                //    {
                //        tree.Add(node);
                //        var id = (int)RefHelper.GetValue(node, "Id");

                //        var nodeCat = SiteManager.Catalog.GetById(id);

                //    }
                //}
            }
            else
            {
                tParentId = RefHelper.GetValue(parentTree.Node, "Id") as int?;
                parentTree.Add(catDef);
            }


            var catalog = SiteManager.Catalog.GetAll().FirstOrDefault(cat => cat.ParentId == tParentId && cat.Name == (string)RefHelper.GetValue(catDef, "Name"));

            if (catalog == null)
            {
                catalog = new Base_Catalog()
                {
                    Name       = (string)RefHelper.GetValue(catDef, "Name"),
                    ParentId   = tParentId,
                    State      = ArticleState.Static + ArticleState.Published,
                    OwnerType  = CatalogOwnerType.System,
                    EditTime   = DateTime.Now,
                    CreateTime = DateTime.Now
                };

                ChangeExts(catalog, catType);
                SiteManager.Catalog.Add(catalog);
            }
            else if (ChangeExts(catalog, catType))
            {
                SiteManager.Catalog.Change(catalog);
            }

            PropertyInfo pi = catType.GetProperty("Id", BindingFlags.Public | BindingFlags.Instance);

            pi.SetValue(catDef, catalog.Id, null);

            return(catDef);
        }