3

I've been looking for a module that could support my requirement for hours now and failed.

What I want to do is to restrict anonymous user to viewing content type pages based on a specific users. Lets say I have user1,user2 and user3. Anon should be able to view contents created by user1, but not user2 and user3.

I'm thinking of using node_access_hooks but I'm very new to drupal and I have no idea where to start with that. Can anyone help me with this?

3 Answers 3

1

I don't think it looks like the best idea - sometimes you have to fire an author and get someone new. Author-based solutions may get complicated in such situations. Usually I use Taxonomy Access Control - I'm restricting users to view only content with particular term, and only give access to that term to particular authors. Simple, and without need of any custom coding.

Note one important restriction:

While Drupal 7 makes support for additional databases possible, TAC does not currently support databases other than MySQL/MariaDB/etc. or Postgres. See #1266674: Support other databases for more information.

1

You can do it with Node access user reference module. Gives content access permissions to users for content that references the users with User reference or Entity reference.

enter image description here

This is great if you want your content authors to be able to choose additional authors, or choose who can view their content, as well as being able to display the list of authors or viewers within your content, or in views displays.

1
  • I'm a bit confused with Node Access user reference module. I can't seem to find the setting to prevent anon user from accessing other author's content except for the the one I mentioned?
    – Richard
    Commented Sep 24, 2014 at 8:06
1

I can't be sure this is a good idea, but in case you want to implement this in code you could create a small custom module. Let's say your content type is called "blog" and you want to prevent user with uid=897 to view that specific node type.

/**
 * Access control for specific node type and specific user IDs.
 *
 * Implements hook_node_access().
*/
function yourmodulename_node_access($node, $op, $account) {
  global $user;

  // find out the content type of the node
  $type = is_string($node) ? $node : $node->type;

  // only affect certain types of nodes
  if ($type == 'blog') {
    // We only need to consider the 'view' case as the rest is presumable handled by content type permissions in your site
    if ($op == 'view') {
      if ($user->uid == 897) {
        return NODE_ACCESS_DENY;
      } else {
        return NODE_ACCESS_ALLOW;
      }
    }
  }

Please note that:

  • access control needs to be tested, as there may be another module that overrides this permission
  • instead of hard coding the uid 897 you can add a function to look up users blocked by a certain criteria
  • using roles for this is a good idea, e.g. adding a "antiblog" role, adding those users you want to prevent reading the blog to that role, and then without custom coding you could user role-based permissions for that content type
  • I haven't tested this code, but it probably works if you just create the .module and .info files

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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