public ActionResult GetPermission(int roleId, string selectedModules) { var input = new GetPermissionInput() { RoleId = roleId, SelectedModules = selectedModules }; RoleSelectedPermissionModel model = _roleService.GetPermission(input); return(PartialView("Permission", model)); }
public ActionResult GetPermission(int roleId, string selectedModules) { //选中模块 List <int> selectedModuleId = new List <int>(); string[] strSelectedModules = selectedModules.Split(','); foreach (var Id in strSelectedModules) { selectedModuleId.Add(Convert.ToInt32(Id)); } //权限列表 var model = new RoleSelectedPermissionModel(); model.HeaderPermissionList = PermissionService.Permissions.Where(t => t.IsDeleted == false && t.Enabled == true) .OrderBy(t => t.OrderSort) .Select(t => new PermissionModel { PermissionId = t.Id, PermissionName = t.Name, OrderSort = t.OrderSort }).ToList(); //权限列表 (从选中的菜单获取) foreach (var moduleId in selectedModuleId.Distinct()) { var module = ModuleService.Modules.FirstOrDefault(t => t.Id == moduleId); var modulePermissionModel = new ModulePermissionModel { ModuleId = module.Id, ParentId = module.ParentId, ModuleName = module.Name, Code = module.Code }; //所有权限列表 foreach (var permission in model.HeaderPermissionList) { modulePermissionModel.PermissionDataList.Add(new PermissionModel { PermissionId = permission.PermissionId, PermissionName = permission.PermissionName, OrderSort = permission.OrderSort, }); } //模块包含的按钮 var modulePermission = ModulePermissionService.ModulePermissions.Where(t => t.ModuleId == moduleId && t.IsDeleted == false); var selectedModulePermission = RoleModulePermissionService.RoleModulePermissions.Where(t => t.RoleId == roleId && t.ModuleId == moduleId && t.IsDeleted == false); if (module.ChildModule.Count > 0 && selectedModulePermission.Count() > 0) { modulePermissionModel.Selected = true; } foreach (var mp in modulePermission) { var permission = PermissionService.Permissions.FirstOrDefault(t => t.Id == mp.PermissionId); foreach (var p in modulePermissionModel.PermissionDataList) { if (p.PermissionId == permission.Id) { //设置Checkbox可用 p.Enabled = true; //设置选中 var rmp = RoleModulePermissionService.RoleModulePermissions.FirstOrDefault(t => t.RoleId == roleId && t.ModuleId == moduleId && t.PermissionId == permission.Id && t.IsDeleted == false); if (rmp != null) { //设置父节点选中 modulePermissionModel.Selected = true; p.Selected = true; } } } } model.ModulePermissionDataList.Add(modulePermissionModel); } //权限按照Code排序 model.ModulePermissionDataList = model.ModulePermissionDataList.OrderBy(t => t.Code).ToList(); return(PartialView("Permission", model)); }
public ActionResult GetPermission(string roleId, string selectedModules) { //选中模块 List <string> selectedModuleId = new List <string>(); string[] strSelectedModules = selectedModules.Split(','); foreach (var Id in strSelectedModules) { selectedModuleId.Add(Id); } //权限模块列表 var model = new RoleSelectedPermissionModel(); //权限下的模块 var role = RoleService.Roles.Where(t => t._ID == roleId && t.IsDeleted == false).FirstOrDefault(); if (role != null) { var permissions = PermissionService.Permissions.Where(t => t.IsDeleted == false && t.Enabled == true) .OrderBy(t => t.OrderSort).ToList() .Select(t => new PermissionModel { Enabled = t.Enabled, PermissionId = t._ID, PermissionName = t.Name, OrderSort = t.OrderSort }).ToList(); model.HeaderPermissionList = permissions; var module = ModuleService.Modules.Where(t => selectedModuleId.Contains(t._ID)).ToList(); foreach (var m in module) { var modulePermissionModel = new ModulePermissionModel { ModuleId = m._ID, ParentId = m.ParentId, ModuleName = m.Name, Code = m.Code }; var rmodule = new RoleModulePermission(); if (role.RoleModulePermissions != null) { rmodule = role.RoleModulePermissions.FirstOrDefault(x => x.ModuleId == m._ID); } if (rmodule == null) { rmodule = new RoleModulePermission(); } rmodule.PermissionIds = rmodule.PermissionIds ?? new List <string>(); foreach (var p in permissions) { var selected = rmodule.PermissionIds.FirstOrDefault(x => x == p.PermissionId) != null; if (selected && !modulePermissionModel.Selected) { modulePermissionModel.Selected = true; var pa = model.ModulePermissionDataList.FirstOrDefault(x => x.ModuleId == modulePermissionModel.ParentId); if (pa != null && !pa.Selected) { pa.Selected = true; } } modulePermissionModel.PermissionDataList.Add(new PermissionModel { PermissionId = p.PermissionId, PermissionName = p.PermissionName, OrderSort = p.OrderSort, Enabled = m.PermissionIds.FirstOrDefault(x => x == p.PermissionId) != null, Selected = selected }); } model.ModulePermissionDataList.Add(modulePermissionModel); } //权限按照Code排序 model.ModulePermissionDataList = model.ModulePermissionDataList.OrderBy(t => t.Code).ToList(); } return(PartialView("Permission", model)); }