来源: [转载]Asp.Net MVC 权限控制(三):Controller和Action级别控制 – Jetlian – 博客园
再次在重构!这次对Controller和Action进行验证。
思路:系统有很多功能集,功能集对应很多Controller和Action,角色分配很多功能集。
首先构建一个基础数据:
1.功能集初始化:
<div class="container"> <div class="line number1 index0 alt2"><code class="csharp color1">/// <summary></code></div> <div class="line number2 index1 alt1"><code class="csharp spaces"> </code><code class="csharp color1">/// 系统模块</code></div> <div class="line number3 index2 alt2"><code class="csharp spaces"> </code><code class="csharp color1">/// </summary></code></div> <div class="line number4 index3 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">class</code> <code class="csharp plain">SystemModule</code></div> <div class="line number5 index4 alt2"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div> <div class="line number6 index5 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp plain">SystemModule()</code></div> <div class="line number7 index6 alt2"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div> <div class="line number8 index7 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">this</code><code class="csharp plain">.ID = Guid.NewGuid();</code></div> <div class="line number9 index8 alt2"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div> <div class="line number10 index9 alt1"></div> <div class="line number11 index10 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp plain">Guid ID { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div> <div class="line number12 index11 alt1"></div> <div class="line number13 index12 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">string</code> <code class="csharp plain">Name { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div> <div class="line number14 index13 alt1"></div> <div class="line number15 index14 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">string</code> <code class="csharp plain">Description { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div> <div class="line number16 index15 alt1"></div> <div class="line number17 index16 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp plain">SystemModule Parent { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div> <div class="line number18 index17 alt1"></div> <div class="line number19 index18 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp plain">List<SystemModuleController> SystemModuleControllers { </code><code class="csharp keyword">get</code><code class="csharp plain">; </code><code class="csharp keyword">set</code><code class="csharp plain">; }</code></div> <div class="line number20 index19 alt1"></div> <div class="line number21 index20 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">public</code> <code class="csharp keyword">static</code> <code class="csharp plain">List<SystemModule> Init()</code></div> <div class="line number22 index21 alt1"><code class="csharp spaces"> </code><code class="csharp plain">{</code></div> <div class="line number23 index22 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">var</code> <code class="csharp plain">m1 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModule { Name = </code><code class="csharp string">"资源监测"</code> <code class="csharp plain">};</code></div> <div class="line number24 index23 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">var</code> <code class="csharp plain">m2 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModule { Name = </code><code class="csharp string">"规划管理"</code> <code class="csharp plain">};</code></div> <div class="line number25 index24 alt2"></div> <div class="line number26 index25 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">var</code> <code class="csharp plain">c1 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModuleController { ControllerName = </code><code class="csharp string">"PlanManagement"</code><code class="csharp plain">, ActionName = </code><code class="csharp string">"Search"</code> <code class="csharp plain">};</code></div> <div class="line number27 index26 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">var</code> <code class="csharp plain">c2 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModuleController { ControllerName = </code><code class="csharp string">"PlanManagement"</code><code class="csharp plain">, ActionName = </code><code class="csharp string">"Add"</code> <code class="csharp plain">};</code></div> <div class="line number28 index27 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">var</code> <code class="csharp plain">c3 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModuleController { ControllerName = </code><code class="csharp string">"PlanManagement"</code><code class="csharp plain">, ActionName = </code><code class="csharp string">"Edit"</code> <code class="csharp plain">};</code></div> <div class="line number29 index28 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">var</code> <code class="csharp plain">c4 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModuleController { ControllerName = </code><code class="csharp string">"PlanManagement"</code><code class="csharp plain">, ActionName = </code><code class="csharp string">"Delete"</code> <code class="csharp plain">};</code></div> <div class="line number30 index29 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">var</code> <code class="csharp plain">c5 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModuleController { ControllerName = </code><code class="csharp string">"PlanManagement"</code><code class="csharp plain">, ActionName = </code><code class="csharp string">"Approval"</code> <code class="csharp plain">};</code></div> <div class="line number31 index30 alt2"></div> <div class="line number32 index31 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">var</code> <code class="csharp plain">m21 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModule { Name = </code><code class="csharp string">"规划信息查询"</code><code class="csharp plain">, Parent = m2, SystemModuleControllers = </code><code class="csharp keyword">new</code> <code class="csharp plain">List<SystemModuleController> { c1 } };</code></div> <div class="line number33 index32 alt2"><code class="csharp spaces"> </code><code class="csharp keyword">var</code> <code class="csharp plain">m22 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModule { Name = </code><code class="csharp string">"规划信息管理"</code><code class="csharp plain">, Parent = m2, SystemModuleControllers = </code><code class="csharp keyword">new</code> <code class="csharp plain">List<SystemModuleController> { c2, c3, c4 } };</code></div> <div class="line number34 index33 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">var</code> <code class="csharp plain">m23 = </code><code class="csharp keyword">new</code> <code class="csharp plain">SystemModule { Name = </code><code class="csharp string">"规划辅助审批"</code><code class="csharp plain">, Parent = m2, SystemModuleControllers = </code><code class="csharp keyword">new</code> <code class="csharp plain">List<SystemModuleController> { c5 } };</code></div> <div class="line number35 index34 alt2"></div> <div class="line number36 index35 alt1"><code class="csharp spaces"> </code><code class="csharp keyword">return</code> <code class="csharp keyword">new</code> <code class="csharp plain">List<SystemModule> { m1, m2, m12, m21, m22, m23 };</code></div> <div class="line number37 index36 alt2"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div> <div class="line number38 index37 alt1"><code class="csharp spaces"> </code><code class="csharp plain">}</code></div> <div class="line number38 index37 alt1">