You are here

public function RateWidgetBase::canVote in Rate 8.2

Checks whether currentUser is allowed to vote.

Return value

bool True if user is allowed to vote

File

src/Plugin/RateWidgetBase.php, line 200

Class

RateWidgetBase
Base class for Rate widget plugins.

Namespace

Drupal\rate\Plugin

Code

public function canVote($vote, $account = FALSE) {
  if (!$account) {
    $account = $this->account;
  }
  $entity = $this->entityTypeManager
    ->getStorage($vote
    ->getVotedEntityType())
    ->load($vote
    ->getVotedEntityId());
  if (!$entity) {
    return FALSE;
  }
  if ($vote
    ->getVotedEntityType() == 'comment') {
    $perm = 'cast rate vote on ' . $entity
      ->getFieldName() . ' on ' . $entity
      ->getCommentedEntityTypeId() . ' of ' . $entity
      ->getCommentedEntity()
      ->bundle();
  }
  else {
    $perm = 'cast rate vote on ' . $vote
      ->getVotedEntityType() . ' of ' . $entity
      ->bundle();
  }
  $can_vote = $account
    ->hasPermission($perm);

  // Allow modules to implement custom logic for user vote check.
  $this->moduleHandler
    ->invokeAll('rate_can_vote', [
    &$can_vote,
    $vote,
    $entity,
    $account,
  ]);
  return $can_vote;
}