增加用户指定角色功能
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>
|
||||
|
@ -54,3 +62,6 @@
|
|||
</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">
|
||||
|
|
|
@ -19,7 +19,7 @@ namespace Bootstrap.DataAccess
|
|||
/// </summary>
|
||||
/// <param name="tId"></param>
|
||||
/// <returns></returns>
|
||||
public static IEnumerable<Role> RetrieveRole(string tId = null)
|
||||
public static IEnumerable<Role> RetrieveRoles(string tId = null)
|
||||
{
|
||||
string sql = "select * from Roles";
|
||||
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));
|
||||
}
|
||||
/// <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>
|
||||
|
|
|
@ -28,9 +28,9 @@ namespace Bootstrap.DataAccess.Tests
|
|||
[TestMethod()]
|
||||
public void RetrieveRoleTest()
|
||||
{
|
||||
var result = RoleHelper.RetrieveRole("1");
|
||||
var result = RoleHelper.RetrieveRoles("1");
|
||||
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语句");
|
||||
}
|
||||
[TestMethod()]
|
||||
|
@ -42,7 +42,7 @@ namespace Bootstrap.DataAccess.Tests
|
|||
RoleName = "RoleUnitTest",
|
||||
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()), "删除用户失败");
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue