setting page access permissions in drupal

10,777

Solution 1

Since you've mentioned you're new to Drupal, a word of caution - using PHP filter for your custom code is considered bad practice. It's a hack and it's a security problem.

It's much better to write your custom module, and it can implement its own custom permissions, that you can check for, etc. etc. There's a nice sample module that you can check out to see how it should look. Obviously, check out the handbook as well. It's really not as hard as it sounds.

But back to your question. You can put the following line in your PHP-filtered page:

global $user;

This will give you access to an object that represents the current user. $user->roles is an array that represents the user's roles. You can just check if it has authenticated user (or slightly better - a custom role you create).

Solution 2

Having read above answers, you may also consider the readily available simple access module. It suited my requirements and if you're to avoid PHP filtering and don't want to create your own module (for whatever reasons), you can check out simple access.

Solution 3

To prevent anonymous users accessing a page you can use the following php code:

<?php
// we have to access the content of the global-scope variable $user
global $user; 

//Checking if the user is registered
if (in_array('authenticated user', $user->roles)==FALSE) {
header('Location:/content/restrictedaccess') ;
}
?>

//Page content

-- More info on how I created my site with Drupal here: http://modeling-languages.com/content/how-i-created-site-drupal

Share:
10,777
Niks
Author by

Niks

A Software Practitioner and Learner of Clean Code.

Updated on June 13, 2022

Comments

  • Niks
    Niks almost 2 years

    I'm new to drupal. Its a very dumb question I guess. I have enabled PHP filter module so that I can create a new page with my own PHP code in it. This particular page I want to be accessible to only authenticated users, not visible to anonymous users. How can I achieve this? Can I set permissions for individual pages in drupal? Or is it possible to identify that an anonymous user is trying to access the current page through PHP code?

  • Niks
    Niks over 14 years
    Thanks Eli :) Sorry, i'm thanking you quite late. I took your word of caution quite seriously :) Your suggestions and Jordi's code made it very easy! Although I soon replaced it with the "simple access" module. It is really as simple as it states and it matched my requirements perfectly. I'll be using your link for sample module for creating own modules in future. Its a good starter!