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