You are here

public function VotingApiWidgetBase::canVote in Votingapi Widgets 8

Checks whether currentUser is allowed to vote.

Return value

bool True if user is allowed to vote

File

src/Plugin/VotingApiWidgetBase.php, line 166

Class

VotingApiWidgetBase
Base class for Voting api widget plugins.

Namespace

Drupal\votingapi_widgets\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;
  }
  $perm = 'vote on ' . $vote
    ->getVotedEntityType() . ':' . $entity
    ->bundle() . ':' . $vote->field_name->value;
  if (!$vote
    ->isNew()) {
    $perm = 'edit own vote on ' . $vote
      ->getVotedEntityType() . ':' . $entity
      ->bundle() . ':' . $vote->field_name->value;
  }
  return $account
    ->hasPermission($perm);
}