How to validate extranet user access to a certain page in Dynamicweb CMS using the Dynamicweb API

Sometimes you need to figure out how to detect whether an extranet user has access to a certain page in Dynamicweb or not. Normally, I would have accomplished this by using a SQL statement. The Dynamicweb API is a bit messy, and often it is quicker to just write your own SQL statement instead of searching for some method in the API.

This time, though, I decided to give the API a chance. And the result was very positive – I managed to find the method very quickly. Take a look at the code below:


using System.Web;
using Dynamicweb;
using Dynamicweb.Extensibility;

namespace Login
{
 [Subscribe(Dynamicweb.Notifications.Standard.Page.Loaded)]
 public class PageLoadedObserver : NotificationSubscriber
 {
 public override void OnNotify(string notification, object[] args)
 {
 if (args == null || args.Length == 0)
 return;

 object obj = args[0];

 if (!(obj is Dynamicweb.Frontend.PageView))
 return;

 var pw = (Dynamicweb.Frontend.PageView)obj;

 if (Security.IsCurrentUserAllowed(pw.Page))
 {
 // Do nothing
 }
 else
 {
 // Redirect
 HttpContext.Current.Response.Redirect("/Login.ashx");
 }
 }
 }
}

It’s the following couple of lines we’re interested in:


var pw = (Dynamicweb.Frontend.PageView)obj;

 if (Security.IsCurrentUserAllowed(pw.Page))

This code takes a pageview object and validates the current user by using the method called Security.IsCurrentUserAllowed.

Plain and simple….

Advertisements

One thought on “How to validate extranet user access to a certain page in Dynamicweb CMS using the Dynamicweb API

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s