Need to control which users can access a node/page of a particular content type on your Drupal site?
The Drupal Node API provides us a quick way to do this. It provides the hook_nodeapi function to react to the actions affecting all kinds of nodes. We can easily implement this hook in our module or theme. A prototype of this hook looks like this:
function myCustomModuleName_nodeapi (&$node, $op) {
/*Your code and conditions here*/
}
Here myCustomModuleName is the name of the module in which this hook is defined. The two parameters are $node, which represents the node on which the action is being performed and the other one $op, is the kind of action which is being performed. The $op can have values like view, alter, delete, print and so on…
As an example, I was looking to design such a condition for my website:
- If enrolled user creates content type for "recipe", restrict access to recipe except for node author and admin.
So, I needed to develop a mechanism through which for each node of type “recipe”:
- Everyone can see nodes created by admin including Anonymous user.
This is a preview of
Controlling Access to Your Node according to User’s Role/ID in Drupal
.
Read the full post (362 words, estimated 1:27 mins reading time)