1. 程式人生 > >OA之為用戶設置角色和為用戶設置權限

OA之為用戶設置角色和為用戶設置權限

code remove art url tpm begin -a list entity

1.為用戶設置角色

 1 {
 2     Layout = null;
 3 }
 4 @using OA.Model 
 5 <!DOCTYPE html>
 6 
 7 <html>
 8 <head>
 9     <meta name="viewport" content="width=device-width" />
10     <title>SetRoleInfo</title>
11     <script src="~/Scripts/jquery-1.8.2.min.js"></script>
12
<script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> 13 <script type="text/javascript"> 14 function subForm() 15 { 16 $(#form1).submit(); 17 } 18 function a() 19 { 20 alert("a"); 21 } 22 function afterSubmit(data)
23 { 24 window.parent.AfterSubmit(data) 25 //parent.document.getElementsByTagName("roleFrame")["roleFrame"].AfterSubmit(data); 26 } 27 </script> 28 </head> 29 <body> 30 <div> 31 為用戶@{ 32 UserInfo userInfo = ViewBag.UserInfo;
33 @userInfo.UName 34 }添加角色 35 @using (Ajax.BeginForm("AddRole", "UserInfo", new { }, new AjaxOptions() { HttpMethod = "Post", OnSuccess = "afterSubmit" }, new { id = "form1" })) 36 { 37 <input type="hidden" name="userId" value="@userInfo.ID"/> 38 IList<RoleInfo> roleInfoList=ViewBag.RoleInfoList; 39 IList<int> roleIds=ViewBag.roleIds; 40 string ckName="ck_"; 41 42 foreach (RoleInfo role in roleInfoList) 43 { 44 string checkName=ckName+role.ID; 45 if (roleIds.Contains(role.ID)) 46 { 47 <input type="checkbox" value="@role.ID" name="@checkName" id="@role.ID" checked="checked"/>@role.RoleName<br/> 48 } 49 else 50 { 51 <input type="checkbox" value="@role.ID" name="@checkName" id="@role.ID" />@role.RoleName<br/> 52 } 53 54 } 55 } 56 </div> 57 </body> 58 </html>

後臺代碼

 1  public ActionResult AddRole()
 2         {
 3             int userId = int.Parse(Request["userId"]);
 4             string[] allKeys = Request.Form.AllKeys;
 5             IList<int> roleIdList = new List<int>();
 6             foreach (string k in allKeys)
 7             {
 8                 if (k.StartsWith("ck_"))
 9                 {
10                     string k1 = k.Replace("ck_", "");
11                     roleIdList.Add(int.Parse(k1));
12                 }
13             }
14             string result = userInfoService.SetRole(userId, roleIdList);
15             return Content(result);
16         }

2.為用戶設置權限

  1 @{
  2     Layout = null;
  3 }
  4 @using OA.Model
  5 <!DOCTYPE html>
  6 
  7 <html>
  8 <head>
  9     <meta name="viewport" content="width=device-width" />
 10     <title>SetUserActionInfo</title>
 11     <link href="~/Content/themes/default/easyui.css" rel="stylesheet" />
 12     <link href="~/Content/themes/icon.css" rel="stylesheet" />
 13     <link href="~/Content/tableStyle.css" rel="stylesheet" />
 14     <script src="~/Scripts/jquery-1.8.2.min.js"></script>
 15     <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
 16     <script src="~/Scripts/easyui-lang-zh_CN.js"></script>
 17     <script src="~/Scripts/jquery.easyui.min.js"></script>
 18 </head>
 19 <body>
 20     <div>
 21         為用戶@{
 22             UserInfo user = ViewBag.User as UserInfo;
 23             @user.UName
 24 
 25         }分配權限
 26         <table>
 27             <tr>
 28                 <th>權限編號</th>
 29                 <th>權限名稱</th>
 30                 <th>地址</th>
 31                 <th>請求方式</th>
 32                 <th>操作</th>
 33             </tr>
 34 
 35             @{
 36 
 37                 IList<ActionInfo> actionInfoList = ViewBag.ActionInfoList as IList<ActionInfo>;
 38                 IList<R_UserInfo_ActionInfo> userActionList = ViewBag.UserActionList as IList<R_UserInfo_ActionInfo>;
 39                 foreach (ActionInfo action in actionInfoList)
 40                 {
 41                     <tr>
 42                         <td>@action.ID</td>
 43                         <td>@action.ActionInfoName</td>
 44                         <td>@action.Url</td>
 45                         <td>@action.HttpMethod</td>
 46                         <td>
 47                             @{
 48                     var result =( from a in userActionList
 49                                  where a.ActionInfoID == action.ID
 50                                  select a).ToList();
 51                     if (result != null&&result .Count >0)
 52                     {
 53                         R_UserInfo_ActionInfo rua = result.FirstOrDefault();
 54                         if (rua.IsPass)
 55                         {
 56                             <label for="[email protected]">允許</label><input type="radio" class="radiosecelction" checked="checked" name="[email protected]" id="[email protected]" ids="@action.ID" value="1" />
 57 
 58                                 <label for="[email protected]">禁止</label><input type="radio" class="radiosecelction" name="[email protected]" id="[email protected]" ids="@action.ID" value="2" />
 59     <input type="button" value="清除" class="clearbtn" ids="@action.ID" />
 60                         }
 61                         else
 62                         {
 63                             <label for="[email protected]">允許</label><input type="radio" name="[email protected]" class="radiosecelction" id="[email protected]" value="1" ids="@action.ID" />
 64 
 65                                 <label for="[email protected]">禁止</label><input type="radio" checked="checked" class="radiosecelction" name="[email protected]" id="[email protected]" value="2" ids="@action.ID" />
 66     <input type="button" value="清除" class="clearbtn" ids="@action.ID" />
 67                         }
 68                     }
 69                     else
 70                     {
 71                         <label for="[email protected]">允許</label><input type="radio" class="radiosecelction" name="[email protected]" id="[email protected]" value="1" ids="@action.ID" />
 72 
 73                                 <label for="[email protected]">禁止</label><input type="radio" class="radiosecelction" name="[email protected]" id="[email protected]" value="2" ids="@action.ID" />
 74                                 <input type="button" value="清除" class="clearbtn" ids="@action.ID" />
 75                     }
 76                             }
 77                         </td>
 78                     </tr>
 79                 }
 80             }
 81         </table>
 82     </div>
 83 </body>
 84 </html>
 85 <script type="text/javascript">
 86     $(".radiosecelction").click(function () {
 87         var obj=$(this);
 88         $.post(
 89             "/UserInfo/SetUserAction",
 90             { "userId": @user.ID, "actionId": obj.attr("ids"), "isPass": obj.val() },
 91             function (data)
 92             {
 93                 if (data == "OK") {
 94                     $.messager.show({
 95                         title: 提示,
 96                         msg: 權限修改成功,
 97                         showType: show
 98                     });
 99                 }
100                 else {
101                     $.messager.show({
102                         title: 提示,
103                         msg: 權限修改失敗,
104                         showType: show
105                     });
106                 }
107 
108             }
109 
110             )
111     });
112     $(".clearbtn").click(function () {
113 
114         var obj = $(this);
115         $.post(
116             "/UserInfo/ClearUserAction",
117             { "userId": @user.ID, "actionId": obj.attr("ids") },
118             function (data) {
119                 if (data == "OK") {
120                     obj.parent().find(".radiosecelction").removeAttr("checked")
121                     $.messager.show({
122                         title: 提示,
123                         msg: 權限刪除成功,
124                         showType: show
125                     });
126                 }
127                 else {
128                     $.messager.show({
129                         title: 提示,
130                         msg: 權限修改失敗,
131                         showType: show
132                     });
133                 }
134 
135             }
136 
137             )
138     });
139 </script>

後天代碼

  public ActionResult SetActionInfo()
        {
            int id = Request["id"] == null ? -1 : int.Parse(Request["id"]);
            if (id >= 0)
            {
                //需要獲取所有的權限信息進行展示
                //需要獲取用戶已經擁有的權限信息進行展示
                //需要獲取用戶的信息,如用戶的名稱
                UserInfo user = userInfoService.LoadEntity(u => u.ID == id).FirstOrDefault();
                ViewBag.User = user;
                IList<ActionInfo> actionInfoList = actionInfoService.LoadEntity(a => a.DelFlag == (short)DelFlagEnum.Noraml).ToList();
                ViewBag.ActionInfoList = actionInfoList;
                IList<R_UserInfo_ActionInfo> userActionList = user.R_UserInfo_ActionInfo.ToList();
                ViewBag.UserActionList = userActionList;
            }
            return View("SetUserActionInfo");
        }

        //為用戶修改權限
        public ActionResult SetUserAction()
        {
            int userId = int.Parse(Request["userId"]);
            int actionId = int.Parse(Request["actionId"]);
            bool isPass = Request["isPass"] == "1" ? true : false;
            string result = userInfoService.SetUserAction(userId, actionId, isPass);
            return Content(result);
        }

OA之為用戶設置角色和為用戶設置權限