/// <summary> /// Gets the current user. /// </summary> /// <param name="userIsOnline">if set to <c>true</c> [user is online].</param> /// <returns></returns> public static User GetCurrentUser(bool userIsOnline) { string userName = User.GetCurrentUserName(); if (userName != string.Empty) { if (userName.StartsWith("rckipid=")) { Rock.CRM.PersonService personService = new CRM.PersonService(); Rock.CRM.Person impersonatedPerson = personService.GetByEncryptedKey(userName.Substring(8)); if (impersonatedPerson != null) { return(impersonatedPerson.ImpersonatedUser); } } else { UserService userService = new UserService(); User user = userService.GetByUserName(userName); if (user != null && userIsOnline) { user.LastActivityDate = DateTime.Now; userService.Save(user, null); } return(user); } } return(null); }
/// <summary> /// Loads all of the configured blocks for the current page into the control tree /// </summary> /// <param name="e"></param> protected override void OnInit( EventArgs e ) { // Add the ScriptManager to each page ScriptManager sm = ScriptManager.GetCurrent( this.Page ); if ( sm == null ) { sm = new ScriptManager(); sm.ID = "sManager"; Page.Form.Controls.AddAt( 0, sm ); } // Recurse the page controls to find the rock page title and zone controls PageTitles = new List<PageTitle>(); Zones = new Dictionary<string, KeyValuePair<string, Zone>>(); FindRockControls( this.Controls ); // Add a Rock version meta tag string version = typeof(Rock.Web.UI.Page).Assembly.GetName().Version.ToString(); HtmlMeta rockVersion = new HtmlMeta(); rockVersion.Attributes.Add( "name", "generator" ); rockVersion.Attributes.Add( "content", string.Format( "Rock v{0}", version ) ); AddMetaTag( this.Page, rockVersion ); // If the logout parameter was entered, delete the user's forms authentication cookie and redirect them // back to the same page. if ( PageParameter( "logout" ) != string.Empty ) { FormsAuthentication.SignOut(); Session.Remove("UserIsAuthenticated"); CurrentPerson = null; CurrentUser = null; Response.Redirect( BuildUrl( new PageReference( PageInstance.Id, PageInstance.RouteId ), null ), true ); } // If the impersonated query key was included then set the current person string impersonatedPersonKey = PageParameter( "rckipid" ); if ( !String.IsNullOrEmpty( impersonatedPersonKey ) ) { Rock.CRM.PersonService personService = new CRM.PersonService(); Rock.CRM.Person impersonatedPerson = personService.GetByEncryptedKey( impersonatedPersonKey ); if ( impersonatedPerson != null ) { FormsAuthentication.SetAuthCookie("rckipid=" + impersonatedPerson.EncryptedKey, false ); Session["UserIsAuthenticated"] = false; CurrentUser = impersonatedPerson.ImpersonatedUser; } } // Get current user/person info Rock.CMS.User user = CurrentUser; // If there is a logged in user, see if it has an associated Person Record. If so, set the UserName to // the person's full name (which is then cached in the Session state for future page requests) if ( user != null ) { UserName = user.UserName; int? personId = user.PersonId; if ( personId.HasValue) { string personNameKey = "PersonName_" + personId.Value.ToString(); if ( Session[personNameKey] != null ) { UserName = Session[personNameKey].ToString(); } else { Rock.CRM.PersonService personService = new CRM.PersonService(); Rock.CRM.Person person = personService.Get( personId.Value ); if ( person != null ) { UserName = person.FullName; CurrentPerson = person; } Session[personNameKey] = UserName; } } } // If a PageInstance exists if ( PageInstance != null ) { // check if page should have been loaded via ssl if ( !Request.IsSecureConnection && PageInstance.RequiresEncryption ) { string redirectUrl = Request.Url.ToString().Replace( "http:", "https:" ); Response.Redirect( redirectUrl ); } // Verify that the current user is allowed to view the page. If not, and // the user hasn't logged in yet, redirect to the login page if ( !PageInstance.Authorized( "View", user ) ) { if ( user == null ) FormsAuthentication.RedirectToLoginPage(); } else { // Set current models (context) PageInstance.Context = new Dictionary<string, Data.KeyModel>(); try { char[] delim = new char[1] { ',' }; foreach (string param in PageParameter( "context" ).Split( delim, StringSplitOptions.RemoveEmptyEntries )) { string contextItem = Rock.Security.Encryption.DecryptString( param ); string[] parts = contextItem.Split('|'); if (parts.Length == 2) PageInstance.Context.Add(parts[0], new Data.KeyModel(parts[1])); } } catch {} // set page title if ( PageInstance.Title != null && PageInstance.Title != "" ) SetTitle( PageInstance.Title ); else SetTitle( PageInstance.Name ); // set viewstate on/off this.EnableViewState = PageInstance.EnableViewstate; // Cache object used for block output caching ObjectCache cache = MemoryCache.Default; bool canConfigPage = PageInstance.Authorized( "Configure", user ); // Create a javascript object to store information about the current page for client side scripts to use string script = string.Format( @" var rock = {{ pageId:{0}, layout:'{1}', baseUrl:'{2}' }}; ", PageInstance.Id, PageInstance.Layout, AppPath ); this.Page.ClientScript.RegisterStartupScript( this.GetType(), "rock-js-object", script, true ); // Add config elements if ( PageInstance.IncludeAdminFooter ) { AddPopupControls(); if ( canConfigPage ) AddConfigElements(); } // Load the blocks and insert them into page zones foreach ( Rock.Web.Cache.BlockInstance blockInstance in PageInstance.BlockInstances ) { // Get current user's permissions for the block instance bool canConfig = blockInstance.Authorized( "Configure", user ); bool canEdit = blockInstance.Authorized( "Edit", user ); bool canView = blockInstance.Authorized( "View", user ); // Make sure user has access to view block instance if ( canConfig || canEdit || canView ) { // Create block wrapper control (implements INamingContainer so child control IDs are unique for // each block instance HtmlGenericContainer blockWrapper = new HtmlGenericContainer( "div" ); blockWrapper.ID = string.Format("bid_{0}", blockInstance.Id); blockWrapper.Attributes.Add( "zoneloc", blockInstance.BlockInstanceLocation.ToString() ); blockWrapper.ClientIDMode = ClientIDMode.Static; FindZone( blockInstance.Zone ).Controls.Add( blockWrapper ); blockWrapper.Attributes.Add( "class", "block-instance " + ( canConfig || canEdit ? "can-configure " : "" ) + HtmlHelper.CssClassFormat( blockInstance.Block.Name ) ); // Check to see if block is configured to use a "Cache Duration' string blockCacheKey = string.Format( "Rock:BlockInstanceOutput:{0}", blockInstance.Id ); if ( blockInstance.OutputCacheDuration > 0 && cache.Contains( blockCacheKey ) ) { // If the current block exists in our custom output cache, add the cached output instead of adding the control blockWrapper.Controls.Add( new LiteralControl( cache[blockCacheKey] as string ) ); } else { // Load the control and add to the control tree Control control; try { control = TemplateControl.LoadControl( blockInstance.Block.Path ); control.ClientIDMode = ClientIDMode.AutoID; } catch ( Exception ex ) { HtmlGenericControl div = new HtmlGenericControl( "div" ); div.Attributes.Add( "class", "alert-message block-message error" ); div.InnerHtml = string.Format( "Error Loading Block:<br/><br/><strong>{0}</strong>", ex.Message ); control = div; } Block block = null; // Check to see if the control was a PartialCachingControl or not if ( control is Block ) block = control as Block; else { if ( control is PartialCachingControl && ( ( PartialCachingControl )control ).CachedControl != null ) block = ( Block )( ( PartialCachingControl )control ).CachedControl; } // If the current control is a block, set it's properties if ( block != null ) { block.PageInstance = PageInstance; block.BlockInstance = blockInstance; block.ReadAdditionalActions(); // If the block's AttributeProperty values have not yet been verified verify them. // (This provides a mechanism for block developers to define the needed blockinstance // attributes in code and have them automatically added to the database) if ( !blockInstance.Block.InstancePropertiesVerified ) { block.CreateAttributes(); blockInstance.Block.InstancePropertiesVerified = true; } // Add the block configuration scripts and icons if user is authorized if (PageInstance.IncludeAdminFooter) AddBlockConfig(blockWrapper, block, blockInstance, canConfig, canEdit); } HtmlGenericContainer blockContent = new HtmlGenericContainer( "div" ); blockContent.Attributes.Add( "class", "block-content" ); blockWrapper.Controls.Add( blockContent ); // Add the block blockContent.Controls.Add( control ); } } } // Add favicon and apple touch icons to page if ( PageInstance.Site.FaviconUrl != null ) { System.Web.UI.HtmlControls.HtmlLink faviconLink = new System.Web.UI.HtmlControls.HtmlLink(); faviconLink.Attributes.Add( "rel", "shortcut icon" ); faviconLink.Attributes.Add( "href", ResolveUrl("~/" + PageInstance.Site.FaviconUrl) ); PageInstance.AddHtmlLink( this.Page, faviconLink ); } if ( PageInstance.Site.AppleTouchUrl != null ) { System.Web.UI.HtmlControls.HtmlLink touchLink = new System.Web.UI.HtmlControls.HtmlLink(); touchLink.Attributes.Add( "rel", "apple-touch-icon" ); touchLink.Attributes.Add( "href", ResolveUrl("~/" + PageInstance.Site.AppleTouchUrl) ); PageInstance.AddHtmlLink( this.Page, touchLink ); } // Add the page admin footer if the user is authorized to edit the page if ( PageInstance.IncludeAdminFooter && canConfigPage) { HtmlGenericControl adminFooter = new HtmlGenericControl( "div" ); adminFooter.ID = "cms-admin-footer"; adminFooter.ClientIDMode = System.Web.UI.ClientIDMode.Static; this.Form.Controls.Add( adminFooter ); phLoadTime = new PlaceHolder(); adminFooter.Controls.Add( phLoadTime ); HtmlGenericControl buttonBar = new HtmlGenericControl( "div" ); adminFooter.Controls.Add( buttonBar ); buttonBar.Attributes.Add( "class", "button-bar" ); // Block Config HtmlGenericControl aBlockConfig = new HtmlGenericControl( "a" ); buttonBar.Controls.Add( aBlockConfig ); aBlockConfig.Attributes.Add( "class", "block-config icon-button" ); aBlockConfig.Attributes.Add( "href", "#" ); aBlockConfig.Attributes.Add( "Title", "Block Configuration" ); // Page Properties HtmlGenericControl aAttributes = new HtmlGenericControl( "a" ); buttonBar.Controls.Add( aAttributes ); aAttributes.Attributes.Add( "class", "properties icon-button show-modal-iframe" ); aAttributes.Attributes.Add( "height", "500px" ); aAttributes.Attributes.Add( "href", ResolveUrl( string.Format( "~/PageProperties/{0}?t=Page Properties", PageInstance.Id ) ) ); // Child Pages HtmlGenericControl aChildPages = new HtmlGenericControl( "a" ); buttonBar.Controls.Add( aChildPages ); aChildPages.Attributes.Add( "class", "page-child-pages icon-button show-modal-iframe" ); aChildPages.Attributes.Add( "height", "500px" ); aChildPages.Attributes.Add( "href", ResolveUrl( string.Format( "~/pages/{0}?t=Child Pages&pb=&sb=Done", PageInstance.Id ) ) ); // Page Zones HtmlGenericControl aPageZones = new HtmlGenericControl( "a" ); buttonBar.Controls.Add( aPageZones ); aPageZones.Attributes.Add( "class", "page-zones icon-button" ); aPageZones.Attributes.Add( "href", "#" ); aPageZones.Attributes.Add( "Title", "Page Zones" ); // Page Security HtmlGenericControl aPageSecurity = new HtmlGenericControl( "a" ); buttonBar.Controls.Add( aPageSecurity ); aPageSecurity.Attributes.Add( "class", "page-security icon-button show-modal-iframe" ); aPageSecurity.Attributes.Add( "height", "500px" ); aPageSecurity.Attributes.Add( "href", ResolveUrl( string.Format( "~/Secure/{0}/{1}?t=Page Security&pb=&sb=Done", Security.Authorization.EncodeEntityTypeName( PageInstance.GetType() ), PageInstance.Id ) ) ); } // Check to see if page output should be cached. The RockRouteHandler // saves the PageCacheData information for the current page to memorycache // so it should always exist if ( PageInstance.OutputCacheDuration > 0 ) { Response.Cache.SetCacheability( System.Web.HttpCacheability.Public ); Response.Cache.SetExpires( DateTime.Now.AddSeconds( PageInstance.OutputCacheDuration ) ); Response.Cache.SetValidUntilExpires( true ); } } } }