You are here

class FlagAccessCheck 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
  2. 8.5 modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck
  3. 8.6 modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck
  4. 8.8 modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck
  5. 10.3.x modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck
  6. 10.0.x modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck
  7. 10.1.x modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck
  8. 10.2.x modules/social_features/social_content_report/src/Access/FlagAccessCheck.php \Drupal\social_content_report\Access\FlagAccessCheck

Class FlagAccessCheck.

Hierarchy

Expanded class hierarchy of FlagAccessCheck

1 file declares its use of FlagAccessCheck
RouteSubscriber.php in modules/social_features/social_content_report/src/Routing/RouteSubscriber.php

File

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

Namespace

Drupal\social_content_report\Access
View source
class FlagAccessCheck implements AccessInterface, ContainerInjectionInterface {

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The content report service.
   *
   * @var \Drupal\social_content_report\ContentReportServiceInterface
   */
  protected $socialContentReport;

  /**
   * FlagAccessCheck constructor.
   *
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\social_content_report\ContentReportServiceInterface $social_content_report
   *   The content report service.
   */
  public function __construct(EntityTypeManagerInterface $entity_type_manager, ContentReportServiceInterface $social_content_report) {
    $this->entityTypeManager = $entity_type_manager;
    $this->socialContentReport = $social_content_report;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('entity_type.manager'), $container
      ->get('social_content_report.content_report_service'));
  }

  /**
   * Checks if user is allowed to flag.
   *
   * @param \Drupal\Core\Session\AccountInterface $account
   *   Run access checks for this account.
   * @param \Drupal\flag\FlagInterface $flag
   *   The flag type.
   * @param int $entity_id
   *   The entity ID which is being reported.
   *
   * @return \Drupal\Core\Access\AccessResult
   *   Allowed if user may use the flag and hasn't reported it yet.
   */
  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();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FlagAccessCheck::$entityTypeManager protected property The entity type manager.
FlagAccessCheck::$socialContentReport protected property The content report service.
FlagAccessCheck::access public function Checks if user is allowed to flag.
FlagAccessCheck::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
FlagAccessCheck::__construct public function FlagAccessCheck constructor.