/// <summary>插入菜单权限</summary> protected int InsertMenuPermission() { var result = 0; if (!String.IsNullOrEmpty(MenuPermission)) { List <MenuPermissionDto> mps = JsonConvert.DeserializeObject <List <MenuPermissionDto> >(MenuPermission); if (mps != null && mps.Count > 0) { foreach (MenuPermissionDto item in mps) { RoleMenu rm = new RoleMenu(); rm.RoleID = ID; rm.MenuID = item.ID; rm.IsAllow = item.IsAllow; rm.FieldNames = item.FieldNames; rm.Insert(); if (item.MenuButtons != null && item.MenuButtons.Count > 0) { EntityList <RoleMenuButton> list = new EntityList <RoleMenuButton>(); foreach (var mb in item.MenuButtons) { RoleMenuButton rmb = new RoleMenuButton(); rmb.RoleID = ID; rmb.MenuID = item.ID; rmb.ButtonID = mb.ButtonID; rmb.MenuButtonID = mb.MenuButtonID; list.Add(rmb); } result += list.Insert(); } } } } return(result); }
/// <summary>获取权限管理器实例</summary> public static PermissionManager GetInstance() { var user = ManageProvider.User as Admin; if (user == null) { return(null); } //是否是超级管理员 if (user.IsAdmin) { EntityList <Menu> menus = Menu.Root.AllChilds; if (menus == null || menus.Count == 0) { return(null); } //将菜单集合转成菜单权限集合,只有管理员是超管的时候使用 List <Permission> pers = PermissionManager.FromMenus(menus); #region 创建菜单权限树结构 PermissionManager pm1 = new PermissionManager(); pm1.CreatePermissionTree(pers); #endregion return(pm1); } var adminRoles = user.AdminRoles; if (adminRoles == null || adminRoles.Count == 0) { return(null); } PermissionManager pm = new PermissionManager(); //以菜单id为键,权限为值的字典 Dictionary <Int32, Permission> dic = new Dictionary <Int32, Permission>(); //数据权限 List <DataPermission> dpList = new List <DataPermission>(); foreach (var item in adminRoles) { Role r = item.Role; //不合理的数据,跳过 if (r == null) { continue; } #region 计算数据权限 //先计算数据权限 if (r.RoleDataPermissions != null && r.RoleDataPermissions.Count > 0) { foreach (RoleDataPermission rdp in r.RoleDataPermissions) { DataPermission dp = rdp.DataPermission; //不合理的数据 if (dp == null) { continue; } //是否已经存在列表中 if (!dpList.Exists(p => p.ID == dp.ID)) { dpList.Add(dp); } } } #endregion #region 计算菜单权限 var roleMenus = r.RoleMenus; //该角色没有菜单权限 if (roleMenus == null || roleMenus.Count == 0) { continue; } foreach (RoleMenu rm in roleMenus) { Menu m = rm.Menu; //不合理的数据,跳过 if (m == null) { continue; } Permission per = null; //是否已经添加过相同的菜单 if (dic.ContainsKey(m.ID)) { per = dic[m.ID]; //计算字段权限并集 //计算字段个数 Int32 oldLen = String.IsNullOrEmpty(per.RoleMenu.FieldNames) ? 0 : per.RoleMenu.FieldNames.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Length; Int32 newLen = String.IsNullOrEmpty(rm.FieldNames) ? 0 : rm.FieldNames.Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Length; //谁的字段数量多,以谁为主 per.RoleMenu = oldLen >= newLen ? per.RoleMenu : rm; } else { per = new Permission(); per.Role = r; per.Menu = m; //因为会修改角色菜单,为了不影响缓存中的实体,这里使用克隆 per.RoleMenu = rm.CloneEntity(); dic.Add(m.ID, per); } //该角色所拥有的菜单角色按钮 EntityList <RoleMenuButton> rmbs = RoleMenuButton.FindAllByRoleIDAndMenuID(r.ID, m.ID); if (rmbs == null || rmbs.Count == 0) { continue; } //计算按钮权限并集 foreach (RoleMenuButton rmb in rmbs) { Button b = rmb.Button; //无效的数据 if (b == null) { continue; } //按钮列表是否已经包含该按钮 if (per.Buttons.Exists(p => p.ID == b.ID)) { continue; } per.Buttons.Add(b); } } #endregion } pm.DataPermissions = dpList; #region 创建菜单权限树结构 if (dic.Count > 0) { var values = dic.Values; pm.CreatePermissionTree(values); } #endregion return(pm); }