完成资源分配前端模块化处理

This commit is contained in:
yubaolee 2016-04-17 23:16:58 +08:00
parent 2f0a9a3719
commit 40ffa5f1e8
16 changed files with 285 additions and 398 deletions

View File

@ -48,6 +48,7 @@
<Compile Include="ModuleElementManagerApp.cs" />
<Compile Include="ModuleManagerApp.cs" />
<Compile Include="ResourceManagerApp.cs" />
<Compile Include="RevelanceManagerApp.cs" />
<Compile Include="RoleManagerApp.cs" />
<Compile Include="StockManagerApp.cs" />
<Compile Include="UserManagerApp.cs" />

View File

@ -118,7 +118,7 @@ namespace OpenAuth.App
public List<ResourceVM> LoadWithAccess(string accessType, int firstId, int cId)
{
var listVms = new List<ResourceVM>();
if (cId == 0) return listVms;
// if (cId == 0) return listVms;
foreach (var element in _repository.LoadInOrgs(cId))
{
@ -137,17 +137,6 @@ namespace OpenAuth.App
return listVms;
}
/// <summary>
/// 为用户分配资源
/// </summary>
/// <param name="userId">用户ID</param>
/// <param name="resIds">资源ID数组</param>
public void AssignResForUser(int userId, int[] resIds)
{
_relevanceRepository.DeleteBy("UserResource", resIds);
_relevanceRepository.AddRelevance("UserResource", resIds.ToLookup(u => userId));
}
/// <summary>
/// 为角色分配资源
/// </summary>
@ -155,8 +144,12 @@ namespace OpenAuth.App
/// <param name="resIds">资源ID数组</param>
public void AssignResForRole(int roleId, int[] resIds)
{
_relevanceRepository.DeleteBy("RoleResource", resIds);
_relevanceRepository.AddRelevance("RoleResource", resIds.ToLookup(u => roleId));
}
public void DelResForRole(int roleId, int[] resIds)
{
_relevanceRepository.DeleteBy("RoleResource", resIds.ToLookup(u =>roleId));
}
}
}

View File

@ -0,0 +1,44 @@

using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using Infrastructure;
using OpenAuth.App.ViewModel;
namespace OpenAuth.App
{
public class RevelanceManagerApp
{
private readonly IRelevanceRepository _relevanceRepository;
public RevelanceManagerApp(IRelevanceRepository relevanceRepository)
{
_relevanceRepository = relevanceRepository;
}
/// <summary>
/// 添加关联
/// <para>比如给用户分配资源那么firstId就是用户IDsecIds就是资源ID列表</para>
/// </summary>
/// <param name="type">关联的类型,如"UserResource"</param>
public void Assign(string type, int firstId, int[] secIds)
{
_relevanceRepository.AddRelevance(type, secIds.ToLookup(u => firstId));
}
/// <summary>
/// 取消关联
/// </summary>
/// <param name="type">关联的类型,如"UserResource"</param>
/// <param name="firstId">The first identifier.</param>
/// <param name="secIds">The sec ids.</param>
public void UnAssign(string type, int firstId, int[] secIds)
{
_relevanceRepository.DeleteBy(type, secIds.ToLookup(u =>firstId));
}
}
}

View File

@ -42,9 +42,10 @@ namespace OpenAuth.Mvc
builder.RegisterType<ModuleElementManagerApp>();
builder.RegisterType<CategoryManagerApp>();
builder.RegisterType<ResourceManagerApp>();
builder.RegisterType<StockManagerApp>();
builder.RegisterType<StockManagerApp>();
builder.RegisterType<RevelanceManagerApp>();
// Register your MVC controllers.
// Register your MVC controllers.
builder.RegisterControllers(typeof(MvcApplication).Assembly);
// OPTIONAL: Register model binders that require DI.

View File

@ -0,0 +1,113 @@
// ***********************************************************************
// Assembly : OpenAuth.Mvc
// Author : yubaolee
// Created : 04-16-2016
//
// Last Modified By : yubaolee
// Last Modified On : 04-16-2016
// ***********************************************************************
// <copyright file="userRes.js" company="www.cnblogs.com/yubaolee">
// 版权所有(C) 2015
// </copyright>
// <summary>分配资源模块处理</summary>
// ***********************************************************************
$(document).ready(function () {
$.CurrentDialog.find("#btnAccess").on("click", function () {
var ids = dlgList.getSelectedProperties('Id');
if (ids == null) return;
$.post('/RelevanceManager/Assign', {
type: $("#moduleType").val(),
firstId: $('#firstId').val(),
secIds: ids
}, function (json) {
dlgList.reload();
});
});
$.CurrentDialog.find("#btnDelAccess").on("click", function () {
var ids = dlgList.getSelectedProperties('Id');
if (ids == null) return;
$.post('/RelevanceManager/UnAssign', {
type: $("#moduleType").val(),
firstId: $('#firstId').val(),
secIds: ids
}, function (json) {
dlgList.reload();
});
});
});
//grid列表模块
function DialogList() {
var selectedId = 0; //选中的ID
var url = '/ResourceManager/LoadWithAccess?cId=';
this.maingrid = $.CurrentDialog.find('#maingrid').datagrid({
showToolbar: false,
filterThead: false,
target: $(this),
columns: [
{
name: 'Id',
label: '角色ID',
hide: true
},
{
name: 'Name',
label: '资源名称',
width: 100
},
{
name: 'IsBelongUser',
label: '是否已经授权',
type: 'select',
align: 'center',
items: [{ 'false': '未授权', 'true': '已授权' }],
width: 100
}
],
dataUrl: url + selectedId + '&key=' + $('#moduleType').val() + '&firstId=' + $('#firstId').val(),
fullGrid: true,
showLinenumber: true,
showCheckboxcol: true,
paging: true,
filterMult: false,
showTfoot: false,
height: '100%'
});
this.reload = function (id) {
if (id != undefined) selectedId = id;
this.maingrid.datagrid('reload', { dataUrl: url + selectedId + '&key=' + $('#moduleType').val() + '&firstId=' + $('#firstId').val() });
}
};
DialogList.prototype = new Grid();
var dlgList = new DialogList();
var ztree = function () {
var setting = {
view: { selectedMulti: false },
data: {
key: {
name: 'Name',
title: 'Name'
},
simpleData: {
enable: true,
idKey: 'Id',
pIdKey: 'ParentId',
rootPId: 'null'
}
},
callback: { onClick: zTreeOnClick }
};
$.getJSON('/CategoryManager/LoadForTree', function (json) {
var zTreeObj = $.fn.zTree.init($.CurrentDialog.find("#tree"), setting, json);
zTreeObj.expandAll(true);
});
}();
function zTreeOnClick(event, treeId, treeNode) {
dlgList.reload(treeNode.Id);
}

View File

@ -272,12 +272,13 @@ function openRoleReourceAccess(obj) {
$(obj).dialog({
id: 'accessUserRole',
url: '/ResourceManager/LookupMultiForRole',
url: '/ResourceManager/AssignRes',
title: '为角色分配资源',
width: 600,
height: 380,
data: {
roleId: selected.Id
firstId: selected.Id,
key:"RoleResource"
}
});
}

View File

@ -300,12 +300,13 @@ function openUserReourceAccess(obj) {
$(obj).dialog({
id: 'accessUserRole',
url: '/ResourceManager/LookupMultiForUser',
url: '/ResourceManager/AssignRes',
title: '为用户分配资源',
width: 600,
height: 380,
data: {
userId: selected.Id
firstId: selected.Id,
key:"UserResource"
}
});
}

View File

@ -41,15 +41,6 @@ namespace OpenAuth.Mvc.Controllers
return View();
}
/// <summary>
/// 选择上级机构页面
/// </summary>
/// <returns>ActionResult.</returns>
public ActionResult LookupParent()
{
return View();
}
public ActionResult AddOrg()
{
return View();

View File

@ -0,0 +1,55 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Mvc;
using Infrastructure;
using OpenAuth.App;
using OpenAuth.Mvc.Models;
namespace OpenAuth.Mvc.Controllers
{
public class RelevanceManagerController : BaseController
{
private RevelanceManagerApp _app;
public RelevanceManagerController()
{
_app = AutofacExt.GetFromFac<RevelanceManagerApp>();
}
[HttpPost]
[Anonymous]
public string Assign(string type, int firstId, string secIds)
{
try
{
var secIdList = JsonHelper.Instance.Deserialize<int[]>(secIds);
_app.Assign(type, firstId, secIdList);
}
catch (Exception ex)
{
BjuiResponse.statusCode = "300";
BjuiResponse.message = ex.Message;
}
return JsonHelper.Instance.Serialize(BjuiResponse);
}
[HttpPost]
[Anonymous]
public string UnAssign(string type, int firstId, string secIds)
{
try
{
var secIdList = JsonHelper.Instance.Deserialize<int[]>(secIds);
_app.UnAssign(type, firstId, secIdList);
}
catch (Exception ex)
{
BjuiResponse.statusCode = "300";
BjuiResponse.message = ex.Message;
}
return JsonHelper.Instance.Serialize(BjuiResponse);
}
}
}

View File

@ -76,63 +76,31 @@ namespace OpenAuth.Mvc.Controllers
return JsonHelper.Instance.Serialize(BjuiResponse);
}
#region
public ActionResult LookupMultiForUser(int userId)
/// <summary>
/// 为用户或角色分配权限
/// </summary>
/// <param name="firstId">关联表中的firstId.</param>
/// <param name="key">关联表中的Key
/// <para>如UserResource/RoleResource</para>
/// </param>
/// <returns>ActionResult.</returns>
public ActionResult AssignRes(int firstId, string key)
{
ViewBag.UserId = userId;
ViewBag.FirstId = firstId;
ViewBag.ModuleType = key;
return View();
}
public string LoadWithUserAccess(int cId, int userId)
/// <summary>
/// 加载带有授权的资源信息
/// </summary>
/// <param name="cId">分类ID</param>
/// <param name="firstId">关联表中的firstId</param>
/// <param name="key">关联表中的key</param>
/// <returns>System.String.</returns>
public string LoadWithAccess(int cId, int firstId, string key)
{
return JsonHelper.Instance.Serialize(_app.LoadWithAccess("UserResource",userId, cId));
return JsonHelper.Instance.Serialize(_app.LoadWithAccess(key,firstId, cId));
}
public string AccessForUser(int userId, string ids)
{
try
{
var resIds = ids.Split(',').Select(id => int.Parse(id)).ToArray();
_app.AssignResForUser(userId, resIds);
}
catch (Exception e)
{
BjuiResponse.message = e.Message;
BjuiResponse.statusCode = "300";
}
return JsonHelper.Instance.Serialize(BjuiResponse);
}
#endregion
#region
public ActionResult LookupMultiForRole(int roleId)
{
ViewBag.RoleId = roleId;
return View();
}
public string LoadWithRoleAccess(int cId, int roleId)
{
return JsonHelper.Instance.Serialize(_app.LoadWithAccess("RoleResource", roleId, cId));
}
public string AccessForRole(int roleId, string ids)
{
try
{
var resIds = ids.Split(',').Select(id => int.Parse(id)).ToArray();
_app.AssignResForRole(roleId, resIds);
}
catch (Exception e)
{
BjuiResponse.message = e.Message;
BjuiResponse.statusCode = "300";
}
return JsonHelper.Instance.Serialize(BjuiResponse);
}
#endregion
}
}

View File

@ -138,6 +138,7 @@
<Compile Include="Controllers\ModuleElementManagerController.cs" />
<Compile Include="Controllers\ModuleManagerController.cs" />
<Compile Include="Controllers\OrgManagerController.cs" />
<Compile Include="Controllers\RelevanceManagerController.cs" />
<Compile Include="Controllers\ResourceManagerController.cs" />
<Compile Include="Controllers\RoleManagerController.cs" />
<Compile Include="Controllers\StockManagerController.cs" />
@ -161,6 +162,7 @@
<Content Include="BllScripts\usermanager.js" />
<Content Include="BllScripts\roleModuleElement.js" />
<Content Include="BllScripts\userModuleElement.js" />
<Content Include="BllScripts\assignRes.js" />
<Content Include="BllScripts\userRoleManager.js" />
<Content Include="Content\BJUI\images\captcha.jpeg" />
<Content Include="Content\BJUI\images\datagrid\AU.gif" />
@ -609,8 +611,7 @@
<Content Include="Views\Home\Index.cshtml" />
<Content Include="Views\ResourceManager\Index.cshtml" />
<Content Include="Views\RoleManager\LookupMulti.cshtml" />
<Content Include="Views\ResourceManager\LookupMultiForUser.cshtml" />
<Content Include="Views\ResourceManager\LookupMultiForRole.cshtml" />
<Content Include="Views\ResourceManager\AssignRes.cshtml" />
<Content Include="Views\StockManager\Index.cshtml" />
<Content Include="Views\OrgManager\LookupMultiForRole.cshtml" />
<Content Include="Views\OrgManager\LookupMultiForUser.cshtml" />
@ -676,7 +677,6 @@
<Content Include="Views\ModuleElementManager\AssignForUser.cshtml" />
<None Include="Views\ModuleElementManager\Index.cshtml" />
<Content Include="Views\ModuleElementManager\AssignForRole.cshtml" />
<Content Include="Views\OrgManager\LookupParent.cshtml" />
<Content Include="Views\UserManager\Index.cshtml" />
<Content Include="Views\RoleManager\Index.cshtml" />
</ItemGroup>

View File

@ -1,62 +0,0 @@
@model dynamic
@{
ViewBag.Title = "title";
Layout = null;
}
<div class="bjui-pageContent">
<div style="float: left; width: 100%; height: 240px; overflow: auto;" class="table table-bordered">
<ul id="lookupTree" class="ztree"></ul>
</div>
</div>
<div class="bjui-pageFooter">
<ul>
<li><button type="button" class="btn-close" data-icon="close">关闭</button></li>
<li>
<a href="javascript:;" id="btnSelected" data-toggle="lookupback"
data-args=""
class="btn btn-blue" title="选择本项" data-icon="check">选择</a>
</li>
</ul>
</div>
<script type="text/javascript">
$(document).ready(function () {
Init();
});
function Init() {
var setting = {
view: {
selectedMulti: false
},
data: {
key: {
name: 'Name',
title: 'Name'
},
simpleData: {
enable: true,
idKey: 'Id',
pIdKey: 'ParentId',
rootPId: 'null'
}
},
callback: {
onClick: zTreeOnClick
}
};
$.getJSON('/OrgManager/LoadOrg', function (json) {
var zTreeObj = $.fn.zTree.init($('#lookupTree'), setting, json);
zTreeObj.expandAll(true);
});
}
function zTreeOnClick(event, treeId, treeNode) {
var selected = "{ParentName:'" + treeNode.Name + "', ParentId:" + treeNode.Id + "}";
$("#btnSelected").attr("data-args",selected);
}
//@@ sourceURL=lookupParent.js
</script>

View File

@ -0,0 +1,31 @@
<div class="bjui-pageHeader">
<div class="bjui-searchBar">
<input style="display: none" id="firstId" value="@ViewBag.FirstId"/>
<input style="display: none" id="moduleType", value="@ViewBag.ModuleType"/>
<div class="pull-right">
<div class="alert alert-info search-inline">
<i class="fa fa-info-circle"></i> 点击行为单选,点击复选框可多选统一授权
</div>&nbsp;
<button type="button" class="btn btn-green" data-num="1" data-icon="plus" id="btnAccess">
授权选中
</button>&nbsp;
<button type="button" class="btn btn-danger" data-num="1" data-icon="trash" id="btnDelAccess">
取消授权
</button>&nbsp;
</div>
</div>
</div>
<div class="bjui-pageContent tableContent">
<div class="clearfix">
<div style="float: left; width: 220px; overflow: auto;" class="table table-bordered">
<ul id="tree" class="ztree"></ul>
</div>
<div style="margin-left: 225px;">
<table id="maingrid" class="table table-bordered table-hover table-striped table-top"></table>
</div>
</div>
</div>
<script src="~/BllScripts/grid.js"></script>
<script src="~/BllScripts/assignRes.js"></script>

View File

@ -1,126 +0,0 @@
@{
string _prefix = "assignResForRole";
var _treeId = _prefix + "Tree";
var _gridId = _prefix + "Grid";
var _treeDetail = _prefix + "Detail";
}
<div class="bjui-pageHeader">
<div class="bjui-searchBar">
<input style="display: none" id="roleId" value="@ViewBag.RoleId" />
<div class="pull-right">
<div class="alert alert-info search-inline">
<i class="fa fa-info-circle"></i> 点击行为单选,点击复选框可多选统一授权
</div>&nbsp;
<button type="button" class="btn-green" data-num="1" data-icon="plus" onclick="assign()">
授权选中项目
</button>&nbsp;
</div>
</div>
</div>
<div class="bjui-pageContent tableContent">
<div class="clearfix">
<div style="float: left; width: 220px; overflow: auto;" class="table table-bordered">
<ul id="@_treeId" class="ztree"></ul>
</div>
<div id="@_treeDetail" style="margin-left: 225px;">
</div>
</div>
</div>
<script type="text/javascript">
var selectedId = 0;
$(document).ready(function () {
initZtree();
loadDataGrid();
});
//加载数据到datagrid
function loadDataGrid() {
//b-jui的datagrid需要重新处理HTML
$('#@_treeDetail').empty()
.append('<table id="@_gridId" class="table table-bordered table-hover table-striped table-top"></table>');
$('#@_gridId').datagrid({
showToolbar: false,
filterThead: false,
columns: [
{
name: 'Id',
label: '角色ID',
hide: true
},
{
name: 'Name',
label: '资源名称',
width: 100
},
{
name: 'IsBelongUser',
label: '是否已经授权',
type: 'select',
align: 'center',
items: [{ 'false': '未授权', 'true': '已授权' }],
width: 100
}
],
dataUrl: '/ResourceManager/LoadWithRoleAccess?cId=' + selectedId + '&roleId=' + $('#roleId').val(),
fullGrid: true,
showLinenumber: true,
showCheckboxcol: true,
paging: true,
filterMult: false,
showTfoot: false
});
}
function zTreeOnClick(event, treeId, treeNode) {
selectedId = treeNode.Id;
loadDataGrid();
}
function initZtree() {
var setting = {
view: { selectedMulti: false },
data: {
key: {
name: 'Name',
title: 'Name'
},
simpleData: {
enable: true,
idKey: 'Id',
pIdKey: 'ParentId',
rootPId: 'null'
}
},
callback: { onClick: zTreeOnClick }
};
$.getJSON('/CategoryManager/LoadForTree', function (json) {
var zTreeObj = $.fn.zTree.init($('#@_treeId'), setting, json);
zTreeObj.expandAll(true);
});
}
//授权选中的
function assign() {
var selected = getSelectedMany('#@_gridId', 2);
if (selected == null) return;
$.post('/ResourceManager/AccessForRole', {
roleId: $('#roleId').val(),
ids: selected
},
function (json) {
// var rel = $.parseJSON(json);
refreshGrid();
});
}
function refreshGrid() {
$('#@_gridId').datagrid('refresh');
// loadDataGrid();
}
//@@ sourceURL=RoleLookup.js
</script>

View File

@ -1,126 +0,0 @@
@{
string _prefix = "assignResForUser";
var _treeId = _prefix + "Tree";
var _gridId = _prefix + "Grid";
var _treeDetail = _prefix + "Detail";
}
<div class="bjui-pageHeader">
<div class="bjui-searchBar">
<input style="display: none" id="userId" value="@ViewBag.UserId" />
<div class="pull-right">
<div class="alert alert-info search-inline">
<i class="fa fa-info-circle"></i> 点击行为单选,点击复选框可多选统一授权
</div>&nbsp;
<button type="button" class="btn-green" data-num="1" data-icon="plus" onclick="assign()">
授权选中项目
</button>&nbsp;
</div>
</div>
</div>
<div class="bjui-pageContent tableContent">
<div class="clearfix">
<div style="float: left; width: 220px; overflow: auto;" class="table table-bordered">
<ul id="@_treeId" class="ztree"></ul>
</div>
<div id="@_treeDetail" style="margin-left: 225px;">
</div>
</div>
</div>
<script type="text/javascript">
var selectedId = 0;
$(document).ready(function () {
initZtree();
loadDataGrid();
});
//加载数据到datagrid
function loadDataGrid() {
//b-jui的datagrid需要重新处理HTML
$('#@_treeDetail').empty()
.append('<table id="@_gridId" class="table table-bordered table-hover table-striped table-top"></table>');
$('#@_gridId').datagrid({
showToolbar: false,
filterThead: false,
columns: [
{
name: 'Id',
label: '角色ID',
hide: true
},
{
name: 'Name',
label: '资源名称',
width: 100
},
{
name: 'IsBelongUser',
label: '是否已经授权',
type: 'select',
align: 'center',
items: [{ 'false': '未授权', 'true': '已授权' }],
width: 100
}
],
dataUrl: '/ResourceManager/LoadWithUserAccess?cId=' + selectedId + '&userId=' + $('#userId').val(),
fullGrid: true,
showLinenumber: true,
showCheckboxcol: true,
paging: true,
filterMult: false,
showTfoot: false
});
}
function zTreeOnClick(event, treeId, treeNode) {
selectedId = treeNode.Id;
loadDataGrid();
}
function initZtree() {
var setting = {
view: { selectedMulti: false },
data: {
key: {
name: 'Name',
title: 'Name'
},
simpleData: {
enable: true,
idKey: 'Id',
pIdKey: 'ParentId',
rootPId: 'null'
}
},
callback: { onClick: zTreeOnClick }
};
$.getJSON('/CategoryManager/LoadForTree', function (json) {
var zTreeObj = $.fn.zTree.init($('#@_treeId'), setting, json);
zTreeObj.expandAll(true);
});
}
//授权选中的
function assign() {
var selected = getSelectedMany('#@_gridId', 2);
if (selected == null) return;
$.post('/ResourceManager/AccessForUser', {
userId: $('#userId').val(),
ids: selected
},
function (json) {
// var rel = $.parseJSON(json);
refreshGrid();
});
}
function refreshGrid() {
$('#@_gridId').datagrid('refresh');
// loadDataGrid();
}
//@@ sourceURL=RoleLookup.js
</script>

View File

@ -19,7 +19,9 @@ namespace OpenAuth.Repository
public IEnumerable<Resource> LoadInOrgs(params int[] orgId)
{
var result = from resource in Context.Resources.Where(u => orgId.Contains(u.CategoryId)) select resource;
bool isZero = orgId.Length == 1 && orgId[0] == 0; //判断传进来的是否为0
var result = from resource in Context.Resources.Where(u =>isZero
|| orgId.Contains(u.CategoryId)) select resource;
return result;
}