You are here

function comment_perm_access in Comment Permissions 7

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

Can the current user add comments to a given node?

4 calls to comment_perm_access()
comment_perm_comment in ./comment_perm.module
Implementation of hook_comment().
comment_perm_comment_view_alter in ./comment_perm.module
comment_perm_form_alter in ./comment_perm.module
Implementation of hook_form_alter().
comment_perm_node_view_alter in ./comment_perm.module
Implementation of hook_node_view_alter().

File

./comment_perm.module, line 162
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;
}