diff --git a/Bootstrap.Admin/Bootstrap.Admin.csproj b/Bootstrap.Admin/Bootstrap.Admin.csproj
index 0b1b3510..b84cdf54 100644
--- a/Bootstrap.Admin/Bootstrap.Admin.csproj
+++ b/Bootstrap.Admin/Bootstrap.Admin.csproj
@@ -226,6 +226,7 @@
+
Web.config
diff --git a/Bootstrap.Admin/Content/js/framework.js b/Bootstrap.Admin/Content/js/framework.js
index f87932ee..80f88be2 100644
--- a/Bootstrap.Admin/Content/js/framework.js
+++ b/Bootstrap.Admin/Content/js/framework.js
@@ -254,6 +254,39 @@
processRolesData({ Id: menuId, callback: callback, method: "PUT", data: { type: "menu", roleIds: roleIds } });
};
+ var processUsersData = function (options) {
+ var data = $.extend({ data: { type: "" }, method: "POST", Id: "" }, options);
+ $.ajax({
+ url: '../api/Users/' + data.Id,
+ data: data.data,
+ type: data.method,
+ success: function (result) {
+ if ($.isFunction(data.callback)) {
+ if ($.isArray(result)) {
+ var html = $.map(result, function (element, index) {
+ return $.format('
{1}
', element.ID, element.DisplayName, element.Checked);
+ }).join('');
+ data.callback(html);
+ }
+ else
+ data.callback(result);
+ }
+ else { data.callback(false); }
+ },
+ error: function (XMLHttpRequest, textStatus, errorThrown) {
+ if ($.isFunction(data.callback)) data.callback(false);
+ }
+ });
+ }
+
+ User = {};
+ User.getUsersByRoleId = function (roleId, callback) {
+ processUsersData({ Id: roleId, callback: callback, data: { type: "role" } });
+ };
+ User.saveUsersByRoleId = function (roleId, userIds, callback) {
+ processUsersData({ Id: roleId, callback: callback, method: "PUT", data: { type: "role", userIds: userIds } });
+ }
+
Group = {};
Group.getGroupsByUserId = function (userId) {
diff --git a/Bootstrap.Admin/Controllers/UsersController.cs b/Bootstrap.Admin/Controllers/UsersController.cs
index 117fd0ae..fcce52d4 100644
--- a/Bootstrap.Admin/Controllers/UsersController.cs
+++ b/Bootstrap.Admin/Controllers/UsersController.cs
@@ -2,6 +2,8 @@
using Bootstrap.DataAccess;
using System.Web.Http;
using System.Linq;
+using System.Collections.Generic;
+using Newtonsoft.Json.Linq;
namespace Bootstrap.Admin.Controllers
{
@@ -20,6 +22,28 @@ namespace Bootstrap.Admin.Controllers
{
return value.RetrieveData();
}
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ [HttpPost]
+ public IEnumerable Post(int id, [FromBody]JObject value)
+ {
+ var ret = new List();
+ dynamic json = value;
+ switch ((string)json.type)
+ {
+ case "role":
+ ret = UserHelper.RetrieveUsersByRoleId(id).ToList();
+ break;
+ default:
+ break;
+ }
+ return ret;
+ }
+
///
///
///
@@ -39,6 +63,24 @@ namespace Bootstrap.Admin.Controllers
{
return UserHelper.SaveUser(value);
}
+
+ [HttpPut]
+ public bool Put(int id, [FromBody]JObject value)
+ {
+ var ret = false;
+ dynamic json = value;
+ string userIds = json.userIds;
+ switch ((string)json.type)
+ {
+ case "role":
+ ret = UserHelper.SaveUsersByRoleId(id, userIds);
+ break;
+ default:
+ break;
+ }
+ return ret;
+ }
+
///
///
///
diff --git a/Bootstrap.Admin/Scripts/Roles.js b/Bootstrap.Admin/Scripts/Roles.js
index 202e0ad0..8167cb05 100644
--- a/Bootstrap.Admin/Scripts/Roles.js
+++ b/Bootstrap.Admin/Scripts/Roles.js
@@ -7,7 +7,46 @@
RoleName: "roleName",
Description: "roleDesc"
}
- })
+ }),
+ click: {
+ assign: [{
+ id: 'btn_assignUser',
+ click: function (row) {
+ User.getUsersByRoleId(row.ID, function (data) {
+ $("#dialogUser.modal-title").text($.format('{0}-用户授权窗口', row.RoleName));
+ $('#dialogUser form').html(data);
+ $('#dialogUser').modal('show');
+ })
+ }
+ }, {
+ id: 'btn_assignGroup',
+ click: function (row) {
+ var roleId = row.ID;
+ }
+ }, {
+ id: 'btnSubmitRoleUser',
+ click: function (row) {
+ var roleId = row.ID;
+ var userIds = $('#dialogUser :checked').map(function (index, element) {
+ return $(element).val();
+ }).toArray().join(',');
+ User.saveUsersByRoleId(roleId, userIds, function (result) {
+ if (result) {
+ $('#dialogUser').modal('hide');
+ swal("成功", "修改用户", "success");
+ }
+ else {
+ swal("失败", "修改用户", "error");
+ }
+ });
+ }
+ }]
+ },
+ success: function (src, data) {
+ if (src === 'save' && data.ID === $('#roleId').val()) {
+ //$('.username').text(data.DisplayName);
+ }
+ }
});
$('table').smartTable({
diff --git a/Bootstrap.Admin/Views/Admin/Roles.cshtml b/Bootstrap.Admin/Views/Admin/Roles.cshtml
index 18f39d99..a97e22a3 100644
--- a/Bootstrap.Admin/Views/Admin/Roles.cshtml
+++ b/Bootstrap.Admin/Views/Admin/Roles.cshtml
@@ -19,7 +19,7 @@
-
+
@@ -27,6 +27,14 @@
}
+@section toolbar{
+
+
+}
@section modal {
+}
+@section customModal{
+ @Html.Partial("UserConfig")
}
\ No newline at end of file
diff --git a/Bootstrap.Admin/Views/Shared/UserConfig.cshtml b/Bootstrap.Admin/Views/Shared/UserConfig.cshtml
new file mode 100644
index 00000000..17544335
--- /dev/null
+++ b/Bootstrap.Admin/Views/Shared/UserConfig.cshtml
@@ -0,0 +1,17 @@
+
diff --git a/Bootstrap.Admin/Web.config b/Bootstrap.Admin/Web.config
index 5ee79f09..b9a3cffb 100644
--- a/Bootstrap.Admin/Web.config
+++ b/Bootstrap.Admin/Web.config
@@ -25,6 +25,7 @@
+
diff --git a/Bootstrap.DataAccess/User.cs b/Bootstrap.DataAccess/User.cs
index 4bf9f5ba..1dc44c5b 100644
--- a/Bootstrap.DataAccess/User.cs
+++ b/Bootstrap.DataAccess/User.cs
@@ -25,5 +25,9 @@
/// 获取/设置 显示名称
///
public string DisplayName { get; set; }
+ ///
+ /// 获取/设置 角色用户关联状态 checked 标示已经关联 '' 标示未关联
+ ///
+ public string Checked { get; set; }
}
}
diff --git a/Bootstrap.DataAccess/UserHelper.cs b/Bootstrap.DataAccess/UserHelper.cs
index 3f7d7b0f..403f6066 100644
--- a/Bootstrap.DataAccess/UserHelper.cs
+++ b/Bootstrap.DataAccess/UserHelper.cs
@@ -6,6 +6,7 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
+using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
@@ -18,6 +19,7 @@ namespace Bootstrap.DataAccess
{
private const string UserDataKey = "UserData-CodeUserHelper";
private const string UserDisplayNameDataKey = "UserData-CodeUserHelper-";
+ private const string UserRoleIDDataKey = "UserData-CodeUserHelper-Role-";
///
/// 查询所有用户
///
@@ -161,6 +163,93 @@ namespace Bootstrap.DataAccess
{
CacheManager.Clear(key => key == UserDataKey);
CacheManager.Clear(key => key.Contains(UserDisplayNameDataKey));
+ CacheManager.Clear(key => key.Contains(UserRoleIDDataKey));
+ }
+
+
+ ///
+ /// 通过roleId获取所有用户
+ ///
+ ///
+ ///
+ public static IEnumerable RetrieveUsersByRoleId(int roleId)
+ {
+
+ string key = string.Format("{0}{1}", UserRoleIDDataKey, roleId);
+ return CacheManager.GetOrAdd(key, CacheSection.RetrieveIntervalByKey(UserDisplayNameDataKey), k =>
+ {
+ List Users = new List();
+ string sql = "select u.ID,u.UserName,u.DisplayName,case ur.UserID when u.ID then 'checked' else '' end [status] from Users u left join UserRole ur on u.ID=ur.UserID and RoleID =@RoleID";
+ DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
+ cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleID", roleId, ParameterDirection.Input));
+ try
+ {
+ using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
+ {
+ while (reader.Read())
+ {
+ Users.Add(new User()
+ {
+ ID = (int)reader[0],
+ UserName = (string)reader[1],
+ DisplayName = (string)reader[2],
+ Checked = (string)reader[3]
+ });
+ }
+ }
+ }
+ catch (Exception ex) { ExceptionManager.Publish(ex); }
+ return Users;
+ }, CacheSection.RetrieveDescByKey(UserRoleIDDataKey));
+ }
+ ///
+ /// 通过角色ID保存当前授权用户(插入)
+ ///
+ /// 角色ID
+ /// 用户ID数组
+ ///
+ public static bool SaveUsersByRoleId(int id, string value)
+ {
+ DataTable dt = new DataTable();
+ dt.Columns.Add("RoleID", typeof(int));
+ dt.Columns.Add("UserID", typeof(int));
+ if (!string.IsNullOrEmpty(value))
+ {
+ string[] userIds = value.Split(',');
+ foreach (string userId in userIds)
+ {
+ DataRow dr = dt.NewRow();
+ dr["RoleID"] = id;
+ dr["UserID"] = userId;
+ dt.Rows.Add(dr);
+ }
+ }
+ var trans = DBAccessManager.SqlDBAccess.BeginTransaction();
+ try
+ {
+ using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, string.Empty))
+ {
+ cmd.CommandText = "delete from UserRole where RoleId=@RoleId";
+ cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleId", id, 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 = "UserRole";
+ bulk.ColumnMappings.Add("RoleID", "RoleID");
+ bulk.ColumnMappings.Add("UserID", "UserID");
+ bulk.WriteToServer(dt);
+ }
+ trans.CommitTransaction();
+ ClearCache();
+ return true;
+ }
+ }
+ catch
+ {
+ trans.RollbackTransaction();
+ return false;
+ }
}
}
}
diff --git a/Bootstrap.DataAccessTests/UserHelperTests.cs b/Bootstrap.DataAccessTests/UserHelperTests.cs
index de52f824..08eb6912 100644
--- a/Bootstrap.DataAccessTests/UserHelperTests.cs
+++ b/Bootstrap.DataAccessTests/UserHelperTests.cs
@@ -1,4 +1,5 @@
using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess.Tests
@@ -43,5 +44,16 @@ namespace Bootstrap.DataAccess.Tests
result = UserHelper.SaveUser(users1);
Assert.IsTrue(result == true, "更新用户信息失败,请检查数据库连接或者数据库SQL语句");
}
+ [TestMethod]
+ public void RetrieveUsersByRoleIdTest(){
+ IEnumerable result = UserHelper.RetrieveUsersByRoleId(2);
+ Assert.IsTrue(result.Count() >= 0, "获取该角色的用户信息失败,请检查数据库连接或者数据库SQL语句");
+ }
+ [TestMethod]
+ public void SaveUsersByRoleIdTest()
+ {
+ bool result = UserHelper.SaveUsersByRoleId(2,"2,3");
+ Assert.IsTrue(result, "获取该角色的用户信息失败,请检查数据库连接或者数据库SQL语句");
+ }
}
}