I would like to know how drupal handles the code for the roles in order to verify access to content. This code is valid:

if (in_array('my_role', array_values($user->roles))) {
// some stuffs here.
}

thanks

Comments

nevets’s picture

It is not clear, are you asking a question or providing an example?

bboldi’s picture

Use user_access instead:

Your code would look like this:


if(user_access("my_role"))
{
// some stuff here
}

here is the full doc: http://api.drupal.org/api/drupal/modules--user--user.module/function/use...

WorldFallz’s picture

did you look at the linked function? There's nothing that checks roles there. user_access checks permissions.

nextpic’s picture

Sorry I write an example:
I have two roles "A" and "B" and a content type (CCK file_field) called "news". The users of the role "A" can see "news", while users of the role of "B" should not have access to "news" and should display a message "you are not authorized to view the content." "News" appears as a node is both as a field of view. How can I do?
thank you very much.

In italian:
scusate scrivo un esempio:
ho due ruoli "A" e "B" e un contenuto di tipo (cck file_field) chiamato "news". Gli utenti del ruolo "A" possono vedere "news", mentre gli utenti del ruolo "B" non devono avere accesso a "news" e devono visualizzare una scritta "non sei autorizzato a vedere il contenuto". "News" viene visualizzato sia come nodo sia come campo di una vista. Come posso fare?
Grazie mille

Milena’s picture

Try content access module. It has such functionality.

nextpic’s picture

I tried content access module but this does not make me see the content but does not replace the same with the written access denied.