Page.Validate() is called when CausesValidation=true
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / LoginView.cs
1 //
2 // System.Web.UI.WebControls.LoginView class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //      Konstantin Triger  <kostat@mainsoft.com>
7 //
8 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
9 //
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:
17 // 
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 // 
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.
28 //
29
30 #if NET_2_0
31
32 using System.Collections;
33 using System.ComponentModel;
34 using System.Security.Permissions;
35
36 namespace System.Web.UI.WebControls {
37
38         // CAS
39         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         // attributes
42         [DefaultEvent ("ViewChanged")]
43         [DefaultProperty ("CurrentView")]
44         [Designer ("System.Web.UI.Design.WebControls.LoginViewDesigner," + Consts.AssemblySystem_Design)]
45         [ParseChildren (true)]
46         [PersistChildren (false)]
47         [Themeable (true)]
48         public class LoginView : Control, INamingContainer {
49
50                 private static readonly object viewChangedEvent = new object ();
51                 private static readonly object viewChangingEvent = new object ();
52
53                 private ITemplate anonymousTemplate;
54                 private ITemplate loggedInTemplate;
55                 private bool isAuthenticated;
56                 private bool theming;
57                 private RoleGroupCollection coll;
58
59
60                 public LoginView ()
61                 {
62                         theming = true;
63                 }
64
65
66                 [Browsable (false)]
67                 [DefaultValue (null)]
68                 [PersistenceMode (PersistenceMode.InnerProperty)]
69                 [TemplateContainer (typeof (LoginView))]
70                 public virtual ITemplate AnonymousTemplate {
71                         get { return anonymousTemplate; }
72                         set { anonymousTemplate = value; }
73                 }
74
75                 public override ControlCollection Controls {
76                         get {
77                                 EnsureChildControls();
78                                 return base.Controls;
79                         }
80                 }
81
82                 [Browsable (true)]
83                 public override bool EnableTheming {
84                         get { return theming; }
85                         set { theming = value; }
86                 }
87
88                 [Browsable (false)]
89                 [DefaultValue (null)]
90                 [PersistenceMode (PersistenceMode.InnerProperty)]
91                 [TemplateContainer (typeof (LoginView))]
92                 public virtual ITemplate LoggedInTemplate {
93                         get { return loggedInTemplate; }
94                         set { loggedInTemplate = value; }
95                 }
96
97                 [Filterable (false)]
98                 [MergableProperty (false)]
99                 [PersistenceMode (PersistenceMode.InnerProperty)]
100                 [Themeable (false)]
101                 public RoleGroupCollection RoleGroups {
102                         get {
103                                 if (coll == null)
104                                         coll = new RoleGroupCollection ();
105                                 return coll;
106                         }
107                 }
108
109                 [Browsable (true)]
110                 public override string SkinID {
111                         get { return base.SkinID; }
112                         set { base.SkinID = value; }
113                 }
114
115                 bool IsAuthenticated {
116                         get {
117                                 if (Page != null && !Page.IsPostBack)
118                                         isAuthenticated = Page.Request.IsAuthenticated;
119
120                                 return isAuthenticated;
121                         }
122                 }
123
124                 [MonoTODO("Handle RoleGroups")]
125                 protected internal override void CreateChildControls ()
126                 {
127                         Controls.Clear();
128                         Control c = new Control();
129                         if (IsAuthenticated)
130                                 LoggedInTemplate.InstantiateIn (c);
131                         else
132                                 AnonymousTemplate.InstantiateIn (c);
133
134                         Controls.Add(c);
135                 }
136
137                 public override void DataBind ()
138                 {
139                         EventArgs args = EventArgs.Empty;
140                         OnDataBinding (args);
141                         EnsureChildControls ();
142                         DataBindChildren ();
143                 }
144
145                 [EditorBrowsable (EditorBrowsableState.Never)]
146                 public override void Focus ()
147                 {
148                         // LAMESPEC: throw new InvalidOperationException ();
149                         throw new NotSupportedException ();
150                 }
151
152                 protected internal override void LoadControlState (object savedState)
153                 {
154                         if (savedState == null) {
155                                 base.LoadControlState (savedState);
156                                 return;
157                         }
158
159                         Pair pair = (Pair)savedState;
160                         base.LoadControlState (pair.First);
161                         isAuthenticated = (bool)pair.Second;
162                 }
163
164                 protected internal override void OnInit (EventArgs e)
165                 {
166                         base.OnInit (e);
167                         if (Page != null)
168                                 Page.RegisterRequiresControlState(this);
169                 }
170
171                 protected internal override void OnPreRender (EventArgs e)
172                 {
173                         base.OnPreRender (e);
174                         isAuthenticated = IsAuthenticated;
175                         EnsureChildControls ();
176                 }
177
178                 protected virtual void OnViewChanged (EventArgs e)
179                 {
180                         EventHandler h = (EventHandler)Events [viewChangedEvent];
181                         if (h != null)
182                                 h (this, e);
183                 }
184
185                 protected virtual void OnViewChanging (EventArgs e)
186                 {
187                         EventHandler h = (EventHandler)Events [viewChangingEvent];
188                         if (h != null)
189                                 h (this, e);
190                 }
191
192                 protected internal override void Render(HtmlTextWriter writer) {
193                         EnsureChildControls();
194                         base.Render (writer);
195                 }
196
197                 protected internal override object SaveControlState ()
198                 {
199                         object baseState = base.SaveControlState ();
200                         if (isAuthenticated)
201                                 return new Pair (baseState, isAuthenticated);
202
203                         return baseState;
204                 }
205
206                 [MonoTODO ("for design-time usage - no more details available")]
207                 protected override void SetDesignModeState (IDictionary data)
208                 {
209                         base.SetDesignModeState (data);
210                 }
211
212
213                 // events
214
215                 public event EventHandler ViewChanged {
216                         add { Events.AddHandler (viewChangedEvent, value); }
217                         remove { Events.RemoveHandler (viewChangedEvent, value); }
218                 }
219
220                 public event EventHandler ViewChanging {
221                         add { Events.AddHandler (viewChangingEvent, value); }
222                         remove { Events.RemoveHandler (viewChangingEvent, value); }
223                 }
224         }
225 }
226
227 #endif