You are here

function tmgmt_local_translation_access in Translation Management Tool 8

Same name and namespace in other branches
  1. 7 translators/tmgmt_local/tmgmt_local.module \tmgmt_local_translation_access()

Determine whether the current user is allowed to translate a given translation task.

Parameters

$task: The translation task to be translated.

$account: (Optional) A user object representing the user that is trying to obtain translation access. Determines access for a user other than the current user.

Return value

bool TRUE if the user is allowed to translate the given translation job, FALSE otherwise.

1 call to tmgmt_local_translation_access()
LocalTaskListBuilder::getDefaultOperations in translators/tmgmt_local/src/Entity/ListBuilder/LocalTaskListBuilder.php
Gets this list's default operations.

File

translators/tmgmt_local/tmgmt_local.module, line 193
Main module file for the local translation module.

Code

function tmgmt_local_translation_access(LocalTask $task, AccountInterface $account = NULL) {
  $job = $task
    ->getJob();
  if (!$job || !$job
    ->isActive()) {
    return FALSE;
  }
  $rights =& drupal_static(__FUNCTION__);

  // If no user object is supplied, the access check is for the current user.
  if (empty($account)) {
    $account = \Drupal::currentUser();
  }

  // If we've already checked access for this job and user, return from cache.
  if (isset($rights[$account
    ->id()][$job
    ->id()])) {
    return $rights[$account
      ->id()][$job
      ->id()];
  }

  // We grant access to the translation if both of the following conditions are
  // met:
  // - User is assigned as a assignee to the given task.
  // - User has 'provide translation services' permission.
  // - No modules say to deny access.
  // - At least one module says to grant access.
  // - User has translation abilities for this task.
  if (!\Drupal::currentUser()
    ->hasPermission('provide translation services')) {
    $rights[$account
      ->id()][$job
      ->id()] = FALSE;
    return FALSE;
  }
  if ($task->tuid == $account
    ->id()) {
    $rights[$account
      ->id()][$job
      ->id()] = TRUE;
    return TRUE;
  }
  $access = \Drupal::moduleHandler()
    ->invokeAll('tmgmt_local_translation_access', array(
    $job,
    $account,
  ));
  if (in_array(TMGMT_LOCAL_TRANSLATION_ACCESS_DENY, $access, TRUE)) {
    $rights[$account
      ->id()][$job
      ->id()] = FALSE;
    return FALSE;
  }
  elseif (in_array(TMGMT_LOCAL_TRANSLATION_ACCESS_ALLOW, $access, TRUE)) {
    $rights[$account
      ->id()][$job
      ->id()] = TRUE;
    return TRUE;
  }

  // Lastly, check for the translation abilities.
  $target_languages = tmgmt_local_supported_target_languages($job
    ->getSourceLangcode(), array(
    $account
      ->id(),
  ));
  $rights[$account
    ->id()][$job
    ->id()] = in_array($job
    ->getTargetLangcode(), $target_languages);
  return $rights[$account
    ->id()][$job
    ->id()];
}