Web应用程序系统的多用户权限控制设计及实现-用户模块【7】

前五章均是从整体上讲述了Web应用程序的多用户权限控制实现流程,本章讲述Web权限管理系统的基本模块-用户模块。用户模块涉及到的数据表为用户表。

1.1用户域

为了更规范和方便后期系统的二次开发和维护,对应特定的业务模块采用Area(域)的方式开发,用户模块的开发域如下图所示:

由于在Areas下还建立了一个新的目录SystemManage,故需要改变原来的路由。用户模块的路由文件名称为OperatorManageAreaRegistration。改变路由代码的文件名称为如下:

using System.Web.Mvc;
namespace CodeForMvcTest.Areas.OperatorManage
{
    public class OperatorManageAreaRegistration : AreaRegistration
    {
        public override string AreaName
        {
            get
            {
                return "SystemManage/OperatorManage";
            }
        }
        public override void RegisterArea(AreaRegistrationContext context)
        {
            context.MapRoute(
                "SystemManage_OperatorManage_default",
                "SystemManage/OperatorManage/{controller}/{action}/{id}",
                new { action = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

 1.2Model

分组模块的Model可参看第三章项目架构的系统共有类,对应model为AccountInfo.cs。文件路径为Areas/SystemManage/Models。

 1.3视图

用户模块的视图包含在用户域中,文件路径为Areas/SystemManage/OperatorManage/Views/OperatorManage,视图名称为OperatorManage.cshtml。视图的完整代码如下:

  1 @{
  2     ViewBag.Title = "用户管理";
  3     Layout = "~/Views/Shared/_BaseLayout.cshtml";
  4 }
  5 
  6 <div class="easyui-layout" data-options="fit:true">
  7 
  8     <div data-options="region:'north',split:true" style="height: 50px;">
  9         <form id="searchForm" method="POST" action="@Url.Action("OperatorManage", "OperatorManage")">
 10             <!--筛选栏-->
 11             <table style="margin-left: 5px; margin-top: 5px;">
 12                 <tr>
 13                     <td><span>用户组:</span></td>
 14                     <td>
 15                         <select class="easyui-combobox" name="groupId" id="groupId" style="width: 150px;"
 16                             data-options="editable:false,valueField:'GroupId',textField:'GroupName'">
 17                             @Html.Raw(ViewBag.GroupListWithAll)
 18                         </select>
 19                     </td>
 20                     <td><span style="margin-left: 10px;">用户账号:</span></td>
 21                     <td>
 22                         <input class="easyui-textbox" id="operatorId" name="operatorId" />
 23                     </td>
 24                     <td><span style="margin-left: 10px;">名称:</span></td>
 25                     <td>
 26                         <input class="easyui-textbox" id="operatorName" name="operatorName" />
 27                     </td>
 28                     <td>
 29                         <input type="submit" value="查找" id="btn_submit" style="margin-left: 10px; margin-right: 10px;" />
 30                     </td>
 31                 </tr>
 32             </table>
 33         </form>
 34     </div>
 35 
 36     <div data-options="region:'center',split:true" style="padding-bottom: 10px;" id="centerDiv">
 37         <table id="dataGrid">
 38             <thead>
 39                 <tr>
 40                     <th data-options="field:'OperatorId',align:'left'">用户账号</th>
 41                     <th data-options="field:'OperatorName',align:'left'">名称</th>
 42                     <th data-options="field:'OperatorGroupName',align:'left'">所属用户组</th>
 43                     <th data-options="field:'Sex',align:'center'">性别</th>
 44                     <th data-options="field:'IsOnStaff',align:'center',formatter:statusformater">状态</th>
 45                     <th data-options="field:'AliasName',align:'center',formatter:operateFormater">操作</th>
 46                 </tr>
 47             </thead>
 48             <tbody>
 49                
 50             </tbody>
 51         </table>
 52         <br />
 53     </div>
 54 
 55 </div>
 56 
 57 <!--属性组工具栏-->
 58 <div id="operator_tb" style="height: auto">
 59     <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="openAddWindow();">添加用户</a>
 60 </div>
 61 
 62 
 63 <!--用户信息编辑窗体-->
 64 <div id="operatorEditWin" title="修改用户信息" style="width: 450px; height: 500px; padding: 20px; text-align: center;">
 65     <form id="operatorEditForm" method="POST" action="@Url.Action("UpdateOperator", "OperatorManage")">
 66         <table style="margin: auto;">
 67             <tr>
 68                 <td style="text-align: right;"><span>用户账号:</span></td>
 69                 <td>
 70                     <input class="easyui-validatebox" data-options="required:true" id="e_operatorId" name="operatorId" />
 71                 </td>
 72             </tr>
 73             <tr style="height: 40px;">
 74                 <td style="text-align: right;"><span>名称:</span></td>
 75                 <td>
 76                     <input class="easyui-validatebox" id="e_operatorName" name="operatorName" data-options="required:true" />
 77                 </td>
 78             </tr>
 79             <tr style="height: 30px;">
 80                 <td style="text-align: right;"><span>用户组:</span></td>
 81                 <td>
 82                     <select class="easyui-combobox" name="groupId" id="e_groupId" style="width: 150px;"
 83                         data-options="editable:false,required:true,multiple:true">
 84                         @Html.Raw(ViewBag.GroupList)
 85                     </select>
 86                 </td>
 87             </tr>
 88          
 89             <tr style="height: 40px;">
 90                 <td style="text-align: right;"><span>性别:</span></td>
 91                 <td>
 92                     <select class="easyui-combobox" name="sex" id="e_sex" style="width: 150px;"
 93                         data-options="editable:false,required:true">
 94                         <option value="0"></option>
 95                         <option value="1"></option>
 96                     </select>
 97                 </td>
 98             </tr>
 99             <tr style="height: 40px;">
100                 <td style="text-align: right;"><span>状态:</span></td>
101                 <td>
102                     <select class="easyui-combobox" name="state" id="e_state" style="width: 150px;"
103                         data-options="editable:false,required:true">
104                         <option value="0">禁用</option>
105                         <option value="1">启用</option>
106                     </select>
107                 </td>
108             </tr>
109             <tr style="height: 50px;">
110                 <td colspan="2" style="text-align: right;">
111                     <input type="hidden" id="oldId" name="oldId" />
112                     <input type="submit" value="提交" id="btn_editsubmit" style="margin-left: 10px; margin-right: 10px;" />
113                     <input type="button" value="取消" id="btn_editCancel" onclick="javascript: return $('#operatorEditWin').window('close');"
114                         style="margin-left: 10px; margin-right: 10px;" />
115                 </td>
116             </tr>
117         </table>
118     </form>
119 </div>
120 
121 
122 
123 <!--用户添加窗体-->
124 <div id="operatorAddWin" title="添加用户" style="width: 450px; height: 500px; padding: 20px; text-align: center;">
125     <form id="operatorAddForm" method="POST" action="@Url.Action("AddOperator", "OperatorManage")">
126         <table style="margin: auto;">
127             <tr>
128                 <td style="text-align: right;"><span>用户账号:</span></td>
129                 <td>
130                     <input class="easyui-validatebox" data-options="required:true" id="a_operatorId" name="operatorId" />
131                 </td>
132             </tr>
133             <tr style="height: 30px;">
134                 <td style="text-align: right;"><span>名称:</span></td>
135                 <td>
136                     <input class="easyui-validatebox" id="a_operatorName" name="operatorName" data-options="required:true" />
137                 </td>
138             </tr>
139             <tr style="height: 30px;">
140                 <td style="text-align: right;"><span>密码:</span></td>
141                 <td>
142                     <input type="password" id="a_password" name="password" data-options="required:true" />
143                 </td>
144             </tr>
145             <tr style="height: 30px;">
146                 <td style="text-align: right;"><span>确认密码:</span></td>
147                 <td>
148                     <input type="password" id="a_passwordconfirm" name="passwordconfirm" data-options="required:true" />
149                 </td>
150             </tr>
151             <tr style="height: 30px;">
152                 <td style="text-align: right;"><span>用户组:</span></td>
153                 <td>
154                     <select class="easyui-combobox" name="groupId" id="a_groupId" style="width: 150px;"
155                         data-options="editable:false,required:true,multiple:true">
156                         @Html.Raw(ViewBag.GroupList)
157                     </select>
158                 </td>
159             </tr>
160          
161             
162             <tr style="height: 30px;">
163                 <td style="text-align: right;"><span>性别:</span></td>
164                 <td>
165                     <select class="easyui-combobox" name="sex" id="a_sex" style="width: 150px;"
166                         data-options="editable:false,required:true">
167                         <option value="0"></option>
168                         <option value="1"></option>
169                     </select>
170                 </td>
171             </tr>
172             <tr style="height: 30px;">
173                 <td style="text-align: right;"><span>状态:</span></td>
174                 <td>
175                     <select class="easyui-combobox" name="state" id="a_state" style="width: 150px;"
176                         data-options="editable:false,required:true">
177                         <option value="0">禁用</option>
178                         <option value="1">启用</option>
179                     </select>
180                 </td>
181             </tr>
182             <tr style="height: 50px;">
183                 <td colspan="2" style="text-align: right;">
184                     <input type="submit" value="提交" id="btn_addsubmit" style="margin-left: 10px; margin-right: 10px;" />
185                     <input type="button" value="取消" id="btn_addCancel" onclick="javascript: return $('#operatorAddWin').window('close');"
186                         style="margin-left: 10px; margin-right: 10px;" />
187                 </td>
188             </tr>
189         </table>
190     </form>
191 </div>
192 
193 
194 
195 @section scripts
196 {
197     <script type="text/javascript" src="/Areas/SystemManage/SystemJS/operatorManage.js"></script>
198 }
OperatorMange.cshtml

 1.4JS文件

用户模块相关的JS文件路径为Areas/SystemManage/SystemJS,JS文件名称为operatorManage.js。JS的完整代码如下:

  1 //状态显示格式化
  2 function statusformater(value, row, index) {
  3     if (row.IsOnStaff == "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.OperatorId + "','"
 13         + row.OperatorName + "','" + row.OperatorGroupId + "'," + row.SplitOwnerId + ","
 14         + row.SplitRoadId + ",'" + row.Sex + "'," + row.IsOnStaff + ");\" style='margin-right:10px;'>编辑</a>";
 15     operationStr += "<a  href='#' onclick=\"deleteOperator('" + row.OperatorId + "');\">删除</a>";
 16     return operationStr;
 17 };
 18 
 19 
 20 //开启编辑窗体
 21 function openEditWindow(operatorId, operatorName, groupId, splitownerId,
 22     splitroadId, sex, isOnStaff) {
 23     $("#e_operatorId").val(operatorId);  
 24     $("#e_operatorName").val(operatorName);
 25     $("#e_groupId").combobox('setValues', groupId);
 26     if (sex == "男") {
 27         $("#e_sex").combobox("select", 0);
 28     } else {
 29         $("#e_sex").combobox("select", 1);
 30     }
 31     $("#e_state").combobox("setValue", isOnStaff);
 32     $("#oldId").val(operatorId);
 33     $('#operatorEditWin').window('open');
 34 };
 35 
 36 //点击添加按钮时,打开添加用户窗体”
 37 function openAddWindow() {
 38     $("#a_operatorId").val('');
 39     $("#a_operatorName").val("");
 40     $("#a_password").val("");
 41     $("#a_passwordconfirm").val("");
 42     $("#a_sex").combobox("select", 0);
 43     $("#a_state").combobox("select", 0);
 44     $('#operatorAddWin').window('open');
 45 };
 46 
 47 
 48 //删除用户
 49 function deleteOperator(operatorId) {
 50     startDatagridLoading("dataGrid", "btn_submit");
 51     $.ajax({
 52         url: '/SystemManage/OperatorManage/OperatorManage/DeleteOperator',
 53         type: 'POST',
 54         dataType: 'text',
 55         data: { operatorId: operatorId },
 56         success: function (data) {
 57             if (data.indexOf("/Login/Login") >= 0) {
 58                 window.parent.location.href = '/Login/Login';
 59             }
 60             var groupId = $("#groupId").combobox('getValue');
 61             operatorId = $("#operatorId").val();
 62             var operatorName = $("#operatorName").val();
 63             var url = "/SystemManage/OperatorManage/OperatorManage/OperatorManage?groupId=" + groupId
 64                 + "&operatorId=" + operatorId + "&operatorName=" + operatorName;
 65             alert(data);
 66             reloadDatagrid(url, "dataGrid", "btn_submit", "无用户数据返回!", "查询用户数据出错!");
 67         },
 68         error: function (data) {
 69             commErrorHandle(data, true, 'dataGrid', 'btn_submit', "操作出错!");
 70         }
 71     });
 72 };
 73 
 74 
 75 $(function () {
 76     //设置DataGrid基本属性
 77     var centerHeight = $("#centerDiv").height();
 78     $("#dataGrid").datagrid({
 79         height: centerHeight - 15,
 80         rownumbers: false,
 81         singleSelect: true,
 82         autoRowHeight: false,
 83         fitColumns: true,
 84         pagination: true,
 85         pageSize: 20,
 86         url: '/SystemManage/OperatorManage/OperatorManage/OperatorManage?groupId=-1&operatorId=&operatorName=',
 87         loadMsg: 'Loading... ...',
 88         toolbar: '#operator_tb',
 89         remoteSort: false,
 90         onLoadSuccess: function (data) {
 91             //设置查询按钮可用
 92             $("#btn_submit").attr("disabled", false);
 93             if (data.total <= 0)
 94                 alert("未找到匹配的用户信息!");
 95             $(".datagrid-wrap.panel-body").css("width", '100%');
 96             $(".datagrid-wrap.panel-body .datagrid-view").css("width", '100%');
 97         },
 98         onLoadError: function (data) {
 99             //判断是否是首次加载,不是首次加载再判断错误原因
100             var isFirstLoad = $("#firstLoadFlag").val();
101             if (isFirstLoad == "false") {
102                 //设置查询按钮可用
103                 $("#btn_submit").attr("disabled", false);
104                 if (data.responseText.indexOf("/Login/Login") >= 0) {
105                     window.parent.location.href = '/Login/Login';
106                 } else {
107                     $("#btn_submit").attr("disabled", false);
108                     //隐藏加载状态
109                     $('#dataGrid').datagrid('loaded');
110                     alert("对不起,查询失败!");
111                 }
112             }
113         }
114     });
115 
116     $('#operatorEditWin').window({
117         modal: true,
118         collapsible: false,
119         minimizable: false,
120         maximizable: false,
121         draggable: true,
122         resizable: false,
123         closable: false,
124         closed: true
125     });
126 
127 
128     $('#operatorAddWin').window({
129         modal: true,
130         collapsible: false,
131         minimizable: false,
132         maximizable: false,
133         draggable: true,
134         resizable: false,
135         closable: false,
136         closed: true
137     });
138 
139     //用户信息查询
140     $("#searchForm").submit(function (event) {
141         //中断当前的提交事件
142         event.preventDefault();
143         //将首次加载标志设置为false
144         $("#firstLoadFlag").val("false");
145         //清空数据
146         $("#dataGrid").datagrid('loadData', { total: 1, rows: [] });
147         var groupId = $("#groupId").combobox('getValue');
148         var operatorId = $("#operatorId").val();
149         var operatorName = $("#operatorName").val();
150         $("#btn_submit").attr("disabled", true);
151         $("#dataGrid").datagrid('load', {
152             groupId: groupId,
153             operatorId: operatorId,
154             operatorName: operatorName
155         });
156 
157     });
158 
159     //修改用户信息
160     $("#operatorEditForm").submit(function (event) {
161         //中断当前的提交事件
162         event.preventDefault();
163         //提交的URL,默认为属性组修改或添加路径
164         var url = "/SystemManage/OperatorManage/OperatorManage/UpdateOperator";
165         //账号
166         var operatorId = $("#e_operatorId").val();
167         if (operatorId == "") {
168             alert("账号不能为空!");
169             return false;
170         }
171         //名称
172         var operatorName = $("#e_operatorName").val();
173         if (operatorName == "") {
174             alert("名称不能为空!");
175             return false;
176         }
177 
178         $('#operatorEditWin').window('close');
179     
180         $("#dataGrid").datagrid("loading");
181         $("#btn_submit").attr("disabled", true);
182        
183         var formData = SerializeFormWithArray("operatorEditForm");
184         url = url + "?" + formData;
185 
186         $.ajax({
187             url: url,
188             type: 'POST',
189             dataType: 'text',
190             success: function (data) {
191                 if (data.indexOf("/Login/Login") >= 0) {
192                     window.parent.location.href = '/Login/Login';
193                 }
194                 groupId = $("#groupId").combobox('getValue');
195                 operatorId = $("#operatorId").val();
196                 operatorName = $("#operatorName").val();
197                 url = "/SystemManage/OperatorManage/OperatorManage/OperatorManage?groupId=" + groupId
198                     + "&operatorId=" + operatorId + "&operatorName=" + operatorName;
199 
200                 alert(data);
201 
202                 reloadDatagrid(url, "dataGrid", "btn_submit", "无用户数据返回!", "查询用户数据出错!");
203             },
204             error: function (data) {
205                 commErrorHandle(data, true, 'dataGrid', 'btn_submit', "操作出错!");
206             }
207         });
208     });
209 
210 
211     //添加用户信息
212     $("#operatorAddForm").submit(function (event) {
213         //中断当前的提交事件
214         event.preventDefault();
215         //提交的URL,默认为属性组修改或添加路径
216         var url = "/SystemManage/OperatorManage/OperatorManage/AddOperator";
217         //账号
218         var operatorId = $("#a_operatorId").val();
219         if (operatorId == "") {
220             alert("账号不能为空!");
221             return false;
222         }
223         //名称
224         var operatorName = $("#a_operatorName").val();
225         if (operatorName == "") {
226             alert("名称不能为空!");
227             return false;
228         }
229         //密码
230         var password = $("#a_password").val();
231         var passwordConfirm = $("#a_passwordconfirm").val();
232         if (password == "" || passwordConfirm == "") {
233             alert("密码不能为空!");
234             return false;
235         }
236         if (password != passwordConfirm) {
237             alert("两次输入密码不一致!");
238             return false;
239         }
240 
241         $('#operatorAddWin').window('close');
242         startDatagridLoading('dataGrid', 'btn_submit');
243 
244         var groupId;
245         //表单序列化      
246         var formData = SerializeFormWithArray("operatorAddForm");
247         //完整路径
248         url = url + "?" + formData;
249 
250         $.ajax({
251             url: url,
252             type: 'POST',
253             dataType: 'text',
254             success: function (data) {
255                 if (data.indexOf("/Login/Login") >= 0) {
256                     window.parent.location.href = '/Login/Login';
257                 }
258 
259                 groupId = $("#groupId").combobox('getValue');
260                 operatorId = $("#operatorId").val();
261                 operatorName = $("#operatorName").val();
262                 url = "/SystemManage/OperatorManage/OperatorManage/OperatorManage?groupId=" + groupId
263                     + "&operatorId=" + operatorId + "&operatorName=" + operatorName;
264 
265                 alert(data);
266 
267                 reloadDatagrid(url, "dataGrid", "btn_submit", "无用户数据返回!", "查询用户数据出错!");
268             },
269             error: function (data) {
270                 commErrorHandle(data, true, 'dataGrid', 'btn_submit', "操作出错!");
271             }
272         });
273     });
274 
275 });
operatorManage.js

1.5控制器

用户模块的控制器包含在用户域中,文件路径为Areas/SystemManage/OperatorManage/Controllers,控制器名称为OperatorManageController.cs。控制器的完整代码如下:

  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 System.Data.SqlClient;
 12 using Newtonsoft.Json;
 13 using LogInfo;
 14 
 15 namespace CodeForMvcTest.Areas.OperatorManage.Controllers
 16 {
 17     public class OperatorManageController : BaseController
 18     {
 19         /// <summary>
 20         /// ****************************
 21         /// 功能:用户信息类
 22         /// 作者:王令
 23         /// 时间:2015-7-15
 24         /// 邮箱:[email protected]
 25         /// ****************************
 26         
 27         public ActionResult OperatorManage()
 28         {
 29             IList<AccountInfo> operatorList = GetOperatorByGroup(-1, null, null);
 30             IList<OperatorGroup> groupList = GetOperatorGroup();
 31             //获取分组
 32             ViewBag.OperatorList = operatorList;
 33             ViewBag.GroupListWithAll = DataTransfor.ListToComboboxHtml(groupList, "GroupId", "GroupName", true);
 34             ViewBag.GroupList = DataTransfor.ListToComboboxHtml(groupList, "GroupId", "GroupName", false);
 35             return View();
 36         }
 37 
 38 
 39         /// <summary>
 40         /// 返回用户列表数据
 41         /// </summary>
 42         /// <param name="groupId">用户组ID</param>
 43         /// <param name="operatorId">账号</param>
 44         /// <param name="operatorName">名称</param>
 45         /// <returns></returns>
 46         [HttpPost]
 47         public ActionResult OperatorManage(int groupId, string operatorId, string operatorName)
 48         {
 49             IList<AccountInfo> dataList = GetOperatorByGroup(groupId, operatorId, operatorName);
 50             return Json(dataList);
 51         }
 52 
 53 
 54         /// <summary>
 55         /// 添加用户
 56         /// </summary>
 57         /// <param name="groupId">用户组ID</param>
 58         /// <param name="operatorId">操作员账号</param>
 59         /// <param name="operatorName">操作员名字</param>
 60         /// <param name="sex">性别</param>
 61         /// <param name="state">是否启用</param>
 62         /// <param name="password">密码</param>
 63         /// <returns></returns>
 64         [HttpPost]
 65         public ActionResult AddOperator(string groupId, 
 66             string operatorId, string operatorName, int sex, int state, string password)
 67         {
 68             try
 69             {
 70                 string sql = "select count(*) from operatorinfo where accountid='" + operatorId + "'";
 71                 int count = SqlHelper.ExecuteScalar<int>(SqlSeverConnectionName , sql);
 72                 if (count >= 1)
 73                 {
 74                     Log.SaveOperatorLog(sql, 0, "由于账号已经存在,未能成功添加用户");
 75                     return Content("该账号已经存在!");
 76                 }
 77 
 78                 sql = "insert into operatorinfo (accountid,accountname,sex,isonstaff,groupid,"
 79                     + "netid,opcardid,opcardno,opgroupno,passwords) values('{0}','{1}',{2},{3},'{4}',5000,0,0,0,'{5}')";
 80                
 81                 sql = string.Format(sql, operatorId, operatorName, sex, state, groupId, password);
 82 
 83                 int updateCount = SqlHelper.ExecuteNonQuery(SqlSeverConnectionName , sql);
 84                 if (updateCount < 1)
 85                 {
 86                     Log.SaveOperatorLog(sql, 0, "添加用户" + operatorId);
 87                     return Content("未能成功添加用户!");
 88                 }
 89                 Log.SaveOperatorLog(sql, 1, "添加用户" + operatorId);
 90                 return Content("操作成功!");
 91             }
 92             catch (Exception ex)
 93             {
 94                 Log.SaveErrorLog(ex.ToString(), "添加用户出错");
 95                 return Content("添加用户出错!");
 96             }
 97         }
 98 
 99 
100         /// <summary>
101         /// 修改用户信息
102         /// </summary>
103         /// <param name="groupId">用户组ID</param>
104         /// <param name="oldId">原用户ID</param>
105         /// <param name="operatorName">操作员名字</param>
106         /// <param name="sex">性别</param>
107         /// <param name="state">是否启用</param>
108         /// <returns></returns>
109         [HttpPost]
110         public ActionResult UpdateOperator(string groupId, 
111             string operatorId, string oldId, string operatorName, int sex, int state)
112         {
113             string sql = "update operatorinfo set accountid='{0}',accountname='{1}',groupid='{2}',"
114                     + "isonstaff={3},sex={4} where accountid='{5}'";
115            
116             try
117             {
118                 sql = string.Format(sql, operatorId, operatorName, groupId,state, sex,  oldId);
119                 int updateCount = SqlHelper.ExecuteNonQuery(SqlSeverConnectionName , sql);            
120                 Log.SaveOperatorLog(sql, 1, "修改用户信息");
121                 return Content("操作成功!");
122             }
123             catch (Exception ex)
124             {
125                 Log.SaveErrorLog(ex.ToString(), "修改用户信息出错");
126                 return Content("修改用户信息出错!");
127             }
128         }
129 
130 
131 
132 
133         /// <summary>
134         /// 删除用户信息
135         /// </summary>
136         /// <param name="operatorId">操作员账号</param>
137         /// <returns></returns>
138         [HttpPost]
139         public ActionResult DeleteOperator(string operatorId)
140         {
141             string sql = "delete from operatorinfo where accountid='" + operatorId + "'";
142             try
143             {
144                 int updateCount = SqlHelper.ExecuteNonQuery(SqlSeverConnectionName , sql);
145                 Log.SaveOperatorLog(sql, 1, "成功删除用户信息");
146                 return Content("操作成功!");
147             }
148             catch (Exception ex)
149             {
150                 Log.SaveErrorLog(ex.ToString(), "删除用户信息出错");
151                 return Content("删除用户信息出错!");
152             }
153         }
154 
155 
156 
157 
158         /// <summary>
159         /// 根据用户组获取用户组下的用户列表
160         /// </summary>
161         /// <param name="groupId">用户组ID,-1=全部</param>
162         /// <param name="operatorId">账号</param>
163         /// <param name="operatorName">名称</param>
164         /// <returns></returns>
165         private IList<AccountInfo> GetOperatorByGroup(int groupId, string operatorId, string operatorName)
166         {
167             IList<AccountInfo> dataList = new List<AccountInfo>();
168             string sql = "select o.* from operatorinfo o where 1=1";
169             if (groupId != -1)
170             {
171                 sql += " and  o.groupid='" + groupId + "' ";
172             }
173             if (!string.IsNullOrEmpty(operatorId))
174             {
175                 sql += " and o.accountid='" + operatorId + "'";
176             }
177             if (!string.IsNullOrEmpty(operatorName))
178             {
179                 sql += " and o.accountname='" + operatorName + "'";
180             }
181             sql += " order by o.accountid";
182             try
183             {
184                 DataSet dataSet = SqlHelper.ExecuteQuery(SqlSeverConnectionName , sql);
185                 if (dataSet != null && dataSet.Tables.Count > 0)
186                 {
187                     DataTable table = dataSet.Tables[0];
188                     foreach (DataRow dr in table.Rows)
189                     {
190                         var item = new AccountInfo();
191 
192                         #region 封装用户信息
193 
194                         item.OperatorId = Convert.ToString(dr["accountid"]);
195                         item.OperatorName = DBNull.Value.Equals(dr["accountname"]) ? "" : Convert.ToString(dr["accountname"]);
196 
197                     
198                         item.AliasName = DBNull.Value.Equals(dr["aliasname"]) ? "" : Convert.ToString(dr["aliasname"]);
199                         item.Sex = DBNull.Value.Equals(dr["sex"]) ? "" : (Convert.ToInt32(dr["sex"]) == 0 ? "" : "");
200                         item.OperatorGroupId = DBNull.Value.Equals(dr["groupid"]) ? "" : Convert.ToString(dr["groupid"]);
201                         item.IsOnStaff = DBNull.Value.Equals(dr["isonstaff"]) ? 0 : Convert.ToInt32(dr["isonstaff"]);
202                         item.OperatorGroupName = "";
203 
204                         #endregion
205 
206                         #region 获取用户组名
207 
208                         sql = "select groupname from operatorgroup where groupid in (" + item.OperatorGroupId + ")";
209                         DataSet groupDs = SqlHelper.ExecuteQuery(SqlSeverConnectionName, sql);
210                         if (groupDs != null && groupDs.Tables.Count > 0)
211                         {
212                             DataTable groupTb = groupDs.Tables[0];
213                             foreach (DataRow groupRow in groupTb.Rows)
214                             {
215                                 string groupName = DBNull.Value.Equals(groupRow[0]) ? "" : Convert.ToString(groupRow[0]);
216                                 if (!string.IsNullOrEmpty(groupName))
217                                 {
218                                     item.OperatorGroupName += groupName + ",";
219                                 }
220                             }
221                             if (!string.IsNullOrEmpty(item.OperatorGroupName))
222                             {
223                                 item.OperatorGroupName = item.OperatorGroupName.Substring(0,
224                                     item.OperatorGroupName.Length - 1);
225                             }
226                         }
227 
228                         #endregion
229 
230                         dataList.Add(item);
231                     }
232                 }
233             }
234             catch (Exception ex)
235             {
236                 Log.SaveErrorLog(ex.ToString(), "获取用户信息出错");
237             }
238             return dataList;
239         }
240 
241 
242 
243         /// <summary>
244         /// 获取用户组
245         /// </summary>
246         /// <returns></returns>
247         public IList<OperatorGroup> GetOperatorGroup()
248         {
249             IList<OperatorGroup> groupList = new List<OperatorGroup>();
250             string sql = "select * from operatorgroup order by groupid";
251             try
252             {
253                 DataSet dataSet = SqlHelper.ExecuteQuery(SqlSeverConnectionName , sql);
254                 int[] columnIndexArray = { 0, 1, 3, 4, 7 };
255                 string[] propertyArray = { "GroupId", "GroupName", "OrderNum", "State", "ParentId" };
256                 groupList = DataTransfor.DataSetTransfor<OperatorGroup>(dataSet, columnIndexArray, propertyArray);
257             }
258             catch (Exception ex)
259             {
260                 Log.SaveErrorLog(ex.ToString(), "获取用户组出错!");
261             }
262             return groupList;
263         }
264 
265 
266     }
267 
268 }
OperatorManage.cs

1.6界面运行截图

用户管理主界面如下图所示:

添加用户界面如下图所示:

编辑用户界面如下图所示:

 

你可能感兴趣的:(Web应用程序系统的多用户权限控制设计及实现-用户模块【7】)