增加用户指定角色功能
This commit is contained in:
parent
acc0441957
commit
6abbaa60fd
|
@ -221,6 +221,7 @@
|
|||
<Content Include="Views\Admin\Groups.cshtml" />
|
||||
<Content Include="Views\Admin\Roles.cshtml" />
|
||||
<Content Include="Views\Shared\Glyphicons.cshtml" />
|
||||
<Content Include="Views\Shared\RoleConfig.cshtml" />
|
||||
<None Include="Web.Debug.config">
|
||||
<DependentUpon>Web.config</DependentUpon>
|
||||
</None>
|
||||
|
|
|
@ -18,6 +18,10 @@ ul li {
|
|||
outline: none;
|
||||
}
|
||||
|
||||
.btn span.fa, .btn span.glyphicon {
|
||||
margin-right: 6px;
|
||||
}
|
||||
|
||||
a, a:hover, a:focus {
|
||||
text-decoration: none;
|
||||
outline: none;
|
||||
|
@ -433,6 +437,11 @@ a.logo {
|
|||
clear: both;
|
||||
}
|
||||
|
||||
.modal-footer .btn {
|
||||
padding-left: 22px;
|
||||
padding-right: 22px;
|
||||
}
|
||||
|
||||
.fixed-table-loading {
|
||||
padding-top: 5px;
|
||||
}
|
||||
|
|
|
@ -93,6 +93,17 @@
|
|||
}
|
||||
});
|
||||
|
||||
// enhance window.console.log
|
||||
if (!window.console) {
|
||||
window.console = {
|
||||
log: function () {
|
||||
|
||||
}
|
||||
};
|
||||
}
|
||||
window.console = window.console || {};
|
||||
console.log || (console.log = opera.postError);
|
||||
|
||||
$.fn.extend({
|
||||
autoValidate: function (options) {
|
||||
// validate
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
(function ($) {
|
||||
BootstrapAdmin = function (options) {
|
||||
var that = this;
|
||||
if (options.click !== undefined && options.click.constructor === Object) { options.click = $.extend({}, BootstrapAdmin.settings.click, options.click); }
|
||||
this.options = $.extend({}, BootstrapAdmin.settings, options);
|
||||
|
||||
this.dataEntity = options.dataEntity;
|
||||
|
@ -10,12 +11,21 @@
|
|||
|
||||
// handler click event
|
||||
for (name in this.options.click) {
|
||||
var source = $("#" + this.options.click[name]);
|
||||
source.data('click', name);
|
||||
$("#" + this.options.click[name]).click(function () {
|
||||
var method = $(this).data('click');
|
||||
BootstrapAdmin.prototype[method].apply(that);
|
||||
});
|
||||
var ele = this.options.click[name];
|
||||
var cId = ele;
|
||||
var event = null;
|
||||
if ($.isArray(ele)) {
|
||||
for (index in ele) {
|
||||
if (ele[index].id === undefined) {
|
||||
window.console.log('options.click.assign[{0}].{1}.id 未设置控件id', index, name);
|
||||
continue;
|
||||
}
|
||||
cId = ele[index]['id'];
|
||||
event = ele[index]['click'];
|
||||
handler(cId, event);
|
||||
}
|
||||
}
|
||||
else handler(cId, event);
|
||||
}
|
||||
|
||||
// handler modal window show event
|
||||
|
@ -28,6 +38,16 @@
|
|||
}
|
||||
});
|
||||
}
|
||||
|
||||
function handler(cid, event) {
|
||||
var source = $("#" + cId);
|
||||
source.data('click', name);
|
||||
if (event !== null) source.data('event', event);
|
||||
source.click(function () {
|
||||
var method = $(this).data('click');
|
||||
BootstrapAdmin.prototype[method].call(that, this, $(this).data('event'));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
BootstrapAdmin.VERSION = "1.0";
|
||||
|
@ -44,7 +64,8 @@
|
|||
create: 'btn_add',
|
||||
edit: 'btn_edit',
|
||||
del: 'btn_delete',
|
||||
save: 'btnSubmit'
|
||||
save: 'btnSubmit',
|
||||
assign: []
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -56,14 +77,15 @@
|
|||
constructor: BootstrapAdmin,
|
||||
idEvents: function () {
|
||||
var op = {
|
||||
dataEntity: this.options.dataEntity,
|
||||
dataEntity: $.extend({}, this.options.dataEntity),
|
||||
table: this.options.bootstrapTable,
|
||||
modal: this.options.modal
|
||||
};
|
||||
return {
|
||||
'click .edit': function (e, value, row, index) {
|
||||
op.dataEntity.load(row);
|
||||
$('#' + op.table).bootstrapTable('uncheckAll').bootstrapTable('check', index);
|
||||
$(op.table).bootstrapTable('uncheckAll');
|
||||
$(op.table).bootstrapTable('check', index);
|
||||
$('#' + op.modal).modal("show");
|
||||
}
|
||||
}
|
||||
|
@ -74,23 +96,23 @@
|
|||
},
|
||||
|
||||
create: function () {
|
||||
this.dataEntity.reset();
|
||||
if (this.dataEntity instanceof DataEntity) this.dataEntity.reset();
|
||||
if (this.options.modal.constructor === String) $('#' + this.options.modal).modal("show");
|
||||
if (this.options.bootstrapTable.constructor === String) $(this.options.bootstrapTable).bootstrapTable('uncheckAll');
|
||||
},
|
||||
|
||||
edit: function () {
|
||||
options = this.options;
|
||||
var options = this.options;
|
||||
if (options.bootstrapTable.constructor !== String) return;
|
||||
var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections');
|
||||
if (arrselections.length == 0) {
|
||||
swal('请选择要编辑的条目', "编辑操作", "warning");
|
||||
swal('请选择要编辑的数据', "编辑操作", "warning");
|
||||
}
|
||||
else if (arrselections.length > 1) {
|
||||
swal('请选择一个要编辑的条目', "编辑操作", "warning");
|
||||
swal('请选择一个要编辑的数据', "编辑操作", "warning");
|
||||
}
|
||||
else {
|
||||
this.dataEntity.load(arrselections[0]);
|
||||
if (this.dataEntity instanceof DataEntity) this.dataEntity.load(arrselections[0]);
|
||||
if (options.modal.constructor === String) $('#' + options.modal).modal("show");
|
||||
}
|
||||
},
|
||||
|
@ -100,7 +122,7 @@
|
|||
if (options.bootstrapTable.constructor !== String) return;
|
||||
var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections');
|
||||
if (arrselections.length == 0) {
|
||||
swal('请选择要删除的条目', "删除操作", "warning");
|
||||
swal('请选择要删除的数据', "删除操作", "warning");
|
||||
return;
|
||||
}
|
||||
else {
|
||||
|
@ -134,7 +156,8 @@
|
|||
},
|
||||
|
||||
save: function () {
|
||||
var options = $.extend({}, this.options, { data: this.dataEntity.get() });
|
||||
var options = $.extend({ data: {} }, this.options);
|
||||
if (this.dataEntity instanceof DataEntity) options = $.extend(options, { data: this.dataEntity.get() });
|
||||
if (options.validateForm.constructor === String && !$("#" + options.validateForm).valid()) return;
|
||||
$.ajax({
|
||||
url: options.url,
|
||||
|
@ -170,6 +193,62 @@
|
|||
swal("失败", "保存数据失败", "error");
|
||||
}
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
assign: function (eventSrc, callback) {
|
||||
var options = this.options;
|
||||
if (options.bootstrapTable.constructor !== String) return;
|
||||
var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections');
|
||||
if (arrselections.length == 0) {
|
||||
swal('请选择要编辑的数据', "编辑操作", "warning");
|
||||
}
|
||||
else if (arrselections.length > 1) {
|
||||
swal('请选择一个要编辑的数据', "编辑操作", "warning");
|
||||
}
|
||||
else {
|
||||
if ($.isFunction(callback)) {
|
||||
callback.call(eventSrc, arrselections[0]);
|
||||
}
|
||||
}
|
||||
},
|
||||
|
||||
};
|
||||
|
||||
Role = {};
|
||||
Role.getRolesByUserId = function (userId, callback) {
|
||||
$.ajax({
|
||||
url: '../api/Roles/' + userId,
|
||||
type: 'GET',
|
||||
success: function (result) {
|
||||
callback(result);
|
||||
},
|
||||
error: function (XMLHttpRequest, textStatus, errorThrown) {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
};
|
||||
Role.getRolesByGroupId = function (groupId) {
|
||||
|
||||
};
|
||||
Role.saveRolesByUserId = function (userId, roleIds, callback) {
|
||||
$.ajax({
|
||||
url: '../api/Roles/' + userId,
|
||||
data: { "": roleIds },
|
||||
type: 'PUT',
|
||||
success: function (result) {
|
||||
callback(result);
|
||||
},
|
||||
error: function (XMLHttpRequest, textStatus, errorThrown) {
|
||||
callback();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Group = {};
|
||||
Group.getGroupsByUserId = function (userId) {
|
||||
|
||||
};
|
||||
Group.getGroupsByRoleId = function (roleId) {
|
||||
|
||||
};
|
||||
})(jQuery);
|
|
@ -1,5 +1,6 @@
|
|||
using Bootstrap.Admin.Models;
|
||||
using Bootstrap.DataAccess;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Web.Http;
|
||||
|
||||
|
@ -23,9 +24,19 @@ namespace Bootstrap.Admin.Controllers
|
|||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet]
|
||||
public Role Get(int id)
|
||||
public IEnumerable<Role> Get(int id)
|
||||
{
|
||||
return RoleHelper.RetrieveRole().FirstOrDefault(t => t.ID == id);
|
||||
return RoleHelper.RetrieveRolesByUserId();
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="value"></param>
|
||||
[HttpPut]
|
||||
public bool Put(int id, [FromBody]string value)
|
||||
{
|
||||
return RoleHelper.SaveRolesByUserId(id, value);
|
||||
}
|
||||
/// <summary>
|
||||
///
|
||||
|
|
|
@ -21,7 +21,7 @@ namespace Bootstrap.Admin.Models
|
|||
public QueryData<Role> RetrieveData()
|
||||
{
|
||||
// int limit, int offset, string name, string price, string sort, string order
|
||||
var data = RoleHelper.RetrieveRole(string.Empty);
|
||||
var data = RoleHelper.RetrieveRoles(string.Empty);
|
||||
if (!string.IsNullOrEmpty(RoleName))
|
||||
{
|
||||
data = data.Where(t => t.RoleName.Contains(RoleName));
|
||||
|
|
|
@ -9,6 +9,35 @@
|
|||
DisplayName: "displayName"
|
||||
}
|
||||
}),
|
||||
click: {
|
||||
assign: [{
|
||||
id: 'btn_assignRole',
|
||||
click: function (row) {
|
||||
Role.getRolesByUserId(1, 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);
|
||||
}).join('');
|
||||
$('#dialogRole form').html(data);
|
||||
$('#dialogRole').modal('show');
|
||||
});
|
||||
}
|
||||
}, {
|
||||
id: 'btn_assignGroup',
|
||||
click: function (row) {
|
||||
var userId = row.ID;
|
||||
}
|
||||
}, {
|
||||
id: 'btnSubmitUserRole',
|
||||
click: function (row) {
|
||||
var userId = row.ID;
|
||||
var roleIds = $('#dialogRole :checked').map(function (index, element) {
|
||||
return $(element).val();
|
||||
}).toArray().join(',');
|
||||
Role.saveRolesByUserId(userId, roleIds, function () { });
|
||||
}
|
||||
}]
|
||||
},
|
||||
success: function (src, data) {
|
||||
if (src === 'save' && data.ID === $('#userId').val()) {
|
||||
$('.username').text(data.DisplayName);
|
||||
|
|
|
@ -27,6 +27,14 @@
|
|||
</div>
|
||||
</form>
|
||||
}
|
||||
@section toolbar {
|
||||
<button id="btn_assignRole" type="button" class="btn btn-info">
|
||||
<span class="fa fa-sitemap" aria-hidden="true"></span>指派角色
|
||||
</button>
|
||||
<button id="btn_assignGroup" type="button" class="btn btn-info">
|
||||
<span class="fa fa-home" aria-hidden="true"></span>指派部门
|
||||
</button>
|
||||
}
|
||||
@section modal {
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
||||
|
@ -53,4 +61,7 @@
|
|||
</div>
|
||||
</form>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@section roleModal {
|
||||
@Html.Partial("RoleConfig")
|
||||
}
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
<div class="modal fade" id="dialogRole" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="myRoleModalLabel" aria-hidden="true">
|
||||
<div class="modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
|
||||
<h4 class="modal-title" id="myRoleModalLabel">角色授权窗口</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<form class="form-inline" role="form">
|
||||
</form>
|
||||
</div>
|
||||
<div class="modal-footer">
|
||||
<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
|
||||
<button type="button" class="btn btn-primary" id="btnSubmitUserRole">保存</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
|
@ -42,3 +42,4 @@
|
|||
<!--footer end-->
|
||||
</section>
|
||||
@RenderSection("modal", false)
|
||||
@RenderSection("roleModal", false)
|
||||
|
|
|
@ -35,6 +35,9 @@
|
|||
</div>
|
||||
</div>
|
||||
}
|
||||
@section roleModal {
|
||||
@RenderSection("roleModal", false)
|
||||
}
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">查询条件</div>
|
||||
<div class="panel-body">
|
||||
|
@ -51,6 +54,7 @@
|
|||
<button id="btn_edit" type="button" class="btn btn-primary">
|
||||
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>编辑
|
||||
</button>
|
||||
@RenderSection("toolbar", false)
|
||||
</div>
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading">
|
||||
|
|
|
@ -1,113 +1,137 @@
|
|||
using Longbow;
|
||||
using Longbow.Caching;
|
||||
using Longbow.Caching.Configuration;
|
||||
using Longbow.ExceptionManagement;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bootstrap.DataAccess
|
||||
{
|
||||
public class RoleHelper
|
||||
{
|
||||
private const string RoleDataKey = "RoleData-CodeRoleHelper";
|
||||
/// <summary>
|
||||
/// 查询所有角色
|
||||
/// </summary>
|
||||
/// <param name="tId"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Role> RetrieveRole(string tId = null)
|
||||
{
|
||||
string sql = "select * from Roles";
|
||||
var ret = CacheManager.GetOrAdd(RoleDataKey, CacheSection.RetrieveIntervalByKey(RoleDataKey), key =>
|
||||
{
|
||||
List<Role> roles = new List<Role>();
|
||||
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
||||
try
|
||||
{
|
||||
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
roles.Add(new Role()
|
||||
{
|
||||
ID = (int)reader[0],
|
||||
RoleName = LgbConvert.ReadValue(reader[1], string.Empty),
|
||||
Description = LgbConvert.ReadValue(reader[2], string.Empty)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
||||
return roles;
|
||||
}, CacheSection.RetrieveDescByKey(RoleDataKey));
|
||||
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除角色表
|
||||
/// </summary>
|
||||
/// <param name="IDs"></param>
|
||||
public static bool DeleteRole(string IDs)
|
||||
{
|
||||
bool ret = false;
|
||||
if (string.IsNullOrEmpty(IDs) || IDs.Contains("'")) return ret;
|
||||
try
|
||||
{
|
||||
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Roles where ID in ({0})", IDs);
|
||||
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
||||
{
|
||||
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
||||
ClearCache();
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ExceptionManager.Publish(ex);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存新建/更新的角色信息
|
||||
/// </summary>
|
||||
/// <param name="p"></param>
|
||||
/// <returns></returns>
|
||||
public static bool SaveRole(Role p)
|
||||
{
|
||||
if (p == null) throw new ArgumentNullException("p");
|
||||
bool ret = false;
|
||||
if (!string.IsNullOrEmpty(p.RoleName) && p.RoleName.Length > 50) p.RoleName = p.RoleName.Substring(0, 50);
|
||||
if (!string.IsNullOrEmpty(p.Description) && p.Description.Length > 50) p.Description = p.Description.Substring(0, 500);
|
||||
string sql = p.ID == 0 ?
|
||||
"Insert Into Roles (RoleName, Description) Values (@RoleName, @Description)" :
|
||||
"Update Roles set RoleName = @RoleName, Description = @Description where ID = @ID";
|
||||
try
|
||||
{
|
||||
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
||||
{
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.ID, ParameterDirection.Input));
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleName", p.RoleName, ParameterDirection.Input));
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, ParameterDirection.Input));
|
||||
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
||||
}
|
||||
ret = true;
|
||||
ClearCache();
|
||||
}
|
||||
catch (DbException ex)
|
||||
{
|
||||
ExceptionManager.Publish(ex);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 更新缓存
|
||||
private static void ClearCache()
|
||||
{
|
||||
CacheManager.Clear(key => key.Contains("RoleData-"));
|
||||
}
|
||||
}
|
||||
}
|
||||
using Longbow;
|
||||
using Longbow.Caching;
|
||||
using Longbow.Caching.Configuration;
|
||||
using Longbow.ExceptionManagement;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.Data.Common;
|
||||
using System.Globalization;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bootstrap.DataAccess
|
||||
{
|
||||
public class RoleHelper
|
||||
{
|
||||
private const string RoleDataKey = "RoleData-CodeRoleHelper";
|
||||
/// <summary>
|
||||
/// 查询所有角色
|
||||
/// </summary>
|
||||
/// <param name="tId"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Role> RetrieveRoles(string tId = null)
|
||||
{
|
||||
string sql = "select * from Roles";
|
||||
var ret = CacheManager.GetOrAdd(RoleDataKey, CacheSection.RetrieveIntervalByKey(RoleDataKey), key =>
|
||||
{
|
||||
List<Role> roles = new List<Role>();
|
||||
DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql);
|
||||
try
|
||||
{
|
||||
using (DbDataReader reader = DBAccessManager.SqlDBAccess.ExecuteReader(cmd))
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
roles.Add(new Role()
|
||||
{
|
||||
ID = (int)reader[0],
|
||||
RoleName = LgbConvert.ReadValue(reader[1], string.Empty),
|
||||
Description = LgbConvert.ReadValue(reader[2], string.Empty)
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex) { ExceptionManager.Publish(ex); }
|
||||
return roles;
|
||||
}, CacheSection.RetrieveDescByKey(RoleDataKey));
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Role> RetrieveRolesByUserId()
|
||||
{
|
||||
//UNDONE: 编写通过用户ID获取所有角色的方法
|
||||
return new List<Role>() {
|
||||
new Role() { ID = 1, RoleName = "TestRole1", Description = "测试角色1" },
|
||||
new Role() { ID = 2, RoleName = "TestRole2", Description = "测试角色2" }
|
||||
};
|
||||
}
|
||||
/// <summary>
|
||||
/// 删除角色表
|
||||
/// </summary>
|
||||
/// <param name="IDs"></param>
|
||||
public static bool DeleteRole(string IDs)
|
||||
{
|
||||
bool ret = false;
|
||||
if (string.IsNullOrEmpty(IDs) || IDs.Contains("'")) return ret;
|
||||
try
|
||||
{
|
||||
string sql = string.Format(CultureInfo.InvariantCulture, "Delete from Roles where ID in ({0})", IDs);
|
||||
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
||||
{
|
||||
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
||||
ClearCache();
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
ExceptionManager.Publish(ex);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存新建/更新的角色信息
|
||||
/// </summary>
|
||||
/// <param name="p"></param>
|
||||
/// <returns></returns>
|
||||
public static bool SaveRole(Role p)
|
||||
{
|
||||
if (p == null) throw new ArgumentNullException("p");
|
||||
bool ret = false;
|
||||
if (!string.IsNullOrEmpty(p.RoleName) && p.RoleName.Length > 50) p.RoleName = p.RoleName.Substring(0, 50);
|
||||
if (!string.IsNullOrEmpty(p.Description) && p.Description.Length > 50) p.Description = p.Description.Substring(0, 500);
|
||||
string sql = p.ID == 0 ?
|
||||
"Insert Into Roles (RoleName, Description) Values (@RoleName, @Description)" :
|
||||
"Update Roles set RoleName = @RoleName, Description = @Description where ID = @ID";
|
||||
try
|
||||
{
|
||||
using (DbCommand cmd = DBAccessManager.SqlDBAccess.CreateCommand(CommandType.Text, sql))
|
||||
{
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@ID", p.ID, ParameterDirection.Input));
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@RoleName", p.RoleName, ParameterDirection.Input));
|
||||
cmd.Parameters.Add(DBAccessManager.SqlDBAccess.CreateParameter("@Description", p.Description, ParameterDirection.Input));
|
||||
DBAccessManager.SqlDBAccess.ExecuteNonQuery(cmd);
|
||||
}
|
||||
ret = true;
|
||||
ClearCache();
|
||||
}
|
||||
catch (DbException ex)
|
||||
{
|
||||
ExceptionManager.Publish(ex);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// 更新缓存
|
||||
private static void ClearCache()
|
||||
{
|
||||
CacheManager.Clear(key => key.Contains("RoleData-"));
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,49 +1,49 @@
|
|||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bootstrap.DataAccess.Tests
|
||||
{
|
||||
[TestClass()]
|
||||
public class RoleTests
|
||||
{
|
||||
[TestMethod()]
|
||||
public void SaveRoleTest()
|
||||
{
|
||||
Role role1 = new Role()
|
||||
{
|
||||
RoleName = "管理员",
|
||||
Description = "可以读写所有内容"
|
||||
};
|
||||
var result1 = RoleHelper.SaveRole(role1);
|
||||
Assert.IsTrue(result1 == true, "带有参数的RoleHelper.SaveRole方法添加用户失败,请检查数据库连接或者数据库SQL语句");
|
||||
Role role2 = new Role()
|
||||
{
|
||||
ID = 1,
|
||||
RoleName = "管理员",
|
||||
Description = "读写所有内容"
|
||||
};
|
||||
var result2 = RoleHelper.SaveRole(role2);
|
||||
Assert.IsTrue(result2 == true, "带有参数的RoleHelper.SaveRole方法编辑用户信息失败,请检查数据库连接或者数据库SQL语句");
|
||||
}
|
||||
[TestMethod()]
|
||||
public void RetrieveRoleTest()
|
||||
{
|
||||
var result = RoleHelper.RetrieveRole("1");
|
||||
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句");
|
||||
result = RoleHelper.RetrieveRole();
|
||||
Assert.IsTrue(result.Count() >= 0, "不带参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句");
|
||||
}
|
||||
[TestMethod()]
|
||||
public void DeleteRoleTest()
|
||||
{
|
||||
RoleHelper.SaveRole(new Role()
|
||||
{
|
||||
ID = 0,
|
||||
RoleName = "RoleUnitTest",
|
||||
Description = string.Empty
|
||||
});
|
||||
var role = RoleHelper.RetrieveRole().FirstOrDefault(r => r.RoleName == "RoleUnitTest");
|
||||
Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除用户失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bootstrap.DataAccess.Tests
|
||||
{
|
||||
[TestClass()]
|
||||
public class RoleTests
|
||||
{
|
||||
[TestMethod()]
|
||||
public void SaveRoleTest()
|
||||
{
|
||||
Role role1 = new Role()
|
||||
{
|
||||
RoleName = "管理员",
|
||||
Description = "可以读写所有内容"
|
||||
};
|
||||
var result1 = RoleHelper.SaveRole(role1);
|
||||
Assert.IsTrue(result1 == true, "带有参数的RoleHelper.SaveRole方法添加用户失败,请检查数据库连接或者数据库SQL语句");
|
||||
Role role2 = new Role()
|
||||
{
|
||||
ID = 1,
|
||||
RoleName = "管理员",
|
||||
Description = "读写所有内容"
|
||||
};
|
||||
var result2 = RoleHelper.SaveRole(role2);
|
||||
Assert.IsTrue(result2 == true, "带有参数的RoleHelper.SaveRole方法编辑用户信息失败,请检查数据库连接或者数据库SQL语句");
|
||||
}
|
||||
[TestMethod()]
|
||||
public void RetrieveRoleTest()
|
||||
{
|
||||
var result = RoleHelper.RetrieveRoles("1");
|
||||
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句");
|
||||
result = RoleHelper.RetrieveRoles();
|
||||
Assert.IsTrue(result.Count() >= 0, "不带参数的RoleHelper.RetrieveRole方法调用失败,请检查数据库连接或者数据库SQL语句");
|
||||
}
|
||||
[TestMethod()]
|
||||
public void DeleteRoleTest()
|
||||
{
|
||||
RoleHelper.SaveRole(new Role()
|
||||
{
|
||||
ID = 0,
|
||||
RoleName = "RoleUnitTest",
|
||||
Description = string.Empty
|
||||
});
|
||||
var role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == "RoleUnitTest");
|
||||
Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除用户失败");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue