You are here

function QuizResultAccessControlHandler::checkAccess in Quiz 6.x

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

File

src/Access/QuizResultAccessControlHandler.php, line 16

Class

QuizResultAccessControlHandler

Namespace

Drupal\quiz\Access

Code

function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  $current_user = Drupal::currentUser();
  if ($operation == 'view') {
    if ($current_user
      ->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 ($current_user
      ->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 ($current_user
      ->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);
}