From 96093f8d34823e1d0d203735ce1f0040393ad8d7 Mon Sep 17 00:00:00 2001 From: "liuchun_0206@163.com" Date: Thu, 27 Oct 2016 17:56:00 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E7=BE=A4=E7=BB=84=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E8=A7=92=E8=89=B2=E7=9A=84=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/Content/js/framework.js | 11 +- .../Controllers/RolesController.cs | 9 +- Bootstrap.Admin/Scripts/Groups.js | 44 ++++++- Bootstrap.Admin/Scripts/Roles.js | 2 +- Bootstrap.Admin/Views/Admin/Groups.cshtml | 11 ++ Bootstrap.DataAccess/RoleHelper.cs | 122 ++++++++++++++---- Bootstrap.DataAccessTests/RoleHelperTests.cs | 16 +++ 7 files changed, 179 insertions(+), 36 deletions(-) diff --git a/Bootstrap.Admin/Content/js/framework.js b/Bootstrap.Admin/Content/js/framework.js index 4eb7d992..6466168f 100644 --- a/Bootstrap.Admin/Content/js/framework.js +++ b/Bootstrap.Admin/Content/js/framework.js @@ -247,7 +247,7 @@ Role.getRolesByUserId = function (userId, callback) { processRolesData({ Id: userId, callback: callback, data: { type: "user" } }); }; - Role.getRolesByGroupId = function (groupId) { + Role.getRolesByGroupId = function (groupId, callback) { processRolesData({ Id: groupId, callback: callback, data: { type: "group" } }); }; Role.getRolesByMenuId = function (menuId, callback) { @@ -256,6 +256,9 @@ Role.saveRolesByUserId = function (userId, roleIds, callback) { processRolesData({ Id: userId, callback: callback, method: "PUT", data: { type: "user", roleIds: roleIds } }); } + Role.saveRolesByGroupId = function (groupId, roleIds, callback) { + processRolesData({ Id: groupId, callback: callback, method: "PUT", data: { type: "group", roleIds: roleIds } }); + } Role.saveRolesByMenuId = function (menuId, roleIds, callback) { processRolesData({ Id: menuId, callback: callback, method: "PUT", data: { type: "menu", roleIds: roleIds } }); }; @@ -318,7 +321,7 @@ } if (result) { swal("成功", "授权角色", "success"); } else { swal("失败", "授权角色", "error"); } - if ($.isFunction(data.callback)) data.callback(result); + if ($.isFunction(data.callback)) data.callback(result); }, error: function (XMLHttpRequest, textStatus, errorThrown) { if ($.isFunction(data.callback)) data.callback(false); @@ -333,10 +336,10 @@ Group.saveGroupsByUserId = function (userId, groupIds, callback) { processGroupsData({ Id: userId, callback: callback, method: "PUT", data: { type: "user", groupIds: groupIds } }); }; - Group.getGroupsByRoleId = function (roleId,callback) { + Group.getGroupsByRoleId = function (roleId, callback) { processGroupsData({ Id: roleId, callback: callback, data: { type: "role" } }); }; - Group.saveGroupsByRoleId = function (roleId,groupIds,callback) { + Group.saveGroupsByRoleId = function (roleId, groupIds, callback) { processGroupsData({ Id: roleId, callback: callback, method: "PUT", data: { type: "role", groupIds: groupIds } }); }; })(jQuery); \ No newline at end of file diff --git a/Bootstrap.Admin/Controllers/RolesController.cs b/Bootstrap.Admin/Controllers/RolesController.cs index 83bd0a2d..260e7aae 100644 --- a/Bootstrap.Admin/Controllers/RolesController.cs +++ b/Bootstrap.Admin/Controllers/RolesController.cs @@ -2,8 +2,10 @@ using Bootstrap.DataAccess; using Newtonsoft.Json.Linq; using System.Collections.Generic; -using System.Linq; using System.Web.Http; +using Newtonsoft.Json.Linq; +using System.Linq; + namespace Bootstrap.Admin.Controllers { @@ -45,11 +47,10 @@ namespace Bootstrap.Admin.Controllers } return ret; } - /// - /// + /// 根据GroupID获取 /// /// - /// + /// [HttpPut] public bool Put(int id, [FromBody]JObject value) { diff --git a/Bootstrap.Admin/Scripts/Groups.js b/Bootstrap.Admin/Scripts/Groups.js index 5381b6fe..82d9b262 100644 --- a/Bootstrap.Admin/Scripts/Groups.js +++ b/Bootstrap.Admin/Scripts/Groups.js @@ -5,11 +5,51 @@ map: { ID: "groupID", GroupName: "groupName", - Description: "groupDesc" + Description: "groupDesc" } - }) + }), + click: { + assign: [{ + id: 'btn_assignRole', + click: function (row) { + Role.getRolesByGroupId(row.ID, function (data) { + $("#dialogRole .modal-title").text($.format('{0}-角色授权窗口', row.GroupName)); + $('#dialogRole form').html(data); + $('#dialogRole').modal('show'); + }); + } + }, { + id: 'btn_assignGroup', + click: function (row) { + var userId = row.ID; + } + }, { + id: 'btnSubmitUserRole', + click: function (row) { + var userId = row.ID; + var roleIds = $('#dialogRole :checked').map(function (index, element) { + return $(element).val(); + }).toArray().join(','); + Role.saveRolesByGroupId(userId, roleIds, function (result) { + if (result) { + $('#dialogRole').modal("hide"); + swal("成功", "修改角色", "success"); + } else { + swal("失败", "修改角色", "error"); + } + + }); + } + }] + }, + success: function (src, data) { + if (src === 'save' && data.ID === $('#userId').val()) { + $('.username').text(data.DisplayName); + } + } }); + $('table').smartTable({ url: '../api/Groups', //请求后台的URL(*) sortName: 'GroupName', diff --git a/Bootstrap.Admin/Scripts/Roles.js b/Bootstrap.Admin/Scripts/Roles.js index fb614f5c..ef3a9c21 100644 --- a/Bootstrap.Admin/Scripts/Roles.js +++ b/Bootstrap.Admin/Scripts/Roles.js @@ -5,7 +5,7 @@ map: { ID: "roleID", RoleName: "roleName", - Description: "roleDesc" + Description: "roleDesc" } }), click: { diff --git a/Bootstrap.Admin/Views/Admin/Groups.cshtml b/Bootstrap.Admin/Views/Admin/Groups.cshtml index 77dfcabd..e08bd2cf 100644 --- a/Bootstrap.Admin/Views/Admin/Groups.cshtml +++ b/Bootstrap.Admin/Views/Admin/Groups.cshtml @@ -27,6 +27,14 @@ } +@section toolbar { + + +} @section modal { +} +@section customModal { + @Html.Partial("RoleConfig") } \ No newline at end of file diff --git a/Bootstrap.DataAccess/RoleHelper.cs b/Bootstrap.DataAccess/RoleHelper.cs index d58a275e..c760ab1a 100644 --- a/Bootstrap.DataAccess/RoleHelper.cs +++ b/Bootstrap.DataAccess/RoleHelper.cs @@ -3,6 +3,7 @@ using Longbow.Caching; using Longbow.Caching.Configuration; using Longbow.Data; using Longbow.ExceptionManagement; +using Longbow.Data; using System; using System.Collections.Generic; using System.Data; @@ -10,6 +11,7 @@ using System.Data.Common; using System.Data.SqlClient; using System.Globalization; using System.Linq; +using System.Data.SqlClient; namespace Bootstrap.DataAccess { @@ -19,8 +21,9 @@ namespace Bootstrap.DataAccess public class RoleHelper { private const string RoleDataKey = "RoleData-CodeRoleHelper"; + private const string RolebyGroupDataKey = "RoleData-CodeRoleHelper-"; private const string RoleUserIDDataKey = "RoleData-CodeRoleHelper-"; - private const string RoleNavigationIDDataKey = "RoleData-CodeRoleHelper-Navigation-"; + private const string RoleNavigationIDDataKey = "RoleData-CodeRoleHelper-Navigation"; /// /// 查询所有角色 /// @@ -107,6 +110,8 @@ namespace Bootstrap.DataAccess } return ret; } + + /// /// 查询某个用户所拥有的角色 @@ -141,7 +146,7 @@ namespace Bootstrap.DataAccess return Roles; }, CacheSection.RetrieveDescByKey(RoleUserIDDataKey)); } - + /// /// 删除角色表 /// @@ -206,15 +211,15 @@ namespace Bootstrap.DataAccess /// public static IEnumerable RetrieveRolesByMenuId(int menuId) { + string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join NavigationRole ur on r.ID = ur.RoleID and NavigationID = @NavigationID"; string k = string.Format("{0}{1}", RoleNavigationIDDataKey, menuId); - return CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(RoleUserIDDataKey), key => + var ret = CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(RoleNavigationIDDataKey), key => { List Roles = new List(); - string sql = "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join NavigationRole ur on r.ID = ur.RoleID and NavigationID = @NavigationID"; + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", menuId, ParameterDirection.Input)); try { - DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", menuId, ParameterDirection.Input)); using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { while (reader.Read()) @@ -231,15 +236,9 @@ namespace Bootstrap.DataAccess } catch (Exception ex) { ExceptionManager.Publish(ex); } return Roles; - }, CacheSection.RetrieveDescByKey(RoleUserIDDataKey)); + }, CacheSection.RetrieveDescByKey(RoleNavigationIDDataKey)); + return ret; } - - /// - /// 保存菜单角色关系 - /// - /// - /// - /// public static bool SavaRolesByMenuId(int id, string value) { DataTable dt = new DataTable(); @@ -281,6 +280,7 @@ namespace Bootstrap.DataAccess } catch (Exception ex) { + ExceptionManager.Publish(ex); ret = false; transaction.RollbackTransaction(); } @@ -289,30 +289,102 @@ namespace Bootstrap.DataAccess } } } - // 更新缓存 private static void ClearCache(string cacheKey = null) { CacheManager.Clear(key => string.IsNullOrEmpty(cacheKey) || key == cacheKey); } + /// - /// 查询某个部门所拥有的角色 - /// - /// + /// 根据GroupId查询和该Group有关的所有Roles + /// author:liuchun + /// /// - public static IEnumerable RetrieveRolesByGroupId(int groupId) + public static IEnumerable RetrieveRolesByGroupId(int groupID) { - return null; + string key = string.Format("{0}{1}", RolebyGroupDataKey, groupID); + return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(RolebyGroupDataKey), k => + { + List Roles = new List(); + string sql= "select r.ID, r.RoleName, r.[Description], case ur.RoleID when r.ID then 'checked' else '' end [status] from Roles r left join RoleGroup ur on r.ID = ur.RoleID and GroupID = @GroupID"; + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + try + { + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupID", groupID, ParameterDirection.Input)); + using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) + { + while (reader.Read()) + { + Roles.Add(new Role() + { + ID = (int)reader[0], + RoleName = (string)reader[1], + Description = (string)reader[2], + Checked = (string)reader[3] + }); + } + } + } + catch (Exception ex) { ExceptionManager.Publish(ex); } + return Roles; + }, CacheSection.RetrieveDescByKey(RolebyGroupDataKey)); } + /// - /// 保存部门角色关系 + /// 根据GroupId更新Roles信息,删除旧的Roles信息,插入新的Roles信息 /// - /// - /// + /// /// - public static bool SaveRolesByGroupId(int id, string value) + public static bool SaveRolesByGroupId(int gid, string value) { - return false; + //构造表格 + DataTable dt = new DataTable(); + dt.Columns.Add("RoleID", typeof(int)); + dt.Columns.Add("GroupID", typeof(int)); + if (!string.IsNullOrEmpty(value)) + { + string[] roles = value.Split(','); + foreach (string role in roles) + { + DataRow r = dt.NewRow(); + r["RoleID"] = role; + r["GroupID"] = gid; + dt.Rows.Add(r); + } + } + + var trans = DBAccessManager.SqlDBAccess.BeginTransaction(); + try + { + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, string.Empty)) + { + // 执行第一个sql + cmd.CommandText = "delete from RoleGroup where GroupID=@GroupID"; + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@GroupID", gid, ParameterDirection.Input)); + DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, trans); + } + + using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)trans.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)trans.Transaction)) + { + bulk.BatchSize = 1000; + bulk.DestinationTableName = "RoleGroup"; + bulk.ColumnMappings.Add("RoleID", "RoleID"); + bulk.ColumnMappings.Add("GroupID", "GroupID"); + bulk.WriteToServer(dt); + } + + trans.CommitTransaction(); + ClearCache(); + return true; + } + catch (Exception ex) + { + ExceptionManager.Publish(ex); + trans.RollbackTransaction(); + return false; + } + + } } } \ No newline at end of file diff --git a/Bootstrap.DataAccessTests/RoleHelperTests.cs b/Bootstrap.DataAccessTests/RoleHelperTests.cs index e1576953..a3245452 100644 --- a/Bootstrap.DataAccessTests/RoleHelperTests.cs +++ b/Bootstrap.DataAccessTests/RoleHelperTests.cs @@ -57,5 +57,21 @@ namespace Bootstrap.DataAccess.Tests var result = RoleHelper.SaveRolesByUserId(1,"3"); Assert.IsTrue(result == true, "保存用户角色关系失败"); } + + [TestMethod()] + public void RetrieveRoleByGroupIDTest() + { + var result = RoleHelper.RetrieveRolesByGroupId(1); + Assert.IsTrue((result.Count() == 8), "RetrieveRoleByGroupID测试失败"); + + } + + [TestMethod()] + public void SaveRoleByGroupIDTest() + { + var result = RoleHelper.SaveRolesByGroupId(1,"1,2,3,4"); + Assert.IsTrue(result, "SaveRoleByGroupID测试失败"); + + } } }