Ejemplo n.º 1
0
        private FrameworkMenu GetMenu2(List <FrameworkModule> allModules, string controllerName, List <FrameworkRole> allowedRoles, List <FrameworkUserBase> allowedUsers, int displayOrder)
        {
            var             acts = allModules.Where(x => x.FullName == $"WalkingTec.Mvvm.Admin.Api,{controllerName}" && x.IsApi == true).SelectMany(x => x.Actions).ToList();
            var             rest = acts.Where(x => x.IgnorePrivillege == false).ToList();
            FrameworkAction act  = null;

            if (acts.Count > 0)
            {
                act = acts[0];
            }
            FrameworkMenu menu = GetMenuFromAction(act, true, allowedRoles, allowedUsers, displayOrder);

            if (menu != null)
            {
                menu.Url        = "/" + acts[0].Module.ClassName.ToLower();
                menu.ModuleName = menu.ModuleName;
                menu.PageName   = menu.ModuleName;
                menu.ActionName = "MainPage";
                menu.ClassName  = acts[0].Module.FullName;
                menu.MethodName = null;
                for (int i = 0; i < rest.Count; i++)
                {
                    if (rest[i] != null)
                    {
                        menu.Children.Add(GetMenuFromAction(rest[i], false, allowedRoles, allowedUsers, (i + 1)));
                    }
                }
            }
            return(menu);
        }
Ejemplo n.º 2
0
        private FrameworkMenu GetMenuFromAction(FrameworkAction act, bool isMainLink, List <FrameworkRole> allowedRoles, List <FrameworkUserBase> allowedUsers, int displayOrder = 1)
        {
            if (act == null)
            {
                return(null);
            }
            FrameworkMenu menu = new FrameworkMenu
            {
                ActionId     = act.ID,
                ModuleId     = act.ModuleId,
                Url          = "/" + act.Module.ClassName + "/" + act.MethodName,
                Privileges   = new List <FunctionPrivilege>(),
                ShowOnMenu   = isMainLink,
                FolderOnly   = false,
                Children     = new List <FrameworkMenu>(),
                IsInherit    = true,
                IsPublic     = false,
                IsInside     = true,
                DisplayOrder = displayOrder,
                CreateTime   = DateTime.Now
            };

            if (act.Module.Area != null)
            {
                menu.Url = "/" + act.Module.Area.Prefix + menu.Url;
            }
            if (isMainLink)
            {
                menu.PageName   = act.Module.ModuleName;
                menu.ModuleName = act.Module.ModuleName;
                menu.ActionName = act.ActionName;
            }
            else
            {
                menu.PageName   = act.ActionName;
                menu.ModuleName = act.Module.ModuleName;
                menu.ActionName = act.ActionName;
            }
            if (allowedRoles != null)
            {
                foreach (var role in allowedRoles)
                {
                    menu.Privileges.Add(new FunctionPrivilege {
                        RoleId = role.ID, Allowed = true
                    });
                }
            }
            if (allowedUsers != null)
            {
                foreach (var user in allowedUsers)
                {
                    menu.Privileges.Add(new FunctionPrivilege {
                        UserId = user.ID, Allowed = true
                    });
                }
            }
            return(menu);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 判断某菜单是否有权限访问
        /// </summary>
        /// <param name="menu">菜单项</param>
        /// <param name="menus">所有系统菜单</param>
        /// <returns>true代表可以访问,false代表不能访问</returns>
        public bool IsAccessable(FrameworkMenu menu, List <FrameworkMenu> menus)
        {
            //寻找当前菜单的页面权限
            var find = FunctionPrivileges.Where(x => x.MenuItemId == menu.ID && x.Allowed == true).FirstOrDefault();

            //如果能找到直接对应的页面权限
            if (find != null)
            {
                return(true);
            }
            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 判断某菜单是否有权限访问
        /// </summary>
        /// <param name="menu">菜单项</param>
        /// <param name="menus">所有系统菜单</param>
        /// <returns>true代表可以访问,false代表不能访问</returns>
        public bool IsAccessable(FrameworkMenu menu, List <FrameworkMenu> menus)
        {
            //寻找当前菜单的页面权限
            var find = FunctionPrivileges.Where(x => x.MenuItemId == menu.ID).ToList();

            //如果能找到直接对应的页面权限
            if (find.Count > 0)
            {
                //检查是否有拒绝访问的设定,如果有则直接返回false
                var deny = find.Where(x => x.Allowed == false && x.UserId != null).FirstOrDefault();
                if (deny != null)
                {
                    return(false);
                }
                else
                {
                    //检查是否有允许访问的设定,如果有则直接返回true
                    var allow = find.Where(x => x.Allowed == true && x.UserId != null).FirstOrDefault();
                    if (allow != null)
                    {
                        return(true);
                    }
                    else
                    {
                        //如果用户没有指定任何页面权限,则检查用户所属角色中是否有拒绝访问的设定,如果有则返回false
                        var roleDeny = find.Where(x => x.Allowed == false && x.RoleId != null).FirstOrDefault();
                        if (roleDeny != null)
                        {
                            return(false);
                        }
                        //如果没有则返回true,因为find里面有值,但前三种情况都没有,则肯定是允许角色访问是有的,所以不再做判断了
                        else
                        {
                            return(true);
                        }
                    }
                }
            }
            //如果没有直接对应的,且如果当前menu设定了继承属性,则递归寻找上层菜单是否可以访问
            else
            {
                if (menu.Parent == null)
                {
                    return(false);
                }
                else
                {
                    return(IsAccessable(menu.Parent, menus));
                }
            }
        }
Ejemplo n.º 5
0
        private FrameworkMenu GetMenu(List <FrameworkModule> allModules, string areaName, string controllerName, string actionName, List <FrameworkRole> allowedRoles, List <FrameworkUserBase> allowedUsers, int displayOrder)
        {
            var           acts = allModules.Where(x => x.ClassName == controllerName && (areaName == null || x.Area?.Prefix?.ToLower() == areaName.ToLower())).SelectMany(x => x.Actions).ToList();
            var           act  = acts.Where(x => x.MethodName == actionName).SingleOrDefault();
            var           rest = acts.Where(x => x.MethodName != actionName && x.IgnorePrivillege == false).ToList();
            FrameworkMenu menu = GetMenuFromAction(act, true, allowedRoles, allowedUsers, displayOrder);

            if (menu != null)
            {
                for (int i = 0; i < rest.Count; i++)
                {
                    if (rest[i] != null)
                    {
                        menu.Children.Add(GetMenuFromAction(rest[i], false, allowedRoles, allowedUsers, (i + 1)));
                    }
                }
            }
            return(menu);
        }
Ejemplo n.º 6
0
        private FrameworkMenu GetMenu2(List <FrameworkModule> allModules, string controllerName, List <FrameworkRole> allowedRoles, List <FrameworkUserBase> allowedUsers, int displayOrder)
        {
            var           acts = allModules.Where(x => x.ClassName == "_" + controllerName && x.IsApi == true).SelectMany(x => x.Actions).ToList();
            FrameworkMenu menu = GetMenuFromAction(acts[0], true, allowedRoles, allowedUsers, displayOrder);

            if (menu != null)
            {
                menu.Url        = "/" + acts[0].Module.ClassName.ToLower();
                menu.ModuleName = acts[0].Module.ModuleName;
                menu.PageName   = menu.ModuleName;
                menu.ActionName = "主页面";
                menu.ClassName  = acts[0].Module.ClassName;
                menu.MethodName = null;
                for (int i = 0; i < acts.Count; i++)
                {
                    if (acts[i] != null)
                    {
                        menu.Children.Add(GetMenuFromAction(acts[i], false, allowedRoles, allowedUsers, (i + 1)));
                    }
                }
            }
            return(menu);
        }
Ejemplo n.º 7
0
        private FrameworkMenu GetFolderMenu(string FolderText, List <FrameworkRole> allowedRoles, List <FrameworkUserBase> allowedUsers, bool isShowOnMenu = true, bool isInherite = false)
        {
            FrameworkMenu menu = new FrameworkMenu
            {
                PageName     = FolderText,
                Children     = new List <FrameworkMenu>(),
                Privileges   = new List <FunctionPrivilege>(),
                ShowOnMenu   = isShowOnMenu,
                IsInherit    = isInherite,
                IsInside     = true,
                FolderOnly   = true,
                IsPublic     = false,
                CreateTime   = DateTime.Now,
                DisplayOrder = 1
            };

            if (allowedRoles != null)
            {
                foreach (var role in allowedRoles)
                {
                    menu.Privileges.Add(new FunctionPrivilege {
                        RoleId = role.ID, Allowed = true
                    });
                }
            }
            if (allowedUsers != null)
            {
                foreach (var user in allowedUsers)
                {
                    menu.Privileges.Add(new FunctionPrivilege {
                        UserId = user.ID, Allowed = true
                    });
                }
            }

            return(menu);
        }
Ejemplo n.º 8
0
        /// <summary>
        /// 数据初始化
        /// </summary>
        /// <param name="allModules"></param>
        /// <param name="IsSpa"></param>
        /// <returns>返回true表示需要进行初始化数据操作,返回false即数据库已经存在或不需要初始化数据</returns>
        public async override Task <bool> DataInit(object allModules, bool IsSpa)
        {
            bool rv = await Database.EnsureCreatedAsync();

            //判断是否存在初始数据
            bool emptydb = false;

            try
            {
                emptydb = Set <FrameworkUserBase>().Count() == 0 && Set <FrameworkUserRole>().Count() == 0 && Set <FrameworkMenu>().Count() == 0;
            }
            catch { }

            if (emptydb == true)
            {
                var AllModules = allModules as List <FrameworkModule>;
                var roles      = new FrameworkRole[]
                {
                    new FrameworkRole {
                        RoleCode = "001", RoleName = Program._localizer["Admin"]
                    }
                };
                var users = new FrameworkUserBase[]
                {
                    new FrameworkUserBase {
                        ITCode = "admin", Password = Utils.GetMD5String("000000"), IsValid = true, Name = Program._localizer["Admin"]
                    }
                };
                var userroles = new FrameworkUserRole[]
                {
                    new FrameworkUserRole {
                        User = users[0], Role = roles[0]
                    }
                };

                var adminRole = roles[0];
                if (Set <FrameworkMenu>().Any() == false)
                {
                    var systemManagement = GetFolderMenu(Program._localizer["SystemManagement"], new List <FrameworkRole> {
                        adminRole
                    }, null);
                    var logList = IsSpa ? GetMenu2(AllModules, "ActionLog", new List <FrameworkRole> {
                        adminRole
                    }, null, 1) : GetMenu(AllModules, "_Admin", "ActionLog", "Index", new List <FrameworkRole> {
                        adminRole
                    }, null, 1);
                    var userList = IsSpa ? GetMenu2(AllModules, "FrameworkUser", new List <FrameworkRole> {
                        adminRole
                    }, null, 2) : GetMenu(AllModules, "_Admin", "FrameworkUser", "Index", new List <FrameworkRole> {
                        adminRole
                    }, null, 2);
                    var roleList = IsSpa ? GetMenu2(AllModules, "FrameworkRole", new List <FrameworkRole> {
                        adminRole
                    }, null, 3) : GetMenu(AllModules, "_Admin", "FrameworkRole", "Index", new List <FrameworkRole> {
                        adminRole
                    }, null, 3);
                    var groupList = IsSpa ? GetMenu2(AllModules, "FrameworkGroup", new List <FrameworkRole> {
                        adminRole
                    }, null, 4) : GetMenu(AllModules, "_Admin", "FrameworkGroup", "Index", new List <FrameworkRole> {
                        adminRole
                    }, null, 4);
                    var menuList = IsSpa ? GetMenu2(AllModules, "FrameworkMenu", new List <FrameworkRole> {
                        adminRole
                    }, null, 5) : GetMenu(AllModules, "_Admin", "FrameworkMenu", "Index", new List <FrameworkRole> {
                        adminRole
                    }, null, 5);
                    var dpList = IsSpa ? GetMenu2(AllModules, "DataPrivilege", new List <FrameworkRole> {
                        adminRole
                    }, null, 6) : GetMenu(AllModules, "_Admin", "DataPrivilege", "Index", new List <FrameworkRole> {
                        adminRole
                    }, null, 6);
                    if (logList != null)
                    {
                        systemManagement.Children.AddRange(new FrameworkMenu[] { logList, userList, roleList, groupList, menuList, dpList });
                        Set <FrameworkMenu>().Add(systemManagement);
                    }

                    if (IsSpa == false)
                    {
                        var apifolder = GetFolderMenu("Api", new List <FrameworkRole> {
                            adminRole
                        }, null);
                        apifolder.ShowOnMenu   = false;
                        apifolder.DisplayOrder = 100;
                        var logList2 = GetMenu2(AllModules, "ActionLog", new List <FrameworkRole> {
                            adminRole
                        }, null, 1);
                        var userList2 = GetMenu2(AllModules, "FrameworkUser", new List <FrameworkRole> {
                            adminRole
                        }, null, 2);
                        var roleList2 = GetMenu2(AllModules, "FrameworkRole", new List <FrameworkRole> {
                            adminRole
                        }, null, 3);
                        var groupList2 = GetMenu2(AllModules, "FrameworkGroup", new List <FrameworkRole> {
                            adminRole
                        }, null, 4);
                        var menuList2 = GetMenu2(AllModules, "FrameworkMenu", new List <FrameworkRole> {
                            adminRole
                        }, null, 5);
                        var dpList2 = GetMenu2(AllModules, "DataPrivilege", new List <FrameworkRole> {
                            adminRole
                        }, null, 6);
                        var apis = new FrameworkMenu[] { logList2, userList2, roleList2, groupList2, menuList2, dpList2 };
                        apis.ToList().ForEach(x => { x.ShowOnMenu = false; x.PageName += $"({Program._localizer["BuildinApi"]})"; });
                        apifolder.Children.AddRange(apis);
                        Set <FrameworkMenu>().Add(apifolder);
                    }
                }
                Set <FrameworkRole>().AddRange(roles);
                Set <FrameworkUserBase>().AddRange(users);
                Set <FrameworkUserRole>().AddRange(userroles);
                await SaveChangesAsync();
            }
            return(rv);
        }
Ejemplo n.º 9
0
        /// <summary>
        /// 数据初始化
        /// </summary>
        /// <param name="allModules"></param>
        /// <param name="IsSpa"></param>
        /// <returns>返回true表示需要进行初始化数据操作,返回false即数据库已经存在或不需要初始化数据</returns>
        public async override Task <bool> DataInit(object allModules, bool IsSpa)
        {
            bool rv = await Database.EnsureCreatedAsync();

            //判断是否存在初始数据
            bool emptydb = false;

            try
            {
                emptydb = Set <FrameworkUserBase>().Count() == 0 && Set <FrameworkUserRole>().Count() == 0 && Set <FrameworkMenu>().Count() == 0;
            }
            catch { }

            if (emptydb == true)
            {
                var AllModules = allModules as List <FrameworkModule>;
                var roles      = new FrameworkRole[]
                {
                    new FrameworkRole {
                        RoleCode = "001", RoleName = Program._localizer["Admin"]
                    }
                };
                var users = new FrameworkUserBase[]
                {
                    new FrameworkUserBase {
                        ITCode = "admin", Password = Utils.GetMD5String("000000"), IsValid = true, Name = Program._localizer["Admin"]
                    }
                };
                var userroles = new FrameworkUserRole[]
                {
                    new FrameworkUserRole {
                        User = users[0], Role = roles[0]
                    }
                };

                var adminRole = roles[0];
                if (Set <FrameworkMenu>().Any() == false)
                {
                    var systemManagement = GetFolderMenu("SystemManagement", new List <FrameworkRole> {
                        adminRole
                    }, null);
                    var logList = IsSpa ? GetMenu2(AllModules, "ActionLog", new List <FrameworkRole> {
                        adminRole
                    }, null, 1) : GetMenu(AllModules, "_Admin", "ActionLog", "Index", new List <FrameworkRole> {
                        adminRole
                    }, null, 1);
                    var userList = IsSpa ? GetMenu2(AllModules, "FrameworkUser", new List <FrameworkRole> {
                        adminRole
                    }, null, 2) : GetMenu(AllModules, "_Admin", "FrameworkUser", "Index", new List <FrameworkRole> {
                        adminRole
                    }, null, 2);
                    var roleList = IsSpa ? GetMenu2(AllModules, "FrameworkRole", new List <FrameworkRole> {
                        adminRole
                    }, null, 3) : GetMenu(AllModules, "_Admin", "FrameworkRole", "Index", new List <FrameworkRole> {
                        adminRole
                    }, null, 3);
                    var groupList = IsSpa ? GetMenu2(AllModules, "FrameworkGroup", new List <FrameworkRole> {
                        adminRole
                    }, null, 4) : GetMenu(AllModules, "_Admin", "FrameworkGroup", "Index", new List <FrameworkRole> {
                        adminRole
                    }, null, 4);
                    var menuList = IsSpa ? GetMenu2(AllModules, "FrameworkMenu", new List <FrameworkRole> {
                        adminRole
                    }, null, 5) : GetMenu(AllModules, "_Admin", "FrameworkMenu", "Index", new List <FrameworkRole> {
                        adminRole
                    }, null, 5);
                    var dpList = IsSpa ? GetMenu2(AllModules, "DataPrivilege", new List <FrameworkRole> {
                        adminRole
                    }, null, 6) : GetMenu(AllModules, "_Admin", "DataPrivilege", "Index", new List <FrameworkRole> {
                        adminRole
                    }, null, 6);
                    if (logList != null)
                    {
                        var menus = new FrameworkMenu[] { logList, userList, roleList, groupList, menuList, dpList };
                        foreach (var item in menus)
                        {
                            if (item != null)
                            {
                                systemManagement.Children.Add(item);
                            }
                        }
                        Set <FrameworkMenu>().Add(systemManagement);

                        if (IsSpa == false)
                        {
                            systemManagement.ICon = "layui-icon layui-icon-set";
                            logList.ICon          = "layui-icon layui-icon-form";
                            userList.ICon         = "layui-icon layui-icon-friends";
                            roleList.ICon         = "layui-icon layui-icon-user";
                            groupList.ICon        = "layui-icon layui-icon-group";
                            menuList.ICon         = "layui-icon layui-icon-menu-fill";
                            dpList.ICon           = "layui-icon layui-icon-auz";

                            var apifolder = GetFolderMenu("Api", new List <FrameworkRole> {
                                adminRole
                            }, null);
                            apifolder.ShowOnMenu   = false;
                            apifolder.DisplayOrder = 100;
                            var logList2 = GetMenu2(AllModules, "ActionLog", new List <FrameworkRole> {
                                adminRole
                            }, null, 1);
                            var userList2 = GetMenu2(AllModules, "FrameworkUser", new List <FrameworkRole> {
                                adminRole
                            }, null, 2);
                            var roleList2 = GetMenu2(AllModules, "FrameworkRole", new List <FrameworkRole> {
                                adminRole
                            }, null, 3);
                            var groupList2 = GetMenu2(AllModules, "FrameworkGroup", new List <FrameworkRole> {
                                adminRole
                            }, null, 4);
                            var menuList2 = GetMenu2(AllModules, "FrameworkMenu", new List <FrameworkRole> {
                                adminRole
                            }, null, 5);
                            var dpList2 = GetMenu2(AllModules, "DataPrivilege", new List <FrameworkRole> {
                                adminRole
                            }, null, 6);
                            var apis = new FrameworkMenu[] { logList2, userList2, roleList2, groupList2, menuList2, dpList2 };
                            //apis.ToList().ForEach(x => { x.ShowOnMenu = false;x.PageName += $"({Program._localizer["BuildinApi"]})"; });
                            foreach (var item in apis)
                            {
                                if (item != null)
                                {
                                    apifolder.Children.Add(item);
                                }
                            }
                            Set <FrameworkMenu>().Add(apifolder);
                        }
                        else
                        {
                            systemManagement.ICon = " _wtmicon _wtmicon-icon_shezhi";
                            logList.ICon          = " _wtmicon _wtmicon-chaxun";
                            userList.ICon         = " _wtmicon _wtmicon-zhanghaoquanxianguanli";
                            roleList.ICon         = " _wtmicon _wtmicon-quanxianshenpi";
                            groupList.ICon        = " _wtmicon _wtmicon-zuzhiqunzu";
                            menuList.ICon         = " _wtmicon _wtmicon--lumingpai";
                            dpList.ICon           = " _wtmicon _wtmicon-anquan";
                        }
                    }
                }
                Set <FrameworkRole>().AddRange(roles);
                Set <FrameworkUserBase>().AddRange(users);
                Set <FrameworkUserRole>().AddRange(userroles);
                await SaveChangesAsync();
            }
            return(rv);
        }