2006-10-18 Igor Zelmanovich <igorz@mainsoft.com>
[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                 [Themeable (false)]
93                 public virtual LogoutAction LogoutAction {
94                         get {
95                                 object o = ViewState ["LogoutAction"];
96                                 return (o == null) ? LogoutAction.Refresh : (LogoutAction) o;
97                         }
98                         set {
99                                 if ((value < LogoutAction.Refresh) || (value > LogoutAction.RedirectToLoginPage))
100                                         throw new ArgumentOutOfRangeException ("LogoutAction");
101                                 ViewState ["LogoutAction"] = (int) value;
102                         }
103                 }
104
105                 [DefaultValue ("")]
106                 [Editor ("System.Web.UI.Design.ImageUrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
107                 [UrlProperty]
108                 public virtual string LogoutImageUrl {
109                         get {
110                                 object o = ViewState ["LogoutImageUrl"];
111                                 return (o == null) ? String.Empty : (string) o;
112                         }
113                         set {
114                                 if (value == null)
115                                         ViewState.Remove ("LogoutImageUrl");
116                                 else
117                                         ViewState ["LogoutImageUrl"] = value;
118                         }
119                 }
120
121
122                 [DefaultValue ("")]
123                 [Editor ("System.Web.UI.Design.UrlEditor, " + Consts.AssemblySystem_Design, "System.Drawing.Design.UITypeEditor, " + Consts.AssemblySystem_Drawing)]
124                 [Themeable (false)]
125                 [UrlProperty]
126                 public virtual string LogoutPageUrl {
127                         get {
128                                 object o = ViewState ["LogoutPageUrl"];
129                                 return (o == null) ? String.Empty : (string) o;
130                         }
131                         set {
132                                 if (value == null)
133                                         ViewState.Remove ("LogoutPageUrl");
134                                 else
135                                         ViewState ["LogoutPageUrl"] = value;
136                         }
137                 }
138
139                 [Localizable (true)]
140                 public virtual string LogoutText {
141                         get {
142                                 object o = ViewState ["LogoutText"];
143                                 return (o == null) ? Locale.GetText ("Logout") : (string) o;
144                         }
145                         set {
146                                 if (value == null)
147                                         ViewState.Remove ("LogoutText");
148                                 else
149                                         ViewState ["LogoutText"] = value;
150                         }
151                 }
152
153                 protected override HtmlTextWriterTag TagKey {
154                         get { return HtmlTextWriterTag.A; }
155                 }
156
157
158                 // methods
159
160                 protected internal override void CreateChildControls ()
161                 {
162                         Controls.Clear ();
163
164                         // we create controls for all possibilities
165                         logoutLinkButton = new LinkButton ();
166                         logoutLinkButton.Command += new CommandEventHandler (LogoutClick);
167                         logoutImageButton = new ImageButton ();
168                         logoutImageButton.Command += new CommandEventHandler (LogoutClick);
169                         loginLinkButton = new LinkButton ();
170                         loginLinkButton.Command += new CommandEventHandler (LoginClick);
171                         loginImageButton = new ImageButton ();
172                         loginImageButton.Command += new CommandEventHandler (LoginClick);
173
174                         // adds controls at the end (after setting their properties)
175                         Controls.Add (logoutLinkButton);
176                         Controls.Add (logoutImageButton);
177                         Controls.Add (loginLinkButton);
178                         Controls.Add (loginImageButton);
179                 }
180
181
182                 protected virtual void OnLoggedOut (EventArgs e)
183                 {
184                         // this gets called only if the authentication was successful
185                         EventHandler loggedOut = (EventHandler) Events [loggedOutEvent];
186                         if (loggedOut != null)
187                                 loggedOut (this, e);
188                 }
189
190                 protected virtual void OnLoggingOut (LoginCancelEventArgs e)
191                 {
192                         // this gets called before OnAuthenticate so we can abort the authentication process
193                         LoginCancelEventHandler loggingOut = (LoginCancelEventHandler) Events [loggingOutEvent];
194                         if (loggingOut != null)
195                                 loggingOut (this, e);
196                 }
197
198                 protected internal override void OnPreRender (EventArgs e)
199                 {
200                         base.OnPreRender (e);
201                         // documentation says we select Login*|Logout* here
202                         // but tests shows that the selection is done even 
203                         // if OnPreRender is never called
204                 }
205
206                 protected internal override void Render (HtmlTextWriter writer)
207                 {
208                         if (writer == null)
209                                 return;
210
211                         RenderContents (writer);
212                 }
213
214                 protected internal override void RenderContents (HtmlTextWriter writer)
215                 {
216                         if (writer == null)
217                                 return;
218
219                         EnsureChildControls ();
220
221                         bool authenticated = false;
222                         if (Page != null) {
223                                 Page.VerifyRenderingInServerForm (this);
224                                 authenticated = Page.Request.IsAuthenticated;
225                         }
226
227                         bool logoutImage = (LogoutImageUrl.Length > 0);
228                         logoutLinkButton.Visible = authenticated && !logoutImage;
229                         logoutImageButton.Visible = authenticated && logoutImage;
230
231                         bool loginImage = (LoginImageUrl.Length > 0);
232                         loginLinkButton.Visible = !authenticated && !loginImage;
233                         loginImageButton.Visible = !authenticated && loginImage;
234
235                         if (logoutLinkButton.Visible) {
236                                 logoutLinkButton.Text = LogoutText;
237                                 logoutLinkButton.Render (writer);
238                         } else if (logoutImageButton.Visible) {
239                                 logoutImageButton.AlternateText = LogoutText;
240                                 logoutImageButton.ImageUrl = LogoutImageUrl;
241                                 writer.AddAttribute(HtmlTextWriterAttribute.Name, logoutImageButton.UniqueID);
242                                 logoutImageButton.Render (writer);
243                         } else if (loginLinkButton.Visible) {
244                                 loginLinkButton.Text = LoginText;
245                                 loginLinkButton.Render (writer);
246                         } else if (loginImageButton.Visible) {
247                                 loginImageButton.AlternateText = LoginText;
248                                 loginImageButton.ImageUrl = LoginImageUrl;
249                                 writer.AddAttribute(HtmlTextWriterAttribute.Name, loginImageButton.UniqueID);
250                                 loginImageButton.Render (writer);
251                         }
252                 }
253
254                 [MonoTODO ("for design-time usage - no more details available")]
255                 protected override void SetDesignModeState (IDictionary data)
256                 {
257                         base.SetDesignModeState (data);
258                 }
259
260                 // events
261
262                 public event EventHandler LoggedOut {
263                         add { Events.AddHandler (loggedOutEvent, value); }
264                         remove { Events.RemoveHandler (loggedOutEvent, value); }
265                 }
266
267                 public event LoginCancelEventHandler LoggingOut {
268                         add { Events.AddHandler (loggingOutEvent, value); }
269                         remove { Events.RemoveHandler (loggingOutEvent, value); }
270                 }
271
272                 // private stuff
273
274                 private void LogoutClick (object sender, CommandEventArgs e)
275                 {
276                         LoginCancelEventArgs lcea = new LoginCancelEventArgs (false);
277                         OnLoggingOut (lcea);
278                         if (lcea.Cancel)
279                                 return;
280
281                         FormsAuthentication.SignOut ();
282
283                         switch (LogoutAction) {
284                         case LogoutAction.Refresh:
285                                 HttpContext.Current.Response.Redirect (Page.Request.Url.AbsoluteUri);
286                                 break;
287                         case LogoutAction.RedirectToLoginPage:
288                                 FormsAuthentication.RedirectToLoginPage ();
289                                 break;
290                         case LogoutAction.Redirect:
291                                 string url = LogoutPageUrl;
292                                 if (url.Length == 0)
293                                         url = Page.Request.Url.AbsoluteUri;
294                                 HttpContext.Current.Response.Redirect (url);
295                                 break;
296                         }
297
298                         OnLoggedOut (e);
299                 }
300
301                 private void LoginClick (object sender, CommandEventArgs e)
302                 {
303                         FormsAuthentication.RedirectToLoginPage ();
304                 }
305         }
306 }
307
308 #endif