RyanDing

用编码抒写未来

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

    当我的一个项目到尾声时,发现系统很多模块权限有遗漏。部分模块权限遗漏是由于前期开发速度太快,开发人员未先给模块加权限而后再编写该模块功能代码。当我们检查每个模块权限是否遗漏时,噩梦来了---- 需要花费很长的时间(功能模块太多)。于是乎我写了一个控制台权限同步工具。整体思路如下:

     其实在MVC内每个ActionResult 就可以看成一个新的模块,正因为这样,因此我们可以对所有的模块进行统一管理。由于当前系统中权限树相对简单:一个父级下多个子。所以只要指定好每个ActionResult 的父亲即可。代码如下:

 

其中 ParentModule 这个是自定义特性类似MVC的 [Authorize]。代码如下:

 

1 [ParentModuleAttribute(ParentSystemModules.CustomerManage, "普通客户")]
2 public ActionResult OrdinaryCustomer ()
3 {
4 return View();
5 }
6
7 [ParentModuleAttribute(ParentSystemModules.CustomerManage, "VIP客户")]
8 public ActionResult VIPCustomer()
9 {
10 return View();
11 }
12
13 [ParentModuleAttribute(ParentSystemModules.TechnicalManage, "SQLServer")]
14 public ActionResult SQLSERVERManager()
15 {
16 return View();
17 }
18
19 [ParentModuleAttribute(ParentSystemModules.TechnicalManage, "ASP.NETMVC")]
20 public ActionResult MVCManager()
21 {
22 return View();
23 }

 

ParentModule 自定义特性:

 

1 [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
2 public class ParentModuleAttribute : Attribute
3 {
4
5 private ParentSystemModules _mType;
6 private string _moduleTitle;
7
8 public ParentModuleAttribute(ParentSystemModules mType, string moduleTitle)
9 {
10 _mType = mType;
11 _moduleTitle = moduleTitle;
12 }
13
14 public ParentSystemModules ModuleType
15 {
16 get
17 {
18 return _mType;
19 }
20 }
21
22 public string ModuleTitle
23 {
24 get
25 {
26 return _moduleTitle;
27 }
28 }
29
30 }

其中 ParentSystemModules 是所有的父模块枚举,moduleTitle 为 当前模块名称(可以改造成资源文件,方便日后扩展)。

以上我们每建完一个 ActionResult 随即加上 ParentModuleAttribute 特性。便于下面我的同步工具可以

生成XML权限结构。

截图如下:

查看自动生成的二级xml权限结构:

整个思想主要取决于MVC自身的特点同时还应用到 .NET 特性、 反射、 LINQ 等技术。

同时还增加了检测同一个权限多次被不同的ActionResult(模块)增加的错误。

本文的例子请从 这里 下载。

posted on 2010-10-26 14:34  ryanding  阅读(2007)  评论(5编辑  收藏  举报