You are here

CourseAccessControlHandler.php in Course 3.x

File

src/Access/CourseAccessControlHandler.php
View source
<?php

namespace Drupal\course\Access;

use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\entity\UncacheableEntityAccessControlHandler;

/**
 * Access controller for the Course entity.
 */
class CourseAccessControlHandler extends UncacheableEntityAccessControlHandler {

  /**
   * Handle multiple access denied messages (for enrolling and taking).
   *
   * {@inheritdoc}
   */
  protected function processAccessHookResults(array $access) {

    // @todo sort by weight and allow altering
    return parent::processAccessHookResults($access);
  }

  /**
   * Grant access to reports.
   *
   * {@inheritdoc}
   */
  protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
    if ($operation == 'report') {
      if ($account
        ->hasPermission('access all course reports')) {

        // If user has this permission then allow access without being able to
        // update.
        return AccessResultAllowed::allowed();
      }
      else {

        // Otherwise, check if the user can update this course.
        return parent::checkAccess($entity, 'update', $account);
      }
    }
    return parent::checkAccess($entity, $operation, $account);
  }

}

Classes

Namesort descending Description
CourseAccessControlHandler Access controller for the Course entity.