您的位置:首页 > 编程语言 > ASP

ASP.NET MVC 生成简单的 xml 权限结构

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

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

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

[ParentModuleAttribute(ParentSystemModules.CustomerManage, "普通客户")]
public ActionResult OrdinaryCustomer ()
{
return View();
}

[ParentModuleAttribute(ParentSystemModules.CustomerManage, "VIP客户")]
public ActionResult VIPCustomer()
{
return View();
}

[ParentModuleAttribute(ParentSystemModules.TechnicalManage, "SQLServer")]
public ActionResult SQLSERVERManager()
{
return View();
}

[ParentModuleAttribute(ParentSystemModules.TechnicalManage, "ASP.NETMVC")]
public ActionResult MVCManager()
{
return View();
}


ParentModule 自定义特性:

[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public class ParentModuleAttribute : Attribute
{

private ParentSystemModules _mType;
private string _moduleTitle;

public ParentModuleAttribute(ParentSystemModules mType, string moduleTitle)
{
_mType = mType;
_moduleTitle = moduleTitle;
}

public ParentSystemModules ModuleType
{
get
{
return _mType;
}
}

public string ModuleTitle
{
get
{
return _moduleTitle;
}
}

}


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

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

生成XML权限结构。

截图如下:





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



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

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

本文的例子请从 这里 下载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐