Created
March 25, 2009 11:58
-
-
Save hugowetterberg/85436 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Access checking for comments | |
*/ | |
function _comment_resource_access($op='view', $args=array()) { | |
if (user_access('administer comments')) { | |
return TRUE; | |
} | |
if ($op=='create') { | |
$comment = (object)$args[0]; | |
} | |
else { | |
$comment = _comment_load($args[0]); | |
} | |
switch ($op) { | |
case 'view': | |
// Check if the user has access to comments and that the node has comments enabled | |
return user_access('access comments') && _comment_resource_node_access($comment->nid); | |
case 'edit': | |
// Check if the user may edit the comment, and has access to the input format | |
return comment_access('edit', $comment) && filter_access($comment->format); | |
case 'create': | |
// Check if the user may post comments, and has access to the used format and | |
// check if the node has comments enabled, and that the user has access to the node | |
return user_access('post comments') && filter_access($comment->format) && | |
_comment_resource_node_access($comment->nid, COMMENT_NODE_READ_WRITE); | |
case 'delete': | |
// Check if the user may edit the comment | |
return comment_access('edit', $comment); | |
} | |
} | |
function _comment_resource_node_access($nid, $comment=COMMENT_NODE_READ) { | |
/* | |
* TODO: Check if we really have to perform a full node load here, | |
* it could be enough to load nid, type, uid, comment, status, format. | |
*/ | |
// Check if the node has comments enabled, and that the user has access to the node | |
$node = node_load($comment->nid); | |
return $node->comment >= $comment && node_access('view', $node); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment