Propeople Blog

A regular dose of fresh news, events, products, Drupal development resources and more.

October 1, 2012

Drupal: Set up restricted access for certain nodes

Sometimes clients request us to restrict some of the pages (nodes) from anonymous users. I would like to show a quite elegant (in my opinion) way to set it up without any line of code.

The idea is that when an anonymous user opens a restricted page, he got redirected to the login screen and after he fills up his credentials, he is redirected back to the page he wanted to see in the first place. This can be achieved by using Page Manager module from the CTools package.

Let's play with the Page content type to achieve this.

First of all we need to add a boolean field to define what page has restricted access.

ctools restrict access field

Next we must create a page with restricted access.

ctools restricted page created

Now is the time to set up some Page Manager settings. So in the Admin → Structure → Pages page (admin/structure/pages) we need to enable “Node template”.

ctools enable node template

Now in the settings of the Page we need to use HTTP Response and Selection rules to attribute this rule to anonymous users only.

ctools variant settings http response

After “Create variant” you need to add a selection rule for “anonymous users” only.

ctools selection rule anonymous user

Now we add a rule to use our Restricted Access field as following

ctools restrict access

ctools restrict access checkbox

After you click “Save”, use the “Continue” button to get to the settings of HTTP Response. We must use the 301 Redirect response code. We redirect users to the user/login page with destination variable to the current page. That means that after login form, the submission user will be redirected back to the current page.

ctools page manager http redirect settings

Then you “Create variant” and you are all set. Of course people who develop the code could do simple redirect fairly easy in a small custom code, but why we need to if we have these nice Page Manager settings? Hope you have enjoyed it.

0 Responses to this post