You are here

public function FlagAccessCheck::access in Open Social 8.7

Same name and namespace in other branches
  1. 8.9 modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck::access()
  2. 8.5 modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck::access()
  3. 8.6 modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck::access()
  4. 8.8 modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck::access()
  5. 10.3.x modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck::access()
  6. 10.0.x modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck::access()
  7. 10.1.x modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck::access()
  8. 10.2.x modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck::access()

Checks if user is allowed to flag.

Parameters

\Drupal\Core\Session\AccountInterface $account: Run access checks for this account.

\Drupal\flag\FlagInterface $flag: The flag type.

int $entity_id: The entity ID which is being reported.

Return value

\Drupal\Core\Access\AccessResult Allowed if user may use the flag and hasn't reported it yet.

File

modules/social_features/social_content_report/src/Access/FlagAccessCheck.php, line 72

Class

FlagAccessCheck
Class FlagAccessCheck.

Namespace

Drupal\social_content_report\Access

Code

public function access(AccountInterface $account, FlagInterface $flag, $entity_id) {
  if (in_array($flag
    ->id(), $this->socialContentReport
    ->getReportFlagTypes())) {

    // Make sure user is allowed to use the flag.
    if (!$account
      ->hasPermission('flag ' . $flag
      ->id())) {
      return AccessResult::forbidden();
    }
    $entity = $this->entityTypeManager
      ->getStorage($flag
      ->getFlaggableEntityTypeId())
      ->load($entity_id);
    $flagged = $flag
      ->isFlagged($entity, $account);

    // If the user already flagged the content they aren't allowed to do it
    // again.
    return $flagged ? AccessResult::forbidden() : AccessResult::allowed();
  }
  return AccessResult::neutral();
}