2 // System.Web.UI.WebControls.LoginView class
5 // Sebastien Pouliot <sebastien@ximian.com>
6 // Konstantin Triger <kostat@mainsoft.com>
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Security.Permissions;
35 using System.Security.Principal;
36 using System.Web.Security;
38 namespace System.Web.UI.WebControls {
41 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42 [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
44 [DefaultEvent ("ViewChanged")]
45 [DefaultProperty ("CurrentView")]
46 [Designer ("System.Web.UI.Design.WebControls.LoginViewDesigner," + Consts.AssemblySystem_Design)]
47 [ParseChildren (true)]
48 [PersistChildren (false)]
50 public class LoginView : Control, INamingContainer
52 static readonly object viewChangedEvent = new object ();
53 static readonly object viewChangingEvent = new object ();
55 ITemplate anonymousTemplate;
56 ITemplate loggedInTemplate;
59 RoleGroupCollection coll;
69 [PersistenceMode (PersistenceMode.InnerProperty)]
70 [TemplateContainer (typeof (LoginView))]
71 public virtual ITemplate AnonymousTemplate {
72 get { return anonymousTemplate; }
73 set { anonymousTemplate = value; }
76 public override ControlCollection Controls {
78 EnsureChildControls();
84 public override bool EnableTheming {
85 get { return theming; }
86 set { theming = value; }
91 [PersistenceMode (PersistenceMode.InnerProperty)]
92 [TemplateContainer (typeof (LoginView))]
93 public virtual ITemplate LoggedInTemplate {
94 get { return loggedInTemplate; }
95 set { loggedInTemplate = value; }
99 [MergableProperty (false)]
100 [PersistenceMode (PersistenceMode.InnerProperty)]
102 public RoleGroupCollection RoleGroups {
105 coll = new RoleGroupCollection ();
111 public override string SkinID {
112 get { return base.SkinID; }
113 set { base.SkinID = value; }
116 bool IsAuthenticated {
118 return isAuthenticated;
121 if (value == isAuthenticated)
124 isAuthenticated = value;
126 OnViewChanging (EventArgs.Empty);
127 ChildControlsCreated = false;
128 OnViewChanged (EventArgs.Empty);
132 ITemplate GetTemplateFromRoleGroup (RoleGroup rg, IPrincipal user)
137 foreach (string role in rg.Roles) {
138 if (user.IsInRole (role))
139 return rg.ContentTemplate;
145 protected internal override void CreateChildControls ()
148 Control c = new Control ();
149 ITemplate template = null;
151 if (Page != null && Page.Request.IsAuthenticated) {
152 isAuthenticated = true;
154 RoleGroupCollection rgc;
155 HttpContext ctx = HttpContext.Current;
156 IPrincipal user = ctx != null ? ctx.User : null;
158 if (Roles.Enabled && (rgc = RoleGroups) != null && rgc.Count > 0) {
159 foreach (RoleGroup rg in rgc) {
160 template = GetTemplateFromRoleGroup (rg, user);
161 if (template != null)
166 if (template == null)
167 template = LoggedInTemplate;
169 isAuthenticated = false;
170 template = AnonymousTemplate;
173 if (template != null)
174 template.InstantiateIn (c);
178 public override void DataBind ()
180 EventArgs args = EventArgs.Empty;
181 OnDataBinding (args);
182 EnsureChildControls ();
186 [EditorBrowsable (EditorBrowsableState.Never)]
187 public override void Focus ()
189 // LAMESPEC: throw new InvalidOperationException ();
190 throw new NotSupportedException ();
193 protected internal override void LoadControlState (object savedState)
195 if (savedState == null)
198 isAuthenticated = (bool) savedState;
201 protected internal override void OnInit (EventArgs e)
205 Page.RegisterRequiresControlState(this);
208 protected internal override void OnPreRender (EventArgs e)
210 base.OnPreRender (e);
212 IsAuthenticated = Page.Request.IsAuthenticated;
215 protected virtual void OnViewChanged (EventArgs e)
217 EventHandler h = (EventHandler)Events [viewChangedEvent];
222 protected virtual void OnViewChanging (EventArgs e)
224 EventHandler h = (EventHandler)Events [viewChangingEvent];
229 protected internal override void Render(HtmlTextWriter writer) {
230 EnsureChildControls();
231 base.Render (writer);
234 protected internal override object SaveControlState ()
237 return isAuthenticated;
242 [MonoTODO ("for design-time usage - no more details available")]
243 protected override void SetDesignModeState (IDictionary data)
245 base.SetDesignModeState (data);
251 public event EventHandler ViewChanged {
252 add { Events.AddHandler (viewChangedEvent, value); }
253 remove { Events.RemoveHandler (viewChangedEvent, value); }
256 public event EventHandler ViewChanging {
257 add { Events.AddHandler (viewChangingEvent, value); }
258 remove { Events.RemoveHandler (viewChangingEvent, value); }