通用权限管理系统数据权限设置功能解析_javascript


在权限管理系统中,数据权限是比较难的,在我们通用权限系统中,数据权限指的是用户基于某个权限域对某些基础数据的操作权限,如上图,公司管理这个菜单被定义是数据权限,表示某些人在公司管理是可指定访问哪些基础数据,这个要与应用结合。如下图,可以限制该9999xudeng003用户在公司管理页面只能管理其中的几个公司。


通用权限管理系统数据权限设置功能解析_javascript_02

目前基于用户的数据权限BS的管理功能已完成,这个是权限系统最难开发的一部分。

通用权限管理系统数据权限设置功能解析_javascript_03


下面是MVC控制器调的通用权限管理系统底层的方法:

//-----------------------------------------------------------------------
// <copyright file="PermissionController.cs" company="Hairihan TECH, Ltd.">
// Copyright (c) 2015 , All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Mvc;

namespace DotNet.MVC.Controllers
{
using DotNet.Business;
using DotNet.MVC.Attributes;
using DotNet.MVC.Infrastructure;
using DotNet.Utilities;
using DotNet.Model;
using DotNet.MVC.Models;

/// <summary>
/// PermissionController
/// 权限服务
///
/// 修改纪录
///
/// 2016-01-17 版本:1.0 SongBiao 创建文件。
///
/// <author>
/// <name>SongBiao</name>
/// <date>2016-01-17</date>
/// </author>
/// </summary>
[CheckLogin]
public class PermissionController : BaseController
{
//
// GET: /Permission/
public ActionResult Index()
{
return View();
}

/// <summary>
/// 授予用户权限
/// </summary>
/// <param name="userIds"></param>
/// <param name="permissionIds"></param>
/// <param name="systemCode"></param>
/// <returns></returns>
public ActionResult GrantUserPermissions(string userIds, string permissionIds, string systemCode = null)
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string tableName = systemCode + "Permission";
var manager = new BaseUserPermissionManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
// 小心异常,检查一下参数的有效性
int result = 0;
string[] grantUserIds = userIds.Split(',');
string[] grantPermissionIds = permissionIds.Split(',');
if (grantUserIds.Any() && grantPermissionIds.Any())
{
result = manager.Grant(systemCode, grantUserIds, grantPermissionIds);
}
BaseResult baseResult = new BaseResult();
if (result > 0)
{
baseResult.Status = true;
baseResult.StatusMessage = "授权成功。";
}
else
{
baseResult.Status = false;
baseResult.StatusMessage = "没有授权。";
}
return Json(baseResult, JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 撤消用户操作权限
/// </summary>
/// <param name="userIds"></param>
/// <param name="permissionIds"></param>
/// <param name="systemCode"></param>
/// <returns></returns>
public ActionResult RevokeUserPermissions(string userIds, string permissionIds, string systemCode = null)
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string tableName = systemCode + "Permission";
var manager = new BaseUserPermissionManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
// 小心异常,检查一下参数的有效性
int result = 0;
string[] grantUserIds = userIds.Split(',');
string[] grantPermissionIds = permissionIds.Split(',');
if (grantUserIds.Any() && grantPermissionIds.Any())
{
result = manager.Revoke(systemCode, grantUserIds, grantPermissionIds);
}
BaseResult baseResult = new BaseResult();
if (result > 0)
{
baseResult.Status = true;
baseResult.StatusMessage = "撤消用户操作权限成功。";
}
else
{
baseResult.Status = false;
baseResult.StatusMessage = "没有授撤消用户操作权限。";
}
return Json(baseResult, JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 角色授权
/// </summary>
/// <param name="roleIds"></param>
/// <param name="permissionIds"></param>
/// <param name="systemCode"></param>
/// <returns></returns>
public ActionResult GrantRolePermissions(string roleIds, string permissionIds, string systemCode = null)
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string tableName = systemCode + "Permission";
var manager = new BaseRolePermissionManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
// 小心异常,检查一下参数的有效性
int result = 0;
string[] grantroleIds = roleIds.Split(',');
string[] grantPermissionIds = permissionIds.Split(',');
if (grantroleIds.Any() && grantPermissionIds.Any())
{
result = manager.Grant(systemCode, grantroleIds, grantPermissionIds);
}
BaseResult baseResult = new BaseResult();
if (result > 0)
{
baseResult.Status = true;
baseResult.StatusMessage = "授权成功。";
}
else
{
baseResult.Status = false;
baseResult.StatusMessage = "没有授权。";
}
return Json(baseResult, JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 撤消角色的权限
/// </summary>
/// <param name="roleIds"></param>
/// <param name="permissionIds"></param>
/// <param name="systemCode"></param>
/// <returns></returns>
public ActionResult RevokeRolePermissions(string roleIds, string permissionIds, string systemCode = null)
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string tableName = systemCode + "Permission";
var manager = new BaseRolePermissionManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
// 小心异常,检查一下参数的有效性
int result = 0;
string[] grantroleIds = roleIds.Split(',');
string[] grantPermissionIds = permissionIds.Split(',');
if (grantroleIds.Any() && grantPermissionIds.Any())
{
result = manager.Revoke(systemCode, grantroleIds, grantPermissionIds);
}
BaseResult baseResult = new BaseResult();
if (result > 0)
{
baseResult.Status = true;
baseResult.StatusMessage = "撤销成功。";
}
else
{
baseResult.Status = false;
baseResult.StatusMessage = "没有撤销。";
}
return Json(baseResult, JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 组织机构授权
/// </summary>
/// <param name="organizeIds"></param>
/// <param name="permissionIds"></param>
/// <param name="systemCode"></param>
/// <returns></returns>
public ActionResult GrantOrganizePermissions(string organizeIds, string permissionIds, string systemCode = null)
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string tableName = systemCode + "Permission";
var manager = new BaseOrganizePermissionManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
// 小心异常,检查一下参数的有效性
int result = 0;
string[] grantorganizeIds = organizeIds.Split(',');
string[] grantPermissionIds = permissionIds.Split(',');
if (grantorganizeIds.Any() && grantPermissionIds.Any())
{
result = manager.Grant(systemCode, grantorganizeIds, grantPermissionIds);
}
BaseResult baseResult = new BaseResult();
if (result > 0)
{
baseResult.Status = true;
baseResult.StatusMessage = "授权成功。";
}
else
{
baseResult.Status = false;
baseResult.StatusMessage = "没有授权。";
}
return Json(baseResult, JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 撤销组织机构授权
/// </summary>
/// <param name="organizeIds"></param>
/// <param name="permissionIds"></param>
/// <param name="systemCode"></param>
/// <returns></returns>
public ActionResult RevokeOrganizePermissions(string organizeIds, string permissionIds, string systemCode = null)
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string tableName = systemCode + "Permission";
var manager = new BaseOrganizePermissionManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
// 小心异常,检查一下参数的有效性
int result = 0;
string[] grantorganizeIds = organizeIds.Split(',');
string[] grantPermissionIds = permissionIds.Split(',');
if (grantorganizeIds.Any() && grantPermissionIds.Any())
{
result = manager.Revoke(systemCode, grantorganizeIds, grantPermissionIds);
}
BaseResult baseResult = new BaseResult();
if (result > 0)
{
baseResult.Status = true;
baseResult.StatusMessage = "撤销成功。";
}
else
{
baseResult.Status = false;
baseResult.StatusMessage = "没有撤销。";
}
return Json(baseResult, JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 撤销用户的组织机构范围权限
/// </summary>
/// <param name="userId"></param>
/// <param name="organizeIds"></param>
/// <param name="systemCode"></param>
/// <param name="permissionCode"></param>
/// <returns></returns>
public ActionResult RevokeUserOrganizeScopes(string userId, string organizeIds, string systemCode = null, string permissionCode = "Resource.ManagePermission")
{
BaseResult baseResult = new BaseResult();
try
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}

string[] revokeOrganizeIds = organizeIds.Split(',');
string tableName = systemCode + "PermissionScope";
var manager = new BaseUserScopeManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
// 小心异常,检查一下参数的有效性
if (revokeOrganizeIds != null)
{
baseResult.RecordCount = manager.RevokeOrganizes(systemCode, userId, revokeOrganizeIds, permissionCode);
}
baseResult.StatusMessage = "用户对组织机构的数据权限已被撤销。";
baseResult.Status = true;
}
catch (Exception ex)
{
baseResult.Status = false;
baseResult.StatusMessage = "设置用户对组织机构的数据权限出现异常:" + ex.Message;
}

return Json(baseResult, JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 数据权限
/// 设置用户的某个权限域的组织范围 用户可以操作那些网点
/// </summary>
/// <param name="userId"></param>
/// <param name="organizeIds"></param>
/// <param name="systemCode"></param>
/// <param name="permissionCode"></param>
/// <returns></returns>
public ActionResult GrantUserOrganizeScopes(string userId, string organizeIds, string systemCode = null, string permissionCode = "Resource.ManagePermission")
{
BaseResult baseResult = new BaseResult();
try
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}

string[] grantOrganizeIds = organizeIds.Split(',');
string tableName = systemCode + "PermissionScope";
var manager = new BaseUserScopeManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
// 小心异常,检查一下参数的有效性
if (!grantOrganizeIds.Any())
{
baseResult.RecordCount = manager.RevokeOrganize(OperateContext.Current.UserInfo.SystemCode, userId, permissionCode);
baseResult.StatusMessage = "用户对组织机构的数据权限已被撤销。";
}
else
{
baseResult.RecordCount = manager.GrantOrganizes(OperateContext.Current.UserInfo.SystemCode, userId, grantOrganizeIds, permissionCode);
baseResult.StatusMessage = "已成功授予用户的组织机构数据权限。";
}
baseResult.Status = true;
}
catch (Exception ex)
{
baseResult.Status = false;
baseResult.StatusMessage = "用户对组织机构的数据权限设置异常:" + ex.Message;
}

return Json(baseResult, JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 设置用户的某个权限域的用户范围
/// </summary>
/// <param name="userId"></param>
/// <param name="userIds"></param>
/// <param name="systemCode"></param>
/// <param name="permissionId"></param>
/// <returns></returns>
public ActionResult GrantUserUserScopes(string userId, string userIds, string permissionId, string systemCode = null)//string permissionCode = "Resource.ManagePermission"
{
BaseResult baseResult = new BaseResult();
try
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string[] grantUserIds = userIds.Split(',');
string tableName = systemCode + "PermissionScope";
var manager = new BaseUserScopeManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
// 小心异常,检查一下参数的有效性
if (grantUserIds.Any())
{
baseResult.RecordCount = manager.GrantUsers(userId, grantUserIds, permissionId, systemCode);
}
baseResult.Status = true;
baseResult.StatusMessage = "成功设置用户的用户范围权限域。";

}
catch (Exception ex)
{
baseResult.Status = false;
baseResult.StatusMessage = "设置用户的权限域的用户范围出现异常:" + ex.Message;
}

return Json(baseResult, JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 撤销用户的某个权限域的用户范围
/// </summary>
/// <param name="userId"></param>
/// <param name="userIds"></param>
/// <param name="permissionId"></param>
/// <param name="systemCode"></param>
/// <returns></returns>
public ActionResult RevokeUserUserScopes(string userId, string userIds, string permissionId, string systemCode)//string permissionCode = "Resource.ManagePermission"
{
BaseResult baseResult = new BaseResult();
try
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string[] revokeUserIds = userIds.Split(',');
string tableName = systemCode + "PermissionScope";
var manager = new BaseUserScopeManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
// 小心异常,检查一下参数的有效性
if (revokeUserIds.Any())
{
baseResult.RecordCount = manager.RevokeUsers(userId, revokeUserIds, permissionId);
}
baseResult.Status = true;
baseResult.StatusMessage = "成功撤销用户的权限域的用户范围。";
}
catch (Exception ex)
{
baseResult.Status = false;
baseResult.StatusMessage = "撤销用户的某个权限域的用户范围出现异常:" + ex.Message;
}

return Json(baseResult, JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 设置用户的某个权限域的角色范围
/// </summary>
/// <param name="userId"></param>
/// <param name="roleIds"></param>
/// <param name="permissionId"></param>
/// <param name="systemCode"></param>
/// <returns></returns>
public ActionResult GrantUserRoleScopes(string userId, string roleIds, string permissionId, string systemCode = null) //string permissionCode = "Resource.ManagePermission"
{
BaseResult baseResult = new BaseResult();
try
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string tableNameModule = systemCode + "Module";
BaseModuleManager moduleManager = new BaseModuleManager(tableNameModule);
BaseModuleEntity moduleEntity = moduleManager.GetObject(permissionId);
string permissionCode = moduleEntity.Code;

string[] grantRoleIds = roleIds.Split(',');
string tableName = systemCode + "PermissionScope";

var manager = new BaseUserScopeManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
// 小心异常,检查一下参数的有效性
if (grantRoleIds.Any())
{
baseResult.RecordCount = manager.GrantRoles(systemCode, userId, grantRoleIds, permissionCode);
}
baseResult.Status = true;
baseResult.StatusMessage = "成功设置用户的角色范围权限域。";

}
catch (Exception ex)
{
baseResult.Status = false;
baseResult.StatusMessage = "设置用户的某个权限域的角色范围出现异常:" + ex.Message;
}

return Json(baseResult, JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 撤销用户的某个权限域的角色范围
/// </summary>
/// <param name="userId"></param>
/// <param name="roleIds"></param>
/// <param name="permissionId"></param>
/// <param name="systemCode"></param>
/// <returns></returns>
public ActionResult RevokeUserRoleScopes(string userId, string roleIds, string permissionId, string systemCode)//string permissionCode = "Resource.ManagePermission"
{
BaseResult baseResult = new BaseResult();
try
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string tableNameModule = systemCode + "Module";
BaseModuleManager moduleManager = new BaseModuleManager(tableNameModule);
BaseModuleEntity moduleEntity = moduleManager.GetObject(permissionId);
string permissionCode = moduleEntity.Code;

string[] revokeRoleIds = roleIds.Split(',');
string tableName = systemCode + "PermissionScope";

var manager = new BaseUserScopeManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
// 小心异常,检查一下参数的有效性
if (revokeRoleIds.Any())
{
baseResult.RecordCount = manager.RevokeRoles(systemCode, userId, revokeRoleIds, permissionCode);
}
baseResult.Status = true;
baseResult.StatusMessage = "成功撤销用户的角色范围权限域。";

}
catch (Exception ex)
{
baseResult.Status = false;
baseResult.StatusMessage = "撤销用户的某个权限域的角色范围出现异常:" + ex.Message;
}

return Json(baseResult, JsonRequestBehavior.AllowGet);
}

/// <summary>
/// 获取用户的某个权限域的组织范围
/// </summary>
/// <param name="userId">用户主键</param>
/// <param name="systemCode"></param>
/// <param name="permissionCode">权限编号</param>
/// <returns>主键数组</returns>
private string[] GetUserScopeOrganizeIds(string userId, string systemCode = null, string permissionCode = "Resource.ManagePermission")
{
string[] result = null;
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string tableName = systemCode + "PermissionScope";
var manager = new BaseUserScopeManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
result = manager.GetOrganizeIds(systemCode, userId, permissionCode);

return result;
}

/// <summary>
/// 获取用户的某个权限域的组织范围 用户组织机构数据权限
/// </summary>
/// <param name="userId"></param>
/// <param name="direction"></param>
/// <param name="systemCode"></param>
/// <param name="permissionCode"></param>
/// <param name="sort"></param>
/// <returns></returns>
public ActionResult GetUserScopeOrganizeList(string userId, Pager pager, string sort, string direction, string systemCode = null, string permissionCode = "Resource.ManagePermission")
{
string[] organizeIds = GetUserScopeOrganizeIds(userId, systemCode, permissionCode);
List<BaseOrganizeEntity> list = new List<BaseOrganizeEntity>();
int recordCount = 0;
if (organizeIds != null && organizeIds.Any())
{
string whereClause = " (" + BaseOrganizeEntity.TableName + "." + BaseOrganizeEntity.FieldId + " IN (" + BaseBusinessLogic.ObjectsToList(organizeIds, "'") + ")) ";
//list = new BaseOrganizeManager().GetList2<BaseOrganizeEntity>(whereClause);
string orderby = sort + " " + direction;
IDataReader dr = DbLogic.ExecuteReaderByPage(UserCenterDbHelper, out recordCount, BaseOrganizeEntity.TableName, "*", pager.pageNo, pager.pageSize, whereClause, null, orderby);
list = BaseEntity.GetList<BaseOrganizeEntity>(dr);
}

return JsonPager(pager, list, recordCount, sort, direction, BeginTime);
}

/// <summary>
/// 获取用户的某个权限域的用户范围 数据权限
/// </summary>
/// <param name="userId"></param>
/// <param name="direction"></param>
/// <param name="systemCode"></param>
/// <param name="permissionId"></param>
/// <param name="sort"></param>
/// <returns></returns>
public ActionResult GetUserScopeUserList(string userId, Pager pager, string sort, string direction, string permissionId, string systemCode = null)//string permissionCode = "Resource.ManagePermission"
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string tableName = systemCode + "PermissionScope";
var manager = new BaseUserScopeManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
string[] userIds = manager.GetUserIds(userId, permissionId);
List<BaseUserEntity> list = new List<BaseUserEntity>();
int recordCount = 0;
if (userIds != null && userIds.Any())
{
string whereClause = " (" + BaseUserEntity.TableName + "." + BaseUserEntity.FieldId + " IN (" + BaseBusinessLogic.ObjectsToList(userIds, "'") + ")) ";
//list = new BaseOrganizeManager().GetList2<BaseOrganizeEntity>(whereClause);
string orderby = sort + " " + direction;
IDataReader dr = DbLogic.ExecuteReaderByPage(UserCenterDbHelper, out recordCount, BaseUserEntity.TableName, "*", pager.pageNo, pager.pageSize, whereClause, null, orderby);
list = BaseEntity.GetList<BaseUserEntity>(dr);
}

return JsonPager(pager, list, recordCount, sort, direction, BeginTime);
}

/// <summary>
/// 获取用户的某个权限域的角色范围 数据权限
/// </summary>
/// <param name="userId"></param>
/// <param name="direction"></param>
/// <param name="systemCode"></param>
/// <param name="permissionId"></param>
/// <param name="sort"></param>
/// <returns></returns>
public ActionResult GetUserScopeRoleList(string userId, Pager pager, string sort, string direction, string permissionId, string systemCode = null)//string permissionCode = "Resource.ManagePermission"
{
if (string.IsNullOrWhiteSpace(systemCode))
{
systemCode = BaseSystemInfo.SystemCode;
}
string tableName = systemCode + "PermissionScope";
var manager = new BaseUserScopeManager(UserCenterDbHelper, OperateContext.Current.UserInfo, tableName);
string[] roleIds = manager.GetRoleIds(systemCode,userId, permissionId);
List<BaseRoleEntity> list = new List<BaseRoleEntity>();
int recordCount = 0;
string roleTable = systemCode + "Role";
if (roleIds != null && roleIds.Any())
{
string whereClause = " (" + roleTable + "." + BaseRoleEntity.FieldId + " IN (" + BaseBusinessLogic.ObjectsToList(roleIds, "'") + ")) ";
//list = new BaseOrganizeManager().GetList2<BaseOrganizeEntity>(whereClause);
string orderby = sort + " " + direction;
IDataReader dr = DbLogic.ExecuteReaderByPage(UserCenterDbHelper, out recordCount, roleTable, "*", pager.pageNo, pager.pageSize, whereClause, null, orderby);
list = BaseEntity.GetList<BaseRoleEntity>(dr);
}

return JsonPager(pager, list, recordCount, sort, direction, BeginTime);
}

}
}


前段最难设计的权限配置界面Views视图代码,如果没有很好的利用第三方前端控件,几乎是无法实现的。


@using DotNet.Model
@using DotNet.MVC.Infrastructure
@{
ViewBag.Title = "用户数据权限设置";
// 控制用户对那些数据有权限
Layout = "~/Views/QUILayout/MainContent.cshtml";
BaseUserEntity userEntity = ViewBag.userEntity;
BaseModuleEntity moduleEntity = ViewBag.moduleEntity;
var systemCode = ViewBag.systemCode;
}
@section Head
{
<!--数据表格start-->
<script src="@BusinessSystemInfo.QuiPath/libs/js/table/quiGrid.js" type="text/javascript"></script>
<!--数据表格end-->
<!--布局控件start-->
<script type="text/javascript" src="@BusinessSystemInfo.QuiPath/libs/js/nav/layout.js"></script>
<!--布局控件end-->
<!--基本选项卡start-->
<script type="text/javascript" src="@BusinessSystemInfo.QuiPath/libs/js/nav/basicTab.js"></script>
<!--基本选项卡end-->
<script type="text/javascript">
function initComplete() {
var layout = $("#layout1").layout({
leftWidth: 150, topHeight: 34, bottomHeight: 30, onEndResize: function () {
triggerCustomHeightSet();
}
});
layout.setRightCollapse(true);

// 数据权限范围选中事件
$("input:radio[name='dataScope']").change(function () {
var permissionOrganizeScope = $("input:radio[name='dataScope']:checked").val();
$.ajax({
type: 'POST',
url: "/UserPermissionScope/SetUserOrganizeScope",
data: {
"targetUserId": "@userEntity.Id",
"permissionOrganizeScope": permissionOrganizeScope,
"permissionCode": "@moduleEntity.Code",
"systemCode": "@systemCode"
},
dataType: 'json',
success: function (result) {
if (result.Status) {
top.Dialog.alert("设置成功!");
} else {
top.Dialog.alert(result.StatusMessage);
}
},
error: function (a) {
top.Dialog.alert("出错了!");
}
});
});


}
function customHeightSet(contentHeight) {
$(".layout_content").height(contentHeight - 94);
}
</script>
}

<div id="layout1">
<div position="top" id="topCon" style="">
<div class="box_tool_min padding_top0 padding_bottom6 padding_right5">
<div class="center">
<div class="left">
<div class="right">
<div class="padding_top3 padding_left10 padding_right10">
<div style="float: left">
设置用户【 @userEntity.RealName】在【@moduleEntity.FullName】上的数据权限
</div>
<div style="float: right">
@*<div style="float: left">
<a href="javascript:;" onclick="addUnit()"><span class="icon_add">区域权明细...</span></a>
</div>*@
<div style="float: right">
<a href="javascript:;" onclick="addUnit()"><span class="icon_add">添加...</span></a>
<a href="javascript:;" onclick="removeUnit()"><span class="icon_delete">移除</span></a>
<a href="javascript:;" onclick="top.Dialog.close();"><span class="icon_exit">关闭</span></a>
</div>

</div>
<div ></div>
</div>
</div>
</div>
</div>
<div ></div>
</div>
</div>
@*<div position="left" style="" paneltitle="数据权限范围">
<div class="layout_content">
<input type="radio" id="rdbAllData" name="dataScope" value="AllData" /><label for="rdbAllData" class="hand">所有数据</label><br />
<input type="radio" id="rdbProvince" name="dataScope" value="Province" /><label for="rdbProvince" class="hand">所在省</label><br />
<input type="radio" id="rdbCity" name="dataScope" value="City" /><label for="rdbCity" class="hand">所在市</label><br />
<input type="radio" id="rdbDistrict" name="dataScope" value="District" /><label for="rdbDistrict" class="hand">所在市</label><br />
<input type="radio" id="rdbStreet" name="dataScope" value="Street" /><label for="rdbStreet" class="hand">所在市</label><br />
<input type="radio" id="rdbUserCompany" name="dataScope" value="UserCompany" /><label for="rdbUserCompany" class="hand">所在市</label><br />
<input type="radio" id="rdbUserSubCompany" name="dataScope" value="UserSubCompany" /><label for="rdbUserSubCompany" class="hand">所在市</label><br />
<input type="radio" id="rdbUserDepartment" name="dataScope" value="UserDepartment" /><label for="rdbUserDepartment" class="hand">所在部门</label><br />
<input type="radio" id="rdbUserSubDepartment" name="dataScope" value="UserSubDepartment" /><label for="rdbUserSubDepartment" class="hand">所在子部门</label><br />
<input type="radio" id="rdbUserWorkgroup" name="dataScope" value="UserWorkgroup" /><label for="rdbUserWorkgroup" class="hand">所在工作组</label><br />
<input type="radio" id="rdbOnlyOwnData" name="dataScope" value="OnlyOwnData" /><label for="rdbOnlyOwnData" class="hand">仅本人</label><br />
<input type="radio" id="rdbByDetails" name="dataScope" value="ByDetails" /><label for="rdbByDetails" class="hand">按明细设置</label><br />
<input type="radio" id="rdbNotAllowed" name="dataScope" value="NotAllowed" /><label for="rdbNotAllowed" class="hand">无</label><br />
</div>
</div>*@
<div position="center" style="" id="centerCon">
<div class="basicTab" id="tabView" selectedidx="1">
<div name="区域" itemdisabled="false">
<div id="dataBasicByArea">
</div>
</div>
<div name="网点" itemdisabled="false">
<div id="dataBasicByOrganize">
</div>
</div>
<div name="用户" itemdisabled="false">
<div id="dataBasicByUser">
</div>
</div>
<div name="角色" itemdisabled="false">
<div id="dataBasicByRole">
</div>
</div>
</div>
</div>
<div position="bottom" id="bottomCon" style="">
</div>
</div>

@section Footer
{
<script type="text/javascript">
var userId = "@userEntity.Id";
var systemCode = "@ViewBag.SystemCode";
var permissionId = "@moduleEntity.Id";
var gridArea, gridOrganize, gridUser, gridRole;
var id = "#dataBasicByArea";
var currentTabId = 0;

// tab切换事件处理
function InitPage(iTab) {
if (iTab === 0) {
id = "#dataBasicByArea";
gridArea = $(id).quiGrid({
columns:
[
{
display: '编号',
name: 'Code',
align: 'center',
width: 100
},
{
display: '名称',
name: 'FullName',
align: 'center',
width: 100
},
{ display: '所属公司', name: 'ParentName', align: 'center', width: 100 },
{ display: '省份', name: 'Province', align: 'center', wdith: 120 },
{ display: '城市', name: 'City', align: 'center', wdith: 120 },
{ display: '区县', name: 'District', align: 'center', wdith: 120 }
],
url: '/Permission/GetUserScopeOrganizeList?systemCode=' + systemCode + "&userId=" + userId + "&permissionId=" + permissionId,
sortName: 'Id',
rownumbers: true,
checkbox: true,
height: '100%',
width: '100%',
pageSizeOptions: [30, 50, 100],
pageSize: 50,
showPageInfo: true,
onLoading: gridonLoading,
onLoaded: gridonLoaded,
onBeforeShowData: gridOnBeforeShowData,
// onSuccess: gridOnSuccess,
onError: gridOnError
});
} else if (iTab === 1) {
id = "#dataBasicByOrganize";
gridOrganize = $(id).quiGrid({
columns:
[
{ display: '编号', name: 'Code', align: 'center', width: 100 },
{ display: '名称', name: 'FullName', align: 'center', width: 100 },
{ display: '所属公司', name: 'ParentName', align: 'center', width: 100 },
{ display: '省份', name: 'Province', align: 'center', wdith: 120 },
{ display: '城市', name: 'City', align: 'center', wdith: 120 },
{ display: '区县', name: 'District', align: 'center', wdith: 120 }
],
url: '/Permission/GetUserScopeOrganizeList?systemCode=' + systemCode + "&userId=" + userId + "&permissionId=" + permissionId,
sortName: 'Id',
rownumbers: true,
checkbox: true,
height: '100%',
width: '100%',
pageSizeOptions: [30, 50, 100],
pageSize: 50,
showPageInfo: true,
onLoading: gridonLoading,
onLoaded: gridonLoaded,
onBeforeShowData: gridOnBeforeShowData,
// onSuccess: gridOnSuccess,
onError: gridOnError
});

} else if (iTab === 2) {
id = "#dataBasicByUser";
gridUser = $(id).quiGrid({
columns:
[
{
display: '编号',
name: 'Code',
align: 'center',
width: 100
},
{
display: '登录账号',
name: 'NickName',
align: 'center',
width: 100
},
{
display: '姓名',
name: 'RealName',
align: 'center',
width: 100
},
{
display: '公司',
name: 'CompanyName',
align: 'center',
width: 100
},
{
display: '部门',
name: 'DepartmentName',
align: 'center',
width: 100
}
],
url: '/Permission/GetUserScopeUserList?systemCode=' + systemCode + "&userId=" + userId + "&permissionId=" + permissionId,
sortName: 'Id',
//params: $("#queryForm").formToArray(),
rownumbers: true,
height: '100%',
width: '100%',
pageSizeOptions: [30, 50, 100],
pageSize: 50,
checkbox: true,
showPageInfo: true,
onLoading: gridonLoading,
onLoaded: gridonLoaded,
onBeforeShowData: gridOnBeforeShowData,
onSuccess: gridOnSuccess,
onError: gridOnError
});
} else if (iTab === 3) {
id = "#dataBasicByRole";
gridRole = $(id).quiGrid({
columns:
[
{
display: '编号',
name: 'Code',
align: 'center',
width: 100
},
{
display: '名称',
name: 'RealName',
align: 'center',
width: 100
},
{
display: '备注',
name: 'Description',
align: 'center',
width: 300
}
],
url: '/Permission/GetUserScopeRoleList?systemCode=' + systemCode + "&userId=" + userId + "&permissionId=" + permissionId,
sortName: 'Id',
rownumbers: true,
height: '100%',
width: '100%',
pageSizeOptions: [30, 50, 100],
pageSize: 50,
showPageInfo: true,
checkbox: true,
onLoading: gridonLoading,
onLoaded: gridonLoaded,
onBeforeShowData: gridOnBeforeShowData,
// onSuccess: gridOnSuccess,
onError: gridOnError
});
}
currentTabId = iTab;
objGrid = id;
}

function initComplete() {
// 绑定Tab点击事件
$("#tabView").bind("actived", function (e, i) {
if (i === 0) {
id = "#dataBasicByArea";
if (gridArea == null) {
InitPage(0);
}
gridArea.resetHeight();
} else if (i === 1) {
id = "#dataBasicByOrganize";
if (gridOrganize == null) {
InitPage(1);
}
gridOrganize.resetHeight();
} else if (i === 2) {
id = "#dataBasicByUser";
if (gridUser == null) {
InitPage(2);
}
gridUser.resetHeight();
} else if (i === 3) {
id = "#dataBasicByRole";
if (gridRole == null) {
InitPage(3);
}
gridRole.resetHeight();
}
currentTabId = i;
// 设置grid下方统计信息时使用
objGrid = id;
//$(id + " .l-bar-text:first").show();
//$(id).unmask();
//$("#queryForm").unmask();
});

InitPage(1);

}

// 添加
function addUnit() {
if (currentTabId === 0) {
top.Dialog.open({ URL: "/Area/ChooseArea?systemCode=" + systemCode + "&from=userpermissionscope", Title: "请选择", Width: 800, Height: 600 });
} else if (currentTabId === 1) {
top.Dialog.open({ URL: "/Organize/ChooseOrganize?systemCode=" + systemCode + "&from=userpermissionscope", Title: "请选择", Width: 800, Height: 600 });
} else if (currentTabId === 2) {
top.Dialog.open({ URL: "/User/ChooseUser?systemCode=" + systemCode + "&from=userpermissionscope", Title: "请选择", Width: 800, Height: 600 });
} else if (currentTabId === 3) {
top.Dialog.open({ URL: "/Role/ChooseRole?systemCode=" + systemCode + "&from=userpermissionscope", Title: "请选择", Width: 800, Height: 600 });
}
};

// 设置用户的某个权限域的组织范围 用户可以操作那些网点
function grantUserOrganizeScopes(ids) {
$("#container").mask("系统处理中...");
$.ajax({
type: 'POST',
url: '/Permission/GrantUserOrganizeScopes',
data: {
"userId": userId,
"organizeIds": ids,
"permissionId": permissionId,
"systemCode": systemCode
},
dataType: 'json',
success: function (result) {
if (result.Status) {
top.Dialog.alert("操作成功:" + result.StatusMessage, function () {
refreshGrid(currentTabId);
top.Dialog.close();
});
} else {
top.Dialog.alert("添加失败:" + result.StatusMessage);
}
$("#container").unmask();
},
error: function (a) {
top.Dialog.alert("访问服务器端出错!");
$("#container").unmask();
}
});
};

// 设置用户的某个权限域的用户范围
function grantUserUserScopes(ids) {
$("#container").mask("系统处理中...");
$.ajax({
type: 'POST',
url: '/Permission/GrantUserUserScopes',
data: {
"userId": userId,
"userIds": ids,
"permissionId": permissionId,
"systemCode": systemCode
},
dataType: 'json',
success: function (result) {
if (result.Status) {
top.Dialog.alert("操作成功:" + result.StatusMessage, function () {
refreshGrid(currentTabId);
top.Dialog.close();
});
} else {
top.Dialog.alert("添加失败:" + result.StatusMessage);
}
$("#container").unmask();
},
error: function (a) {
top.Dialog.alert("访问服务器端出错!");
$("#container").unmask();
}
});
};

// 设置用户的某个权限域的角色范围
function grantUserRoleScopes(ids) {
$("#container").mask("系统处理中...");
$.ajax({
type: 'POST',
url: '/Permission/GrantUserRoleScopes',
data: {
"userId": userId,
"roleIds": ids,
"permissionId": permissionId,
"systemCode": systemCode
},
dataType: 'json',
success: function(result) {
if (result.Status) {
top.Dialog.alert("操作成功:" + result.StatusMessage, function() {
refreshGrid(currentTabId);
top.Dialog.close();
});
} else {
top.Dialog.alert("添加失败:" + result.StatusMessage);
}
$("#container").unmask();
},
error: function(a) {
top.Dialog.alert("访问服务器端出错!");
$("#container").unmask();
}
});
};


// 移除
function removeUnit() {
if (currentTabId === 0) {
// revokeUserAreaScopes(gridUser);
} else if (currentTabId === 1) {
revokeUserOrganizeScopes(gridOrganize);
} else if (currentTabId === 2) {
revokeUserUserScopes(gridUser);
} else if (currentTabId === 3) {
revokeUserRoleScopes(gridRole);
}
};

// 移除用户某个权限于的组织机构范围权限
function revokeUserOrganizeScopes(grid) {
var rows = grid.getSelectedRows();
var rowsLength = rows.length;
if (rowsLength === 0) {
top.Dialog.alert("请选中一条记录。");
} else {
top.Dialog.confirm("确定要移除这些公司吗?", function () {
$("#container").mask("系统处理中...");
$.ajax({
type: 'POST',
url: '/Permission/RevokeUserOrganizeScopes',
data: {
"userId": userId,
"organizeIds": getSelectIds(grid),
"permissionId": permissionId,
"systemCode": systemCode
},
dataType: 'json',
success: function (result) {
if (result.Status) {
top.Dialog.alert("操作成功:" + result.StatusMessage, function () {
//top.document.getElementById("_DialogFrame_selectWin").contentWindow.refreshGrid(currentTabId);
});
} else {
top.Dialog.alert("操作失败:" + result.StatusMessage);
}
refreshGrid(currentTabId);
$("#container").unmask();
},
error: function (a) {
top.Dialog.alert("访问服务器端出错!");
$("#container").unmask();
}
});
});
}
};

// 移除用户某个权限于的用户范围权限
function revokeUserUserScopes(grid) {
var rows = grid.getSelectedRows();
var rowsLength = rows.length;
if (rowsLength === 0) {
top.Dialog.alert("请选中一条记录。");
} else {
top.Dialog.confirm("确定要移除这些用户吗?", function () {
$("#container").mask("系统处理中...");
$.ajax({
type: 'POST',
url: '/Permission/RevokeUserUserScopes',
data: {
"userId": userId,
"userIds": getSelectIds(grid),
"permissionId": permissionId,
"systemCode": systemCode
},
dataType: 'json',
success: function (result) {
if (result.Status) {
top.Dialog.alert("操作成功:" + result.StatusMessage, function () {
//top.document.getElementById("_DialogFrame_selectWin").contentWindow.refreshGrid(currentTabId);
});
} else {
top.Dialog.alert("操作失败:" + result.StatusMessage);
}
refreshGrid(currentTabId);
$("#container").unmask();
},
error: function (a) {
top.Dialog.alert("访问服务器端出错!");
$("#container").unmask();
}
});
});
}
};

// 移除用户某个权限于的角色范围权限
function revokeUserRoleScopes(grid) {
var rows = grid.getSelectedRows();
var rowsLength = rows.length;
if (rowsLength === 0) {
top.Dialog.alert("请选中一条记录。");
} else {
top.Dialog.confirm("确定要移除这些角色吗?", function() {
$("#container").mask("系统处理中...");
$.ajax({
type: 'POST',
url: '/Permission/RevokeUserRoleScopes',
data: {
"userId": userId,
"roleIds": getSelectIds(grid),
"permissionId": permissionId,
"systemCode": systemCode
},
dataType: 'json',
success: function(result) {
if (result.Status) {
top.Dialog.alert("操作成功:" + result.StatusMessage, function() {
//top.document.getElementById("_DialogFrame_selectWin").contentWindow.refreshGrid(1);
});
} else {
top.Dialog.alert("操作失败:" + result.StatusMessage);
}
refreshGrid(currentTabId);
$("#container").unmask();
},
error: function(a) {
top.Dialog.alert("访问服务器端出错!");
$("#container").unmask();
}
});
});
}
};


// 获取所有选中行获取选中行的id
function getSelectIds(objGrid) {
var selectedRows = objGrid.getSelectedRows();
var selectedRowsLength = selectedRows.length;
var ids = "";
for (var i = 0; i < selectedRowsLength; i++) {
if (selectedRows[i].Id == null) continue;
ids += selectedRows[i].Id + ",";
}
ids = ids.substring(0, ids.length - 1);
return ids;
};

// 刷新用户选择
function refreshGrid(iTab) {
InitPage(iTab);
}

//function customHeightSet(contentHeight) {
// $("#centerCon").height(contentHeight - 100);
//};
</script>
}


另外提供一个选择角色的界面

@using DotNet.Model
@using DotNet.MVC.Infrastructure
@{
ViewBag.Title = "为角色选择添加用户";
Layout = "~/Views/QUILayout/MainContent.cshtml";
BaseOrganizeEntity organizeEntity = ViewBag.OrganizeEntity;
if (organizeEntity == null)
{
organizeEntity = new BaseOrganizeEntity();
}
BaseRoleEntity roleEntity = ViewBag.RoleEntity;
}
@section head{
<!--数据表格start-->
<script src="@BusinessSystemInfo.QuiPath/libs/js/table/quiGrid.js" type="text/javascript"></script>
<!--数据表格end-->
<!-- 表单start -->
<script src="@BusinessSystemInfo.QuiPath/libs/js/form/form.js" type="text/javascript"></script>
<!-- 表单end -->
<!--自动提示框start-->
<script src='@BusinessSystemInfo.QuiPath/libs/js/form/suggestion.js' type='text/javascript'> </script>
<!--自动提示框end-->
}

<form action="" id="queryForm" method="post">
<input type="hidden" id="showEnableUse" name="showEnableUse" value="true" />
<table>
<tr>
<td>
公司:
</td>
<td>
@if (OperateContext.Current.UserInfo.IsAdministrator)
{
<div style="position: relative; width: 125px;">
<div id="companyId" name="entity.BaseUser.CompanyId" class="suggestion"
url="/Organize/GetOrganizesByCharKey?type=3"
minchars="2" delay="1000" reltext="@organizeEntity.FullName"
relvalue="@organizeEntity.Id"
suggestmode="remote"
style="float: left; width: 50px;">
</div>
</div>
}
else
{
@OperateContext.Current.UserInfo.CompanyName
<input type="hidden" name="entity.BaseUser.CompanyId" value="@OperateContext.Current.UserInfo.CompanyId" />
}
</td>
<td>
<select name="chooseType" selwidth="90" data='{"list":[{"value":"nickName","key":"登录账号"},{"value":"realName","key":"姓名"},{"value":"Id","key":"Id"},{"value":"simplePinYing","key":"简拼"},{"value":"fullPinYing","key":"全拼"}]}'></select>
</td>
<td>
<select name="chooseCompare" selwidth="80" data='{"list":[{"value":"equals","key":"相等于"},{"value":"like","key":"相似于"}]}'></select>
</td>
<td>
<input type="text" name="searchKey" />
</td>
<td>
<button type="button" id="souSuo" onclick="UserList.Query()">
<span class="icon_find">搜索</span>
</button>
</td>
<td>
<button type="button" onclick="resetSearch()">
<span class="icon_reload">重置</span>
</button>
</td>
</tr>
</table>
</form>
<div class="padding_right5">
<div id="dataBasic">
</div>
</div>
@section Footer
{
<script type="text/javascript">
var grid = null;
function initComplete() //初始化函数
{
//top.Dialog.close(); //当提交表单刷新本页面时关闭弹窗
//window.setTimeout(function () {
// initGrid(); //延迟初始化grid组件
//}, 100);
try {
UserList.InitGrid();
} catch (e) {
alert(e.message);
}
}

var systemCode = "@ViewBag.SystemCode";
var roleId = "@roleEntity.Id";
var UserList = {
InitGrid: function () {
grid = $("#dataBasic").quiGrid({
columns: [
{ display: '公司名称', name: 'CompanyName', width: 160 },
//{ display: '部门名称', name: 'DepartmentName', width: 160 },
{ display: '登录账号', name: 'NickName', width: 120 },
{ display: '真实姓名', name: 'RealName', width: 120 },
{ display: '编号', name: 'Code', width: 90 }
],
url: '/User/GetList',
params: $("#queryForm").formToArray(),
sortName: 'CompanyName',
rownumbers: true,
checkbox: true,
height: '100%',
width: '100%',
pageSizeOptions: [10, 15, 20, 30, 50],
pageSize: 15,
toolbar: { //工具栏配置
items: [
{ text: '批量添加', click: UserList.addUnit, iconClass: 'icon_add' }
]
},
onLoading: function () {
$("#dataBasic").mask("加载中...");
$(".l-bar-text:first").hide();
},
onLoaded: function () {
$("#dataBasic").unmask();
},
onError: gridOnError,
onSuccess: gridOnSuccess //加载完成之后,不管数据有没有正确加载
});
},
//获取所有选中行
GetSelectId: function (grid) {
var selectedRows = grid.getSelectedRows();
var selectedRowsLength = selectedRows.length;
var listId = "";
for (var i = 0; i < selectedRowsLength; i++) {
listId += selectedRows[i].Id + ",";
}
if (listId != "") {
listId = listId.substring(0, listId.length - 1);
}
return listId;
},
// 重置;
ResetPageHandler: function () {
var url = window.location.href;
Utilities.ResetHandler(url);
},
// 添加
addUnit: function () {
var rows = grid.getSelectedRows();
var rowsLength = rows.length;
if (rowsLength === 0) {
top.Dialog.alert("请选中要添加的用户。");
return;
}
top.Dialog.confirm("确定要添加选中的用户吗?", function () {
var userIds = UserList.GetSelectId(grid);
$("#dataBasic").mask("系统处理中...");
$.post("/Role/UpdateRoleUser", { "action": "addToRole", "roleIds": roleId, "userIds": userIds, "systemCode": systemCode },
function (result) {
if (result.Status) {
top.Dialog.alert("操作成功:" + result.StatusMessage, function () {
top.document.getElementById("_DialogFrame_selectWin").contentWindow.refresh();
});
} else {
top.Dialog.alert("添加失败:" + result.StatusMessage);
}
$("#dataBasic").unmask();
}, "json");
});
},
// 查询
Query: function () {
try {
var queryArray = $("#queryForm").formToArray();
grid.setOptions({ params: queryArray, url: '/User/GetList' });
grid.setNewPage(1);
//grid.loadData();
} catch (e) {
alert(e);
}
}
};
</script>

}