You are here

function workflow_tab_access in Workflow 7.2

Same name and namespace in other branches
  1. 7 workflow.module \workflow_tab_access()

Menu access control callback. Determine access to Workflow tab.

The History tab should not be used with multiple workflows per node. Use the dedicated view for this use case.

@todo D8: remove this in favour of View 'Workflow history per entity'.

2 calls to workflow_tab_access()
workflow_node_tab_access in ./workflow.deprecated.inc
Menu access control callback. Determine access to Workflow tab.
workflow_views_handler_field_node_link_workflow::render_link in workflow_views/handlers/workflow_views_handler_field_node_link_workflow.inc
Renders the link.
1 string reference to 'workflow_tab_access'
workflow_menu_alter in ./workflow.module
Implements hook_menu_alter().

File

./workflow.module, line 219
Support workflows made up of arbitrary states.

Code

function workflow_tab_access($entity_type, $entity) {
  global $user;
  static $access = array();

  // $figure out the $entity's bundle and id.
  list($entity_id, , $entity_bundle) = entity_extract_ids($entity_type, $entity);
  if (isset($access[$user->uid][$entity_type][$entity_id])) {
    return $access[$user->uid][$entity_type][$entity_id];
  }

  // When having multiple workflows per bundle, use Views display
  // 'Workflow history per entity' instead!
  if (!is_null($field_name = workflow_get_field_name($entity, $entity_type, NULL, $entity_id))) {

    // Get the role IDs of the user. Workflow only stores Ids, not role names.
    $roles = array_keys($user->roles);

    // Some entities (e.g., taxonomy_term) do not have a uid.
    $entity_uid = isset($entity->uid) ? $entity->uid : 0;

    // If this is a new page, give the authorship role.
    if (!$entity_id) {
      $roles = array_merge(array(
        WORKFLOW_ROLE_AUTHOR_RID,
      ), $roles);
    }
    elseif ($entity_uid > 0 && $user->uid > 0 && $entity_uid == $user->uid) {
      $roles = array_merge(array(
        WORKFLOW_ROLE_AUTHOR_RID,
      ), $roles);
    }

    // Get the permissions from the workflow settings.
    // @todo: workflow_tab_access(): what to do with multiple workflow_fields per bundle? Use Views instead!
    $tab_roles = array();
    $history_tab_show = FALSE;
    $fields = _workflow_info_fields($entity, $entity_type, $entity_bundle);
    foreach ($fields as $field) {
      $tab_roles += $field['settings']['history']['roles'];
      $history_tab_show |= $field['settings']['history']['history_tab_show'];
    }
    if ($history_tab_show == FALSE) {
      $access[$user->uid][$entity_type][$entity_id] = FALSE;
    }
    elseif (user_access('administer nodes') || array_intersect($roles, $tab_roles)) {
      $access[$user->uid][$entity_type][$entity_id] = TRUE;
    }
    else {
      $access[$user->uid][$entity_type][$entity_id] = FALSE;
    }
    return $access[$user->uid][$entity_type][$entity_id];
  }
  return FALSE;
}