Added non-generic registration interface to IKernel and IWindsor to accommodate dynam...
[castle.git] / MonoRail / Castle.MonoRail.Framework / ViewComponents / SecurityComponent.cs
blobc23df0e3063c021a35c6e1f3018727e1d05b02d5
1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.MonoRail.Framework.ViewComponents
17 /// <summary>
18 /// Only renders the body if the current user has the specified role
19 /// <example>
20 /// <code>
21 /// #blockcomponent(SecurityComponent with "role=IsAdmin")
22 /// Content only available to admin
23 /// #end
24 /// </code>
25 /// </example>
26 /// <para>or for multiple roles (using "or")</para>
27 /// <example>
28 /// <code>
29 /// #blockcomponent(SecurityComponent with "roles=Manager,Admin")
30 /// Content only available to admin or managers
31 /// #end
32 /// </code>
33 /// </example>
34 /// </summary>
35 public class SecurityComponent : ViewComponent
37 private bool shouldRender;
39 /// <summary>
40 /// Called by the framework once the component instance
41 /// is initialized
42 /// </summary>
43 public override void Initialize()
45 string role = (string) ComponentParams["role"];
46 string roles = (string) ComponentParams["roles"];
48 if (role == null && roles == null)
50 throw new MonoRailException("SecurityComponent: you must supply a role (or roles) parameter");
53 shouldRender = false;
55 if (EngineContext.CurrentUser != null)
57 if (role != null)
59 shouldRender = EngineContext.CurrentUser.IsInRole(role);
61 else
63 foreach(string itRole in roles.Split(','))
65 if (EngineContext.CurrentUser.IsInRole(itRole.Trim()))
67 shouldRender = true;
68 break;
75 /// <summary>
76 /// Called by the framework so the component can
77 /// render its content
78 /// </summary>
79 public override void Render()
81 if (shouldRender)
83 Context.RenderBody();