You are here

public function CertificateController::viewAccess in Opigno certificate 3.x

Same name and namespace in other branches
  1. 8 src/Controller/CertificateController.php \Drupal\opigno_certificate\Controller\CertificateController::viewAccess()

Checks access for the controller.

Parameters

string $entity_type: The entity type.

string $entity_id: The entity ID.

Return value

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

Throws

\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException

\Drupal\Component\Plugin\Exception\PluginNotFoundException

1 string reference to 'CertificateController::viewAccess'
opigno_certificate.routing.yml in ./opigno_certificate.routing.yml
opigno_certificate.routing.yml

File

src/Controller/CertificateController.php, line 113

Class

CertificateController
Defines a controller to render a single opigno_certificate.

Namespace

Drupal\opigno_certificate\Controller

Code

public function viewAccess($entity_type, $entity_id) : AccessResultInterface {
  $entity = $this->entityTypeManager
    ->getStorage($entity_type)
    ->load($entity_id);
  if (!$entity instanceof ContentEntityInterface || !$entity
    ->hasField('field_certificate')) {
    return AccessResult::forbidden();
  }
  $opigno_certificate = $entity
    ->get('field_certificate')->entity;
  if (!$opigno_certificate instanceof OpignoCertificateInterface) {
    return AccessResult::forbidden();
  }
  $opigno_certificate
    ->set('referencing_entity', $entity);
  $access_result = AccessResult::allowedIfHasPermission($this->currentUser, 'administer certificates');
  if ($access_result
    ->isAllowed()) {
    return $access_result;
  }

  // Check access against the entity referencing the opigno_certificate
  // instead of the opigno_certificate itself,
  // so that each entity can have its own access check,
  // but use 'view opigno_certificate'
  // so that the access is specific to viewing opigno_certificates.
  $access_result = $entity
    ->access('view certificate', $this->currentUser, TRUE);
  return $access_result instanceof AccessResultInterface ? $access_result : AccessResult::forbidden();
}