1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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
18 /// Only renders the body if the current user has the specified role
21 /// #blockcomponent(SecurityComponent with "role=IsAdmin")
22 /// Content only available to admin
26 /// <para>or for multiple roles (using "or")</para>
29 /// #blockcomponent(SecurityComponent with "roles=Manager,Admin")
30 /// Content only available to admin or managers
35 public class SecurityComponent
: ViewComponent
37 private bool shouldRender
;
40 /// Called by the framework once the component instance
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");
55 if (EngineContext
.CurrentUser
!= null)
59 shouldRender
= EngineContext
.CurrentUser
.IsInRole(role
);
63 foreach(string itRole
in roles
.Split(','))
65 if (EngineContext
.CurrentUser
.IsInRole(itRole
.Trim()))
76 /// Called by the framework so the component can
77 /// render its content
79 public override void Render()