完善用户指派角色功能
This commit is contained in:
parent
b45f9cb121
commit
bb70090a00
|
@ -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);
|
||||
|
|
|
@ -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
|
|||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public IEnumerable<Role> Get(int id)
|
||||
[HttpPost]
|
||||
public IEnumerable<Role> Post(int id, [FromBody]string value)
|
||||
{
|
||||
return RoleHelper.RetrieveRolesByUserId();
|
||||
if (value == "user")
|
||||
{
|
||||
return RoleHelper.RetrieveRolesByUserId(id.ToString());
|
||||
}
|
||||
else
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
|
@ -34,9 +41,13 @@ namespace Bootstrap.Admin.Controllers
|
|||
/// <param name="id"></param>
|
||||
/// <param name="value"></param>
|
||||
[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;
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
|
|
|
@ -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('<div class="checkbox"><label><input type="checkbox" value="{0}">{1}</label></div>', element.ID, element.RoleName);
|
||||
if (element.IsSelect == 1) {
|
||||
return $.format('<div class="checkbox"><label><input type="checkbox" value="{0}" checked="checked">{1}</label></div>', element.ID, element.RoleName);
|
||||
} else if (element.IsSelect == 0) {
|
||||
return $.format('<div class="checkbox"><label><input type="checkbox" value="{0}">{1}</label></div>', 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");
|
||||
}
|
||||
});
|
||||
}
|
||||
}]
|
||||
},
|
||||
|
|
|
@ -28,6 +28,7 @@
|
|||
<add key="UserData-CodeUserHelper" interval="600" desc="用户信息缓存" />
|
||||
<add key="UserData-CodeUserHelper-" interval="600" desc="用户信息缓存" />
|
||||
<add key="RoleData-CodeRoleHelper" interval="600" desc="角色信息缓存" />
|
||||
<add key="RoleData-CodeRoleHelper-" interval="600" desc="角色信息缓存" />
|
||||
<add key="MenuData-CodeMenuHelper" interval="600" desc="菜单信息缓存" />
|
||||
</cacheManager>
|
||||
|
||||
|
|
|
@ -14,5 +14,9 @@
|
|||
/// 获得/设置 角色描述
|
||||
/// </summary>
|
||||
public string Description { get; set; }
|
||||
/// <summary>
|
||||
/// 获取/设置 用户角色状态
|
||||
/// </summary>
|
||||
public int IsSelect { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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-";
|
||||
/// <summary>
|
||||
/// 查询所有角色
|
||||
/// </summary>
|
||||
|
@ -47,28 +50,93 @@ namespace Bootstrap.DataAccess
|
|||
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// 保存用户角色关系
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="value"></param>
|
||||
/// <returns></returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// 查询某个用户所拥有的角色
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Role> RetrieveRolesByUserId()
|
||||
public static IEnumerable<Role> RetrieveRolesByUserId(string userId)
|
||||
{
|
||||
//UNDONE: 编写通过用户ID获取所有角色的方法
|
||||
return new List<Role>() {
|
||||
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<Role> Roles = new List<Role>();
|
||||
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;
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除角色表
|
||||
|
|
|
@ -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, "保存用户角色关系失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue