Minor changes to improve testability of helpers
[castle.git] / MonoRail / Castle.MonoRail.Framework / ViewComponents / AuthenticatedContent.cs
blobcf744b80940fdf335996799d7f64e3dd5fcac948
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 using System.Security.Principal;
19 /// <summary>
20 /// This component renders different inner
21 /// sections based on the current principal state (authenticated or not)
22 /// <code>
23 /// #blockcomponent(AuthenticatedContent)
24 /// #logged
25 /// Welcome back $context.CurrentUser.Identity.Name
26 /// #end
27 /// #notlogged
28 /// Create your account by clicking here.
29 /// #end
30 /// #end
31 /// </code>
32 /// </summary>
33 public class AuthenticatedContent : ViewComponent
35 private const string LoggedSection = "logged";
36 private const string NotLoggedSection = "notlogged";
38 /// <summary>
39 /// Called by the framework so the component can
40 /// render its content
41 /// </summary>
42 public override void Render()
44 if (IsAuthenticated())
46 Context.RenderSection(LoggedSection);
48 else
50 Context.RenderSection(NotLoggedSection);
54 /// <summary>
55 /// Implementor should return true only if the
56 /// <c>name</c> is a known section the view component
57 /// supports.
58 /// </summary>
59 /// <param name="name">section being added</param>
60 /// <returns>
61 /// <see langword="true"/> if section is supported
62 /// </returns>
63 public override bool SupportsSection(string name)
65 return name == LoggedSection || name == NotLoggedSection;
68 private bool IsAuthenticated()
70 IPrincipal user = RailsContext.CurrentUser;
72 return user != null && user.Identity != null && user.Identity.IsAuthenticated;