You are here

comment_perm.module in Comment Permissions 6

Module to control commenting permissions by role and by node type.

File

comment_perm.module
View source
<?php

/**
 * @file
 * Module to control commenting permissions by role and by node type.
 */

/**
 * Implementation of hook_menu().
 */
function comment_perm_menu() {
  $items = array();
  $items['admin/content/comment_perm'] = array(
    'title' => 'Comment permissions',
    'description' => 'Setup comment permissions by user role and by node type.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'comment_perm_admin_settings',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'comment_perm.admin.inc',
  );
  return $items;
}

/**
 * Implementation of hook_perm().
 */
function comment_perm_perm() {
  $perms = array();
  $types = variable_get('comment_perm_node_types', array());
  if (is_array($types)) {
    foreach ($types as $type) {
      if ($type) {
        $perms[] = $type . ': comment on any ' . $type . ' content';
        $perms[] = $type . ': comment without approval on any ' . $type . ' content';
        $perms[] = $type . ': comment without approval on own ' . $type . ' content';
        $perms[] = $type . ': edit own comments on ' . $type . ' content';
      }
    }
  }
  return $perms;
}

/**
 * Implementation of hook_comment().
 */
function comment_perm_comment(&$comment, $op) {
  switch ($op) {
    case 'validate':
      $nid = $comment['nid'];
      if (!comment_perm_access($nid)) {
        form_set_error('', _comment_perm_access_denied_message());
      }
      break;
    case 'insert':
      $current_status = $comment['status'];
      $desired_status = comment_perm_post_without_permission_access($comment['nid']) ? COMMENT_PUBLISHED : COMMENT_NOT_PUBLISHED;
      if ($current_status != $desired_status) {
        db_query("UPDATE {comments} SET status=%d WHERE cid=%d", $desired_status, $comment['cid']);
        $comment['status'] = $desired_status;
      }
      break;
  }
}

/**
 * Implementation of hook_form_alter().
 */
function comment_perm_form_alter(&$form, $form_state, $form_id) {
  if ($form_id == 'comment_form') {
    $nid = $form['nid']['#value'];
    if (!comment_perm_access($nid)) {

      // for comment reply pages, redirect back to the node
      if (arg(0) == 'comment' && arg(1) == 'reply' && is_numeric(arg(2))) {
        drupal_set_message(_comment_perm_access_denied_message(), 'error');
        drupal_goto('node/' . arg(2));
      }
      else {

        // for inline comment forms, such remove their fields so nothing will show
        unset($form['comment_filter']);
        foreach ($form as $key => $item) {
          if ($type = $item['#type']) {
            switch ($type) {
              case 'value':
              case 'hidden':

                // keep these
                break;
              default:

                // remove all others
                unset($form[$key]);
                break;
            }
          }
        }

        // display a message to users who can't post comments
        $form['text'] = array(
          '#value' => '<p id="comment-perm-access-denied">' . _comment_perm_access_denied_message() . '</p>',
        );
      }
    }
    if (arg(0) == 'comment' && arg(1) == 'edit' && is_numeric(arg(2))) {
      if (!comment_perm_edit_access($nid)) {
        drupal_set_message(_comment_perm_access_denied_message(), 'error');
        drupal_goto('node/' . $nid);
      }
    }
  }
}

/**
 * Implementation of hook_link_alter().
 */
function comment_perm_link_alter(&$links, $node) {
  if (!comment_perm_access($node)) {
    unset($links['comment_add']);
    if (module_exists('quote')) {
      unset($links['quote']);
    }

    // hide comments links entirely if that setting is enabled
    if (variable_get('comment_perm_hide_comments', 0)) {
      unset($links['comment_comments']);
      unset($links['comment_new_comments']);
    }
  }
}

/**
 * Implementation of hook_nodeapi().
 */
function comment_perm_nodeapi(&$node, $op, $arg = 0) {
  switch ($op) {
    case 'alter':
      if (variable_get('comment_perm_hide_comments', 0) && !comment_perm_access($node)) {
        $node->comment = COMMENT_NODE_DISABLED;
      }
      break;
  }
}

/**
 * Can the current user add comments to a given node?
 */
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;
}

/**
 * Can the current user post comments without permission?
 */
function comment_perm_post_without_permission_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 ($types[$node->type]) {

    // allow comment administrators to edit any comment
    if (user_access('administer comments')) {
      return TRUE;
    }

    // get assigned permissions for this user's role
    $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 comments without permission on nodes of this type?
    if ($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;
    }

    // get assigned permissions for this user's role
    if (user_access($node->type . ': edit own comments on ' . $node->type . ' content')) {
      return TRUE;
    }

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

  // non-comment_perm controlled node types default to whatever permission Drupal gives them.
  return user_access('post comments without approval') ? TRUE : FALSE;
}

/**
 * Can the current user edit comments?
 */
function comment_perm_edit_access($node) {
  if (is_numeric($node)) {
    $node = node_load($node);
  }

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

    // allow comment administrators to edit any comment
    if (user_access('administer comments')) {
      return TRUE;
    }

    // get assigned permissions for this user's role
    if (user_access($node->type . ': edit own comments on ' . $node->type . ' content')) {
      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;
}
function _comment_perm_access_denied_message() {
  global $user;
  if ($user->uid == 0) {
    return variable_get('comment_perm_message_anon', t('Login or register to post comments!'));
  }
  else {
    return variable_get('comment_perm_message_reg', t("We're sorry, but you can't post comments here!"));
  }
}

/**
 * Process variables for comment.tpl.php.
 *
 * @see theme_comment()
 */
function comment_perm_preprocess_comment(&$variables) {

  // remove edit link
  if (isset($variables['links']) && !user_access('administer comments')) {
    if (!comment_perm_edit_access($variables['node'])) {
      $variables['links'] = preg_replace('|<li class="comment_edit.*?</li>|i', '', $variables['links']);
    }
    if (!comment_perm_access($variables['node'])) {
      $variables['links'] = preg_replace('|<li class="comment_reply.*?</li>|i', '', $variables['links']);
    }
    if (strpos($variables['links'], '<li') === FALSE) {
      $variables['links'] = '';
    }
  }
}

/**
 * 'Post new comment' forms on the node page should be removed
 * if a user doesn't have permission to post comments.
 */
function comment_perm_preprocess_box(&$variables) {
  if (arg(0) == 'node' && is_numeric(arg(1))) {
    if (!comment_perm_access(arg(1))) {
      if ($variables['title'] == t('Post new comment')) {
        unset($variables['title']);
        unset($variables['content']);
      }
    }
  }
}

Functions

Namesort descending Description
comment_perm_access Can the current user add comments to a given node?
comment_perm_comment Implementation of hook_comment().
comment_perm_edit_access Can the current user edit comments?
comment_perm_form_alter Implementation of hook_form_alter().
comment_perm_link_alter Implementation of hook_link_alter().
comment_perm_menu Implementation of hook_menu().
comment_perm_nodeapi Implementation of hook_nodeapi().
comment_perm_perm Implementation of hook_perm().
comment_perm_post_without_permission_access Can the current user post comments without permission?
comment_perm_preprocess_box 'Post new comment' forms on the node page should be removed if a user doesn't have permission to post comments.
comment_perm_preprocess_comment Process variables for comment.tpl.php.
_comment_perm_access_denied_message