Almost eliminate wrong use of Constants.WsaNamespace.
[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 using System.Security.Principal;
36 using System.Web.Security;
37
38 namespace System.Web.UI.WebControls
39 {
40         // CAS
41         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
43         // attributes
44         [DefaultEvent ("ViewChanged")]
45         [DefaultProperty ("CurrentView")]
46         [Designer ("System.Web.UI.Design.WebControls.LoginViewDesigner," + Consts.AssemblySystem_Design)]
47         [ParseChildren (true)]
48         [PersistChildren (false)]
49         [Themeable (true)]
50         [Bindable (true)]
51         public class LoginView : Control, INamingContainer 
52         {
53                 static readonly object viewChangedEvent = new object ();
54                 static readonly object viewChangingEvent = new object ();
55
56                 ITemplate anonymousTemplate;
57                 ITemplate loggedInTemplate;
58                 bool isAuthenticated;
59                 bool theming;
60                 RoleGroupCollection coll;
61
62                 public LoginView ()
63                 {
64                         theming = true;
65                 }
66
67
68                 [Browsable (false)]
69                 [DefaultValue (null)]
70                 [PersistenceMode (PersistenceMode.InnerProperty)]
71                 [TemplateContainer (typeof (LoginView))]
72                 public virtual ITemplate AnonymousTemplate {
73                         get { return anonymousTemplate; }
74                         set { anonymousTemplate = value; }
75                 }
76
77                 public override ControlCollection Controls {
78                         get {
79                                 EnsureChildControls();
80                                 return base.Controls;
81                         }
82                 }
83
84                 [Browsable (true)]
85                 public override bool EnableTheming {
86                         get { return theming; }
87                         set { theming = value; }
88                 }
89
90                 [Browsable (false)]
91                 [DefaultValue (null)]
92                 [PersistenceMode (PersistenceMode.InnerProperty)]
93                 [TemplateContainer (typeof (LoginView))]
94                 public virtual ITemplate LoggedInTemplate {
95                         get { return loggedInTemplate; }
96                         set { loggedInTemplate = value; }
97                 }
98
99                 [Filterable (false)]
100                 [MergableProperty (false)]
101                 [PersistenceMode (PersistenceMode.InnerProperty)]
102                 [Themeable (false)]
103                 public RoleGroupCollection RoleGroups {
104                         get {
105                                 if (coll == null)
106                                         coll = new RoleGroupCollection ();
107                                 return coll;
108                         }
109                 }
110
111                 [Browsable (true)]
112                 public override string SkinID {
113                         get { return base.SkinID; }
114                         set { base.SkinID = value; }
115                 }
116
117                 bool IsAuthenticated {
118                         get {
119                                 return isAuthenticated;
120                         }
121                         set {
122                                 if (value == isAuthenticated)
123                                         return;
124                                 
125                                 isAuthenticated = value;
126
127                                 OnViewChanging (EventArgs.Empty);
128                                 ChildControlsCreated = false;
129                                 OnViewChanged (EventArgs.Empty);
130                         }
131                 }
132
133                 ITemplate GetTemplateFromRoleGroup (RoleGroup rg, IPrincipal user)
134                 {
135                         if (user == null)
136                                 return null;
137                         
138                         foreach (string role in rg.Roles) {
139                                 if (user.IsInRole (role))
140                                         return rg.ContentTemplate;
141                         }
142                         
143                         return null;
144                 }
145                 
146                 protected internal override void CreateChildControls ()
147                 {
148                         Controls.Clear ();
149                         Control c = new Control ();
150                         ITemplate template = null;
151                         
152                         if (Page != null && Page.Request.IsAuthenticated) {
153                                 isAuthenticated = true;
154
155                                 RoleGroupCollection rgc;
156                                 HttpContext ctx = HttpContext.Current;
157                                 IPrincipal user = ctx != null ? ctx.User : null;
158
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)
163                                                         break;
164                                         }
165                                 }
166
167                                 if (template == null)
168                                         template = LoggedInTemplate;
169                         } else {
170                                 isAuthenticated = false;
171                                 template = AnonymousTemplate;
172                         }
173
174                         if (template != null)
175                                 template.InstantiateIn (c);
176                         Controls.Add (c);
177                 }
178
179                 public override void DataBind ()
180                 {
181                         EventArgs args = EventArgs.Empty;
182                         OnDataBinding (args);
183                         EnsureChildControls ();
184                         DataBindChildren ();
185                 }
186
187                 [EditorBrowsable (EditorBrowsableState.Never)]
188                 public override void Focus ()
189                 {
190                         // LAMESPEC: throw new InvalidOperationException ();
191                         throw new NotSupportedException ();
192                 }
193
194                 protected internal override void LoadControlState (object savedState)
195                 {
196                         if (savedState == null)
197                                 return;
198
199                         isAuthenticated = (bool) savedState;
200                 }
201
202                 protected internal override void OnInit (EventArgs e)
203                 {
204                         base.OnInit (e);
205                         if (Page != null)
206                                 Page.RegisterRequiresControlState(this);
207                 }
208
209                 protected internal override void OnPreRender (EventArgs e)
210                 {
211                         base.OnPreRender (e);
212                         if (Page != null)
213                                 IsAuthenticated = Page.Request.IsAuthenticated;
214                 }
215
216                 protected virtual void OnViewChanged (EventArgs e)
217                 {
218                         EventHandler h = (EventHandler)Events [viewChangedEvent];
219                         if (h != null)
220                                 h (this, e);
221                 }
222
223                 protected virtual void OnViewChanging (EventArgs e)
224                 {
225                         EventHandler h = (EventHandler)Events [viewChangingEvent];
226                         if (h != null)
227                                 h (this, e);
228                 }
229
230                 protected internal override void Render(HtmlTextWriter writer) {
231                         EnsureChildControls();
232                         base.Render (writer);
233                 }
234
235                 protected internal override object SaveControlState ()
236                 {
237                         if (isAuthenticated)
238                                 return isAuthenticated;
239
240                         return null;
241                 }
242
243                 [MonoTODO ("for design-time usage - no more details available")]
244                 protected override void SetDesignModeState (IDictionary data)
245                 {
246                         base.SetDesignModeState (data);
247                 }
248
249
250                 // events
251
252                 public event EventHandler ViewChanged {
253                         add { Events.AddHandler (viewChangedEvent, value); }
254                         remove { Events.RemoveHandler (viewChangedEvent, value); }
255                 }
256
257                 public event EventHandler ViewChanging {
258                         add { Events.AddHandler (viewChangingEvent, value); }
259                         remove { Events.RemoveHandler (viewChangingEvent, value); }
260                 }
261         }
262 }
263
264 #endif