diff --git a/Bootstrap.Admin/Content/js/framework.js b/Bootstrap.Admin/Content/js/framework.js index e19bb468..2de92779 100644 --- a/Bootstrap.Admin/Content/js/framework.js +++ b/Bootstrap.Admin/Content/js/framework.js @@ -218,7 +218,8 @@ Role.getRolesByUserId = function (userId, callback) { $.ajax({ url: '../api/Roles/' + userId, - type: 'GET', + data: { "": "user" }, + type: 'POST', success: function (result) { callback(result); }, @@ -233,7 +234,7 @@ Role.saveRolesByUserId = function (userId, roleIds, callback) { $.ajax({ url: '../api/Roles/' + userId, - data: { "": roleIds }, + data: { "roleIds": roleIds,"type":"user" }, type: 'PUT', success: function (result) { callback(result); diff --git a/Bootstrap.Admin/Controllers/RolesController.cs b/Bootstrap.Admin/Controllers/RolesController.cs index cea6f2f4..8385a0e2 100644 --- a/Bootstrap.Admin/Controllers/RolesController.cs +++ b/Bootstrap.Admin/Controllers/RolesController.cs @@ -1,7 +1,7 @@ using Bootstrap.Admin.Models; using Bootstrap.DataAccess; +using Newtonsoft.Json.Linq; using System.Collections.Generic; -using System.Linq; using System.Web.Http; namespace Bootstrap.Admin.Controllers @@ -23,10 +23,17 @@ namespace Bootstrap.Admin.Controllers /// /// /// - [HttpGet] - public IEnumerable Get(int id) + [HttpPost] + public IEnumerable Post(int id, [FromBody]string value) { - return RoleHelper.RetrieveRolesByUserId(); + if (value == "user") + { + return RoleHelper.RetrieveRolesByUserId(id.ToString()); + } + else + { + return null; + } } /// /// @@ -34,9 +41,13 @@ namespace Bootstrap.Admin.Controllers /// /// [HttpPut] - public bool Put(int id, [FromBody]string value) + public bool Put(int id, [FromBody]JObject value) { - return RoleHelper.SaveRolesByUserId(id, value); + dynamic json = value; + string roleIds = json.roleIds; + if (json.type == "user") + return RoleHelper.SaveRolesByUserId(id, roleIds); + return false; } /// /// diff --git a/Bootstrap.Admin/Scripts/Users.js b/Bootstrap.Admin/Scripts/Users.js index 60b6d32f..34f01472 100644 --- a/Bootstrap.Admin/Scripts/Users.js +++ b/Bootstrap.Admin/Scripts/Users.js @@ -13,10 +13,14 @@ assign: [{ id: 'btn_assignRole', click: function (row) { - Role.getRolesByUserId(1, function (roles) { + Role.getRolesByUserId(row.ID, function (roles) { $("#dialogRole .modal-title").text($.format('{0}-角色授权窗口', row.DisplayName)); var data = $.map(roles, function (element, index) { - return $.format('
', element.ID, element.RoleName); + 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'); @@ -34,7 +38,14 @@ var roleIds = $('#dialogRole :checked').map(function (index, element) { return $(element).val(); }).toArray().join(','); - Role.saveRolesByUserId(userId, roleIds, function () { }); + Role.saveRolesByUserId(userId, roleIds, function (result) { + if (result) { + $('#dialogRole').modal("hide"); + swal("成功", "修改角色", "success"); + } else { + swal("失败", "修改角色", "error"); + } + }); } }] }, diff --git a/Bootstrap.Admin/Web.config b/Bootstrap.Admin/Web.config index 4327c445..23096ec1 100644 --- a/Bootstrap.Admin/Web.config +++ b/Bootstrap.Admin/Web.config @@ -28,6 +28,7 @@ + diff --git a/Bootstrap.DataAccess/Role.cs b/Bootstrap.DataAccess/Role.cs index bd8784ba..c4ae3986 100644 --- a/Bootstrap.DataAccess/Role.cs +++ b/Bootstrap.DataAccess/Role.cs @@ -14,5 +14,9 @@ /// 获得/设置 角色描述 ///
public string Description { get; set; } + /// + /// 获取/设置 用户角色状态 + /// + public int IsSelect { get; set; } } } diff --git a/Bootstrap.DataAccess/RoleHelper.cs b/Bootstrap.DataAccess/RoleHelper.cs index 31931267..efec2c0d 100644 --- a/Bootstrap.DataAccess/RoleHelper.cs +++ b/Bootstrap.DataAccess/RoleHelper.cs @@ -1,11 +1,13 @@ using Longbow; using Longbow.Caching; using Longbow.Caching.Configuration; +using Longbow.Data; using Longbow.ExceptionManagement; using System; using System.Collections.Generic; using System.Data; using System.Data.Common; +using System.Data.SqlClient; using System.Globalization; using System.Linq; @@ -14,6 +16,7 @@ namespace Bootstrap.DataAccess public class RoleHelper { private const string RoleDataKey = "RoleData-CodeRoleHelper"; + private const string RoleUserIDDataKey = "RoleData-CodeRoleHelper-"; /// /// 查询所有角色 /// @@ -47,28 +50,93 @@ namespace Bootstrap.DataAccess return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase)); } /// - /// + /// 保存用户角色关系 /// /// /// /// public static bool SaveRolesByUserId(int id, string value) { - //UNDONE: 编写通过用户ID保存当前授权角色的方法 - return true; - } + DataTable dt = new DataTable(); + dt.Columns.Add("UserID", 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["UserID"] = id; + row["RoleID"] = roleID; + dt.Rows.Add(row); + } + } + string sql = "delete from UserRole where UserID=@UserID;"; + using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql)) + { + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", 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 = "UserRole"; + bulk.ColumnMappings.Add("UserID", "UserID"); + 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; + } + } + } + } /// - /// + /// 查询某个用户所拥有的角色 /// /// - public static IEnumerable RetrieveRolesByUserId() + public static IEnumerable RetrieveRolesByUserId(string userId) { - //UNDONE: 编写通过用户ID获取所有角色的方法 - return new List() { - new Role() { ID = 1, RoleName = "TestRole1", Description = "测试角色1" }, - new Role() { ID = 2, RoleName = "TestRole2", Description = "测试角色2" } - }; + 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 => + { + List Roles = new List(); + DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql); + cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@UserID", userId, 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(RoleUserIDDataKey)); + return ret; } /// /// 删除角色表 diff --git a/Bootstrap.DataAccessTests/RoleHelperTests.cs b/Bootstrap.DataAccessTests/RoleHelperTests.cs index 50230fdf..54ed412b 100644 --- a/Bootstrap.DataAccessTests/RoleHelperTests.cs +++ b/Bootstrap.DataAccessTests/RoleHelperTests.cs @@ -45,5 +45,17 @@ namespace Bootstrap.DataAccess.Tests var role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == "RoleUnitTest"); Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除用户失败"); } + [TestMethod()] + public void RetrieveRolesByUserIdTest() + { + var result = RoleHelper.RetrieveRolesByUserId("1"); + Assert.IsTrue(result.Count() >= 0, "用户查询角色关系失败!"); + } + [TestMethod()] + public void SaveRolesByUserIdTest() + { + var result = RoleHelper.SaveRolesByUserId(1,"3"); + Assert.IsTrue(result == true, "保存用户角色关系失败"); + } } }