ConnectionAccessControlHandler.php in RedHen CRM 8
File
modules/redhen_connection/src/ConnectionAccessControlHandler.php
View source
<?php
namespace Drupal\redhen_connection;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
class ConnectionAccessControlHandler extends EntityAccessControlHandler {
protected $viewLabelOperation = TRUE;
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
$entity_bundle = $entity
->getType();
switch ($operation) {
case 'view label':
case 'view':
if ($entity
->isActive()) {
$view_access = AccessResult::allowedIfHasPermissions($account, [
'view active connection entities',
'view active ' . $entity_bundle . ' connection',
], 'OR');
}
else {
$view_access = AccessResult::allowedIfHasPermissions($account, [
'view inactive connection entities',
'view inactive ' . $entity_bundle . ' connection',
], 'OR');
}
return $view_access;
case 'update':
$edit_access = AccessResult::allowedIfHasPermissions($account, [
'edit connection entities',
'edit ' . $entity_bundle . ' connection',
], 'OR');
return $edit_access;
case 'delete':
$delete_access = AccessResult::allowedIfHasPermissions($account, [
'delete connection entities',
'delete ' . $entity_bundle . ' connection',
], 'OR');
return $delete_access;
}
return AccessResult::neutral();
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
if (!$entity_bundle) {
$types = \Drupal::entityTypeManager()
->getStorage('redhen_connection_type')
->loadMultiple();
if ($types && count($types) == 1) {
$entity_bundle = array_keys($types)[0];
}
}
return AccessResult::allowedIfHasPermissions($account, [
'add connection entities',
'add ' . $entity_bundle . ' connection',
], 'OR');
}
}