You are here

function comment_perm_access in Comment Permissions 6

Same name and namespace in other branches
  1. 5 comment_perm.module \comment_perm_access()
  2. 7.2 comment_perm.module \comment_perm_access()
  3. 7 comment_perm.module \comment_perm_access()

Can the current user add comments to a given node?

6 calls to comment_perm_access()
comment_perm_comment in ./comment_perm.module
Implementation of hook_comment().
comment_perm_form_alter in ./comment_perm.module
Implementation of hook_form_alter().
comment_perm_link_alter in ./comment_perm.module
Implementation of hook_link_alter().
comment_perm_nodeapi in ./comment_perm.module
Implementation of hook_nodeapi().
comment_perm_preprocess_box in ./comment_perm.module
'Post new comment' forms on the node page should be removed if a user doesn't have permission to post comments.

... See full list

File

./comment_perm.module, line 149
Module to control commenting permissions by role and by node type.

Code

function comment_perm_access($node) {
  global $user;
  if (is_numeric($node)) {
    $node = node_load($node);
  }

  // get node types managed by comment_perm
  $types = variable_get('comment_perm_node_types', array());
  if (isset($types[$node->type])) {

    // allow comment administrators to post wherever they want
    if (user_access('administer comments')) {
      return TRUE;
    }

    // get assigned permissions for this user's role
    $access_moderated = user_access($node->type . ': comment on any ' . $node->type . ' content');
    $access_all = user_access($node->type . ': comment without approval on any ' . $node->type . ' content');
    $access_own = user_access($node->type . ': comment without approval on own ' . $node->type . ' content');

    // does the user have permission to post comment on nodes of this type?
    if ($access_moderated || $access_all) {
      return TRUE;
    }

    // does the user have permission to post comments on their own nodes?
    if ($access_own && $user->uid == $node->uid) {
      return TRUE;
    }

    // comment_perm controlled node types default to no permissions
    return FALSE;
  }

  // non-comment_perm controlled node types default to whatever permission Drupal gives them.
  return TRUE;
}