You are here

class QuizResultAccessControlHandler in Quiz 6.x

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

Hierarchy

Expanded class hierarchy of QuizResultAccessControlHandler

File

src/Access/QuizResultAccessControlHandler.php, line 11

Namespace

Drupal\quiz\Access
View source
class QuizResultAccessControlHandler extends UncacheableEntityAccessControlHandler {

  /**
   * {@inheritdoc}
   */
  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);
  }

}

Members