在權限管理系統中,數據權限是比較難的,在我們通用權限系統中,數據權限指的是用戶基於某個權限域對某些基礎數據的操作權限,如上圖,公司管理這個菜單被定義是數據權限,表示某些人在公司管理是可指定訪問哪些基礎數據,這個要與應用結合。如下圖,可以限制該9999xudeng003用戶在公司管理頁面只能管理其中的幾個公司。
目前基於用戶的數據權限BS的管理功能已完成,這個是權限系統最難開發的一部分。
下面是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 class="clear"></div> </div> </div> </div> </div> <div class="clear"></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> }