Issue 403 for specific nodes

How do you issue 403 for specific nodes? I tried using drupal_access_denied. While I received the Access Denied message, the watchdog timer is full:

Unable to change header information - headers already submitted

This is normal? I do not use drupal_access_denied correctly?

+4
source share
1 answer

Using the suggested hook_nodeapi() is still too late.

If you use $op = load , you are likely to run out of memory, because Drupal has already performed all the usual actions that it performs when a node loads (including loading it several times).

If you use $op = view , you can "fake" it if you do the following:

 function mymodule_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { switch ($op) { case 'view': drupal_access_denied(); exit(); break; } } 

But this is not a real 403: it will not be reported as such, except in Watchdog, and all normal things will still load and display as if there was a node.

For easy hacking you can use hook_init() :

 function mymodule_init() { $nodes_403 = array(42, 69, 187); if (arg(0) == 'node' && in_array(arg(1), $nodes_403)) drupal_access_denied(); exit(); } } 

But bypassing Drupal's built-in permissions system is unnecessary. Instead, you want to use Drupal node permissions to deny access to the node.

If you defined your own content type in the module, you can use hook_access() :

 function mymodule_access($op, $node, $account) { $nodes_403 = array(42, 69, 187); if (in_array($node->nid, $nodes_403)) { return FALSE; } } 

But if you do not define your own content types, hook_access() never called. So instead, you need to redefine the node access callback path with your own:

 function mymodule_menu_alter(&$items) { $items['node/%node']['access callback'] = 'mymodule_access'; } function mymodule_access($op, $node, $account = NULL) { $nodes_403 = array(42, 69, 187); if ($op == 'view' && in_array($node->nid, $nodes_403)) { return FALSE; } return node_access($op, $node, $account); } 

Due to hook_menu_alter() make sure you restore your menu system after implementing the above.

+5
source

All Articles