修改用户权限,登陆菜单权限过滤。完成权限校验。非菜单权限过滤(即所有控制器继承BaseController)
OA项目第六天 修改用户权限 首先先在Index的view视图添加toolbar(loadData方法下的)
1 2 3 4 5 6 7 8 9 { id: 'btnEidt', text: '编辑用户权限', iconCls: 'icon-edit', handler: function () { showUserActionInfo();//编辑用户权限信息 } }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 function showUserActionInfo ( ) { var rows = $('#tt' ).datagrid('getSelections' ); if (rows.length != 1 ) { $.messager.alert("提示" , "请选择要修改的用户" , "error" ); return ; } $("#setUserActionFrame" ).attr("src" , "/UserInfo/ShowUserAction?aid=" + rows[0 ].ID); $("#showUserAction" ).css("display" , "block" ); $("#showUserAction" ).dialog({ title: '修改用户权限' , width: 400 , height: 300 , collapsible: true , maximizable: true , resizable: true , modal: true , buttons: [{ text: 'Ok' , iconCls: 'icon-ok' , handler: function ( ) { } }, { text: 'Cancel' , handler: function ( ) { $('#setUsrActionDiv' ).dialog('close' ); } }] }); }
控制器下的ShowUserAction方法,展示视图
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 public ActionResult ShowUserAction ( ) { int actionId = int .Parse(Request["aid" ]); var user = bll.LoadEntities(u => u.ID == actionId).FirstOrDefault(); ViewBag.User = user; var actionlist = abll.LoadEntities(a => a.DelFlag == 0 ).ToList(); ViewBag.AllActionList = actionlist; var allactionlist = (from a in user.R_UserInfo_ActionInfo select a).ToList(); ViewBag.AllActionIDList = allactionlist; return View(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 public ActionResult SerUserAction ( ) { int actionID = int .Parse(Request["aid" ]); bool ispass = Request["ispass" ] == "true" ? true : false ; int id = int .Parse(Request["id" ]); if (bll.SetUserAction(id, actionID, ispass)) { return Content("OK" ); } else { return Content("NO" ); } }
我们还会在每行的最后添加一个清除的效果,也就是删除用户角色的中间表的相关记录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public ActionResult ClearUserAction ( ) { int actionID = int .Parse(Request["aid" ]); int id = int .Parse(Request["id" ]); var rua= rbll.LoadEntities(r => r.ActionInfoID == actionID && r.UserInfoID == id).FirstOrDefault(); if (rua!=null ) { if (rbll.DeleteEntity(rua)) { return Content("OK:操作成功" ); } else { return Content("NO:操作失败" ); } } else { return Content("NO:操作失败" ); } }
view视图的展示用户权限
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 <body > <div > 为@{ UserInfo userinfo = ViewBag.User; <span style ="font-size: 14px; color: red;font-weight: bold" > @userinfo.UName</span > }用户修改权限 <br /> <table > <tr > <th > 编号</th > <th > 权限名称</th > <th > Url</th > <th > HttpMethod</th > <th > 操作</th > </tr > @{ List<ActionInfo > actionInfoList = ViewBag.AllActionList; List<R_UserInfo_ActionInfo > userActionList = ViewBag.AllActionIDList; foreach (ActionInfo actionInfo in actionInfoList) { var actionName = "cba_" + actionInfo.ID; var actionNoName = "nocba_" + actionInfo.ID; <tr > <td > @actionInfo.ID</td > <td > @actionInfo.ActionInfoName</td > <td > @actionInfo.Url</td > <td > @actionInfo.HttpMethod</td > <td > @{ var action = (from r in userActionList where r.ActionInfoID == actionInfo.ID select r).FirstOrDefault(); if (action != null) { if (action.IsPass)//要是查询到用户权限信息,并且用户具有此权限 { <label for ="@actionName" > 允许 </label > <input type ="radio" value ="true" checked ="checked" name ="@actionName" class ="selectActions" ids ="@actionInfo.ID" id ="@actionName" /> <label for ="actionNoName" > 禁止 </label > <input type ="radio" value ="false" name ="@actionName" class ="selectActions" ids ="@actionInfo.ID" id ="@actionNoName" /> } else { <label for ="@actionName" > 允许 </label > <input type ="radio" value ="true" name ="@actionName" class ="selectActions" ids ="@actionInfo.ID" id ="@actionName" /> <label for ="actionNoName" > 禁止 </label > <input type ="radio" value ="false" checked ="checked" name ="@actionName" class ="selectActions" ids ="@actionInfo.ID" id ="@actionNoName" /> } } else { <label for ="@actionName" > 允许 </label > <input type ="radio" value ="true" name ="@actionName" class ="selectActions" ids ="@actionInfo.ID" id ="@actionName" /> <label for ="actionNoName" > 禁止 </label > <input type ="radio" value ="false" name ="@actionName" class ="selectActions" ids ="@actionInfo.ID" id ="@actionNoName" /> } } </td > <td > <input type ="button" value ="删除" class ="clearUserAction" ids ="@actionInfo.ID" /> </td > </tr > } } </table > </div > </body > </html > <script type ="text/javascript" > $(function ( ) { $(".clearUserAction" ).click(function ( ) { var aid = $(this ).attr("ids" ); var id = @userinfo.ID; $.post("/UserInfo/ClearUserAction" , { "aid" : aid, "id" :id}, function (data ) { var str = data.split(':' ); if (str[0 ] == "OK" ) { $control.parent().find(".selectActions" ).removeAttr("checked" ); $.messager.show({ title:'提示' , msg: str[1], showType:'show' }); } else { $.messager.show({ title:'提示' , msg: str[1], showType:'show' }); } }); }); $(".selectActions" ).click(function ( ) { var ispass = $(this ).val(); var aid = $(this ).attr("ids" ); var id = @userinfo.ID; $.post("/UserInfo/SerUserAction" , { "aid" : aid, "ispass" : ispass ,"id" :id}, function (data ) { if (data == "OK" ) { $.messager.show({ title:'提示' , msg:'修改成功' , showType:'show' }); } else { $.messager.show({ title:'提示' , msg:'修改失败' , showType:'show' }); } }); }); })</script >
登陆菜单权限过滤 我们的登录流程是先检查Memcache的值,然后完成权限校验跳转到Login登陆页面(在没有Memcache值的情况下),然后再跳转到home的index页面,这个页面中,我们需要做一个菜单权限过滤,以在桌面上显示属于该登陆者的菜单。首先我们的home的index页面先把所有的菜单注释掉,然后我们发布ajax请求
1 2 3 4 5 6 7 .... $.ligerui.win.createTaskbar(); //页面加载时创建任务栏 $.getJSON("/Home/Getmenus", {}, function (data) { links = data; linksInit(); onResize(); });
Getmenus方法,过滤的步骤如注释所写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 public ActionResult GetMeNu ( ) { var userinfo = bll.LoadEntities(u => u.ID == LoginUser.ID).FirstOrDefault(); var userInfoRole = userinfo.RoleInfo; var userRoleActionList = (from a in userInfoRole from roleInfo in a.ActionInfo where roleInfo.ActionTypeEnum == 1 select roleInfo).ToList(); var ActionList = from a in userinfo.R_UserInfo_ActionInfo where a.IsPass == true select a.ActionInfo; var userActionList = ActionList.Where(a=>a.ActionTypeEnum==1 ).ToList(); userActionList.AddRange(userRoleActionList); var newuserActionList = userActionList.Distinct(); var json = from i in newuserActionList select new { icon=i.MenuIcon, title=i.ActionInfoName,url=i.Url }; return Json(json,JsonRequestBehavior.AllowGet); }
这样一来我们就可以实现我们的菜单过滤了
非菜单权限过滤(即所有控制器继承BaseController) 这里我们做的是 //完成权限校验。这样一来所有的控制器的方法都必须往数据库里面添加
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 public class BaseController : Controller { public UserInfo LoginUser { get ; set ; } protected override void OnActionExecuting (ActionExecutingContext filterContext ) { base .OnActionExecuting(filterContext); bool isSucess = false ; if (Request.Cookies["sessionId" ]!=null ) { string sessionId = Request.Cookies["sessionId" ].Value; object obj=Common.MemcacheHelper.Get(sessionId); if (obj!=null ) { UserInfo userInfo = Common.SerializeHelper.DeserializeToObject<UserInfo>(obj.ToString()); LoginUser = userInfo; isSucess = true ; Common.MemcacheHelper.Set(sessionId, obj, DateTime.Now.AddMinutes(20 )); string url = Request.Url.AbsolutePath.ToLower(); string httpMehotd = Request.HttpMethod; IApplicationContext ctx = ContextRegistry.GetContext(); IBLL.IActionInfoService ActionInfoService = (IBLL.IActionInfoService)ctx.GetObject("ActionInfoService" ); var actionInfo= ActionInfoService.LoadEntities(a=>a.Url==url&&a.HttpMethod==httpMehotd).FirstOrDefault(); IUserInfoService UserInfoService = (IUserInfoService)ctx.GetObject("UserInfoService" ); var loginUserInfo = UserInfoService.LoadEntities(u=>u.ID==LoginUser.ID).FirstOrDefault(); var isExt =(from a in loginUserInfo.R_UserInfo_ActionInfo where a.ActionInfoID == actionInfo.ID select a).FirstOrDefault(); if (isExt != null ) { if (isExt.IsPass) { return ; } else { filterContext.Result = Redirect("/Error.html" ); return ; } } var loginUserRole = loginUserInfo.RoleInfo; var count = (from r in loginUserRole from a in r.ActionInfo where a.ID == actionInfo.ID select a).Count(); if (count < 1 ) { filterContext.Result = Redirect("/Error.html" ); return ; } } } if (!isSucess) { filterContext.Result = Redirect("/Login/Index" ); } } }