LoginView.cs 6.0 KB

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