From 35c915c3e8b9358e08e3f12471751f729553f153 Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Fri, 28 Oct 2016 13:36:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E6=9D=8E=E7=90=A6=E6=8F=90?= =?UTF-8?q?=E4=BA=A4=E7=9A=84=E9=80=9A=E8=BF=87=E7=94=A8=E6=88=B7=E6=8C=87?= =?UTF-8?q?=E6=B4=BE=E8=A7=92=E8=89=B2=E5=8A=9F=E8=83=BD=EF=BC=8C=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=A0=B7=E5=BC=8F=EF=BC=8C=E6=A0=BC=E5=BC=8F=E5=8C=96?= =?UTF-8?q?=E8=A7=92=E8=89=B2=E5=88=97=E8=A1=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Bootstrap.Admin/Content/css/admin.css | 12 +++ Bootstrap.Admin/Content/js/framework.js | 33 +++---- .../Controllers/RolesController.cs | 42 +++++--- Bootstrap.Admin/Scripts/Users.js | 6 +- Bootstrap.DataAccess/Role.cs | 44 ++++----- Bootstrap.DataAccess/RoleHelper.cs | 98 +++++++++++-------- Bootstrap.DataAccessTests/RoleHelperTests.cs | 2 +- 7 files changed, 139 insertions(+), 98 deletions(-) diff --git a/Bootstrap.Admin/Content/css/admin.css b/Bootstrap.Admin/Content/css/admin.css index 063d94de..da4f940a 100644 --- a/Bootstrap.Admin/Content/css/admin.css +++ b/Bootstrap.Admin/Content/css/admin.css @@ -146,3 +146,15 @@ ul.sidebar-menu { transition: all 0.3s ease; color: #aeb2b7; } + +/*Modal Dialog Checkbox*/ +.modal-body .checkbox { + padding: 5px; + margin-top: 0; + margin-bottom: 0; +} + + .modal-body .checkbox label input[type='checkbox'] { + margin-right: 6px; + top: 2px; + } diff --git a/Bootstrap.Admin/Content/js/framework.js b/Bootstrap.Admin/Content/js/framework.js index 2ee8e251..0c6e879f 100644 --- a/Bootstrap.Admin/Content/js/framework.js +++ b/Bootstrap.Admin/Content/js/framework.js @@ -214,22 +214,27 @@ }; - Role = {}; - Role.getRolesByUserId = function (userId, callback) { + var processRolesData = function (options) { + var data = $.extend({ data: { type: "" }, method: "POST", Id: "" }, options); $.ajax({ - url: '../api/Roles/' + userId, - data: { "": "user" }, - type: 'POST', + url: '../api/Roles/' + data.Id, + data: data.data, + type: data.method, success: function (result) { - callback(result); + if ($.isFunction(data.callback)) data.callback(result); }, error: function (XMLHttpRequest, textStatus, errorThrown) { - callback(); + if ($.isFunction(data.callback)) data.callback(false); } }); + } + + Role = {}; + Role.getRolesByUserId = function (userId, callback) { + processRolesData({ Id: userId, callback: callback, data: { type: "user" } }); }; Role.getRolesByGroupId = function (groupId) { - + processRolesData({ Id: groupId, callback: callback, data: { type: "group" } }); }; //查询菜单对应角色 @@ -248,17 +253,7 @@ }; Role.saveRolesByUserId = function (userId, roleIds, callback) { - $.ajax({ - url: '../api/Roles/' + userId, - data: { "roleIds": roleIds,"type":"user" }, - type: 'PUT', - success: function (result) { - callback(result); - }, - error: function (XMLHttpRequest, textStatus, errorThrown) { - callback(); - } - }); + processRolesData({ Id: userId, callback: callback, method: "PUT", data: { type: "user", roleIds: roleIds } }); } //保存菜单对应角色 diff --git a/Bootstrap.Admin/Controllers/RolesController.cs b/Bootstrap.Admin/Controllers/RolesController.cs index 6381acfe..9e198f40 100644 --- a/Bootstrap.Admin/Controllers/RolesController.cs +++ b/Bootstrap.Admin/Controllers/RolesController.cs @@ -2,6 +2,7 @@ using Bootstrap.DataAccess; using Newtonsoft.Json.Linq; using System.Collections.Generic; +using System.Linq; using System.Web.Http; namespace Bootstrap.Admin.Controllers @@ -24,15 +25,22 @@ namespace Bootstrap.Admin.Controllers /// /// [HttpPost] - public IEnumerable Post(int id, [FromBody]string value) + public IEnumerable Post(int id, [FromBody]JObject value) { - if (value == "user") + var ret = new List(); + dynamic json = value; + switch ((string)json.type) { - return RoleHelper.RetrieveRolesByUserId(id.ToString()); - } - else if(value == "menu") - { - return RoleHelper.RetrieveRolesByMenuId(id.ToString()); + case "user": + ret = RoleHelper.RetrieveRolesByUserId(id).ToList(); + break; + case "group": + ret = RoleHelper.RetrieveRolesByGroupId(id).ToList(); + break; + case "menu" + ret = RoleHelper.RetrieveRolesByMenuId(id.ToString()); + default: + break; } else { @@ -47,13 +55,23 @@ namespace Bootstrap.Admin.Controllers [HttpPut] public bool Put(int id, [FromBody]JObject value) { + var ret = false; dynamic json = value; string roleIds = json.roleIds; - if (json.type == "user") - return RoleHelper.SaveRolesByUserId(id, roleIds); - if (json.type == "menu") - return RoleHelper.SavaRolesByMenuId(id,roleIds); - return false; + switch ((string)json.type) + { + case "user": + ret = RoleHelper.SaveRolesByUserId(id, roleIds); + break; + case "group": + ret = RoleHelper.SaveRolesByGroupId(id, roleIds); + break; + case "menu": + ret = RoleHelper.SavaRolesByMenuId(id, roleIds); + default: + break; + } + return ret; } /// /// diff --git a/Bootstrap.Admin/Scripts/Users.js b/Bootstrap.Admin/Scripts/Users.js index 34f01472..44de58c4 100644 --- a/Bootstrap.Admin/Scripts/Users.js +++ b/Bootstrap.Admin/Scripts/Users.js @@ -16,11 +16,7 @@ Role.getRolesByUserId(row.ID, function (roles) { $("#dialogRole .modal-title").text($.format('{0}-角色授权窗口', row.DisplayName)); 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); - } + return $.format('
', element.ID, element.RoleName, element.Checked, element.Description); }).join(''); $('#dialogRole form').html(data); $('#dialogRole').modal('show'); diff --git a/Bootstrap.DataAccess/Role.cs b/Bootstrap.DataAccess/Role.cs index c4ae3986..ebab5ce3 100644 --- a/Bootstrap.DataAccess/Role.cs +++ b/Bootstrap.DataAccess/Role.cs @@ -1,22 +1,22 @@ -namespace Bootstrap.DataAccess -{ - public class Role - { - /// - /// 获得/设置 角色主键ID - /// - public int ID { get; set; } - /// - /// 获得/设置 角色名称 - /// - public string RoleName { get; set; } - /// - /// 获得/设置 角色描述 - /// - public string Description { get; set; } - /// - /// 获取/设置 用户角色状态 - /// - public int IsSelect { get; set; } - } -} +namespace Bootstrap.DataAccess +{ + public class Role + { + /// + /// 获得/设置 角色主键ID + /// + public int ID { get; set; } + /// + /// 获得/设置 角色名称 + /// + public string RoleName { get; set; } + /// + /// 获得/设置 角色描述 + /// + public string Description { get; set; } + /// + /// 获取/设置 用户角色关联状态 checked 标示已经关联 '' 标示未关联 + /// + public string Checked { get; set; } + } +} diff --git a/Bootstrap.DataAccess/RoleHelper.cs b/Bootstrap.DataAccess/RoleHelper.cs index d7115882..d43d4e45 100644 --- a/Bootstrap.DataAccess/RoleHelper.cs +++ b/Bootstrap.DataAccess/RoleHelper.cs @@ -13,8 +13,12 @@ using System.Linq; namespace Bootstrap.DataAccess { + /// + /// + /// public class RoleHelper { + // UNDONE: 两个缓存考虑可以共用,待完善 private const string RoleDataKey = "RoleData-CodeRoleHelper"; private const string RoleUserIDDataKey = "RoleData-CodeRoleHelper-"; private const string RoleNavigationIDDataKey = "RoleData-CodeRoleHelper-Navigation"; @@ -54,72 +58,81 @@ namespace Bootstrap.DataAccess /// 保存用户角色关系 ///
/// - /// + /// /// - public static bool SaveRolesByUserId(int id, string value) + public static bool SaveRolesByUserId(int id, string roleIds) { + var ret = false; DataTable dt = new DataTable(); dt.Columns.Add("UserID", typeof(int)); dt.Columns.Add("RoleID", typeof(int)); //判断用户是否选定角色 - if (!string.IsNullOrEmpty(value)) + if (!string.IsNullOrEmpty(roleIds)) { - string[] roleIDs = value.Split(','); - foreach (string roleID in roleIDs) + roleIds.Split(',').ToList().ForEach(roleId => { DataRow row = dt.NewRow(); - row["UserID"] = id; - row["RoleID"] = roleID; - dt.Rows.Add(row); - } + dt.Rows.Add(id, roleId); + }); } - - string sql = "delete from UserRole where UserID=@UserID;"; - using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) + using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction()) { - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", id, ParameterDirection.Input)); - using (TransactionPackage transaction = DBAccessManager.SqlDBAccess.BeginTransaction()) + try { - using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) + // delete user from config table + string sql = "delete from UserRole where UserID = @UserID;"; + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) { - bulk.BatchSize = 1000; - bulk.DestinationTableName = "UserRole"; - bulk.ColumnMappings.Add("UserID", "UserID"); - bulk.ColumnMappings.Add("RoleID", "RoleID"); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", id, ParameterDirection.Input)); + DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction); - bool ret = true; - try + // insert batch data into config table + using (SqlBulkCopy bulk = new SqlBulkCopy((SqlConnection)transaction.Transaction.Connection, SqlBulkCopyOptions.Default, (SqlTransaction)transaction.Transaction)) { - DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd, transaction); + bulk.BatchSize = 1000; + bulk.DestinationTableName = "UserRole"; + bulk.ColumnMappings.Add("UserID", "UserID"); + bulk.ColumnMappings.Add("RoleID", "RoleID"); bulk.WriteToServer(dt); transaction.CommitTransaction(); - ClearCache(); } - catch (Exception ex) - { - ret = false; - transaction.RollbackTransaction(); - } - return ret; } + ret = true; + ClearCache(); + } + catch (Exception ex) + { + ExceptionManager.Publish(ex); + transaction.RollbackTransaction(); } } + return ret; + } + /// + /// + /// + /// + /// + /// + public static bool SaveRolesByGroupId(int id, string value) + { + return true; } /// /// 查询某个用户所拥有的角色 /// /// - public static IEnumerable RetrieveRolesByUserId(string userId) + public static IEnumerable RetrieveRolesByUserId(int userId) { - string sql = "select *,case when (ID in( select RoleID from UserRole where UserID=@UserID)) then 1 else 0 end as IsSelect from Roles"; string k = string.Format("{0}{1}", RoleUserIDDataKey, userId); - var ret = CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(RoleUserIDDataKey), key => + return CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(RoleUserIDDataKey), key => { List Roles = new List(); - DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); - cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", userId, ParameterDirection.Input)); + 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 UserRole ur on r.ID = ur.RoleID and UserID = @UserID"; try { + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", userId, ParameterDirection.Input)); using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd)) { while (reader.Read()) @@ -129,7 +142,7 @@ namespace Bootstrap.DataAccess ID = (int)reader[0], RoleName = (string)reader[1], Description = (string)reader[2], - IsSelect = (int)reader[3] + Checked = (string)reader[3] }); } } @@ -137,7 +150,15 @@ namespace Bootstrap.DataAccess catch (Exception ex) { ExceptionManager.Publish(ex); } return Roles; }, CacheSection.RetrieveDescByKey(RoleUserIDDataKey)); - return ret; + } + /// + /// + /// + /// + /// + public static IEnumerable RetrieveRolesByGroupId(int groupId) + { + return new List(); } /// /// 删除角色表 @@ -163,7 +184,6 @@ namespace Bootstrap.DataAccess } return ret; } - /// /// 保存新建/更新的角色信息 /// @@ -290,9 +310,9 @@ namespace Bootstrap.DataAccess } // 更新缓存 - private static void ClearCache() + private static void ClearCache(string cacheKey = null) { - CacheManager.Clear(key => key.Contains("RoleData-")); + CacheManager.Clear(key => string.IsNullOrEmpty(cacheKey) || key == cacheKey); } } } \ No newline at end of file diff --git a/Bootstrap.DataAccessTests/RoleHelperTests.cs b/Bootstrap.DataAccessTests/RoleHelperTests.cs index 54ed412b..e1576953 100644 --- a/Bootstrap.DataAccessTests/RoleHelperTests.cs +++ b/Bootstrap.DataAccessTests/RoleHelperTests.cs @@ -48,7 +48,7 @@ namespace Bootstrap.DataAccess.Tests [TestMethod()] public void RetrieveRolesByUserIdTest() { - var result = RoleHelper.RetrieveRolesByUserId("1"); + var result = RoleHelper.RetrieveRolesByUserId(1); Assert.IsTrue(result.Count() >= 0, "用户查询角色关系失败!"); } [TestMethod()]