Esempio n. 1
0
        public RoleMapProvider()
        {
            var role = ManageProvider.Get <IRole>();

            EntityType = role.GetType();
            Key        = EntityFactory.CreateOperate(EntityType).Unique?.Name;
        }
Esempio n. 2
0
        /// <summary>注册用户。默认写日志后Insert,注册仅保存基本信息,需要扩展的同学请重载</summary>
        public virtual void Register()
        {
            if (RoleID == 0)
            {
                // 填写角色。最后一个普通角色,如果没有,再管理员角色
                var eop  = ManageProvider.GetFactory <IRole>();
                var list = eop.FindAllWithCache().Cast <IRole>();

                // 找游客组
                var role = list.FirstOrDefault(e => e.Name == "游客");

                // 最后一个非系统角色
                if (role == null)
                {
                    role = list.LastOrDefault(e => !e.IsSystem);
                }
                // 没有非系统角色,随便一个啦
                if (role == null)
                {
                    role = list.LastOrDefault();
                }

                RoleID = role.ID;
            }

            Insert();
        }
Esempio n. 3
0
            /// <summary>扫描命名空间下的控制器并添加为菜单</summary>
            /// <param name="rootName">根菜单名称,所有菜单附属在其下</param>
            /// <param name="asm">要扫描的程序集</param>
            /// <param name="nameSpace">要扫描的命名空间</param>
            /// <returns></returns>
            public virtual IList <IMenu> ScanController(String rootName, Assembly asm, String nameSpace)
            {
                var list = new List <IMenu>();

                // 如果根菜单不存在,则添加
                var root = Root.FindByPath(rootName);

                if (root == null)
                {
                    root = Root.Add(rootName, null, "~/" + rootName);
                    list.Add(root);
                }

                var ms = new List <IMenu>();

                // 遍历该程序集所有类型
                foreach (var type in asm.GetTypes())
                {
                    var name = type.Name;
                    if (!name.EndsWith("Controller"))
                    {
                        continue;
                    }

                    name = name.TrimEnd("Controller");
                    if (type.Namespace != nameSpace)
                    {
                        continue;
                    }

                    var url = root.Url;

                    var node = root;

                    // 添加Controller
                    var controller = node.FindByPath(name);
                    if (controller == null)
                    {
                        url       += "/" + name;
                        controller = FindByUrl(url);
                        if (controller == null)
                        {
                            // DisplayName特性作为中文名
                            controller        = node.Add(name, type.GetDisplayName(), url);
                            controller.Remark = type.GetDescription();

                            list.Add(node);
                        }
                    }

                    // 反射调用控制器的GetActions方法来获取动作
                    var func = type.GetMethodEx("ScanActionMenu");
                    if (func == null)
                    {
                        continue;
                    }

                    //var acts = type.Invoke(func) as MethodInfo[];
                    var acts = func.As <Func <IMenu, IDictionary <MethodInfo, Int32> > >(type.CreateInstance()).Invoke(controller);
                    if (acts == null || acts.Count == 0)
                    {
                        continue;
                    }

                    // 可选权限子项
                    controller.Permissions.Clear();
                    var dic  = new Dictionary <String, Int32>();
                    var mask = 0;

                    // 添加该类型下的所有Action作为可选权限子项
                    foreach (var item in acts)
                    {
                        var method = item.Key;

                        var dn = method.GetDisplayName();
                        if (!dn.IsNullOrEmpty())
                        {
                            dn = dn.Replace("{type}", controller.FriendName);
                        }

                        var pmName = !dn.IsNullOrEmpty() ? dn : method.Name;
                        if (item.Value == 0)
                        {
                            dic.Add(pmName, item.Value);
                        }
                        else
                        {
                            if (item.Value < 0x10)
                            {
                                pmName = ((PermissionFlags)item.Value).GetDescription();
                            }
                            mask |= item.Value;
                            controller.Permissions[item.Value] = pmName;
                        }
                    }

                    // 分配权限位
                    var idx = 0x10;
                    foreach (var item in dic)
                    {
                        while ((mask & idx) != 0)
                        {
                            if (idx >= 0x80)
                            {
                                throw new XException("控制器{0}的Action过多,不够分配权限位", type.Name);
                            }
                            idx <<= 1;
                        }
                        mask |= idx;
                        controller.Permissions[idx] = item.Key;
                    }

                    // 排序
                    ms.Add(controller);

                    //controller.Save();
                }

                // 所有都是新增菜单才排序
                if (ms.All(m => m.Sort == 0))
                {
                    ms = ms.OrderByDescending(m => m.Name).ToList();
                }

                for (int i = 0; i < ms.Count; i++)
                {
                    ms[i].Sort = i;
                    ms[i].Save();
                }

                // 如果新增了菜单,需要检查权限
                if (list.Count > 0)
                {
                    Task.Run(() =>
                    {
                        var eop = ManageProvider.GetFactory <IRole>();
                        eop.EntityType.Invoke("CheckRole");
                    });
                }

                return(list);
            }
Esempio n. 4
0
            /// <summary>扫描命名空间下的控制器并添加为菜单</summary>
            /// <param name="rootName">根菜单名称,所有菜单附属在其下</param>
            /// <param name="asm">要扫描的程序集</param>
            /// <param name="nameSpace">要扫描的命名空间</param>
            /// <returns></returns>
            public virtual IList <IMenu> ScanController(String rootName, Assembly asm, String nameSpace)
            {
                var list = new List <IMenu>();
                var mf   = this as IMenuFactory;

                // 所有控制器
                var types = asm.GetTypes().Where(e => e.Name.EndsWith("Controller") && e.Namespace == nameSpace).ToList();

                if (types.Count == 0)
                {
                    return(list);
                }

                // 如果根菜单不存在,则添加
                var r    = Root as IMenu;
                var root = mf.FindByFullName(nameSpace);

                if (root == null)
                {
                    root = r.FindByPath(rootName);
                }
                //if (root == null) root = r.Childs.FirstOrDefault(e => e.Name.EqualIgnoreCase(rootName));
                //if (root == null) root = r.Childs.FirstOrDefault(e => e.Url.EqualIgnoreCase("~/" + rootName));
                if (root == null)
                {
                    root = r.Add(rootName, null, nameSpace, "~/" + rootName);
                    list.Add(root);
                }
                if (root.FullName != nameSpace)
                {
                    root.FullName = nameSpace;
                    (root as IEntity).Save();
                }

                var ms = new List <IMenu>();

                // 遍历该程序集所有类型
                foreach (var type in types)
                {
                    var name = type.Name.TrimEnd("Controller");
                    var url  = root.Url;
                    var node = root;

                    // 添加Controller
                    var controller = node.FindByPath(name);
                    if (controller == null)
                    {
                        url       += "/" + name;
                        controller = FindByUrl(url);
                        if (controller == null)
                        {
                            // DisplayName特性作为中文名
                            controller = node.Add(name, type.GetDisplayName(), type.FullName, url);

                            //list.Add(controller);
                        }
                    }
                    if (controller.FullName.IsNullOrEmpty())
                    {
                        controller.FullName = type.FullName;
                    }
                    if (controller.Remark.IsNullOrEmpty())
                    {
                        controller.Remark = type.GetDescription();
                    }

                    ms.Add(controller);
                    list.Add(controller);

                    // 反射调用控制器的方法来获取动作
                    var func = type.GetMethodEx("ScanActionMenu");
                    if (func == null)
                    {
                        continue;
                    }

                    var acts = func.As <Func <IMenu, IDictionary <MethodInfo, Int32> > >(type.CreateInstance()).Invoke(controller);
                    if (acts == null || acts.Count == 0)
                    {
                        continue;
                    }

                    // 可选权限子项
                    controller.Permissions.Clear();

                    // 添加该类型下的所有Action作为可选权限子项
                    foreach (var item in acts)
                    {
                        var method = item.Key;

                        var dn = method.GetDisplayName();
                        if (!dn.IsNullOrEmpty())
                        {
                            dn = dn.Replace("{type}", (controller as TEntity)?.FriendName);
                        }

                        var pmName = !dn.IsNullOrEmpty() ? dn : method.Name;
                        if (item.Value <= (Int32)PermissionFlags.Delete)
                        {
                            pmName = ((PermissionFlags)item.Value).GetDescription();
                        }
                        controller.Permissions[item.Value] = pmName;
                    }

                    // 排序
                    if (controller.Sort == 0)
                    {
                        var pi = type.GetPropertyEx("MenuOrder");
                        if (pi != null)
                        {
                            controller.Sort = pi.GetValue(null).ToInt();
                        }
                    }
                }

                for (var i = 0; i < ms.Count; i++)
                {
                    (ms[i] as IEntity).Save();
                }

                // 如果新增了菜单,需要检查权限
                if (list.Count > 0)
                {
                    ThreadPoolX.QueueUserWorkItem(() =>
                    {
                        XTrace.WriteLine("新增了菜单,需要检查权限");
                        var eop = ManageProvider.GetFactory <IRole>();
                        eop.EntityType.Invoke("CheckRole");
                    });
                }

                return(list);
            }