| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- //
- // System.Web.UI.WebControls.LoginView class
- //
- // Author:
- // Sebastien Pouliot <[email protected]>
- // Konstantin Triger <[email protected]>
- //
- // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- #if NET_2_0
- using System.Collections;
- using System.ComponentModel;
- using System.Security.Permissions;
- using System.Security.Principal;
- using System.Web.Security;
- namespace System.Web.UI.WebControls {
- // CAS
- [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- [AspNetHostingPermission (SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- // attributes
- [DefaultEvent ("ViewChanged")]
- [DefaultProperty ("CurrentView")]
- [Designer ("System.Web.UI.Design.WebControls.LoginViewDesigner," + Consts.AssemblySystem_Design)]
- [ParseChildren (true)]
- [PersistChildren (false)]
- [Themeable (true)]
- public class LoginView : Control, INamingContainer {
- private static readonly object viewChangedEvent = new object ();
- private static readonly object viewChangingEvent = new object ();
- private ITemplate anonymousTemplate;
- private ITemplate loggedInTemplate;
- private bool isAuthenticated;
- private bool theming;
- private RoleGroupCollection coll;
- public LoginView ()
- {
- theming = true;
- }
- [Browsable (false)]
- [DefaultValue (null)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [TemplateContainer (typeof (LoginView))]
- public virtual ITemplate AnonymousTemplate {
- get { return anonymousTemplate; }
- set { anonymousTemplate = value; }
- }
- public override ControlCollection Controls {
- get {
- EnsureChildControls();
- return base.Controls;
- }
- }
- [Browsable (true)]
- public override bool EnableTheming {
- get { return theming; }
- set { theming = value; }
- }
- [Browsable (false)]
- [DefaultValue (null)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [TemplateContainer (typeof (LoginView))]
- public virtual ITemplate LoggedInTemplate {
- get { return loggedInTemplate; }
- set { loggedInTemplate = value; }
- }
- [Filterable (false)]
- [MergableProperty (false)]
- [PersistenceMode (PersistenceMode.InnerProperty)]
- [Themeable (false)]
- public RoleGroupCollection RoleGroups {
- get {
- if (coll == null)
- coll = new RoleGroupCollection ();
- return coll;
- }
- }
- [Browsable (true)]
- public override string SkinID {
- get { return base.SkinID; }
- set { base.SkinID = value; }
- }
- bool IsAuthenticated {
- get {
- return isAuthenticated;
- }
- set {
- if (value == isAuthenticated)
- return;
-
- isAuthenticated = value;
- OnViewChanging (EventArgs.Empty);
- ChildControlsCreated = false;
- OnViewChanged (EventArgs.Empty);
- }
- }
- private ITemplate GetTemplateFromRoleGroup (RoleGroup rg, IPrincipal user)
- {
- if (user == null)
- return null;
-
- foreach (string role in rg.Roles) {
- if (user.IsInRole (role))
- return rg.ContentTemplate;
- }
-
- return null;
- }
-
- protected internal override void CreateChildControls ()
- {
- Controls.Clear ();
- Control c = new Control ();
- ITemplate template = null;
-
- if (Page != null && Page.Request.IsAuthenticated) {
- isAuthenticated = true;
- RoleGroupCollection rgc;
- HttpContext ctx = HttpContext.Current;
- IPrincipal user = ctx != null ? ctx.User : null;
- if (Roles.Enabled && (rgc = RoleGroups) != null && rgc.Count > 0) {
- foreach (RoleGroup rg in rgc) {
- template = GetTemplateFromRoleGroup (rg, user);
- if (template != null)
- break;
- }
- }
- if (template == null)
- template = LoggedInTemplate;
- } else {
- isAuthenticated = false;
- template = AnonymousTemplate;
- }
- if (template != null)
- template.InstantiateIn (c);
- Controls.Add (c);
- }
- public override void DataBind ()
- {
- EventArgs args = EventArgs.Empty;
- OnDataBinding (args);
- EnsureChildControls ();
- DataBindChildren ();
- }
- [EditorBrowsable (EditorBrowsableState.Never)]
- public override void Focus ()
- {
- // LAMESPEC: throw new InvalidOperationException ();
- throw new NotSupportedException ();
- }
- protected internal override void LoadControlState (object savedState)
- {
- if (savedState == null)
- return;
- isAuthenticated = (bool) savedState;
- }
- protected internal override void OnInit (EventArgs e)
- {
- base.OnInit (e);
- if (Page != null)
- Page.RegisterRequiresControlState(this);
- }
- protected internal override void OnPreRender (EventArgs e)
- {
- base.OnPreRender (e);
- if (Page != null)
- IsAuthenticated = Page.Request.IsAuthenticated;
- }
- protected virtual void OnViewChanged (EventArgs e)
- {
- EventHandler h = (EventHandler)Events [viewChangedEvent];
- if (h != null)
- h (this, e);
- }
- protected virtual void OnViewChanging (EventArgs e)
- {
- EventHandler h = (EventHandler)Events [viewChangingEvent];
- if (h != null)
- h (this, e);
- }
- protected internal override void Render(HtmlTextWriter writer) {
- EnsureChildControls();
- base.Render (writer);
- }
- protected internal override object SaveControlState ()
- {
- if (isAuthenticated)
- return isAuthenticated;
- return null;
- }
- [MonoTODO ("for design-time usage - no more details available")]
- protected override void SetDesignModeState (IDictionary data)
- {
- base.SetDesignModeState (data);
- }
- // events
- public event EventHandler ViewChanged {
- add { Events.AddHandler (viewChangedEvent, value); }
- remove { Events.RemoveHandler (viewChangedEvent, value); }
- }
- public event EventHandler ViewChanging {
- add { Events.AddHandler (viewChangingEvent, value); }
- remove { Events.RemoveHandler (viewChangingEvent, value); }
- }
- }
- }
- #endif
|