Validation when a page load

Topics: General
Mar 10, 2011 at 5:02 PM

What is the correct way to hook in when a page render here i composite.

 

My problem is that I have a user there is logged on to my site(I have a complete database with users)

I have a metadata field on my content page, where I can mark that the page requere login.
When the page load I wanna make a check if metadata field is true and my session variable is there. I have made a function in my dll that can handle login information and give information about that.

But have do I make the last final small step :-)

Mar 10, 2011 at 5:43 PM
Edited Mar 10, 2011 at 5:45 PM

It depends a little on whether the actual execution of the page is dependent on this session being there or not. Of not, you can write the validation code in a HttpModule which you wire up to the PostRequestHandlerExecute of your HttpApplication and handle the logic in there.

By doing this you can access the currentpage through

PageRenderer.CurrentPage

and for instance do a redirect based on MetaData value and session. The page will still actually be rendered but the output will not be sent to the client, which is perhaps not the most efficient way but perfectly acceptable since the page would have been rendered anyway if the user for instance was logged in.

So a little exampe

public class MyModule : IHttpModule
{
   public void Init(HttpApplication app)
   {
      app.PostRequestHandlerExecute += (sender, e) =>
      {
         var currentPage = PageRenderer.CurrentPage;
         // doSomethingWithCurrentPage 
      }
   }
}

Mar 10, 2011 at 7:17 PM

It looks like just what I need.

Is there ar smart way to get the metadata fields there is attached to the page.

I can not find a property on the object Currentpage, only the title and basic attributes.

Mar 10, 2011 at 7:39 PM

PageRenderer.CurrentPage returns a IPage interface that of course only contains the properties and methods defined on this specific interface. Because of the modularity of C1 each of your metadata has its own interface and you can fetch instances of them using a DataConnection.

Personally i have this extensions-class i use for fecthing Page Meta Data, dunno if there is anything similar in the C1 core - i think there should.

    public static class IPageExtensions
    {
        public static T GetProperty<T>(this IPage page) where T : class, IPageData
        {
            using (var conn = new DataConnection())
            {
                return conn.Get<T>().FirstOrDefault(t => t.PageId == page.Id);
            }
        }

        public static IEnumerable<T> GetProperties<T>(this IPage page) where T : class, IPageData
        {
            using (var conn = new DataConnection())
            {
                return conn.Get<T>().Where(t => t.PageId == page.Id);
            }
        }
    }

with this you can fetch a single MetaData instance on your currentpage like this

var myMetaData = PageRenderer.CurrentPage.GetProperty<IYourType>();