public List <FunctionModel> GetFunctionByUserIdAndMenuId(int userId, int menuId) { string cachekey = userId + "-" + menuId; var list = CacheManager.CreateCache().Get <List <FunctionModel> >(cachekey); if (list != null && list.Count > 0) { return(list); } list = new List <FunctionModel>(20); if (CommonAction.IsSysAdmin(userId))//如果是管理员 获取菜单所有的功能 { var builtin = BuiltinRoles.All.Select(n => new FunctionModel { FunctionKey = n, FunctionName = n }); list.Add(new FunctionModel { FunctionKey = "$ADMIN", FunctionName = "$ADMIN" }); list.AddRange(builtin); list.AddRange(RoleNavigationFunctionDbAction.GetFunctionsByNavigationId(menuId)); } else { var roles = UserRolesDbAction.GetUserRolses(userId); roles.ForEach(n => { if (BuiltinRoles.All.Contains(n.RoleName)) { list.Add(new FunctionModel { FunctionKey = n.RoleName, FunctionName = n.RoleName }); } }); //角色分配的功能 var roleIds = roles.Select(p => p.RoleId).ToList(); ICollection <FunctionModel> funcList = RoleNavigationFunctionDbAction.GetFunctionsByRoleIdsAndNavigationId(roleIds, menuId); ICollection <FunctionModel> userFuncList = UserNavigationFunctionDbAction.GetFunctionsByUserIdsAndNavigationId(new List <int> { userId }, menuId); list.AddRange(funcList); list.AddRange(userFuncList.Except(funcList).ToList()); } CacheManager.CreateCache().Add(cachekey, list); return(list); }
public List <MenuResponse> GetMenusByRoleId(int roleId, int appId) { var menus = RoleNavigationFunctionDbAction.GetMenuFunctions(new List <int>() { roleId }, appId); List <MenuResponse> topMenus = new List <MenuResponse>(); foreach (var fun in menus) { GetTopMenu(fun, menus, topMenus); } foreach (var menu in topMenus) { SetChildren(menus, menu); } return(topMenus); }
/// <summary> /// 删除角色和角色对应的中间表数据 /// Esmart_Sys_Roles /// Esmart_Sys_RolesInDeparent /// Esmart_Sys_Role_App /// Esmart_Sys_Role_Navigations /// Esmart_Sys_Role_Navigation_Function /// Esmart_Sys_User_Roles /// </summary> /// <param name="request">角色Id</param> /// <returns></returns> public bool Del(RoleModel request) { RoleDbAction.Delete(request.Id); RoleNavigationsDbAction.Del(request.Id); RoleNavigationFunctionDbAction.Del(request.Id); UserRolesDbAction.Delete(request.Id); DepartmentRolesDbAction.DeleteByRoleId(request.Id); if (LogHelper <RoleModel> .LogAction("Esmart.Permission.Application.Data.DepartmentRolesDbAction") != null) { LogHelper <RoleModel> .LogAction("Esmart.Permission.Application.Data.DepartmentRolesDbAction")(new UserLiteDto { UserID = request.CreatorId == null?1:request.CreatorId.Value }, request); LogHelper <RoleModel> .RemoveAction("Esmart.Permission.Application.Data.DepartmentRolesDbAction"); } var remark = string.Format("用户:{0} 删除角色:{1}", request.CreatorId ?? 0, request.Id); LogManagerDb.Log(request.CreatorId ?? 0, DateTime.Now, remark, "RoleManager.DelByDepartmentId"); return(true); }
public List <MenuResponse> GetMenuByUserId(int userId, int appId, out List <RoleResponse> roles) { roles = UserManagerDb.GetRolesByUserId(userId); List <MenuResponse> menus = null; if (CommonAction.IsSysAdmin(userId)) { menus = RoleNavigationFunctionDbAction.GetAdminMenu(appId); } else { menus = new List <MenuResponse>(); menus.AddRange(RoleNavigationFunctionDbAction.GetMenuFunctions(roles.Select(n => n.RoleId).ToList(), appId)); menus.AddRange(UserNavigationFunctionDbAction.GetMenuFunctions(new List <int> { userId }, appId)); menus = menus.Where((i, j) => menus.FindIndex(f => f.Id == i.Id) == j).ToList(); } //var menus = CommonAction.IsSysAdmin(userId) // ? RoleNavigationFunctionDbAction.GetAdminMenu(appId) // : RoleNavigationFunctionDbAction.GetMenuFunctions(roles.Select(n => n.RoleId).ToList(), appId); var topMenus = new List <MenuResponse>(); foreach (var fun in menus) { GetTopMenu(fun, menus, topMenus); } foreach (var menu in topMenus) { SetChildren(menus, menu); } return(topMenus); }