修改BUG:菜单维护页面,授权角色按钮保存时,不能删除角色

This commit is contained in:
Argo-MacBookPro 2018-10-31 17:08:55 +08:00
parent 2ae1a241de
commit 5c1780cc00
2 changed files with 24 additions and 14 deletions

View File

@ -25,37 +25,37 @@ namespace Bootstrap.Admin.Controllers.Api
return value.RetrieveData(); return value.RetrieveData();
} }
/// <summary> /// <summary>
/// 通过指定ID获得所有角色集合 /// 通过指定用户ID/部门ID/菜单ID获得所有角色集合已经授权的有checked标记
/// </summary> /// </summary>
/// <param name="id">用户ID/部门ID/菜单ID</param> /// <param name="id">用户ID/部门ID/菜单ID</param>
/// <param name="type">类型</param> /// <param name="type">类型</param>
/// <returns></returns> /// <returns></returns>
[HttpPost("{id}")] [HttpPost("{id}")]
public IEnumerable<Role> Post(string id, [FromQuery]string type) public IEnumerable<object> Post(string id, [FromQuery]string type)
{ {
var ret = new List<Role>(); IEnumerable<Role> ret = new List<Role>();
switch (type) switch (type)
{ {
case "user": case "user":
ret = RoleHelper.RetrieveRolesByUserId(id).ToList(); ret = RoleHelper.RetrieveRolesByUserId(id);
break; break;
case "group": case "group":
ret = RoleHelper.RetrieveRolesByGroupId(id).ToList(); ret = RoleHelper.RetrieveRolesByGroupId(id);
break; break;
case "menu": case "menu":
ret = RoleHelper.RetrieveRolesByMenuId(id).ToList(); ret = RoleHelper.RetrieveRolesByMenuId(id);
break; break;
default: default:
break; break;
} }
return ret; return ret.Select(m => new { m.Id, m.Checked, m.RoleName, m.Description });
} }
/// <summary> /// <summary>
/// 根据GroupID获取 /// 保存角色
/// </summary> /// </summary>
/// <param name="id"></param> /// <param name="id">用户ID/部门ID/菜单ID</param>
/// <param name="roleIds"></param> /// <param name="roleIds">选中的角色ID集合</param>
/// <param name="type"></param> /// <param name="type">type=menu时菜单维护页面对角色授权弹框保存按钮调用</param>
/// <returns></returns> /// <returns></returns>
[HttpPut("{id}")] [HttpPut("{id}")]
public bool Put(string id, [FromBody]IEnumerable<string> roleIds, [FromQuery]string type) public bool Put(string id, [FromBody]IEnumerable<string> roleIds, [FromQuery]string type)

View File

@ -122,14 +122,24 @@ namespace Bootstrap.DataAccess.MongoDB
/// <returns></returns> /// <returns></returns>
public override bool SavaRolesByMenuId(string menuId, IEnumerable<string> roleIds) public override bool SavaRolesByMenuId(string menuId, IEnumerable<string> roleIds)
{ {
roleIds.ToList().ForEach(rId => var roles = MongoDbAccessManager.Roles.Find(md => md.Menus != null && md.Menus.Contains(menuId)).ToList();
// Remove roles
roles.ForEach(p =>
{
var menus = p.Menus == null ? new List<string>() : p.Menus.ToList();
menus.Remove(menuId);
MongoDbAccessManager.Roles.UpdateOne(md => md.Id == p.Id, Builders<Role>.Update.Set(md => md.Menus, menus));
});
roles = MongoDbAccessManager.Roles.Find(md => roleIds.Contains(md.Id)).ToList();
roles.ForEach(role =>
{ {
var role = MongoDbAccessManager.Roles.Find(md => md.Id == rId).FirstOrDefault();
var menus = role.Menus == null ? new List<string>() : role.Menus.ToList(); var menus = role.Menus == null ? new List<string>() : role.Menus.ToList();
if (!menus.Contains(menuId)) if (!menus.Contains(menuId))
{ {
menus.Add(menuId); menus.Add(menuId);
MongoDbAccessManager.Roles.UpdateOne(md => md.Id == rId, Builders<Role>.Update.Set(md => md.Menus, menus)); MongoDbAccessManager.Roles.UpdateOne(md => md.Id == role.Id, Builders<Role>.Update.Set(md => md.Menus, menus));
} }
}); });
return true; return true;