2
0

LoginView.cs 6.6 KB

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