修复部分BUG,完成为角色分配菜单的界面

This commit is contained in:
yubaolee 2015-12-06 23:40:34 +08:00
parent 9e419296c2
commit d29eb36833
10 changed files with 327 additions and 56 deletions

View File

@ -1,53 +1,110 @@
// ***********************************************************************
// Assembly : OpenAuth.App
// Author : Yubao Li
// Created : 12-02-2015
//
// Last Modified By : Yubao Li
// Last Modified On : 12-02-2015
// ***********************************************************************
// <copyright file="ModuleElementManagerApp.cs" company="">
// Copyright (c) . All rights reserved.
// </copyright>
// <summary>模块元素</summary>
// ***********************************************************************
using System.Collections.Generic;
using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
namespace OpenAuth.App
{
public class ModuleElementManagerApp
{
private readonly IRepository<ModuleElement> _repository;
public ModuleElementManagerApp(IRepository<ModuleElement> repository)
{
_repository = repository;
}
public void AddOrUpdate(ModuleElement model)
{
if (model.Id == 0)
{
_repository.Add(model);
}
else
{
_repository.Update(model);
}
}
public IEnumerable<ModuleElement> LoadByModuleId(int id)
{
var modules = _repository.Find(u => u.ModuleId == id);
return modules;
}
public void Delete(int id)
{
_repository.Delete(u =>u.Id ==id);
}
}
}
// ***********************************************************************
// Assembly : OpenAuth.App
// Author : Yubao Li
// Created : 12-02-2015
//
// Last Modified By : Yubao Li
// Last Modified On : 12-02-2015
// ***********************************************************************
// <copyright file="ModuleElementManagerApp.cs" company="">
// Copyright (c) . All rights reserved.
// </copyright>
// <summary>模块元素</summary>
// ***********************************************************************
using System.Collections.Generic;
using System.Linq;
using Infrastructure;
using OpenAuth.App.ViewModel;
using OpenAuth.Domain;
using OpenAuth.Domain.Interface;
namespace OpenAuth.App
{
public class ModuleElementManagerApp
{
private readonly IRepository<ModuleElement> _repository;
private IModuleRepository _moduleRepository;
private IRelevanceRepository _relevanceRepository;
public ModuleElementManagerApp(IRepository<ModuleElement> repository,
IRelevanceRepository relevanceRepository,
IModuleRepository moduleRepository )
{
_repository = repository;
_moduleRepository = moduleRepository;
_relevanceRepository = relevanceRepository;
}
public void AddOrUpdate(ModuleElement model)
{
if (model.Id == 0)
{
_repository.Add(model);
}
else
{
_repository.Update(model);
}
}
public IEnumerable<ModuleElement> LoadByModuleId(int id)
{
var modules = _repository.Find(u => u.ModuleId == id);
return modules;
}
/// <summary>
/// 获取带有授权状态的菜单列表
/// </summary>
/// <param name="accessType">授权类型当前有RoleElement/UserElement</param>
/// <param name="firstId">
/// 当为RoleElement时表示RoleId
/// 当为UserElement时表示UserId
/// </param>
/// <param name="moduleId">模块ID</param>
public List<ModuleElementVM> LoadWithAccess(string accessType, int firstId, int moduleId)
{
//TODO:多个Repository使用的是不同的Context不能进行联表查询要用UnitOfWork处理
//var results = from element in _repository.Find(u => u.ModuleId == moduleId)
// join module in _moduleRepository.Find(null) on element.ModuleId equals module.Id
// join relev in _relevanceRepository.Find(u => u.Key == accessType && u.FirstId == firstId)
// on element.Id equals relev.SecondId into temp
// from t in temp.DefaultIfEmpty()
// select new ModuleElementVM
// {
// DomId = element.DomId,
// Id = element.Id,
// ModuleId = element.ModuleId,
// ModuleName = module.Name,
// Name = element.Name,
// Accessed = t != null
// };
var listVms = new List<ModuleElementVM>();
if (moduleId == 0) return listVms;
string modulename = _moduleRepository.FindSingle(u => u.Id == moduleId).Name;
foreach (var element in LoadByModuleId(moduleId))
{
var accessed = _relevanceRepository.FindSingle(u =>u.Key == accessType
&& u.FirstId == firstId && u.SecondId == element.Id);
ModuleElementVM vm = new ModuleElementVM
{
Id = element.Id,
Name = element.Name,
ModuleId = element.ModuleId,
DomId = element.DomId,
ModuleName = modulename,
Accessed = accessed != null
};
listVms.Add(vm);
}
return listVms;
}
public void Delete(int id)
{
_repository.Delete(u =>u.Id ==id);
}
}
}

View File

@ -115,7 +115,7 @@ namespace OpenAuth.App
_relevanceRepository.Find(u => u.FirstId == userId && u.Key == "UserModule")
.Select(u => u.SecondId)
.ToList();
if (!moduleIds.Any()) return null;
if (!moduleIds.Any()) return new List<Module>();
return _repository.Find(u => moduleIds.Contains(u.Id)).ToList();
}
@ -140,7 +140,7 @@ namespace OpenAuth.App
_relevanceRepository.Find(u => u.FirstId == roleId && u.Key == "RoleModule")
.Select(u => u.SecondId)
.ToList();
if (!moduleIds.Any()) return null;
if (!moduleIds.Any()) return new List<Module>();
return _repository.Find(u => moduleIds.Contains(u.Id)).ToList();
}

View File

@ -51,6 +51,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="OrgManagerApp.cs" />
<Compile Include="ViewModel\LoginUserVM.cs" />
<Compile Include="ViewModel\ModuleElementVM.cs" />
<Compile Include="ViewModel\ModuleView.cs" />
<Compile Include="ViewModel\RoleVM.cs" />
<Compile Include="ViewModel\UserView.cs" />

View File

@ -39,6 +39,7 @@ namespace OpenAuth.App
/// </summary>
public dynamic Load(int orgId, int pageindex, int pagesize)
{
if (pageindex < 1) pageindex = 1; //TODO:如果列表为空新增加一个用户后前端会传一个0过来奇怪
IEnumerable<Role> roles;
int total = 0;
if (orgId == 0)

View File

@ -39,7 +39,7 @@ namespace OpenAuth.App
/// </summary>
public dynamic Load(int orgId, int pageindex, int pagesize)
{
if (pageindex < 1) pageindex = 1; //如果列表为空新增加一个用户后前端会传一个0过来奇怪
if (pageindex < 1) pageindex = 1; //TODO:如果列表为空新增加一个用户后前端会传一个0过来奇怪
IEnumerable<User> users;
int total = 0;
if (orgId == 0)

View File

@ -0,0 +1,49 @@
namespace OpenAuth.App.ViewModel
{
/// <summary>
/// 用户ID
/// </summary>
public class ModuleElementVM
{
/// <summary>
/// 用户ID
/// </summary>
/// <returns></returns>
public int Id { get; set; }
/// <summary>
/// DOM ID
/// </summary>
/// <returns></returns>
public string DomId { get; set; }
/// <summary>
/// 组织名称
/// </summary>
/// <returns></returns>
public string Name { get; set; }
//模块ID
public int ModuleId { get; set; }
/// <summary>
/// 所属模块名称
/// </summary>
public string ModuleName { get; set; }
/// <summary>
/// 授权状态
/// </summary>
public bool Accessed { get; set; }
public ModuleElementVM()
{
this.Id = 0;
this.DomId = string.Empty;
this.Name = string.Empty;
this.ModuleId = 0;
this.ModuleName = string.Empty;
this.Accessed = false;
}
}
}

View File

@ -13,10 +13,12 @@
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Web.Mvc;
using Infrastructure;
using OpenAuth.App;
using OpenAuth.App.ViewModel;
using OpenAuth.Domain;
using OpenAuth.Mvc.Models;
@ -68,5 +70,19 @@ namespace OpenAuth.Mvc.Controllers
}
return JsonHelper.Instance.Serialize(_bjuiResponse);
}
#region
public ActionResult AssignForRole(int roleId)
{
ViewBag.RoleId = roleId;
return View();
}
public string Load(int roleId, int orgId)
{
return JsonHelper.Instance.Serialize(_app.LoadWithAccess("RoleElement", roleId, orgId));
}
#endregion
}
}

View File

@ -625,6 +625,7 @@
<Content Include="Views\ModuleManager\LookupMultiForRole.cshtml" />
<None Include="Views\Home\MenuHeader.cshtml" />
<None Include="Views\ModuleElementManager\Index.cshtml" />
<Content Include="Views\ModuleElementManager\AssignForRole.cshtml" />
<None Include="Views\OrgManager\AddOrg.cshtml" />
<Content Include="Views\OrgManager\LookupParent.cshtml" />
<Content Include="Views\UserManager\Index.cshtml" />

View File

@ -0,0 +1,132 @@
@{
string _prefix = "assignForRole";
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" data-toggle="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 gridid = '#@_gridId';
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: '元素名称',
hide: true
},
{
name: 'Name',
label: '元素名称',
width: 80
},
{
name: 'ModuleName',
label: '所属模块',
width: 80
},
{
name: 'Accessed',
label: '是否已经授权',
type: 'select',
align: 'center',
items: [{ 'false': '未授权', 'true': '已授权' }],
width: 80
}
],
dataUrl: 'ModuleElementManager/Load?orgId=' + selectedId +'&roleId='+$('#roleId').val(),
fullGrid: true,
showLinenumber: true,
showCheckboxcol: true,
paging: true,
filterMult: false,
showTfoot: true
});
}
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('ModuleManager/LoadForTree', function (json) {
var zTreeObj = $.fn.zTree.init($('#@_treeId'), setting, json);
zTreeObj.expandAll(true);
});
}
//授权选中的
function assign() {
var selected = getSelected(gridid, 2);
if (selected == null) return;
$(this).dialog({
id: 'assign',
url: '/ModuleManager/Add?id=' + selected,
title: '编辑',
onClose: function () {
refreshGrid();
}
});
}
function refreshGrid() {
$('#@_gridId').datagrid('refresh');
// loadDataGrid();
}
//@@ sourceURL=ModuleManagerIndex.js
</script>

View File

@ -170,5 +170,19 @@
});
}
//为角色分配菜单
function openAssignElement(obj) {
var selected = getSelected(gridid, 2);
if (selected == null) return;
$(obj).dialog({
id: 'assignElement',
url: '/ModuleElementManager/AssignForRole?roleId=' + selected,
title: '为角色分配菜单',
width: 700,
height:380
});
}
//@@ sourceURL=RoleManagerIndex.js
</script>