I would like to use PHP code in nodes of a specific content type. I can check the user's permission with the global $user if the user can access the node.

Is there any module to set view for created page content?
Is it secure to check page access with this code?

global $user;

// Check to see if $user has the administrator role.
if (in_array('administrator', array_values($user->roles))) {
  // Do something.

I think you are looking for a node access module. There are many of them, for example http://drupal.org/project/content_access (configure access with roles directly) or http://drupal.org/project/tac_lite (control access by adding special taxonomy terms) and many more.

For your second question, you should always define some permissions (with hook_perm in D6, hook_permission() in D7) and then check that. That allows to configure through the UI, which roles have that permission and the code is not tied to your specific configuration.

| improve this answer | |
  • if i use global $user it is secure? – user780 Apr 19 '11 at 6:02
  • Yes, it is secure. – Berdir Apr 19 '11 at 6:36
  • Just make sure not to mess it up :) – Alex Weber Apr 19 '11 at 20:02

Usually, on Drupal you don't verify if the users has a specific role, but if they have a specific permission; for example, instead of checking of the user has the administrator role, you check if the user has the permission of administrating nodes. This allows to add the permission to other roles without to modify the existing code.
Referring to a specific role is not generic enough to be used in a module that could be installed on different Drupal sites; in the case you want to verify if the users have one of specific roles, you should check each role, while with the permission you possibly check only one permission.

Apart from the modules reported by Berdir, if the content type is a book page, then you can use Book access, which is specific for book pages, and allows to set the users who can move book pages between books.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.