public function CertificateController::viewAccess in Opigno certificate 3.x
Same name and namespace in other branches
- 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'
File
- src/
Controller/ CertificateController.php, line 113
Class
- CertificateController
- Defines a controller to render a single opigno_certificate.
Namespace
Drupal\opigno_certificate\ControllerCode
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();
}