diff --git a/Bootstrap.Admin/Bootstrap.Admin.csproj b/Bootstrap.Admin/Bootstrap.Admin.csproj
index b84cdf54..31d20a4b 100644
--- a/Bootstrap.Admin/Bootstrap.Admin.csproj
+++ b/Bootstrap.Admin/Bootstrap.Admin.csproj
@@ -227,6 +227,7 @@
+
Web.config
diff --git a/Bootstrap.Admin/Content/js/framework.js b/Bootstrap.Admin/Content/js/framework.js
index c05c8547..2d5d63d4 100644
--- a/Bootstrap.Admin/Content/js/framework.js
+++ b/Bootstrap.Admin/Content/js/framework.js
@@ -297,9 +297,41 @@
processUsersData({ Id: roleId, callback: callback, method: "PUT", data: { type: "role", userIds: userIds } });
}
- Group = {};
- Group.getGroupsByUserId = function (userId) {
+ var processGroupsData = function (options) {
+ var data = $.extend({ data: { type: "" }, method: "POST", Id: "" }, options);
+ $.ajax({
+ url: '../api/Groups/' + 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('
', element.ID, element.GroupName, element.Checked, element.Description);
+ }).join('');
+ data.callback(html);
+ return;
+ }
+ }
+ else if ($.isPlainObject(data.callback) && data.callback.modal !== undefined) {
+ $("#" + data.callback.modal).modal('hide');
+ }
+ if (result) { swal("成功", "授权角色", "success"); }
+ else { swal("失败", "授权角色", "error"); }
+ if ($.isFunction(data.callback)) data.callback(result);
+ },
+ error: function (XMLHttpRequest, textStatus, errorThrown) {
+ if ($.isFunction(data.callback)) data.callback(false);
+ }
+ });
+ }
+ Group = {};
+ Group.getGroupsByUserId = function (userId, callback) {
+ processGroupsData({ Id: userId, callback: callback, data: { type: "user" } });
+ };
+ Group.saveGroupsByUserId = function (userId, groupIds, callback) {
+ processGroupsData({ Id: userId, callback: callback, method: "PUT", data: { type: "user", groupIds: groupIds } });
};
Group.getGroupsByRoleId = function (roleId) {
diff --git a/Bootstrap.Admin/Controllers/GroupsController.cs b/Bootstrap.Admin/Controllers/GroupsController.cs
index c08f11c5..305ad8e2 100644
--- a/Bootstrap.Admin/Controllers/GroupsController.cs
+++ b/Bootstrap.Admin/Controllers/GroupsController.cs
@@ -1,5 +1,7 @@
using Bootstrap.Admin.Models;
using Bootstrap.DataAccess;
+using Newtonsoft.Json.Linq;
+using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
@@ -45,5 +47,48 @@ namespace Bootstrap.Admin.Controllers
{
return GroupHelper.DeleteGroup(value);
}
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ [HttpPost]
+ public IEnumerable Post(int id, [FromBody]JObject value)
+ {
+ var ret = new List();
+ dynamic json = value;
+ switch ((string)json.type)
+ {
+ case "user":
+ ret = GroupHelper.RetrieveGroupsByUserId(id).ToList();
+ break;
+ default:
+ break;
+ }
+ return ret;
+ }
+ ///
+ ///
+ ///
+ ///
+ ///
+ ///
+ [HttpPut]
+ public bool Put(int id, [FromBody]JObject value)
+ {
+ var ret = false;
+ dynamic json = value;
+ string groupIds = json.groupIds;
+ switch ((string)json.type)
+ {
+ case "user":
+ ret = GroupHelper.SaveGroupsByUserId(id, groupIds);
+ break;
+ default:
+ break;
+ }
+ return ret;
+ }
}
}
diff --git a/Bootstrap.Admin/Scripts/Users.js b/Bootstrap.Admin/Scripts/Users.js
index ad4f7abc..08ce70eb 100644
--- a/Bootstrap.Admin/Scripts/Users.js
+++ b/Bootstrap.Admin/Scripts/Users.js
@@ -22,7 +22,11 @@
}, {
id: 'btn_assignGroup',
click: function (row) {
- var userId = row.ID;
+ Group.getGroupsByUserId(row.ID, function (data) {
+ $("#dialogGroup .modal-title").text($.format('{0}-部门授权窗口', row.DisplayName));
+ $('#dialogGroup form').html(data);
+ $('#dialogGroup').modal('show');
+ });
}
}, {
id: 'btnSubmitUserRole',
@@ -33,6 +37,15 @@
}).toArray().join(',');
Role.saveRolesByUserId(userId, roleIds, { modal: 'dialogRole' });
}
+ }, {
+ id: 'btnSubmitUserGroup',
+ click: function (row) {
+ var userId = row.ID;
+ var groupIds = $('#dialogGroup :checked').map(function (index, element) {
+ return $(element).val();
+ }).toArray().join(',');
+ Group.saveGroupsByUserId(userId, groupIds, { modal: 'dialogGroup' });
+ }
}]
},
success: function (src, data) {
diff --git a/Bootstrap.Admin/Views/Admin/Users.cshtml b/Bootstrap.Admin/Views/Admin/Users.cshtml
index 61db9430..6a1375cb 100644
--- a/Bootstrap.Admin/Views/Admin/Users.cshtml
+++ b/Bootstrap.Admin/Views/Admin/Users.cshtml
@@ -64,4 +64,5 @@
}
@section customModal {
@Html.Partial("RoleConfig")
+ @Html.Partial("GroupConfig")
}
diff --git a/Bootstrap.Admin/Views/Shared/GroupConfig.cshtml b/Bootstrap.Admin/Views/Shared/GroupConfig.cshtml
new file mode 100644
index 00000000..c0ff4fbf
--- /dev/null
+++ b/Bootstrap.Admin/Views/Shared/GroupConfig.cshtml
@@ -0,0 +1,17 @@
+
diff --git a/Bootstrap.DataAccess/Group.cs b/Bootstrap.DataAccess/Group.cs
index 6ec7cced..35b05da8 100644
--- a/Bootstrap.DataAccess/Group.cs
+++ b/Bootstrap.DataAccess/Group.cs
@@ -20,5 +20,10 @@
/// 获得/设置 群组描述
///
public string Description { get; set; }
+
+ ///
+ /// 获取/设置 用户群组关联状态 checked 标示已经关联 '' 标示未关联
+ ///
+ public string Checked { get; set; }
}
}
diff --git a/Bootstrap.DataAccess/GroupHelper.cs b/Bootstrap.DataAccess/GroupHelper.cs
index a02a3a81..092e72b8 100644
--- a/Bootstrap.DataAccess/GroupHelper.cs
+++ b/Bootstrap.DataAccess/GroupHelper.cs
@@ -1,10 +1,12 @@
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;
@@ -17,6 +19,7 @@ namespace Bootstrap.DataAccess
public static class GroupHelper
{
private const string GroupDataKey = "GroupData-CodeGroupHelper";
+ private const string GroupUserIDDataKey = "GroupData-CodeGroupHelper-";
///
/// 查询所有群组信息
///
@@ -105,10 +108,101 @@ namespace Bootstrap.DataAccess
}
return ret;
}
+ ///
+ /// 根据用户查询部门信息
+ ///
+ ///
+ ///
+ public static IEnumerable RetrieveGroupsByUserId(int userId)
+ {
+ string sql = "select g.ID,g.GroupName,g.[Description],case ug.GroupID when g.ID then 'checked' else '' end [status] from Groups g left join UserGroup ug on g.ID=ug.GroupID and UserID=@UserID";
+ string k = string.Format("{0}{1}", GroupUserIDDataKey, userId);
+ var ret = CacheManager.GetOrAdd(k, CacheSection.RetrieveIntervalByKey(GroupUserIDDataKey), key =>
+ {
+ List Groups = 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())
+ {
+ Groups.Add(new Group()
+ {
+ ID = (int)reader[0],
+ GroupName = (string)reader[1],
+ Description = (string)reader[2],
+ Checked = (string)reader[3]
+ });
+ }
+ }
+ }
+ catch (Exception ex) { ExceptionManager.Publish(ex); }
+ return Groups;
+ }, CacheSection.RetrieveDescByKey(GroupUserIDDataKey));
+ return ret;
+ }
+ ///
+ /// 保存用户部门关系
+ ///
+ ///
+ ///
+ ///
+ public static bool SaveGroupsByUserId(int id, string value)
+ {
+ DataTable dt = new DataTable();
+ dt.Columns.Add("UserID", typeof(int));
+ dt.Columns.Add("GroupID", typeof(int));
+ //判断用户是否选定角色
+ if (!string.IsNullOrEmpty(value))
+ {
+ string[] groupIDs = value.Split(',');
+ foreach (string groupID in groupIDs)
+ {
+ DataRow row = dt.NewRow();
+ row["UserID"] = id;
+ row["GroupID"] = groupID;
+ dt.Rows.Add(row);
+ }
+ }
+
+ string sql = "delete from UserGroup 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 = "UserGroup";
+ bulk.ColumnMappings.Add("UserID", "UserID");
+ bulk.ColumnMappings.Add("GroupID", "GroupID");
+
+ 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()
{
CacheManager.Clear(key => key == GroupDataKey);
+ CacheManager.Clear(key => key == GroupUserIDDataKey);
}
}
}
diff --git a/Bootstrap.DataAccessTests/GroupHelperTests.cs b/Bootstrap.DataAccessTests/GroupHelperTests.cs
index 3afec8b6..42cffb06 100644
--- a/Bootstrap.DataAccessTests/GroupHelperTests.cs
+++ b/Bootstrap.DataAccessTests/GroupHelperTests.cs
@@ -48,6 +48,18 @@ namespace Bootstrap.DataAccess.Tests
}
}
+ [TestMethod()]
+ public void RetrieveGroupsByUserIdTest()
+ {
+ var result = GroupHelper.RetrieveGroupsByUserId(1);
+ Assert.IsTrue(result.Count() > 0, "根据用户查询群组失败");
+ }
+ [TestMethod()]
+ public void SaveGroupsByUserIdTest()
+ {
+ var result = GroupHelper.SaveGroupsByUserId(1, "1,2");
+ Assert.IsTrue(result == true, "保存用户群组关系失败");
+ }
}
}