增加用户指定角色功能

This commit is contained in:
Argo-Lenovo 2016-10-25 18:47:33 +08:00
parent acc0441957
commit 6abbaa60fd
13 changed files with 381 additions and 183 deletions

View File

@ -221,6 +221,7 @@
<Content Include="Views\Admin\Groups.cshtml" /> <Content Include="Views\Admin\Groups.cshtml" />
<Content Include="Views\Admin\Roles.cshtml" /> <Content Include="Views\Admin\Roles.cshtml" />
<Content Include="Views\Shared\Glyphicons.cshtml" /> <Content Include="Views\Shared\Glyphicons.cshtml" />
<Content Include="Views\Shared\RoleConfig.cshtml" />
<None Include="Web.Debug.config"> <None Include="Web.Debug.config">
<DependentUpon>Web.config</DependentUpon> <DependentUpon>Web.config</DependentUpon>
</None> </None>

View File

@ -18,6 +18,10 @@ ul li {
outline: none; outline: none;
} }
.btn span.fa, .btn span.glyphicon {
margin-right: 6px;
}
a, a:hover, a:focus { a, a:hover, a:focus {
text-decoration: none; text-decoration: none;
outline: none; outline: none;
@ -433,6 +437,11 @@ a.logo {
clear: both; clear: both;
} }
.modal-footer .btn {
padding-left: 22px;
padding-right: 22px;
}
.fixed-table-loading { .fixed-table-loading {
padding-top: 5px; padding-top: 5px;
} }

View File

@ -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({ $.fn.extend({
autoValidate: function (options) { autoValidate: function (options) {
// validate // validate

View File

@ -1,6 +1,7 @@
(function ($) { (function ($) {
BootstrapAdmin = function (options) { BootstrapAdmin = function (options) {
var that = this; 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.options = $.extend({}, BootstrapAdmin.settings, options);
this.dataEntity = options.dataEntity; this.dataEntity = options.dataEntity;
@ -10,12 +11,21 @@
// handler click event // handler click event
for (name in this.options.click) { for (name in this.options.click) {
var source = $("#" + this.options.click[name]); var ele = this.options.click[name];
source.data('click', name); var cId = ele;
$("#" + this.options.click[name]).click(function () { var event = null;
var method = $(this).data('click'); if ($.isArray(ele)) {
BootstrapAdmin.prototype[method].apply(that); 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 // 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"; BootstrapAdmin.VERSION = "1.0";
@ -44,7 +64,8 @@
create: 'btn_add', create: 'btn_add',
edit: 'btn_edit', edit: 'btn_edit',
del: 'btn_delete', del: 'btn_delete',
save: 'btnSubmit' save: 'btnSubmit',
assign: []
} }
}; };
@ -56,14 +77,15 @@
constructor: BootstrapAdmin, constructor: BootstrapAdmin,
idEvents: function () { idEvents: function () {
var op = { var op = {
dataEntity: this.options.dataEntity, dataEntity: $.extend({}, this.options.dataEntity),
table: this.options.bootstrapTable, table: this.options.bootstrapTable,
modal: this.options.modal modal: this.options.modal
}; };
return { return {
'click .edit': function (e, value, row, index) { 'click .edit': function (e, value, row, index) {
op.dataEntity.load(row); op.dataEntity.load(row);
$('#' + op.table).bootstrapTable('uncheckAll').bootstrapTable('check', index); $(op.table).bootstrapTable('uncheckAll');
$(op.table).bootstrapTable('check', index);
$('#' + op.modal).modal("show"); $('#' + op.modal).modal("show");
} }
} }
@ -74,23 +96,23 @@
}, },
create: function () { 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.modal.constructor === String) $('#' + this.options.modal).modal("show");
if (this.options.bootstrapTable.constructor === String) $(this.options.bootstrapTable).bootstrapTable('uncheckAll'); if (this.options.bootstrapTable.constructor === String) $(this.options.bootstrapTable).bootstrapTable('uncheckAll');
}, },
edit: function () { edit: function () {
options = this.options; var options = this.options;
if (options.bootstrapTable.constructor !== String) return; if (options.bootstrapTable.constructor !== String) return;
var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections'); var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections');
if (arrselections.length == 0) { if (arrselections.length == 0) {
swal('请选择要编辑的条目', "编辑操作", "warning"); swal('请选择要编辑的数据', "编辑操作", "warning");
} }
else if (arrselections.length > 1) { else if (arrselections.length > 1) {
swal('请选择一个要编辑的条目', "编辑操作", "warning"); swal('请选择一个要编辑的数据', "编辑操作", "warning");
} }
else { 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"); if (options.modal.constructor === String) $('#' + options.modal).modal("show");
} }
}, },
@ -100,7 +122,7 @@
if (options.bootstrapTable.constructor !== String) return; if (options.bootstrapTable.constructor !== String) return;
var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections'); var arrselections = $(options.bootstrapTable).bootstrapTable('getSelections');
if (arrselections.length == 0) { if (arrselections.length == 0) {
swal('请选择要删除的条目', "删除操作", "warning"); swal('请选择要删除的数据', "删除操作", "warning");
return; return;
} }
else { else {
@ -134,7 +156,8 @@
}, },
save: function () { 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; if (options.validateForm.constructor === String && !$("#" + options.validateForm).valid()) return;
$.ajax({ $.ajax({
url: options.url, url: options.url,
@ -170,6 +193,62 @@
swal("失败", "保存数据失败", "error"); 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); })(jQuery);

View File

@ -1,5 +1,6 @@
using Bootstrap.Admin.Models; using Bootstrap.Admin.Models;
using Bootstrap.DataAccess; using Bootstrap.DataAccess;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Web.Http; using System.Web.Http;
@ -23,9 +24,19 @@ namespace Bootstrap.Admin.Controllers
/// <param name="id"></param> /// <param name="id"></param>
/// <returns></returns> /// <returns></returns>
[HttpGet] [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> /// <summary>
/// ///

View File

@ -21,7 +21,7 @@ namespace Bootstrap.Admin.Models
public QueryData<Role> RetrieveData() public QueryData<Role> RetrieveData()
{ {
// int limit, int offset, string name, string price, string sort, string order // 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)) if (!string.IsNullOrEmpty(RoleName))
{ {
data = data.Where(t => t.RoleName.Contains(RoleName)); data = data.Where(t => t.RoleName.Contains(RoleName));

View File

@ -9,6 +9,35 @@
DisplayName: "displayName" 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) { success: function (src, data) {
if (src === 'save' && data.ID === $('#userId').val()) { if (src === 'save' && data.ID === $('#userId').val()) {
$('.username').text(data.DisplayName); $('.username').text(data.DisplayName);

View File

@ -27,6 +27,14 @@
</div> </div>
</form> </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 { @section modal {
<div class="modal-header"> <div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button> <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
@ -54,3 +62,6 @@
</form> </form>
</div> </div>
} }
@section roleModal {
@Html.Partial("RoleConfig")
}

View File

@ -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">&times;</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>

View File

@ -42,3 +42,4 @@
<!--footer end--> <!--footer end-->
</section> </section>
@RenderSection("modal", false) @RenderSection("modal", false)
@RenderSection("roleModal", false)

View File

@ -35,6 +35,9 @@
</div> </div>
</div> </div>
} }
@section roleModal {
@RenderSection("roleModal", false)
}
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading">查询条件</div> <div class="panel-heading">查询条件</div>
<div class="panel-body"> <div class="panel-body">
@ -51,6 +54,7 @@
<button id="btn_edit" type="button" class="btn btn-primary"> <button id="btn_edit" type="button" class="btn btn-primary">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>编辑 <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>编辑
</button> </button>
@RenderSection("toolbar", false)
</div> </div>
<div class="panel panel-default"> <div class="panel panel-default">
<div class="panel-heading"> <div class="panel-heading">

View File

@ -19,7 +19,7 @@ namespace Bootstrap.DataAccess
/// </summary> /// </summary>
/// <param name="tId"></param> /// <param name="tId"></param>
/// <returns></returns> /// <returns></returns>
public static IEnumerable<Role> RetrieveRole(string tId = null) public static IEnumerable<Role> RetrieveRoles(string tId = null)
{ {
string sql = "select * from Roles"; string sql = "select * from Roles";
var ret = CacheManager.GetOrAdd(RoleDataKey, CacheSection.RetrieveIntervalByKey(RoleDataKey), key => var ret = CacheManager.GetOrAdd(RoleDataKey, CacheSection.RetrieveIntervalByKey(RoleDataKey), key =>
@ -47,6 +47,30 @@ namespace Bootstrap.DataAccess
return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase)); return string.IsNullOrEmpty(tId) ? ret : ret.Where(t => tId.Equals(t.ID.ToString(), StringComparison.OrdinalIgnoreCase));
} }
/// <summary> /// <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> /// </summary>
/// <param name="IDs"></param> /// <param name="IDs"></param>

View File

@ -28,9 +28,9 @@ namespace Bootstrap.DataAccess.Tests
[TestMethod()] [TestMethod()]
public void RetrieveRoleTest() public void RetrieveRoleTest()
{ {
var result = RoleHelper.RetrieveRole("1"); var result = RoleHelper.RetrieveRoles("1");
Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的RoleHelper.RetrieveRole方法调用失败请检查数据库连接或者数据库SQL语句"); Assert.IsTrue((result.Count() == 0 || result.Count() == 1), "带有参数的RoleHelper.RetrieveRole方法调用失败请检查数据库连接或者数据库SQL语句");
result = RoleHelper.RetrieveRole(); result = RoleHelper.RetrieveRoles();
Assert.IsTrue(result.Count() >= 0, "不带参数的RoleHelper.RetrieveRole方法调用失败请检查数据库连接或者数据库SQL语句"); Assert.IsTrue(result.Count() >= 0, "不带参数的RoleHelper.RetrieveRole方法调用失败请检查数据库连接或者数据库SQL语句");
} }
[TestMethod()] [TestMethod()]
@ -42,7 +42,7 @@ namespace Bootstrap.DataAccess.Tests
RoleName = "RoleUnitTest", RoleName = "RoleUnitTest",
Description = string.Empty Description = string.Empty
}); });
var role = RoleHelper.RetrieveRole().FirstOrDefault(r => r.RoleName == "RoleUnitTest"); var role = RoleHelper.RetrieveRoles().FirstOrDefault(r => r.RoleName == "RoleUnitTest");
Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除用户失败"); Assert.IsTrue(RoleHelper.DeleteRole(role.ID.ToString()), "删除用户失败");
} }
} }