Web应用程序系统的多用户权限控制设计及实现-权限模块【6】
前五章均是从整体上讲述了Web应用程序的多用户权限控制实现流程,从本章起则重点讲述每一个模块的实现的流程。
首先讲解基本模块-用户组管理模块,涉及到的数据表为分组表。分组表定义了每一个用户属于的一种角色类型。本系统支持一个用户多种角色的情况,系统在登陆成功后,首先就加载用户的分组信息从而查看用户的可操作模块。
1.1分组域
为了更规范和方便后期系统的二次开发和维护,对应特定的业务模块采用Area(域)的方式开发,分组模块的开发域如下图所示:
由于在Areas下还建立了一个新的目录SystemManage,故需要改变原来的路由。分组模块的路由文件名称为SystemManageAreaRegistration。改变路由代码的文件名称为如下:
using System.Web.Mvc;
namespace CodeForMvcTest.Areas.SystemManage
{
public class SystemManageAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "SystemManage/OperatorGroup";
}
}
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"SystemManage_OperatorGroup",
"SystemManage/OperatorGroup/{controller}/{action}/{id}",
new { action = "index", id = UrlParameter.Optional }
);
}
}
}
1.2Model
分组模块的Model可参看第三章项目架构的系统共有类,对应model为OperatorGroup.cs。文件路径为Areas/SystemManage/Models。
1.3视图
分组模块的视图包含在分组域中,文件路径为Areas/SystemManage/OperatorGroup/Views/OperatorGroupManage,视图名称为OperatorGroupManage.cshtml。视图的完整代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 @{ 2 ViewBag.Title = "用户组管理"; 3 Layout = "~/Views/Shared/_BaseLayout.cshtml"; 4 } 5 6 7 <div class="easyui-layout" data-options="fit:true"> 8 9 <div data-options="region:'north',split:true" style="height: 50px;"> 10 @using (Ajax.BeginForm("OperatorGroupManage", "OperatorGroupManage", new AjaxOptions 11 { 12 HttpMethod = "POST", 13 OnSuccess = "loadDataGrid(data,'dataGrid','btn_submit','未找到匹配的用户组信息!')", 14 OnBegin = "startDatagridLoading('dataGrid','btn_submit')", 15 OnFailure = "commErrorHandle(data,true,'dataGrid','btn_submit','查找用户组信息出错!')" 16 })) 17 { 18 <!--属性组筛选栏--> 19 <table style="margin-left: 5px; margin-top: 5px;"> 20 <tr> 21 <td><span style="margin-left: 10px;">用户组名称:</span></td> 22 <td> 23 <input id="groupName" name="groupName" /> 24 </td> 25 <td><span style="margin-left: 10px;">状态:</span></td> 26 <td> 27 <select class="easyui-combobox" name="state" id="state" style="width: 150px;" 28 data-options="editable:false,required:true"> 29 <option value="-1">全部</option> 30 <option value="0">禁用</option> 31 <option value="1">启用</option> 32 </select> 33 </td> 34 <td> 35 <input type="submit" value="查找" id="btn_submit" style="margin-left: 10px; margin-right: 10px;" /> 36 </td> 37 </tr> 38 </table> 39 } 40 </div> 41 42 <div data-options="region:'center',split:true" style="padding-bottom: 10px;"> 43 <table class="easyui-datagrid" id="dataGrid" title="用户组列表" 44 data-options=" 45 rownumbers:false, 46 singleSelect:true, 47 autoRowHeight:false, 48 toolbar:'#group_tb', 49 loadMsg:'Loading... ...'"> 50 <thead> 51 <tr> 52 <th data-options="field:'GroupId',align:'left'">用户组ID</th> 53 <th data-options="field:'GroupName',align:'left'">用户组名称</th> 54 <th data-options="field:'OrderNum',align:'left'">排序值</th> 55 <th data-options="field:'State',align:'center',formatter:statusformater">状态</th> 56 <th data-options="field:'ParentId',align:'center',formatter:operateFormater">操作</th> 57 </tr> 58 </thead> 59 <tbody> 60 @Html.Raw(ViewBag.GroupList) 61 </tbody> 62 </table> 63 <br /> 64 </div> 65 66 </div> 67 68 <!--用户组工具栏--> 69 <div id="group_tb" style="height: auto"> 70 <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="openAddWindow();">添加用户组</a> 71 </div> 72 73 74 <!--用户组信息编辑窗体--> 75 <div id="groupEditWin" title="修改用户组信息" style="width: 450px; height: 250px; padding: 20px; text-align: center;"> 76 <form id="groupEditForm" method="POST" action="@Url.Action("UpdateGroup", "OperatorGroupManage")"> 77 <table style="margin: auto;"> 78 <tr> 79 <td style="text-align: right;"><span>名称:</span></td> 80 <td> 81 <input class="easyui-validatebox" id="e_groupName" name="groupName" data-options="required:true" /> 82 </td> 83 </tr> 84 <tr style="height: 40px;"> 85 <td style="text-align: right;"><span>排序值:</span></td> 86 <td> 87 <input class="easyui-numberbox" data-options="required:true" id="e_orderNum" name="orderNum" /> 88 </td> 89 </tr> 90 <tr style="height: 40px;"> 91 <td style="text-align: right;"><span>状态:</span></td> 92 <td> 93 <select class="easyui-combobox" name="state" id="e_state" style="width: 150px;" 94 data-options="editable:false,required:true"> 95 <option value="0">禁用</option> 96 <option value="1">启用</option> 97 </select> 98 </td> 99 </tr> 100 <tr style="height: 50px;"> 101 <td colspan="2" style="text-align: right;"> 102 <input type="hidden" id="e_groupId" name="groupId" /> 103 <input type="submit" value="提交" id="btn_editsubmit" style="margin-left: 10px; margin-right: 10px;" /> 104 <input type="button" value="取消" id="btn_editCancel" onclick="javascript: return $('#groupEditWin').window('close');" 105 style="margin-left: 10px; margin-right: 10px;" /> 106 </td> 107 </tr> 108 </table> 109 </form> 110 </div> 111 112 113 114 <!--添加用户组窗体--> 115 <div id="groupAddWin" title="添加用户组" style="width: 450px; height: 250px; padding: 20px; text-align: center;"> 116 <form id="groupAddForm" method="POST" action="@Url.Action("AddGroup", "OperatorGroupManage")"> 117 <table style="margin: auto;"> 118 <tr> 119 <td style="text-align: right;"><span>名称:</span></td> 120 <td> 121 <input class="easyui-validatebox" id="a_groupName" name="groupName" data-options="required:true" /> 122 </td> 123 </tr> 124 <tr style="height: 40px;"> 125 <td style="text-align: right;"><span>排序值:</span></td> 126 <td> 127 <input class="easyui-numberbox" data-options="required:true" id="a_orderNum" name="orderNum" /> 128 </td> 129 </tr> 130 <tr style="height: 40px;"> 131 <td style="text-align: right;"><span>状态:</span></td> 132 <td> 133 <select class="easyui-combobox" name="state" id="a_state" style="width: 150px;" 134 data-options="editable:false,required:true"> 135 <option value="0">禁用</option> 136 <option value="1">启用</option> 137 </select> 138 </td> 139 </tr> 140 <tr style="height: 50px;"> 141 <td colspan="2" style="text-align: right;"> 142 <input type="submit" value="提交" id="btn_addsubmit" style="margin-left: 10px; margin-right: 10px;" /> 143 <input type="button" value="取消" id="btn_addCancel" onclick="javascript: return $('#groupAddWin').window('close');" 144 style="margin-left: 10px; margin-right: 10px;" /> 145 </td> 146 </tr> 147 </table> 148 </form> 149 </div> 150 151 152 153 @section scripts 154 { 155 <script type="text/javascript" src="/Areas/SystemManage/SystemJS/operatorGroupManage.js"></script> 156 }
1.4JS文件
分组模块相关的JS文件路径为Areas/SystemManage/SystemJS,JS文件名称为operatorGroupManage.js。JS的完整代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //状态显示格式化 2 function statusformater(value, row, index) { 3 if (row.State == "0") { 4 return "禁用"; 5 } else { 6 return "启用"; 7 } 8 }; 9 10 //属性组操作显示格式化 11 function operateFormater(value, row, index) { 12 var operationStr = "<a href='#' onclick=\"openEditWindow(" + row.GroupId + ",'" 13 + row.GroupName + "'," + row.OrderNum + "," + row.State + ");\" style='margin-right:10px;'>编辑</a>"; 14 return operationStr; 15 }; 16 17 18 //打开用户组编辑窗体 19 function openEditWindow(groupId, groupName, orderNum, state) { 20 $("#e_groupName").val(groupName); 21 $("#e_orderNum").numberbox('setValue', orderNum); 22 $("#e_state").combobox("select", state); 23 $("#e_groupId").val(groupId); 24 $('#groupEditWin').window('open'); 25 }; 26 27 28 //打开添加用户组窗体 29 function openAddWindow() { 30 $("#a_groupName").val(""); 31 $("#a_orderNum").numberbox('setValue', ""); 32 $("#a_state").combobox("select", "0"); 33 $('#groupAddWin').window('open'); 34 }; 35 36 37 $(function () { 38 $('#groupEditWin').window({ 39 modal: true, 40 collapsible: false, 41 minimizable: false, 42 maximizable: false, 43 draggable: true, 44 resizable: false, 45 closable: false, 46 closed: true 47 }); 48 49 50 $('#groupAddWin').window({ 51 modal: true, 52 collapsible: false, 53 minimizable: false, 54 maximizable: false, 55 draggable: true, 56 resizable: false, 57 closable: false, 58 closed: true 59 }); 60 61 62 //修改用户信息 63 $("#groupEditForm").submit(function (event) { 64 //中断当前的提交事件 65 event.preventDefault(); 66 //提交的URL,默认为属性组修改或添加路径 67 var url = "/SystemManage/OperatorGroup/OperatorGroupManage/UpdateGroup"; 68 //名称 69 var groupName = $("#e_groupName").val(); 70 if (groupName == "") { 71 alert("用户组名称不能为空!"); 72 return false; 73 } 74 //排序值 75 var orderNum = $("#e_orderNum").numberbox('getValue'); 76 if (orderNum == "") { 77 alert("请输入排序值!"); 78 return false; 79 } 80 81 $('#groupEditWin').window('close'); 82 startDatagridLoading('dataGrid', 'btn_submit'); 83 84 //表单序列化 85 var formData = $("#groupEditForm").serialize(); 86 //完整路径 87 url = url + "?" + formData; 88 89 $.ajax({ 90 url: url, 91 type: 'POST', 92 dataType: 'text', 93 success: function (data) { 94 if (data.indexOf("/Login/Login") >= 0) { 95 window.parent.location.href = '/Login/Login'; 96 } 97 groupName = $("#groupName").val(); 98 var state = $("#state").combobox("getValue"); 99 url = "/SystemManage/OperatorGroup/OperatorGroupManage/OperatorGroupManage?groupName=" + groupName 100 + "&state=" + state; 101 102 alert(data); 103 104 reloadDatagrid(url, "dataGrid", "btn_submit", "无用户组数据返回!", "查询用户组数据出错!"); 105 }, 106 error: function (data) { 107 commErrorHandle(data, true, 'dataGrid', 'btn_submit', "操作出错!"); 108 } 109 }); 110 }); 111 112 113 //添加用户信息 114 $("#groupAddForm").submit(function (event) { 115 //中断当前的提交事件 116 event.preventDefault(); 117 //提交的URL,默认为属性组修改或添加路径 118 var url = "/SystemManage/OperatorGroup/OperatorGroupManage/AddGroup"; 119 //账号 120 var groupName = $("#a_groupName").val(); 121 if (groupName == "") { 122 alert("用户组名不能为空!"); 123 return false; 124 } 125 //排序值 126 var orderNum = $("#a_orderNum").numberbox('getValue'); 127 if (orderNum == "") { 128 alert("请输入排序值!"); 129 return false; 130 } 131 132 $('#groupAddWin').window('close'); 133 startDatagridLoading('dataGrid', 'btn_submit'); 134 135 var groupId; 136 //表单序列化 137 var formData = $("#groupAddForm").serialize(); 138 //完整路径 139 url = url + "?" + formData; 140 141 $.ajax({ 142 url: url, 143 type: 'POST', 144 dataType: 'text', 145 success: function (data) { 146 if (data.indexOf("/Login/Login") >= 0) { 147 window.parent.location.href = '/Login/Login'; 148 } 149 groupName = $("#groupName").val(); 150 var state = $("#state").combobox("getValue"); 151 url = "/SystemManage/OperatorGroup/OperatorGroupManage/OperatorGroupManage?groupName=" + groupName 152 + "&state=" + state; 153 154 alert(data); 155 156 reloadDatagrid(url, "dataGrid", "btn_submit", "无用户组数据返回!", "查询用户组数据出错!"); 157 }, 158 error: function (data) { 159 commErrorHandle(data, true, 'dataGrid', 'btn_submit', "操作出错!"); 160 } 161 }); 162 }); 163 164 });
1.5控制器
分组模块的控制器包含在分组域中,文件路径为Areas/SystemManage/OperatorGroup/Controller,控制器名称为OperatorGroupManageController.cs。控制器的完整代码如下:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Web; 5 using System.Web.Mvc; 6 using Session; 7 using OdbcDbAccess; 8 using System.Data; 9 using Models; 10 using Controllers; 11 using LogInfo; 12 13 namespace CodeForMvcTest.Areas.SystemManage.Controllers 14 { 15 public class OperatorGroupManageController : BaseController 16 { 17 /// <summary> 18 /// **************************** 19 /// 功能:分组处理类 20 /// 作者:王令 21 /// 时间:2015-7-12 22 /// 邮箱:1129137758@qq.com 23 /// **************************** 24 25 public ActionResult OperatorGroupManage() 26 { 27 IList<OperatorGroup> groupList = GetOperatorGroup("", -1); 28 string[] propertyArray = { "GroupId", "GroupName", "OrderNum", "State" }; 29 30 ViewBag.GroupList = DataTransfor.ListToTableHtml(groupList, propertyArray); 31 32 return View(); 33 } 34 35 36 /// <summary> 37 /// 返回用户组查询结果列表 38 /// </summary> 39 /// <param name="groupName">用户组名称</param> 40 /// <param name="state">状态</param> 41 /// <returns></returns> 42 [HttpPost] 43 public ActionResult OperatorGroupManage(string groupName, int state) 44 { 45 IList<OperatorGroup> groupList = GetOperatorGroup(groupName, state); 46 return Json(groupList); 47 } 48 49 /// <summary> 50 /// 修改用户组信息 51 /// </summary> 52 /// <param name="groupId">用户组ID</param> 53 /// <param name="groupName">用户组名称</param> 54 /// <param name="orderNum">排序值</param> 55 /// <param name="state">状态</param> 56 /// <returns></returns> 57 [HttpPost] 58 public ActionResult UpdateGroup(int groupId, string groupName, int orderNum, int state) 59 { 60 string sql = "update operatorgroup set groupname='" + groupName + "',ordernumber='" + orderNum + "',state='" + state + "' where groupid=" + groupId + ""; 61 try 62 { 63 SqlHelper.ExecuteNonQuery(SqlSeverConnectionName, sql); 64 Log.SaveOperatorLog(sql, 1, "成功修改用户组信息"); 65 66 return Content("成功修改用户组信息!"); 67 } 68 catch (Exception ex) 69 { 70 Log.SaveErrorLog(ex.ToString(), "修改用户组信息出错"); 71 return Content("修改用户组信息出错!"); 72 } 73 } 74 75 76 /// <summary> 77 /// 添加用户组 78 /// </summary> 79 /// <param name="groupName">用户组名称</param> 80 /// <param name="orderNum">排序值</param> 81 /// <param name="state">状态</param> 82 /// <returns></returns> 83 [HttpPost] 84 public ActionResult AddGroup(string groupName, int orderNum, int state) 85 { 86 string sql = "select count(*) as data from operatorgroup where groupname='" + groupName + "'"; 87 try 88 { 89 int count = SqlHelper.ExecuteScalar<int>(SqlSeverConnectionName, sql); 90 if (count > 0) 91 { 92 Log.SaveOperatorLog(sql, 0, "因用户组名称已经存在,未能成功添加用户组"); 93 return Content("已经存在相同名称的用户组!"); 94 } 95 sql = "insert into operatorgroup (groupname,ordernumber,state,updatetime,createtime) " 96 + "values ('" + groupName + "','" + orderNum + "','" + state + "','" 97 + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "','" + 98 DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')"; 99 100 SqlHelper.ExecuteNonQuery(SqlSeverConnectionName, sql); 101 102 Log.SaveOperatorLog(sql, 1, "成功添加用户组"); 103 return Content("添加成功!"); 104 } 105 catch (Exception ex) 106 { 107 Log.SaveErrorLog(ex.ToString(), "添加用户组出错"); 108 return Content("添加用户组出错!"); 109 } 110 } 111 /// <summary> 112 /// 查询分组 113 /// </summary> 114 /// <param name="groupName">分组名称</param> 115 /// <param name="state">状态</param> 116 /// <returns></returns> 117 private IList<OperatorGroup> GetOperatorGroup(string groupName, int state) 118 { 119 IList<OperatorGroup> groupList = new List<OperatorGroup>(); 120 try 121 { 122 string sql = "select * from operatorgroup where 1=1"; 123 if (!string.IsNullOrEmpty(groupName)) 124 { 125 sql += " and groupname='" + groupName + "'"; 126 } 127 if (state != -1) 128 { 129 sql += " and state=" + state; 130 } 131 sql += " order by ordernumber"; 132 DataSet dataSet = SqlHelper.ExecuteQuery(SqlSeverConnectionName, sql); 133 int[] columnArray = { 0, 1, 3, 4 }; 134 string[] propertyArray = { "GroupId", "GroupName", "OrderNum", "State" }; 135 groupList = DataTransfor.DataSetTransfor<OperatorGroup>(dataSet, columnArray, 136 propertyArray); 137 } 138 catch (Exception ex) 139 { 140 Log.SaveErrorLog(ex.ToString(), "查询用户组出错"); 141 } 142 return groupList; 143 } 144 145 146 } 147 }
1.6界面运行截图
用户组管理主界面如下图所示:
添加用户组界面如下图所示:
编辑用户组界面如下图所示:
喜欢请赞赏一下啦^_^
微信赞赏
支付宝赞赏