svn path=/branches/mono-1-1-9/mcs/; revision=50438
[mono.git] / mcs / class / System.Web / System.Web.UI.WebControls / LoginStatus.cs
1 //
2 // System.Web.UI.WebControls.LoginStatus class
3 //
4 // Author:
5 //      Sebastien Pouliot  <sebastien@ximian.com>
6 //
7 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 #if NET_2_0
30
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Globalization;
34 using System.Security.Permissions;
35 using System.Web.Security;
36
37 namespace System.Web.UI.WebControls {
38
39         // CAS
40         [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
41         [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
42         [Bindable (false)]
43         [DefaultEvent ("LoggingOut")]
44         [Designer ("System.Web.UI.Design.WebControls.LoginStatusDesigner, " + Consts.AssemblySystem_Design, "System.ComponentModel.Design.IDesigner")]
45         public class LoginStatus : CompositeControl {
46
47                 private static readonly object loggedOutEvent = new object ();
48                 private static readonly object loggingOutEvent = new object ();
49
50                 private LinkButton logoutLinkButton;
51                 private ImageButton logoutImageButton;
52                 private LinkButton loginLinkButton;
53                 private ImageButton loginImageButton;
54
55
56                 public LoginStatus ()
57                 {
58                 }
59
60
61                 [DefaultValue ("")]
62                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
63                 [UrlProperty]
64                 public virtual string LoginImageUrl {
65                         get {
66                                 object o = ViewState ["LoginImageUrl"];
67                                 return (o == null) ? String.Empty : (string) o;
68                         }
69                         set {
70                                 if (value == null)
71                                         ViewState.Remove ("LoginImageUrl");
72                                 else
73                                         ViewState ["LoginImageUrl"] = value;
74                         }
75                 }
76
77                 [Localizable (true)]
78                 public virtual string LoginText {
79                         get {
80                                 object o = ViewState ["LoginText"];
81                                 return (o == null) ? Locale.GetText ("Login") : (string) o;
82                         }
83                         set {
84                                 if (value == null)
85                                         ViewState.Remove ("LoginText");
86                                 else
87                                         ViewState ["LoginText"] = value;
88                         }
89                 }
90
91                 [DefaultValue (LogoutAction.Refresh)]
92                 public virtual LogoutAction LogoutAction {
93                         get {
94                                 object o = ViewState ["LogoutAction"];
95                                 return (o == null) ? LogoutAction.Refresh : (LogoutAction) o;
96                         }
97                         set {
98                                 if ((value < LogoutAction.Refresh) || (value > LogoutAction.RedirectToLoginPage))
99                                         throw new ArgumentOutOfRangeException ("LogoutAction");
100                                 ViewState ["LogoutAction"] = (int) value;
101                         }
102                 }
103
104                 [DefaultValue ("")]
105                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
106                 [UrlProperty]
107                 public virtual string LogoutImageUrl {
108                         get {
109                                 object o = ViewState ["LogoutImageUrl"];
110                                 return (o == null) ? String.Empty : (string) o;
111                         }
112                         set {
113                                 if (value == null)
114                                         ViewState.Remove ("LogoutImageUrl");
115                                 else
116                                         ViewState ["LogoutImageUrl"] = value;
117                         }
118                 }
119
120
121                 [DefaultValue ("")]
122                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
123                 [UrlProperty]
124                 public virtual string LogoutPageUrl {
125                         get {
126                                 object o = ViewState ["LogoutPageUrl"];
127                                 return (o == null) ? String.Empty : (string) o;
128                         }
129                         set {
130                                 if (value == null)
131                                         ViewState.Remove ("LogoutPageUrl");
132                                 else
133                                         ViewState ["LogoutPageUrl"] = value;
134                         }
135                 }
136
137                 [Localizable (true)]
138                 public virtual string LogoutText {
139                         get {
140                                 object o = ViewState ["LogoutText"];
141                                 return (o == null) ? Locale.GetText ("Logout") : (string) o;
142                         }
143                         set {
144                                 if (value == null)
145                                         ViewState.Remove ("LogoutText");
146                                 else
147                                         ViewState ["LogoutText"] = value;
148                         }
149                 }
150
151                 protected override HtmlTextWriterTag TagKey {
152                         get { return HtmlTextWriterTag.A; }
153                 }
154
155
156                 // methods
157
158                 protected internal override void CreateChildControls ()
159                 {
160                         Controls.Clear ();
161
162                         // we create controls for all possibilities
163                         logoutLinkButton = new LinkButton ();
164                         logoutLinkButton.Command += new CommandEventHandler (LogoutClick);
165                         logoutImageButton = new ImageButton ();
166                         logoutImageButton.Command += new CommandEventHandler (LogoutClick);
167                         loginLinkButton = new LinkButton ();
168                         loginLinkButton.Command += new CommandEventHandler (LoginClick);
169                         loginImageButton = new ImageButton ();
170                         loginImageButton.Command += new CommandEventHandler (LoginClick);
171
172                         // adds controls at the end (after setting their properties)
173                         Controls.Add (logoutLinkButton);
174                         Controls.Add (logoutImageButton);
175                         Controls.Add (loginLinkButton);
176                         Controls.Add (loginImageButton);
177                 }
178
179
180                 protected virtual void OnLoggedOut (EventArgs e)
181                 {
182                         // this gets called only if the authentication was successful
183                         EventHandler loggedOut = (EventHandler) Events [loggedOutEvent];
184                         if (loggedOut != null)
185                                 loggedOut (this, e);
186                 }
187
188                 protected virtual void OnLoggingOut (LoginCancelEventArgs e)
189                 {
190                         // this gets called before OnAuthenticate so we can abort the authentication process
191                         LoginCancelEventHandler loggingOut = (LoginCancelEventHandler) Events [loggingOutEvent];
192                         if (loggingOut != null)
193                                 loggingOut (this, e);
194                 }
195
196                 protected internal override void OnPreRender (EventArgs e)
197                 {
198                         base.OnPreRender (e);
199                         // documentation says we select Login*|Logout* here
200                         // but tests shows that the selection is done even 
201                         // if OnPreRender is never called
202                 }
203
204                 protected internal override void Render (HtmlTextWriter writer)
205                 {
206                         if (writer == null)
207                                 return;
208
209                         RenderContents (writer);
210                 }
211
212                 protected internal override void RenderContents (HtmlTextWriter writer)
213                 {
214                         if (writer == null)
215                                 return;
216
217                         EnsureChildControls ();
218
219                         bool authenticated = false;
220                         if (Page != null) {
221                                 Page.VerifyRenderingInServerForm (this);
222                                 authenticated = Page.Request.IsAuthenticated;
223                         }
224
225                         bool logoutImage = (LogoutImageUrl.Length > 0);
226                         logoutLinkButton.Visible = authenticated && !logoutImage;
227                         logoutImageButton.Visible = authenticated && logoutImage;
228
229                         bool loginImage = (LoginImageUrl.Length > 0);
230                         loginLinkButton.Visible = !authenticated && !loginImage;
231                         loginImageButton.Visible = !authenticated && loginImage;
232
233                         if (logoutLinkButton.Visible) {
234                                 logoutLinkButton.Text = LogoutText;
235                                 logoutLinkButton.Render (writer);
236                         } else if (logoutImageButton.Visible) {
237                                 logoutImageButton.AlternateText = LogoutText;
238                                 logoutImageButton.ImageUrl = LogoutImageUrl;
239                                 writer.AddAttribute(HtmlTextWriterAttribute.Name, logoutImageButton.UniqueID);
240                                 logoutImageButton.Render (writer);
241                         } else if (loginLinkButton.Visible) {
242                                 loginLinkButton.Text = LoginText;
243                                 loginLinkButton.Render (writer);
244                         } else if (loginImageButton.Visible) {
245                                 loginImageButton.AlternateText = LoginText;
246                                 loginImageButton.ImageUrl = LoginImageUrl;
247                                 writer.AddAttribute(HtmlTextWriterAttribute.Name, loginImageButton.UniqueID);
248                                 loginImageButton.Render (writer);
249                         }
250                 }
251
252                 [MonoTODO ("for design-time usage - no more details available")]
253                 protected override void SetDesignModeState (IDictionary data)
254                 {
255                         base.SetDesignModeState (data);
256                 }
257
258                 // events
259
260                 public event EventHandler LoggedOut {
261                         add { Events.AddHandler (loggedOutEvent, value); }
262                         remove { Events.RemoveHandler (loggedOutEvent, value); }
263                 }
264
265                 public event LoginCancelEventHandler LoggingOut {
266                         add { Events.AddHandler (loggingOutEvent, value); }
267                         remove { Events.RemoveHandler (loggingOutEvent, value); }
268                 }
269
270                 // private stuff
271
272                 private void LogoutClick (object sender, CommandEventArgs e)
273                 {
274                         LoginCancelEventArgs lcea = new LoginCancelEventArgs (false);
275                         OnLoggingOut (lcea);
276                         if (lcea.Cancel)
277                                 return;
278
279                         FormsAuthentication.SignOut ();
280
281                         switch (LogoutAction) {
282                         case LogoutAction.Refresh:
283                                 HttpContext.Current.Response.Redirect (Page.Request.Url.AbsoluteUri);
284                                 break;
285                         case LogoutAction.RedirectToLoginPage:
286                                 FormsAuthentication.RedirectToLoginPage ();
287                                 break;
288                         case LogoutAction.Redirect:
289                                 string url = LogoutPageUrl;
290                                 if (url.Length == 0)
291                                         url = Page.Request.Url.AbsoluteUri;
292                                 HttpContext.Current.Response.Redirect (url);
293                                 break;
294                         }
295
296                         OnLoggedOut (e);
297                 }
298
299                 private void LoginClick (object sender, CommandEventArgs e)
300                 {
301                         FormsAuthentication.RedirectToLoginPage ();
302                 }
303         }
304 }
305
306 #endif