implementing hook_access for cck content types
previously, we discussed implementing all of the node hooks for CCK content types except
hook_access. unfortunately, there is no
access op for
hook_nodeapi. adding this to drupal core is the topic of much discussion on drupal.org. so far a resolution to the issue has failed to be included in drupal 5 and drupal 6, and is now on deck for consideration in drupal 7.
this is a complicated issue, and the experts are debating with good cause. in the meantime though, if you need to move on, here's what you can do.
- install this patch to node.module
- you now have an access op exposed in hook_nodeapi
one reason that the debate is dragging on on this topic, is that the drupal developers are concerned that access control is already too complicated, and this addition will simply make drupal access control incomprehensible. this is a valid point, and to use this patch properly you do need to understand the access control order of evaluation. when determining whether or node a user may view a node, here are the questions drupal asks
- does the user have 'administer nodes' permission. if yes, always return true
- does the user have 'access content' permission. if no, always return false
- invoke the new hook_nodeapi methods.
- if no hook_nodeapi returned an explicit opinion on the matter, keep going.
- otherwise, if any hook_nodeapi returned true, return true
- invoke the hook_access method, if any. (note, there may be only one of these!)
- if no hook_nodeapi returned an explicit opinion, keep going
- if an opinion was returned, return that
- now check was the node_access table has to say. if no opinion, keep going
- is the user the author of the node? if yes, return true
- give up, return false
phew, that's a complicated flow of execution. are there any easy guidelines we can draw from this? yes . . .
one downside of granting access control to
hook_nodeapi is that there may now be multiple modules with an opinion on the matter. this forces the drupal core developer to make a choice as to what to do when there are multiple, conflicting answers. in this patch, they have chosen to allow positive responses to dominate over negative responses. i'm personally not convinced they will stick with this decision, so, in the meantime, if you're using this patch, try and stick to a convention in which you implement only one hook_nodeapi access control method per content type. in doing this, you're simply allowing your CCK content types to function like any other content type, rather than opening a huge kettle of access control worms.