首页 > 代码库 > OA之为用户设置角色和为用户设置权限

OA之为用户设置角色和为用户设置权限

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=http://www.mamicode.com/"~/Scripts/jquery-1.8.2.min.js"></script>
12     <script src=http://www.mamicode.com/"~/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=http://www.mamicode.com/"@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=http://www.mamicode.com/"@role.ID" name="@checkName" id="@role.ID" checked="checked"/>@role.RoleName<br/>
48                 }
49                 else
50                 { 
51                      <input type="checkbox" value=http://www.mamicode.com/"@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=http://www.mamicode.com/"~/Content/themes/default/easyui.css" rel="stylesheet" />
 12     <link href=http://www.mamicode.com/"~/Content/themes/icon.css" rel="stylesheet" />
 13     <link href=http://www.mamicode.com/"~/Content/tableStyle.css" rel="stylesheet" />
 14     <script src=http://www.mamicode.com/"~/Scripts/jquery-1.8.2.min.js"></script>
 15     <script src=http://www.mamicode.com/"~/Scripts/jquery.unobtrusive-ajax.min.js"></script>
 16     <script src=http://www.mamicode.com/"~/Scripts/easyui-lang-zh_CN.js"></script>
 17     <script src=http://www.mamicode.com/"~/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="rdo_@action.ID">允许</label><input type="radio" class="radiosecelction" checked="checked" name="rdoY_@action.ID" id="rdo_@action.ID" ids="@action.ID" value=http://www.mamicode.com/"1" />
 57 
 58                                 <label for="rdoY_@action.ID">禁止</label><input type="radio" class="radiosecelction" name="rdoY_@action.ID" id="rdoY_@action.ID" ids="@action.ID" value=http://www.mamicode.com/"2" />
 59     <input type="button" value=http://www.mamicode.com/"清除" class="clearbtn" ids="@action.ID" />
 60                         }
 61                         else
 62                         {
 63                             <label for="rdo_@action.ID">允许</label><input type="radio" name="rdoY_@action.ID" class="radiosecelction" id="rdo_@action.ID" value=http://www.mamicode.com/"1" ids="@action.ID" />
 64 
 65                                 <label for="rdoY_@action.ID">禁止</label><input type="radio" checked="checked" class="radiosecelction" name="rdoY_@action.ID" id="rdoY_@action.ID" value=http://www.mamicode.com/"2" ids="@action.ID" />
 66     <input type="button" value=http://www.mamicode.com/"清除" class="clearbtn" ids="@action.ID" />
 67                         }
 68                     }
 69                     else
 70                     {
 71                         <label for="rdo_@action.ID">允许</label><input type="radio" class="radiosecelction" name="rdoY_@action.ID" id="rdo_@action.ID" value=http://www.mamicode.com/"1" ids="@action.ID" />
 72 
 73                                 <label for="rdoY_@action.ID">禁止</label><input type="radio" class="radiosecelction" name="rdoY_@action.ID" id="rdoY_@action.ID" value=http://www.mamicode.com/"2" ids="@action.ID" />
 74                                 <input type="button" value=http://www.mamicode.com/"清除" 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 =http://www.mamicode.com/= "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 =http://www.mamicode.com/= "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之为用户设置角色和为用户设置权限