The value passed in $node
could also be the content type of the node being created; that is why the following line is necessary to get the content type.
$type = is_string($node) ? $node : $node->type;
The reason the code works is that a non initialized variable, or object property, is set to NULL
. In your case, the control statement is really executing the following code, when the node is being created.
if (($user->uid != NULL) && !user_access("view other's posts")) {
return NODE_ACCESS_DENY;
}
Since $user->uid
is never NULL
, that is the reason why the code work the same, even if it raises a PHP warning.
To avoid the warning message, it is enough to execute code similar to the following one.
function mymodule_node_access( $node, $op, $account) {
if (($op == 'view') && ($node->type == "myCustomType") && ($user->uid != $node->uid)) {
return user_access("view other's posts") ? NODE_ACCESS_ALLOW : NODE_ACCESS_DENY;
}
// Returning nothing from this function would have the same effect.
return NODE_ACCESS_IGNORE;
}
I changed the code because I think that what you are trying to do is the following:
- When the content type is myCustomType, the user is viewing a node created by another user, and the user who is watching the node has the view other's posts permission, allow that user to see the node
- When the content type is myCustomType, the user is viewing a node created by another user, and the user who is watching the node doesn't have the view other's posts permission, don't allow that user to see the node
What your code is doing is:
- When the content type is myCustomType, the user is viewing/editing/deleting a node created by another user, and the user who is acting on the node doesn't have the view other's posts permission, don't allow that user to do anything on that node the node
- In the other cases, let the other modules decide about the access permission
What I notice is that:
- Despite the permission is "view other's posts," it is applied even when the currently logged-in user is doing something else than viewing the node
- In the case the currently logged-in user has the view other's posts permission and is doing something on the node created by somebody else, it lets other modules decide the user's permission to access the node
If the permission is really about any operation done on a node, then it should be renamed, or the code should use more than one permission. The latter is what the Node module does, since for every content type there are a permission to create a node of that content type, to edit nodes of that content type, and to delete nodes of that content type.