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 private static readonly object viewChangedEvent = new object ();
53 private static readonly object viewChangingEvent = new object ();
55 private ITemplate anonymousTemplate;
56 private ITemplate loggedInTemplate;
57 private bool isAuthenticated;
59 private RoleGroupCollection coll;
70 [PersistenceMode (PersistenceMode.InnerProperty)]
71 [TemplateContainer (typeof (LoginView))]
72 public virtual ITemplate AnonymousTemplate {
73 get { return anonymousTemplate; }
74 set { anonymousTemplate = value; }
77 public override ControlCollection Controls {
79 EnsureChildControls();
85 public override bool EnableTheming {
86 get { return theming; }
87 set { theming = value; }
92 [PersistenceMode (PersistenceMode.InnerProperty)]
93 [TemplateContainer (typeof (LoginView))]
94 public virtual ITemplate LoggedInTemplate {
95 get { return loggedInTemplate; }
96 set { loggedInTemplate = value; }
100 [MergableProperty (false)]
101 [PersistenceMode (PersistenceMode.InnerProperty)]
103 public RoleGroupCollection RoleGroups {
106 coll = new RoleGroupCollection ();
112 public override string SkinID {
113 get { return base.SkinID; }
114 set { base.SkinID = value; }
117 bool IsAuthenticated {
119 return isAuthenticated;
122 if (value == isAuthenticated)
125 isAuthenticated = value;
127 OnViewChanging (EventArgs.Empty);
128 ChildControlsCreated = false;
129 OnViewChanged (EventArgs.Empty);
133 private ITemplate GetTemplateFromRoleGroup (RoleGroup rg, IPrincipal user)
138 foreach (string role in rg.Roles) {
139 if (user.IsInRole (role))
140 return rg.ContentTemplate;
146 protected internal override void CreateChildControls ()
149 Control c = new Control ();
150 ITemplate template = null;
152 if (Page != null && Page.Request.IsAuthenticated) {
153 isAuthenticated = true;
155 RoleGroupCollection rgc;
156 HttpContext ctx = HttpContext.Current;
157 IPrincipal user = ctx != null ? ctx.User : null;
159 if (Roles.Enabled && (rgc = RoleGroups) != null && rgc.Count > 0) {
160 foreach (RoleGroup rg in rgc) {
161 template = GetTemplateFromRoleGroup (rg, user);
162 if (template != null)
167 if (template == null)
168 template = LoggedInTemplate;
170 isAuthenticated = false;
171 template = AnonymousTemplate;
174 if (template != null)
175 template.InstantiateIn (c);
179 public override void DataBind ()
181 EventArgs args = EventArgs.Empty;
182 OnDataBinding (args);
183 EnsureChildControls ();
187 [EditorBrowsable (EditorBrowsableState.Never)]
188 public override void Focus ()
190 // LAMESPEC: throw new InvalidOperationException ();
191 throw new NotSupportedException ();
194 protected internal override void LoadControlState (object savedState)
196 if (savedState == null)
199 isAuthenticated = (bool) savedState;
202 protected internal override void OnInit (EventArgs e)
206 Page.RegisterRequiresControlState(this);
209 protected internal override void OnPreRender (EventArgs e)
211 base.OnPreRender (e);
213 IsAuthenticated = Page.Request.IsAuthenticated;
216 protected virtual void OnViewChanged (EventArgs e)
218 EventHandler h = (EventHandler)Events [viewChangedEvent];
223 protected virtual void OnViewChanging (EventArgs e)
225 EventHandler h = (EventHandler)Events [viewChangingEvent];
230 protected internal override void Render(HtmlTextWriter writer) {
231 EnsureChildControls();
232 base.Render (writer);
235 protected internal override object SaveControlState ()
238 return isAuthenticated;
243 [MonoTODO ("for design-time usage - no more details available")]
244 protected override void SetDesignModeState (IDictionary data)
246 base.SetDesignModeState (data);
252 public event EventHandler ViewChanged {
253 add { Events.AddHandler (viewChangedEvent, value); }
254 remove { Events.RemoveHandler (viewChangedEvent, value); }
257 public event EventHandler ViewChanging {
258 add { Events.AddHandler (viewChangingEvent, value); }
259 remove { Events.RemoveHandler (viewChangingEvent, value); }