From 5c1780cc00f11d22aae98d7355e3c67b4c7ae385 Mon Sep 17 00:00:00 2001 From: Argo-MacBookPro Date: Wed, 31 Oct 2018 17:08:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9BUG=EF=BC=9A=E8=8F=9C?= =?UTF-8?q?=E5=8D=95=E7=BB=B4=E6=8A=A4=E9=A1=B5=E9=9D=A2=EF=BC=8C=E6=8E=88?= =?UTF-8?q?=E6=9D=83=E8=A7=92=E8=89=B2=E6=8C=89=E9=92=AE=E4=BF=9D=E5=AD=98?= =?UTF-8?q?=E6=97=B6=EF=BC=8C=E4=B8=8D=E8=83=BD=E5=88=A0=E9=99=A4=E8=A7=92?= =?UTF-8?q?=E8=89=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/RolesController.cs | 22 +++++++++---------- Bootstrap.DataAccess.MongoDB/Role.cs | 16 +++++++++++--- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/Bootstrap.Admin/Controllers/Api/RolesController.cs b/Bootstrap.Admin/Controllers/Api/RolesController.cs index ba3e48af..8f394540 100644 --- a/Bootstrap.Admin/Controllers/Api/RolesController.cs +++ b/Bootstrap.Admin/Controllers/Api/RolesController.cs @@ -25,37 +25,37 @@ namespace Bootstrap.Admin.Controllers.Api return value.RetrieveData(); } /// - /// 通过指定ID获得所有角色集合 + /// 通过指定用户ID/部门ID/菜单ID获得所有角色集合,已经授权的有checked标记 /// /// 用户ID/部门ID/菜单ID /// 类型 /// [HttpPost("{id}")] - public IEnumerable Post(string id, [FromQuery]string type) + public IEnumerable Post(string id, [FromQuery]string type) { - var ret = new List(); + IEnumerable ret = new List(); switch (type) { case "user": - ret = RoleHelper.RetrieveRolesByUserId(id).ToList(); + ret = RoleHelper.RetrieveRolesByUserId(id); break; case "group": - ret = RoleHelper.RetrieveRolesByGroupId(id).ToList(); + ret = RoleHelper.RetrieveRolesByGroupId(id); break; case "menu": - ret = RoleHelper.RetrieveRolesByMenuId(id).ToList(); + ret = RoleHelper.RetrieveRolesByMenuId(id); break; default: break; } - return ret; + return ret.Select(m => new { m.Id, m.Checked, m.RoleName, m.Description }); } /// - /// 根据GroupID获取 + /// 保存角色 /// - /// - /// - /// + /// 用户ID/部门ID/菜单ID + /// 选中的角色ID集合 + /// type=menu时,菜单维护页面对角色授权弹框保存按钮调用 /// [HttpPut("{id}")] public bool Put(string id, [FromBody]IEnumerable roleIds, [FromQuery]string type) diff --git a/Bootstrap.DataAccess.MongoDB/Role.cs b/Bootstrap.DataAccess.MongoDB/Role.cs index 783a6c5f..e733dcbc 100644 --- a/Bootstrap.DataAccess.MongoDB/Role.cs +++ b/Bootstrap.DataAccess.MongoDB/Role.cs @@ -122,14 +122,24 @@ namespace Bootstrap.DataAccess.MongoDB /// public override bool SavaRolesByMenuId(string menuId, IEnumerable 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() : p.Menus.ToList(); + menus.Remove(menuId); + MongoDbAccessManager.Roles.UpdateOne(md => md.Id == p.Id, Builders.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() : role.Menus.ToList(); if (!menus.Contains(menuId)) { menus.Add(menuId); - MongoDbAccessManager.Roles.UpdateOne(md => md.Id == rId, Builders.Update.Set(md => md.Menus, menus)); + MongoDbAccessManager.Roles.UpdateOne(md => md.Id == role.Id, Builders.Update.Set(md => md.Menus, menus)); } }); return true;