OA项目第六天

修改用户权限,登陆菜单权限过滤。完成权限校验。非菜单权限过滤(即所有控制器继承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() {//给每行的单选添加点击事件,发送ajax
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()
{
//1;按照用户-角色-权限 查询出所有权限到一个集合
//找到角色,用户具有的权限
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();//把角色对应的权限ID添加到列表中

//2:按照用户权限路线查询出所有权限,并且过滤掉禁止通过的用户
var ActionList = from a in userinfo.R_UserInfo_ActionInfo
where a.IsPass == true//过滤ispass
select a.ActionInfo;
var userActionList = ActionList.Where(a=>a.ActionTypeEnum==1).ToList();

//3:添加到一个合集
userActionList.AddRange(userRoleActionList);
//4:过滤ispass,上面已经实现
//5:移除两者中重复的权限
var newuserActionList = userActionList.Distinct();

//6:返回需要的json
//{ icon: '/Content/Images/3DSMAX.png', title: '用户信息', url: '/UserInfo/Index' },
var json = from i in newuserActionList
select new { icon=i.MenuIcon, title=i.ActionInfoName,url=i.Url };
return Json(json,JsonRequestBehavior.AllowGet);//千万不能漏了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; }
/// <summary>
/// 执行控制器中的方法之前先执行该方法。
/// </summary>
/// <param name="filterContext"></param>
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
//if (Session["userInfo"] == null)
bool isSucess = false;
if(Request.Cookies["sessionId"]!=null)
{
string sessionId = Request.Cookies["sessionId"].Value;
//根据该值查Memcache.
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));//模拟出滑动过期时间.

//完成权限校验。
//获取用户请求的URL地址.
string url = Request.Url.AbsolutePath.ToLower();
//获取请求的方式.
string httpMehotd = Request.HttpMethod;
//根据获取的URL地址与请求的方式查询权限表。
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();
//1:可以先按照用户权限这条线进行过滤。
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;
}

}
//2:按照用户角色权限这条线进行过滤。
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");//注意.
}
}
}

OA项目第六天
http://www.kju147.com/2018/04/15/OA项目第六天/
作者
个人学习
发布于
2018年4月15日
许可协议