I will start from the beginning.
Say I have two content types, and two user roles. I want User role A to be able to view content type X only, and user role B to be able to view content type Y only.

By default authenticated drupal users can view all content types, so I used Content Access module. (FYI: I need content type level access control only).

Now I can check if a user have the right to create a content type by using

if( user_access("create my_type content") )

How do I check the view permission which are set by Content Access Module. Obviously I tried "view my_type content", "view all my_type content", and "view own my_type content", but none worked.

I have also tried

 if(node_access('view', 'my_type')) 

which didn't work.

Any ideas?

Comments

Tobias Xy’s picture

(I don't know the answer ;-) )

You should create a support request in the issue queue of the content access module (see https://drupal.org/node/644164).
It is more likely, that you will get a good answer there.

nevets’s picture

It is not clear what the context of your code is or why you would need it

pnick107’s picture

Thanks for your interest Nevets,
I want to set the view permission for a content type and use a function to find out if logged in user has the right to view a page (while listing a list of all the pages).

nevets’s picture

They question is why since drupal core handles that for displaying a single node and views when listing nodes. So once again, the context is unclear.

pnick107’s picture

Hello Nevets,
I am not using views for the listing. It is some custom code. I wasn't making use of hook_permission() to grant the view permission, but now when I am, it's causing another issue. (Please see https://drupal.org/node/2100697#comment-7919553)
What would be the best way to achieve this?

nevets’s picture

Why not use views?

pnick107’s picture

Multiple reasons. Most important one is client wants his employees to be able to create views on their own, without any training. This custom code has an easier interface they can work with (when working with multiple tables) and comes with filters as they want them.

BlackyWhoElse’s picture

Hey ho :D

So you say you have:

ROLE A | ROLE B | authenticated user | anonymous user

So now you can make it clear and easy by creating your own permissions.

<?php
function hook_permission() {
  return array(
    'view content a' => array(
      'title' => t('View contenttype a'),
    ),
    'view content b' => array(
      'title' => t('View contenttype b'),
    ),
  );
}
?>

With this you can controll the permissions by View UI and also by code via

<?php
if( user_access("view content a"))
?>

Remember authenticated user is a requierd role for each user so dont add your permission to this role.

pnick107’s picture

Hello BlackyWhoElse,
Thanks for your reply. I will try it and let you know how it went.

Regards,
Nick

pnick107’s picture

Hello Blacky,
It worked, but there is an issue.
Below is the code I used in .Module file

function HOOKNAME_permission(){
    return array(
    'view contenttype_a' => array(
      'title' => t('View A'),
    ),
    'view contenttype_b' => array(
      'title' => t('View B'),
    ),
  );
    }

The following is working

 if( user_access("view contenttype_a") ) echo "in "; else echo "No access";
 if( user_access("view contenttype_b") ) echo "in "; else echo "No access";

In permissions, when A is checked and B is not I get the output "in No Access", which is desired.
The issue: when I created contents of both types from admin account, user was able to see both (When he shouldn't get access to type B.

Thanks!

pnick107’s picture

One yes/no question to anyone who can answer it...

Do I need to place a condition in the template for content to be restricted for a user or drupal does it automatically?
More specifically if I write

'view contenttype_a' => array(
      'title' => t('View A'),
    ),

does drupal know that I mean view right, and provides/blocks view right, or do I use if(user_access("view contenttype_a")) condition to stop text from loading where needed?

Thanks,
Nick!

BlackyWhoElse’s picture

Ok lets see.

This should help you.

<?php
function template_preprocess_node(&$variables) {

    $node = $variables['node'];

    switch ($node->type) {
      case content_a: // Contenttype machine name
        if (user_access("view contenttype_a")) {
          // lets doo this
          // You dont have to do anything here
        }
        else {
          drupal_set_message('Hey you should upgrade your account so you can view this page', 'error');
          drupal_goto('');
        }

        break;

      case content_b: // Contenttype machine name
        if (user_access("view contenttype_b")) {
          // lets doo this
          // You dont have to do anything here
        }
        else {
          drupal_set_message('Hey you should upgrade your account so you can view this page', 'error');
          drupal_goto('');
        }

        break;

      case content_ab: // Contenttype machine name
        if (user_access("view contenttype_a") || user_access("view contenttype_a")) {
          // lets doo this
          // You dont have to do anything here
        }
        else {
          drupal_set_message('Hey you should upgrade your account so you can view this page', 'error');
          drupal_goto('');
        }

        break;

      default:
        break;
    }
  }
?>
pnick107’s picture

Thanks!
I implemented something similar in hook_view and it is doing what I wanted.

Regards,
Nick

BlackyWhoElse’s picture

So if you are using views why you downt use the permission system and set it to Roles?

Page settings -> Access

pnick107’s picture

By default drupal shows create, edit, and delete permissions in permissions page, but not the view permission.
So now I am using hook_permission() to create the view permission, which then is visible in permissions page.
After that I am using user_access() in hook_view() in a condition to check the permission.

Is there a better way of achieving this?
Any code implementing that, or keywords to do the search would be a great help.

Thanks,
Nick

BlackyWhoElse’s picture

Here is an example:

Click me :D

pnick107’s picture

Thanks for taking the time to attach the image BlackyWhoElse,
Actually I am using some custom code for listing, not the drupal's views.

When you said "views" I though you were referring to my "hook_view" message just above it.
I was able to solve the issue with the help of an earlier comment of yours.

Thanks!

Jaypan’s picture

Actually I am using some custom code for listing, not the drupal's views.

You need to be clear about this when discussing Drupal. If you use the term 'view', people will think you are referring to the Views module. I mostly ignored this whole thread because I thought you were talking about the Views module, and I don't have much advice to give regarding that module.

pnick107’s picture

Hello Jay,
I cleared that in my October 30th comment https://drupal.org/node/2100697#comment-7919563

May be the title of the post could be different to avoid the confusion. Thanks for the suggestion.