Hi,

I would like anonymous users to be allowed to view most of the content on my site (forum topics, blog entries, etc.). However, I do not want anonymous users to be allowed to view content of a certain content type (custom_content_type), only viewable by authenticated users. The user permissions choice of "access content" does not allow me to restrict the content types available to anonymous users. Is there any way I can hide all content of a certain content type from anonymous users? Perhaps some PHP code: if the user is anonymous AND the content type is custom_content_type, don't show the node:

<?php
global $user;
$match = TRUE;
$types = array('custom_content_type' => 1);
if (arg(0) == 'node' && is_numeric(arg(1))) {
  $nid = arg(1);
  $node = node_load(array('nid' => $nid));
  $type = $node->type;
  if ((isset($types[$type])) && ($user->uid == 0)) {
        $match = FALSE;
    }
    else {
        $match = TRUE;
    }
  }
}
if($match == TRUE) {
    display the node (I'm not sure how the code goes for this line)
}
?>

This code was based on www.drupal.org/node/64135#comment-834496.

I think I almost got it, but any idea on where to put this code or how else I might more easily approach configuring content visibility to anonymous users by content type?

Thank you,
Andrew G

Comments

cpugeniusmv’s picture

I haven't tested this code, but I think this might do what you want. Create a new directory in your modules directory called deny_anonymous_per_content_type and create a file called deny_anonymous_per_content_type.module inside it. Here is the code that should go in the file:


/**
  * Implementation of hook_nodeapi
  */
function deny_anonymous_per_content_type_nodeapi(&$node, $op) {
  global $user;
  static $types = array('custom_content_type' => 1);

  if ($op == 'view') {
    if (isset($types[$node->type]) && $user->uid == 0) {
      drupal_access_denied();
	  exit;
	}
  }
}