You are here

function QuizResultAccessControlHandler::checkAccess in Quiz 8.5

Same name and namespace in other branches
  1. 8.6 src/Access/QuizResultAccessControlHandler.php \Drupal\quiz\Access\QuizResultAccessControlHandler::checkAccess()
  2. 6.x src/Access/QuizResultAccessControlHandler.php \Drupal\quiz\Access\QuizResultAccessControlHandler::checkAccess()

Performs access checks.

This method is supposed to be overwritten by extending classes that do their own custom access checking.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity for which to check access.

string $operation: The entity operation. Usually one of 'view', 'view label', 'update' or 'delete'.

\Drupal\Core\Session\AccountInterface $account: The user for which to check access.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

Overrides EntityAccessControlHandlerBase::checkAccess

File

src/Access/QuizResultAccessControlHandler.php, line 15

Class

QuizResultAccessControlHandler

Namespace

Drupal\quiz\Access

Code

function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  if ($operation == 'view') {
    if (Drupal::currentUser()
      ->hasPermission('view results for own quiz') && $account
      ->id() == $entity
      ->getQuiz()
      ->get('uid')
      ->getString()) {

      // User can view all quiz results for a quiz they authorized.
      return AccessResultAllowed::allowed();
    }
    if (Drupal::currentUser()
      ->hasPermission('view own quiz_result') && $account
      ->id() == $entity
      ->get('uid')
      ->getString()) {

      // User can view their own quiz result.
      return AccessResultAllowed::allowed();
    }
  }
  if ($operation == 'update') {
    if (Drupal::currentUser()
      ->hasPermission('score own quiz') && $account
      ->id() == $entity
      ->getQuiz()
      ->get('uid')
      ->getString()) {

      // User can view all quiz results for a quiz they authored.
      return AccessResultAllowed::allowed();
    }
  }
  return parent::checkAccess($entity, $operation, $account);
}