You are here

function _edit_limit_user_acccess in Edit Limit 7

Same name and namespace in other branches
  1. 6 edit_limit.module \_edit_limit_user_acccess()

Helper function to perform additional access checks after node_access() has been checked.

1 call to _edit_limit_user_acccess()
edit_limit_node_access in ./edit_limit.module
Helper function to determine if the user has permissions to use whatever grants node_access() grants to this user, as determined by the additional site membership checks.

File

./edit_limit.module, line 95
edit_limit.module Primary module file for Edit Limit. This contains all the hooks needed to run the module.

Code

function _edit_limit_user_acccess($node) {

  // Be liberal here. The user has already passed node_access() by this point.
  $return = TRUE;

  // If this is a new node, let it go.
  if (empty($node->nid)) {
    return $return;
  }

  // Only limit edits if the content type is in the list.
  $allowed_types = variable_get('edit_limit_node_types', array());
  if (empty($allowed_types[$node->type])) {
    return $return;
  }

  // Get the time limit settings
  $edit_limit_node_time_enabled = variable_get('edit_limit_node_time_enabled', FALSE);
  $time_limits['default'] = intval(variable_get('edit_limit_node_time_default', 10)) * 24 * 60 * 60;
  $time_limits['node'][$node->type] = intval(variable_get('edit_limit_node_time_' . $node->type, $time_limits['default']));

  // Get the edit count limit settings
  $edit_limit_node_count_enabled = variable_get('edit_limit_node_count_enabled', FALSE);
  $count_limits['default'] = intval(variable_get('edit_limit_node_count_default', EDIT_LIMIT_NODE_COUNT_DEFAULT));
  $count_limits['node'][$node->type] = intval(variable_get('edit_limit_node_count_' . $node->type, $count_limits['default']));

  // If no options are enabled, we're done.
  if (!$edit_limit_node_time_enabled && !$edit_limit_node_count_enabled) {
    return $return;
  }

  // Test that the node hasn't been edited more than the allowed number of times.
  $count_limit_reached = FALSE;
  if ($edit_limit_node_count_enabled && $return) {
    $edit_count = edit_limit_node_count($node->nid);
    $count_limit = $count_limits['node'][$node->type] ? $count_limits['node'][$node->type] : $count_limits['node']['default'];
    if ($count_limit <= $edit_count) {
      $return = FALSE;
      $count_limit_reached = TRUE;
    }
  }

  // Test that the node was not created longer ago than the allowed time limit.
  if ($edit_limit_node_time_enabled && $return) {
    $time_unit = variable_get('edit_limit_node_time_unit', EDIT_LIMIT_TIME_UNIT_DEFAULT);
    $timeleft = intval($node->created) + intval(_unit_to_seconds(variable_get('edit_limit_node_time_default', EDIT_LIMIT_NODE_TIME_DEFAULT), $time_unit)) - REQUEST_TIME;
    drupal_add_js(drupal_get_path('module', 'edit_limit') . '/edit_limit.js');
    if ($timeleft <= 0) {
      $time_limit_reached = TRUE;
      $return = FALSE;
    }
    elseif (!$count_limit_reached && arg(0) == 'node' && arg(2) == 'edit') {
      static $type;
      if (empty($type)) {
        $type = 'status';
        $message = '<span class="edit-limit-time-unit-' . $time_unit . '">' . t('You have %seconds %time_unit left to edit this content.', array(
          '%seconds' => $timeleft,
          '%time_unit' => t('seconds'),
        )) . '</span>';
        drupal_set_message(filter_xss($message, array(
          'em',
          'span',
        )), $type, FALSE);
      }
    }
  }
  return $return;
}