增加功能:增加角色对应用程序授权功能 #IRY3N

This commit is contained in:
Argo-Surface 2019-02-24 17:27:41 +08:00
parent 1408255878
commit 006c91c198
11 changed files with 212 additions and 8 deletions

View File

@ -0,0 +1,31 @@
using Bootstrap.DataAccess;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace Bootstrap.Admin.Controllers.Api
{
/// <summary>
///
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class AppsController : ControllerBase
{
/// <summary>
///
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
[HttpPost("{id}")]
public IEnumerable<App> Post(string id) => AppHelper.RetrievesByRoleId(id);
/// <summary>
/// 保存应用程序授权
/// </summary>
/// <param name="id"></param>
/// <param name="appIds"></param>
/// <returns></returns>
[HttpPut("{id}")]
public bool Put(string id, [FromBody]IEnumerable<string> appIds) => AppHelper.SaveByRoleId(id, appIds);
}
}

View File

@ -48,6 +48,9 @@
<button id="btn_assignMenu" type="button" class="btn btn-secondary">
<span class="fa fa-dashboard" aria-hidden="true"></span><span>分配菜单</span>
</button>
<button id="btn_assignApp" type="button" class="btn btn-success">
<span class="fa fa-cubes" aria-hidden="true"></span><span>分配应用</span>
</button>
<div class="toolbar btn-group">
<button class="btn btn-secondary dropdown-toggle" data-toggle="dropdown" type="button"><i class="fa fa-gear"></i></button>
<div class="dropdown-menu">
@ -55,6 +58,7 @@
<a id="tb_assignUser" href="#" title="分配用户"><i class="fa fa-user"></i></a>
<a id="tb_assignGroup" href="#" title="分配部门"><i class="fa fa-bank"></i></a>
<a id="tb_assignMenu" href="#" title="分配菜单"><i class="fa fa-dashboard"></i></a>
<a id="tb_assignApp" href="#" title="分配应用"><i class="fa fa-cubes"></i></a>
</div>
</div>
}
@ -83,4 +87,5 @@
@await Html.PartialAsync("UserConfig")
@await Html.PartialAsync("GroupConfig")
@await Html.PartialAsync("NavigatorConfig")
@await Html.PartialAsync("AppConfig")
}

View File

@ -0,0 +1,25 @@
<div class="modal fade" id="dialogApp" tabindex="-1" role="dialog" data-backdrop="static" aria-labelledby="myAppModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="myAppModalLabel">应用授权窗口</h5>
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
</div>
<div class="modal-body">
<form class="form-inline">
<div class="row" id="appForm"></div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">
<i class="fa fa-times"></i>
<span>关闭</span>
</button>
<button type="button" class="btn btn-primary" id="btnSubmitApp">
<i class="fa fa-save"></i>
<span>保存</span>
</button>
</div>
</div>
</div>
</div>

View File

@ -45,7 +45,7 @@
"Enabled": true,
"Widget": "Bootstrap.DataAccess.MongoDB",
"ConnectionStrings": {
"ba": "mongodb://10.211.55.2:27017"
"ba": "mongodb://localhost:27017"
}
}
],
@ -209,6 +209,13 @@
"SlidingExpiration": true,
"Desc": "通过角色ID获得所有部门数据"
},
{
"Enabled": true,
"Key": "AppHelper-RetrieveAppsByRoleId",
"Interval": 600,
"SlidingExpiration": true,
"Desc": "通过角色ID获得所有应用程序数据"
},
{
"Enabled": true,
"Key": "LogHelper-RetrieveLogs",

View File

@ -1,6 +1,6 @@
@media (min-width: 375px) {
.toolbar .dropdown-menu a {
padding: 0 20px;
padding: 0 14px;
}
.toolbar .dropdown-menu a:first-child {
@ -56,15 +56,17 @@
}
@media (min-width: 940px) {
.btn span:last-child {
display: inline;
}
.form-check-input + span, .form-check-input + label {
max-width: 192px;
}
}
@media (min-width: 1024px) {
.bs-bars .btn span:last-child {
display: inline;
}
}
@media (min-width: 992px) {
.modal-lg {
max-width: 940px;

View File

@ -195,7 +195,7 @@ footer {
}
.toolbar .dropdown-menu a {
padding: 0 14px;
padding: 0 12px;
display: table-cell;
color: #504d4d;
}

View File

@ -186,6 +186,12 @@ $(function () {
$('body').toggleClass('sidebar-open');
});
// Apps
App = {
url: 'api/Apps',
title: "分配应用"
};
// Roles
Role = {
url: 'api/Roles',

View File

@ -11,6 +11,9 @@
var $btnSubmitMenu = $('#btnSubmitMenu');
var $nestMenu = $('#nestable_menu');
var $nestMenuInput = $nestMenu.find('div.dd3-content');
var $dialogApp = $("#dialogApp");
var $dialogAppHeader = $('#myAppModalLabel');
var $dialogAppForm = $('#appForm');
$('table').lgbTable({
url: Role.url,
@ -71,6 +74,22 @@
}
});
},
'#btn_assignApp': function (row) {
$.bc({
id: row.Id, url: App.url, method: "post",
callback: function (result) {
var htmlTemplate = this.htmlTemplate;
var html = $.map(result, function (element, index) {
return $.format(htmlTemplate, element.Id, element.AppName, element.Checked, "应用程序名称");
}).join('');
$dialogAppHeader.text($.format('{0}-应用授权窗口', row.RoleName));
$dialogAppForm.html(html).find('[data-toggle="tooltip"]').each(function (index, label) {
if (label.title === "") label.title = "未设置";
}).tooltip();
$dialogApp.modal('show');
}
});
},
'#btnSubmitUser': function (row) {
var roleId = row.Id;
var userIds = $dialogUser.find(':checked').map(function (index, element) {
@ -91,6 +110,13 @@
return $(element).val();
}).toArray();
$.bc({ id: roleId, url: Menu.url, method: "put", data: menuIds, modal: '#dialogMenu', title: Menu.title });
},
'#btnSubmitApp': function (row) {
var roleId = row.Id;
var appIds = $dialogApp.find(':checked').map(function (index, element) {
return $(element).val();
}).toArray();
$.bc({ id: roleId, url: App.url, method: "put", data: appIds, modal: '#dialogApp', title: App.title });
}
}
},

View File

@ -0,0 +1,63 @@
using Longbow.Data;
using System;
using System.Collections.Generic;
using System.Linq;
namespace Bootstrap.DataAccess
{
public class App
{
/// <summary>
/// 获得/设置 应用程序主键ID
/// </summary>
public string Id { get; set; }
/// <summary>
/// 获得/设置 群组名称
/// </summary>
public string AppName { get; set; }
/// <summary>
/// 获取/设置 用户群组关联状态 checked 标示已经关联 '' 标示未关联
/// </summary>
public string Checked { get; set; }
/// <summary>
/// 根据角色ID指派部门
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public virtual IEnumerable<App> RetrievesByRoleId(string roleId)
{
var db = DbManager.Create();
return db.Fetch<App>($"select d.Code as Id, d.Name as AppName, case ra.AppId when d.Code then 'checked' else '' end Checked from Dicts d left join RoleApp ra on d.Code = ra.AppId and ra.RoleId = @1 where d.Code > '0' and d.Category = @0", "应用程序", roleId);
}
/// <summary>
/// 根据角色ID以及选定的App ID保到角色应用表
/// </summary>
/// <param name="roleId"></param>
/// <param name="appIds"></param>
/// <returns></returns>
public virtual bool SaveByRoleId(string roleId, IEnumerable<string> appIds)
{
bool ret = false;
var db = DbManager.Create();
try
{
db.BeginTransaction();
//删除角色应用表该角色所有的应用
db.Execute("delete from RoleApp where RoleID = @0", roleId);
db.InsertBatch("RoleApp", appIds.Select(g => new { RoleID = roleId, AppID = g }));
db.CompleteTransaction();
ret = true;
}
catch (Exception ex)
{
db.AbortTransaction();
throw ex;
}
return ret;
}
}
}

View File

@ -17,9 +17,10 @@ namespace Bootstrap.DataAccess
/// <param name="userIds"></param>
/// <param name="groupIds"></param>
/// <param name="menuIds"></param>
/// <param name="appIds"></param>
/// <param name="dictIds"></param>
/// <param name="cacheKey"></param>
public static void ClearCache(IEnumerable<string> roleIds = null, IEnumerable<string> userIds = null, IEnumerable<string> groupIds = null, IEnumerable<string> menuIds = null, IEnumerable<string> dictIds = null, string cacheKey = null)
public static void ClearCache(IEnumerable<string> roleIds = null, IEnumerable<string> userIds = null, IEnumerable<string> groupIds = null, IEnumerable<string> menuIds = null, IEnumerable<string> appIds = null, IEnumerable<string> dictIds = null, string cacheKey = null)
{
var cacheKeys = new List<string>();
var corsKeys = new List<string>();
@ -71,6 +72,13 @@ namespace Bootstrap.DataAccess
cacheKeys.Add(MenuHelper.RetrieveMenusAll + "*");
corsKeys.Add(MenuHelper.RetrieveMenusAll + "*");
}
if (appIds != null)
{
appIds.ToList().ForEach(id =>
{
cacheKeys.Add(string.Format("{0}-{1}", AppHelper.RetrieveAppsByRoleIdDataKey, id));
});
}
if (dictIds != null)
{
cacheKeys.Add(DictHelper.RetrieveDictsDataKey + "*");

View File

@ -0,0 +1,31 @@
using Longbow.Cache;
using Longbow.Data;
using System.Collections.Generic;
namespace Bootstrap.DataAccess
{
public static class AppHelper
{
public const string RetrieveAppsByRoleIdDataKey = "AppHelper-RetrieveAppsByRoleId";
/// <summary>
/// 根据角色ID指派应用程序
/// </summary>
/// <param name="roleId"></param>
/// <returns></returns>
public static IEnumerable<App> RetrievesByRoleId(string roleId) => CacheManager.GetOrAdd(string.Format("{0}-{1}", RetrieveAppsByRoleIdDataKey, roleId), key => DbContextManager.Create<App>().RetrievesByRoleId(roleId), RetrieveAppsByRoleIdDataKey);
/// <summary>
/// 根据角色ID以及选定的App ID保到角色应用表
/// </summary>
/// <param name="roleId"></param>
/// <param name="appIds"></param>
/// <returns></returns>
public static bool SaveByRoleId(string roleId, IEnumerable<string> appIds)
{
var ret = DbContextManager.Create<App>().SaveByRoleId(roleId, appIds);
if (ret) CacheCleanUtility.ClearCache(appIds: appIds, roleIds: new List<string>() { roleId });
return ret;
}
}
}