You are here

comment_perm.module in Comment Permissions 7

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/config/system/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_permission().
 */
function comment_perm_permission() {
  $perms = array();
  $types = variable_get('comment_perm_node_types', array());
  if (is_array($types)) {
    foreach ($types as $type) {
      if ($type) {
        $perms[$type . ': administer comment settings on any ' . $type . ' content'] = array(
          'title' => t("%type: administer comment settings on any %type content", array(
            '%type' => $type,
          )),
        );
        $perms[$type . ': administer comment settings on own ' . $type . ' content'] = array(
          'title' => t("%type: administer comment settings on own %type content", array(
            '%type' => $type,
          )),
        );
        $perms[$type . ': comment on any ' . $type . ' content'] = array(
          'title' => t("%type: comment on any %type content", array(
            '%type' => $type,
          )),
        );
        $perms[$type . ': comment without approval on any ' . $type . ' content'] = array(
          'title' => t("%type: comment without approval on any %type content", array(
            '%type' => $type,
          )),
        );
        $perms[$type . ': comment without approval on own ' . $type . ' content'] = array(
          'title' => t("%type: comment without approval on own %type content", array(
            '%type' => $type,
          )),
        );
        $perms[$type . ': edit own comments on ' . $type . ' content'] = array(
          'title' => t("%type: edit own comments on %type content", array(
            '%type' => $type,
          )),
        );
      }
    }
  }
  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($nid));
      }
      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;
  }
}
function comment_perm_comment_view_alter(&$build) {
  $types = variable_get('comment_perm_node_types', array());
  $form_id_types = array();
  if (is_array($types)) {
    foreach ($types as $type) {
      if ($build['#bundle'] == 'comment_node_' . $type) {
        if (!comment_perm_edit_access($build['#node'])) {
          unset($build['links']['comment']['#links']['comment-edit']);
        }
        if (!comment_perm_access($build['#node'])) {
          unset($build['links']['comment']['#links']['comment-reply']);
        }
        return;
      }
    }
  }
}

/**
 * Implementation of hook_node_view_alter().
 */
function comment_perm_node_view_alter(&$build) {
  $types = variable_get('comment_perm_node_types', array());
  $form_id_types = array();
  if (is_array($types)) {
    foreach ($types as $type) {
      if ($build['#bundle'] == $type) {
        if (!comment_perm_access($build['#node'])) {
          unset($build['comments']['comment_form']);
          unset($build['links']['comment']['#links']['comment-add']);
          if (variable_get('comment_perm_hide_comments', 0)) {
            unset($build['comments']);
            unset($build['links']['comment']);
          }
        }
        return;
      }
    }
  }
}

/**
 * Implementation of hook_form_alter().
 */
function comment_perm_form_alter(&$form, $form_state, $form_id) {

  #comment_node_merci_reservation_form
  if ($form['#id'] == 'comment-form' and arg(0) == 'comment') {
    $types = variable_get('comment_perm_node_types', array());
    $form_id_types = array();
    if (is_array($types)) {
      foreach ($types as $type) {
        if ($form_id == 'comment_node_' . $type . '_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($nid), 'error');
              drupal_goto('node/' . arg(2));
            }
          }
          if (arg(0) == 'comment' && arg(2) == 'edit' && is_numeric(arg(1))) {
            if (!comment_perm_edit_access($nid)) {
              drupal_set_message(_comment_perm_access_denied_message($nid), 'error');
              drupal_goto('node/' . $nid);
            }
          }
        }
      }
    }
  }

  // Allow users to administer comment settings per content type.
  if (!empty($form['#node_edit_form']) && isset($form['comment_settings'])) {

    // Determine if user has access to administer comments.
    if (comment_perm_administer_access($form['#node'])) {
      $form['comment_settings']['#access'] = TRUE;
    }
  }
}

/**
 * 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($nid = null) {
  global $user;
  if ($user->uid == 0) {
    return theme('comment_post_forbidden', node_load($nid));
  }
  else {
    return variable_get('comment_perm_message_reg', t("We're sorry, but you can't post comments here!"));
  }
}

/**
 * Control access for users with administer comments permissions.
 */
function comment_perm_administer_access($node) {
  global $user;
  $types = variable_get('comment_perm_node_types', array());
  if (isset($types[$node->type])) {
    $access_all = user_access($node->type . ': administer comment settings on any ' . $node->type . ' content');
    $access_own = user_access($node->type . ': administer comment settings on own ' . $node->type . ' content');

    // User has access to administer comments for current type of content.
    if ($access_all) {
      return TRUE;
    }

    // User has access to administer comments on own content.
    if ($access_own && $user->uid == $node->uid) {
      return TRUE;
    }
  }
  return FALSE;
}

Functions

Namesort descending Description
comment_perm_access Can the current user add comments to a given node?
comment_perm_administer_access Control access for users with administer comments permissions.
comment_perm_comment Implementation of hook_comment().
comment_perm_comment_view_alter
comment_perm_edit_access Can the current user edit comments?
comment_perm_form_alter Implementation of hook_form_alter().
comment_perm_menu Implementation of hook_menu().
comment_perm_node_view_alter Implementation of hook_node_view_alter().
comment_perm_permission Implementation of hook_permission().
comment_perm_post_without_permission_access Can the current user post comments without permission?
_comment_perm_access_denied_message