构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(24)-权限管理系统-将权限授权给角色
过了个年回来,回顾一下,我们上次讲了角色管理,我们这一次来讲将权限授权给角色,这一节也是大家比较关心的。因为我们已经跑通了整个系统,知道权限的流转,我们先来看一张图
这张图主要分要3块,角色组----系统模块----操作码授权
选择角色组(表示要授权的角色,选择需要授权的模块,最后授权操作码。当存在一个操作码时候,我们应该改变SysRight表中的rightflag字段,表示他有权限。不知道大家是否还记得,这个图也是我们要做的。由于图中用的是JQGrid看起来跟Easyui有点差别,但是方式却是一样的)
回到头看到SysRightOperate表的IsValid,我们将授权角色和模块和操作码这3张表关联起来,其中IsValid字段是来标识是否有操作的权限,当第一次授权,那么是向SysRightOperate添加一条记录,如果下次更新先判断是否已经授权如果没有,那么删除或者更新IsValid,我这里是更新,你也可以删除掉,一样的道理。
之前我们已经新建过SysRight这个表的DAL层和BLL层了,根据想法,我们只要建立多两个方法
- 更新SysRightOperate(即上面所说)
- 和按选择的角色及模块加载模块的权限项(确定操作码是否被授权,即图中的复选框)
现在向ISysRightRepository添加2个方法
//更新
int UpdateRight(SysRightOperate model);
//按选择的角色及模块加载模块的权限项
List<P_Sys_GetRightByRoleAndModule_Result> GetRightByRoleAndModule(string roleId, string moduleId);
P_Sys_GetRightByRoleAndModule_Result这个是存储过程,由于这2个方法比较复杂,这里用存储过程来做
Create proc [dbo].[P_Sys_GetRightByRoleAndModule]
@roleId varchar(),@moduleId varchar()
as
--按选择的角色及模块加载模块的权限项
begin
select a.Id,a.Name,a.KeyCode,a.ModuleId,ISNULL(b.IsValid,) as isvalid,a.Sort,@roleId+@moduleId as RightId
from SysModuleOperate a
left outer join(
select c.Id,a.IsValid from SysRightOperate a,SysRight b, SysModuleOperate c
where RightId in
(select Id From SysRight where RoleId =@roleId and ModuleId =@moduleId)
and a.RightId=b.Id
and b.ModuleId=c.ModuleId
and a.KeyCode =c.KeyCode) b
on a.Id = b.Id
where a.ModuleId =@moduleId
end
所以必须要把这个存储过程添加到EF,并生成复杂类型的实体P_Sys_GetRightByRoleAndModule_Result
然后创建P_Sys_UpdateSysRightRightFlag
Create proc [dbo].[P_Sys_UpdateSysRightRightFlag]
@moduleId varchar(),@roleId varchar()
as
begin
--计算上级模块的rightflag标识
declare @count int
--第一层:由操作权限项计算模块权限
select @count=COUNT(*) from SysRightOperate where RightId=@roleId+@moduleId and IsValid= if(@count>)
begin
update SysRight set Rightflag= where ModuleId=@moduleId and RoleId=@roleId
end
else
begin
update SysRight set Rightflag= where ModuleId=@moduleId and RoleId=@roleId
end
--计算下一层
declare @parentId varchar()
set @parentId=@moduleId while(@parentId<>'')
begin
select @parentid=ParentId from SysModule where Id=@parentId
if (@parentId is null)
begin
return
end select @count=COUNT(*) from SysRight where ModuleId in
(select Id from SysModule where ParentId=@parentId)
and RoleId =@roleId
and Rightflag= if(@count>)
begin
update SysRight set Rightflag= where ModuleId=@parentId and RoleId=@roleId
end
else
begin
update SysRight set Rightflag= where ModuleId=@parentId and RoleId=@roleId
end
end
end
这个是计算上级模块的rightflag标识也就是开头所说的RightFlag字段,这个字段将决定导航条的显示,所以每一次授权操作都要执行
下面添加SysRightRepository逻辑代码
public int UpdateRight(SysRightOperateModel model)
{
//转换
SysRightOperate rightOperate = new SysRightOperate();
rightOperate.Id = model.Id;
rightOperate.RightId = model.RightId;
rightOperate.KeyCode = model.KeyCode;
rightOperate.IsValid = model.IsValid;
//判断rightOperate是否存在,如果存在就更新rightOperate,否则就添加一条
using (DBContainer db = new DBContainer())
{
SysRightOperate right = db.SysRightOperate.Where(a => a.Id == rightOperate.Id).FirstOrDefault();
if (right != null)
{
right.IsValid = rightOperate.IsValid;
}
else
{
db.SysRightOperate.AddObject(rightOperate);
}
if (db.SaveChanges() > )
{
//更新角色--模块的有效标志RightFlag
var sysRight = (from r in db.SysRight
where r.Id == rightOperate.RightId
select r).First();
db.P_Sys_UpdateSysRightRightFlag(sysRight.ModuleId, sysRight.RoleId);
return ;
}
}
return ;
}
//按选择的角色及模块加载模块的权限项
public List<P_Sys_GetRightByRoleAndModule_Result> GetRightByRoleAndModule(string roleId, string moduleId)
{
List<P_Sys_GetRightByRoleAndModule_Result> result = null;
using (DBContainer db = new DBContainer())
{
result = db.P_Sys_GetRightByRoleAndModule(roleId,moduleId).ToList();
}
return result;
}
按照习惯,我们要向IBLL 和BLL 添加代码,大家自行添加访问DAL层的代码即可
比较繁琐的还是Controller层和页面UI的代码,这些先贴出
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace App.Models.Sys
{
public class SysRightModelByRoleAndModuleModel
{
public string Ids { get; set; }// RightId+ KeyCode ids
public string Name{ get; set; }
public string KeyCode{ get; set; }
public bool? IsValid{ get; set; }
public string RightId{ get; set; }
}
}
SysRightModelByRoleAndModuleModel
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using App.IBLL;
using App.Models;
using App.Common;
using App.Models.Sys; namespace App.Admin.Controllers
{
public class SysRightController : BaseController
{
//
// GET: /SysRight/
[Dependency]
public ISysRightBLL sysRightBLL { get; set; }
[Dependency]
public ISysRoleBLL sysRoleBLL { get; set; }
[Dependency]
public ISysModuleBLL sysModuleBLL { get; set; }
[SupportFilter]
public ActionResult Index()
{
ViewBag.Perm = GetPermission();
return View();
}
//获取角色列表
[SupportFilter(ActionName = "Index")]
[HttpPost]
public JsonResult GetRoleList(GridPager pager)
{
List<SysRoleModel> list = sysRoleBLL.GetList(ref pager, "");
var json = new
{
total = pager.totalRows,
rows = (from r in list
select new SysRoleModel()
{ Id = r.Id,
Name = r.Name,
Description = r.Description,
CreateTime = r.CreateTime,
CreatePerson = r.CreatePerson }).ToArray() }; return Json(json);
}
//获取模组列表
[SupportFilter(ActionName = "Index")]
[HttpPost]
public JsonResult GetModelList(string id)
{
if (id == null)
id = "";
List<SysModuleModel> list = sysModuleBLL.GetList(id);
var json = from r in list
select new SysModuleModel()
{
Id = r.Id,
Name = r.Name,
EnglishName = r.EnglishName,
ParentId = r.ParentId,
Url = r.Url,
Iconic = r.Iconic,
Sort = r.Sort,
Remark = r.Remark,
Enable = r.Enable,
CreatePerson = r.CreatePerson,
CreateTime = r.CreateTime,
IsLast = r.IsLast,
state = (sysModuleBLL.GetList(r.Id).Count > ) ? "closed" : "open"
}; return Json(json);
} //根据角色与模块得出权限
[SupportFilter(ActionName = "Index")]
[HttpPost]
public JsonResult GetRightByRoleAndModule(GridPager pager, string roleId, string moduleId)
{
pager.rows = ;
var right = sysRightBLL.GetRightByRoleAndModule(roleId,moduleId);
var json = new
{
total = pager.totalRows,
rows = (from r in right
select new SysRightModelByRoleAndModuleModel()
{
Ids= r.RightId+ r.KeyCode,
Name= r.Name,
KeyCode =r.KeyCode,
IsValid=r.isvalid,
RightId=r.RightId
}).ToArray() }; return Json(json);
}
//保存
[HttpPost]
[SupportFilter(ActionName = "Save")]
public int UpdateRight(SysRightOperateModel model)
{
return sysRightBLL.UpdateRight(model);
} }
}
Controller
@using App.Common;
@using App.Admin;
@{
ViewBag.Title = "角色授权设置";
Layout = "~/Views/Shared/_Index_Layout.cshtml";
List<App.Models.Sys.permModel> perm = (List<App.Models.Sys.permModel>)ViewBag.Perm; if (perm == null)
{
perm = new List<App.Models.Sys.permModel>();
}
}
<div class="mvctool">
@Html.ToolButton("btnSave", "icon-save", "保存", perm, "Save", true)
</div>
<table style="width: 100%">
<tbody>
<tr>
<td style="width: 420px; padding-right: 3px; vertical-align: top">
<table id="roleList"></table>
</td>
<td style="width: 200px; padding-right: 3px; vertical-align: top">
<table id="moduleList"></table>
</td>
<td>
<table id="operateList"></table>
</td>
</tr>
</tbody>
</table> <script type="text/javascript">
$(function () {
var curModuleId, curRoleId, curModuleName, curRoleName, curSystemId, curSystemName;//选择的模块ID,选中的角色ID,选中的模块名称,角色名称
curRoleName = "?";
curModuleName = "?";
$('#roleList').datagrid({
url: '@Url.Action("GetRoleList")',
width: ,
methord: 'post',
height: $(window).height() - ,
fitColumns: true,
sortName: 'CreateTime',
sortOrder: 'desc',
idField: 'Id',
pageSize: ,
pageList: [, , , , ],
pagination: true,
striped: true, //奇偶行是否区分
singleSelect: true,//单选模式
rownumbers: true,//行号
title: '角色列表',
columns: [[
{ field: 'Id', title: '', width: , hidden: true },
{ field: 'Name', title: '角色组', width: , sortable: true },
{ field: 'Description', title: '说明', width: , sortable: true },
{ field: 'CreateTime', title: '创建时间', width: , sortable: true },
{ field: 'CreatePerson', title: '', width: , sortable: true, hidden: true }
]],
onClickRow: function (index, data) {
var row = $('#roleList').datagrid('getSelected');
if (row != null) {
curRoleName = row.Name;
curRoleId = row.Id;
$('#operateList').datagrid({ url: "/SysRight/GetRightByRoleAndModule?roleId=" + curRoleId + "&moduleId=" + curModuleId + "" });
$('#operateList').datagrid({ 'title': "角色组: " + curRoleName + " >> 模块:" + curModuleName });
}
}
});
$('#moduleList').treegrid({
url: '@Url.Action("GetModelList")',
width: ,
methord: 'post',
height: $(window).height() - ,
fitColumns: true,
treeField: 'Name',
idField: 'Id',
pagination: false,
striped: true, //奇偶行是否区分
singleSelect: true,//单选模式
title: '模块列表',
columns: [[
{ field: 'Id', title: '唯一标识', width: , hidden: true },
{ field: 'Name', title: '名称', width: , sortable: true },
{ field: 'EnglishName', title: '英文名称', width: , sortable: true, hidden: true },
{ field: 'ParentId', title: '上级Id', width: , sortable: true, hidden: true },
{ field: 'Url', title: '链接地址', width: , sortable: true, hidden: true },
{ field: 'Iconic', title: '图标', width: , sortable: true, hidden: true },
{ field: 'Sort', title: '排序号', width: , sortable: true, hidden: true },
{ field: 'Remark', title: '说明', width: , sortable: true, hidden: true },
{
field: 'Enable', title: '是否启用', width: , align: 'center', formatter: function (value) {
if (value) {
return "<img src='/Content/Images/icon/pass.png'/>";
} else {
return "<img src='/Content/Images/icon/no.png'/>";
}
}, hidden: true
},
{ field: 'CreatePerson', title: '创建人', width: , sortable: true, hidden: true },
{ field: 'CreateTime', title: '创建时间', width: , sortable: true, hidden: true },
{
field: 'IsLast', title: '是否最后一项', align: 'center', width: , formatter: function (value) {
if (value) {
return "是";
} else {
return "否";
}
}, hidden: true
},
]],
onClickRow: function (index, data) {
var row = $('#moduleList').treegrid('getSelected');
if (row != null) {
curModuleName = row.Name;
curModuleId = row.Id;
if (curRoleId == null && row.IsLast) {
$.messageBox5s('提示', "请再选择一个角色!");
return;
}
$('#operateList').datagrid({ url: "/SysRight/GetRightByRoleAndModule?roleId=" + curRoleId + "&moduleId=" + curModuleId + "" });
$('#operateList').datagrid({ 'title': "角色组: " + curRoleName + " >> 模块:" + (row.IsLast ? curModuleName : "[请再选择最后菜单项]") });
} }
});
$('#operateList').datagrid({
url: '@Url.Action("GetRightByRoleAndModule")',
width: $(window).width() - ,
methord: 'post',
height: $(window).height() - ,
fitColumns: true,
sortName: 'CreateTime',
sortOrder: 'desc',
idField: 'Id',
striped: true, //奇偶行是否区分
singleSelect: true,//单选模式
title: '授权操作',
//rownumbers: true,//行号
columns: [[
{ field: 'Ids', title: 'Ids', width: , hidden: true },
{ field: 'Name', title: '名称', width: , sortable: true },
{ field: 'KeyCode', title: '操作码', width: , sortable: true },
{
field: 'IsValid', title: "<a href='#' title='@Suggestion.Select' onclick=\"SelAll();\" ><img src='/Content/Images/icon/select.gif'></a> <a href='#' title='@Suggestion.UnSelect' onclick=\"UnSelAll();\" ><img src='/Content/Images/icon/unselect.gif'></a>", align: 'center', width: , formatter: function (value) {
if (value) {
return "<input type='checkbox' checked='checked' value=" + value + " />";
} else {
return "<input type='checkbox' value=" + value + " />";
}
},
},
{ field: 'RightId', title: '模块ID', width: , sortable: true, hidden: true }
]]
});
$("#btnSave").click(function () {
var updateRows = ;
var rows = $("#operateList").datagrid("getRows"); //这段代码是获取当前页的所有行。
for (var i = ; i < rows.length; i++) {
var setFlag = $("td[field='IsValid'] input").eq(i).prop("checked");
if (rows[i].IsValid != setFlag)//判断是否有作修改
{
$.post("@Url.Action("UpdateRight")", { "Id": rows[i].Ids, "RightId": rows[i].RightId, "KeyCode": rows[i].KeyCode, "IsValid": setFlag }, "json");
updateRows++;
}
}
if (updateRows > ) {
$.messageBox5s('提示', '保存成功!');
} else {
$.messageBox5s('提示', '@Suggestion.NoAnyChanges!');
} });
$(window).resize(function () {
$('#operateList').datagrid('resize', {
width: $(window).width() - ,
height: $(window).height() -
}).datagrid('resize', {
width: $(window).width() - ,
height: $(window).height() -
});
$('#moduleList,#roleList').datagrid('resize', {
height: $(window).height() -
}).datagrid('resize', {
height: $(window).height() -
});
});
}); function SelAll() {
$("td[field='IsValid'] input").prop("checked", true);
$("#btnSave").trigger("click");
return;
}
function UnSelAll() {
$("td[field='IsValid'] input").prop("checked", false);
$("#btnSave").trigger("click");
return;
} </script>
Index
最后效果图
这次发布还是做得比较认真的。大家可以详细细读代码和存储过程。不清楚的欢迎留言,必定回答
接下来是讲角色和用户的互相授权,有兴趣的朋友可以先做做看。
最后更新2个js方法来替换DataGrid中的width和height计算
function SetGridWidthSub(w)
{
return $(window).width() - w;
}
function SetGridHeightSub(h) {
return $(window).height() - h
}
构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(24)-权限管理系统-将权限授权给角色的更多相关文章
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(1)-前言与目录(持续更新中...)
转自:http://www.cnblogs.com/ymnets/p/3424309.html 曾几何时我想写一个系列的文章,但是由于工作很忙,一直没有时间更新博客.博客园园龄都1年了,却一直都是空空 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(48)-工作流设计-起草新申请 系列目录 创建新表单之后,我们就可以起草申请了,申请按照严格的表单步骤和分 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(47)-工作流设计-补充
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(47)-工作流设计-补充 系列目录 补充一下,有人要表单的代码,这个用代码生成器生成表Flow_Form表 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(46)-工作流设计-设计分支
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(46)-工作流设计-设计分支 系列目录 步骤设置完毕之后,就要设置好流转了,比如财务申请大于50000元( ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(45)-工作流设计-设计步骤 系列目录 步骤设计很重要,特别是规则的选择. 我这里分为几个规则 1.按自行 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(44)-工作流设计-设计表单 系列目录 设计表单是比较复杂的一步,完成一个表单的设计其实很漫长,主要分为四 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(43)-工作流设计-字段分类设计 系列目录 建立好42节的表之后,每个字段英文表示都是有意义的说明.先建立 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(42)-工作流设计01 工作流在实际应用中还是比较广泛,网络中存在很多工作流的图形化插件,可以做到拉拽的工 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(40)-精准在线人数统计实现-【过滤器+Cache】
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(40)-精准在线人数统计实现-[过滤器+Cache] 系列目录 上次的探讨没有任何结果,我浏览了大量的文章 ...
- 构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构
原文:构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(41)-组织架构 本节开始我们要实现工作流,此工作流可以和之前的所有章节脱离关系,也可以紧密合并. 我们当 ...
随机推荐
- 浅谈WEB页面提速(前端向)
记得面试现在这份工作的时候,一位领导语重心长地谈道——当今的世界是互联网的世界,IT企业之间的竞争是很激烈的,如果一个网页的加载和显示速度,相比别人的站点页面有那么0.1秒的提升,那也是很大的一个成就 ...
- 0.Win8.1,Win10,Windows Server 2012 安装 Net Framework 3.5
后期会在博客首发更新:http://dnt.dkill.net 网站部署之~Windows Server | 本地部署:http://www.cnblogs.com/dunitian/p/482280 ...
- Minor【 PHP框架】1.简介
1.1 Minor是什么 Minor是一个简单但是优秀的符合PSR4的PHP框架,It just did what a framework should do. 只做一个框架应该做的,简单而又强大! ...
- 从零开始编写自己的C#框架(26)——小结
一直想写个总结,不过实在太忙了,所以一直拖啊拖啊,拖到现在,不过也好,有了这段时间的沉淀,发现自己又有了小小的进步.哈哈...... 原想框架开发的相关开发步骤.文档.代码.功能.部署等都简单的讲过了 ...
- 谈谈一些有趣的CSS题目(六)-- 全兼容的多列均匀布局问题
开本系列,谈谈一些有趣的 CSS 题目,题目类型天马行空,想到什么说什么,不仅为了拓宽一下解决问题的思路,更涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题 ...
- 微框架spark--api开发利器
spark简介 Spark(注意不要同Apache Spark混淆)的设计初衷是,可以简单容易地创建REST API或Web应用程序.它是一个灵活.简洁的框架,大小只有1MB.Spark允许用户自己选 ...
- 【绝对干货】仿微信QQ设置图形头像裁剪,让你的App从此炫起来~
最近在做毕业设计,想有一个功能和QQ一样可以裁剪头像并设置圆形头像,额,这是设计狮的一种潮流. 而纵观现在主流的APP,只要有用户系统这个功能,这个需求一般都是在(bu)劫(de)难(bu)逃(xue ...
- ASP.NET Core 中文文档 第四章 MVC(4.1)Controllers, Actions 和 Action Results
原文:Controllers, Actions, and Action Results 作者:Steve Smith 翻译:姚阿勇(Dr.Yao) 校对:许登洋(Seay) Action 和 acti ...
- Linux设备管理(四)_从sysfs回到ktype
sysfs是一个基于ramfs的文件系统,在2.6内核开始引入,用来导出内核对象(kernel object)的数据.属性到用户空间.与同样用于查看内核数据的proc不同,sysfs只关心具有层次结构 ...
- 【流量劫持】SSLStrip 的未来 —— HTTPS 前端劫持
前言 在之前介绍的流量劫持文章里,曾提到一种『HTTPS 向下降级』的方案 -- 将页面中的 HTTPS 超链接全都替换成 HTTP 版本,让用户始终以明文的形式进行通信. 看到这,也许大家都会想到一 ...