From 2e30c812456438dbffea7b02256fa717a51c509b Mon Sep 17 00:00:00 2001 From: sunnian <853128958@qq.com> Date: Fri, 28 Oct 2016 11:12:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E8=8F=9C=E5=8D=95=E8=A7=92?= =?UTF-8?q?=E8=89=B2=E6=8E=88=E6=9D=83=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=AE=8C?= =?UTF-8?q?=E5=96=84=E8=8F=9C=E5=8D=95=E6=9F=A5=E8=AF=A2=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 | 31 +++++++ .../Controllers/RolesController.cs | 6 ++ Bootstrap.Admin/Models/QueryMenuOption.cs | 4 + Bootstrap.Admin/Scripts/Menus.js | 37 +++++++- Bootstrap.Admin/Views/Admin/Menus.cshtml | 8 ++ Bootstrap.Admin/Web.config | 3 +- Bootstrap.DataAccess/RoleHelper.cs | 93 +++++++++++++++++++ 7 files changed, 180 insertions(+), 2 deletions(-) diff --git a/Bootstrap.Admin/Content/js/framework.js b/Bootstrap.Admin/Content/js/framework.js index 2de92779..2ee8e251 100644 --- a/Bootstrap.Admin/Content/js/framework.js +++ b/Bootstrap.Admin/Content/js/framework.js @@ -231,6 +231,22 @@ Role.getRolesByGroupId = function (groupId) { }; + + //查询菜单对应角色 + Role.getRolesByMenuId = function (menuId, callback) { + $.ajax({ + url: '../api/Roles/' + menuId, + data: { "": "menu" }, + type: 'POST', + success: function (result) { + callback(result); + }, + error: function (XMLHttpRequest, textStatus, errorThrown) { + callback(); + } + }); + }; + Role.saveRolesByUserId = function (userId, roleIds, callback) { $.ajax({ url: '../api/Roles/' + userId, @@ -245,6 +261,21 @@ }); } + //保存菜单对应角色 + Role.saveRolesByMenuId = function (menuId, roleIds, callback) { + $.ajax({ + url: '../api/Roles/' + menuId, + data: { "roleIds": roleIds, "type": "menu" }, + type: 'PUT', + success: function (result) { + callback(result); + }, + error: function (XMLHttpRequest, textStatus, errorThrown) { + callback(); + } + }); + }; + Group = {}; Group.getGroupsByUserId = function (userId) { diff --git a/Bootstrap.Admin/Controllers/RolesController.cs b/Bootstrap.Admin/Controllers/RolesController.cs index 8385a0e2..6381acfe 100644 --- a/Bootstrap.Admin/Controllers/RolesController.cs +++ b/Bootstrap.Admin/Controllers/RolesController.cs @@ -30,6 +30,10 @@ namespace Bootstrap.Admin.Controllers { return RoleHelper.RetrieveRolesByUserId(id.ToString()); } + else if(value == "menu") + { + return RoleHelper.RetrieveRolesByMenuId(id.ToString()); + } else { return null; @@ -47,6 +51,8 @@ namespace Bootstrap.Admin.Controllers string roleIds = json.roleIds; if (json.type == "user") return RoleHelper.SaveRolesByUserId(id, roleIds); + if (json.type == "menu") + return RoleHelper.SavaRolesByMenuId(id,roleIds); return false; } /// diff --git a/Bootstrap.Admin/Models/QueryMenuOption.cs b/Bootstrap.Admin/Models/QueryMenuOption.cs index 833d45b6..0afd0276 100644 --- a/Bootstrap.Admin/Models/QueryMenuOption.cs +++ b/Bootstrap.Admin/Models/QueryMenuOption.cs @@ -25,6 +25,10 @@ namespace Bootstrap.Admin.Models { data = data.Where(t => t.Name.Contains(Name)); } + if (!string.IsNullOrEmpty(Category)) + { + data = data.Where(t => t.Category.ToString().Equals(Category)); + } var ret = new QueryData(); ret.total = data.Count(); // TODO: 通过option.Sort属性判断对那列进行排序,现在统一对名称列排序 diff --git a/Bootstrap.Admin/Scripts/Menus.js b/Bootstrap.Admin/Scripts/Menus.js index f18addfd..f1f4f993 100644 --- a/Bootstrap.Admin/Scripts/Menus.js +++ b/Bootstrap.Admin/Scripts/Menus.js @@ -11,7 +11,42 @@ Url: "url", Category: "category" } - }) + }), + click: { + assign: [{ + id: 'btn_assignRole', + click: function (row) { + Role.getRolesByMenuId(row.ID, function (roles) { + $("#dialogRole .modal-title").text($.format('{0}-角色授权窗口', row.Name)); + var data = $.map(roles, function (element, index) { + if (element.IsSelect == 1) { + return $.format('
', element.ID, element.RoleName); + } else if (element.IsSelect == 0) { + return $.format('
', element.ID, element.RoleName); + } + }).join(''); + $('#dialogRole form').html(data); + $('#dialogRole').modal('show'); + }); + } + }, { + id: 'btnSubmitUserRole', + click: function (row) { + var menuId = row.ID; + var roleIds = $('#dialogRole :checked').map(function (index, element) { + return $(element).val(); + }).toArray().join(','); + Role.saveRolesByMenuId(menuId, roleIds, function (result) { + if (result) { + $('#dialogRole').modal("hide"); + swal("成功", "修改角色", "success"); + } else { + swal("失败", "修改角色", "error"); + } + }); + } + }] + } }); $('table').smartTable({ diff --git a/Bootstrap.Admin/Views/Admin/Menus.cshtml b/Bootstrap.Admin/Views/Admin/Menus.cshtml index 41e6730d..cded81bf 100644 --- a/Bootstrap.Admin/Views/Admin/Menus.cshtml +++ b/Bootstrap.Admin/Views/Admin/Menus.cshtml @@ -27,6 +27,11 @@ } +@section toolbar { + +} @section modal { +} +@section customModal { + @Html.Partial("RoleConfig") } \ No newline at end of file diff --git a/Bootstrap.Admin/Web.config b/Bootstrap.Admin/Web.config index 23096ec1..811f1a94 100644 --- a/Bootstrap.Admin/Web.config +++ b/Bootstrap.Admin/Web.config @@ -29,7 +29,8 @@ - + + diff --git a/Bootstrap.DataAccess/RoleHelper.cs b/Bootstrap.DataAccess/RoleHelper.cs index efec2c0d..d7115882 100644 --- a/Bootstrap.DataAccess/RoleHelper.cs +++ b/Bootstrap.DataAccess/RoleHelper.cs @@ -17,6 +17,7 @@ namespace Bootstrap.DataAccess { private const string RoleDataKey = "RoleData-CodeRoleHelper"; private const string RoleUserIDDataKey = "RoleData-CodeRoleHelper-"; + private const string RoleNavigationIDDataKey = "RoleData-CodeRoleHelper-Navigation"; /// /// 查询所有角色 /// @@ -196,6 +197,98 @@ namespace Bootstrap.DataAccess return ret; } + /// + /// 查询某个菜单所拥有的角色 + /// + /// + /// + public static IEnumerable RetrieveRolesByMenuId(string menuId) + { + string sql = "select *,case when (ID in( select RoleID from NavigationRole where NavigationID=@NavigationID)) then 1 else 0 end as IsSelect from Roles"; + string k = string.Format("{0}{1}", RoleNavigationIDDataKey, menuId); + var ret = CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(RoleNavigationIDDataKey), key => + { + List Roles = new List(); + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", menuId, ParameterDirection.Input)); + try + { + 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], + IsSelect = (int)reader[3] + }); + } + } + } + catch (Exception ex) { ExceptionManager.Publish(ex); } + return Roles; + }, CacheSection.RetrieveDescByKey(RoleNavigationIDDataKey)); + return ret; + } + + /// + /// 保存菜单角色关系 + /// + /// + /// + /// + public static bool SavaRolesByMenuId(int id, string value) + { + DataTable dt = new DataTable(); + dt.Columns.Add("NavigationID", typeof(int)); + dt.Columns.Add("RoleID", typeof(int)); + //判断用户是否选定角色 + if (!string.IsNullOrEmpty(value)) + { + string[] roleIDs = value.Split(','); + foreach (string roleID in roleIDs) + { + DataRow row = dt.NewRow(); + row["NavigationID"] = id; + row["RoleID"] = roleID; + dt.Rows.Add(row); + } + } + + string sql = "delete from NavigationRole where NavigationID=@NavigationID;"; + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) + { + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@NavigationID", id, ParameterDirection.Input)); + using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction()) + { + using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) + { + bulk.BatchSize = 1000; + bulk.DestinationTableName = "NavigationRole"; + bulk.ColumnMappings.Add("NavigationID", "NavigationID"); + bulk.ColumnMappings.Add("RoleID", "RoleID"); + + bool ret = true; + try + { + DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction); + bulk.WriteToServer(dt); + transaction.CommitTransaction(); + ClearCache(); + } + catch (Exception ex) + { + ret = false; + transaction.RollbackTransaction(); + } + return ret; + } + } + } + } + // 更新缓存 private static void ClearCache() {