You are here

workbench_moderation_state_access.module in Workbench Moderation State Access 7

File

workbench_moderation_state_access.module
View source
<?php

/**
 * Implements hook_permission().
 *
 * Add a new permission for each state.
 */
function workbench_moderation_state_access_permission() {
  $permissions = array();

  // Get all moderation states.
  $states = workbench_moderation_states();

  // Permissions by moderation state.
  foreach ($states as $state) {
    $permissions["edit all content in {$state->name}"] = array(
      'title' => t('Edit all content in @state', array(
        '@state' => $state->label,
      )),
    );

    // Per-node-type state access.
    if (variable_get('workbench_moderation_per_node_type', FALSE)) {

      // Get all node types.
      $node_types = workbench_moderation_moderate_node_types();
      foreach ($node_types as $node_type) {
        $permissions["edit {$node_type} in {$state->name}"] = array(
          'title' => t('Edit @node_type in @state', array(
            '@node_type' => node_type_get_name($node_type),
            '@state' => $state->label,
          )),
        );
      }
    }
  }
  return $permissions;
}

/**
 * Implements hook_node_access().
 *
 * Deny access to edit nodes by moderation state.
 */
function workbench_moderation_state_access_node_access($node, $op, $account) {
  if ($op == 'update' && isset($node->workbench_moderation)) {

    // Get current state.
    $state = $node->workbench_moderation['current']->state;

    // Check if "per node type" method is enabled.
    $type = variable_get('workbench_moderation_per_node_type', FALSE);

    // Define the permission argument.
    $permission = $type ? "edit {$node->type} in {$state}" : "edit all content in {$state}";

    // Deny access access.
    if (!user_access($permission, $account)) {
      return NODE_ACCESS_DENY;
    }
  }
  return NODE_ACCESS_IGNORE;
}