View source
<?php
namespace Drupal\activity_creator;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Access\AccessResult;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ActivityAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
protected $entityTypeManager;
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('entity_type.manager'));
}
public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entityTypeManager) {
parent::__construct($entity_type);
$this->entityTypeManager = $entityTypeManager;
}
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
switch ($operation) {
case 'view':
$recipient = $entity
->getRecipient();
if ($recipient === NULL) {
return $this
->returnAccessRelatedObject($entity, $operation, $account);
}
$recipient_type = $recipient['0']['target_type'];
if ($recipient_type === 'user') {
$recipient_id = $recipient['0']['target_id'];
if ($this
->checkIfPersonalNotification($entity) === TRUE) {
return AccessResult::allowedIf($account
->id() === $recipient_id);
}
return $this
->returnAccessRelatedObject($entity, $operation, $account);
}
return AccessResult::allowedIfHasPermission($account, 'view all published activity entities');
case 'update':
return AccessResult::allowedIfHasPermission($account, 'edit activity entities');
case 'delete':
return AccessResult::allowedIfHasPermission($account, 'delete activity entities');
}
return AccessResult::neutral();
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'add activity entities');
}
protected function returnAccessRelatedObject(EntityInterface $entity, $operation, $account) {
$related_object = $entity
->get('field_activity_entity')
->getValue();
if (!empty($related_object)) {
$ref_entity_type = $related_object['0']['target_type'];
$ref_entity_id = $related_object['0']['target_id'];
try {
$ref_entity = $this->entityTypeManager
->getStorage($ref_entity_type)
->load($ref_entity_id);
} catch (InvalidPluginDefinitionException|PluginNotFoundException $e) {
return AccessResult::neutral(sprintf('No opinion on access due to: %s', $e
->getMessage()));
}
return AccessResult::allowedIf($ref_entity
->access($operation, $account));
}
return AccessResult::neutral('No opinion on access due to: no related object found');
}
protected function checkIfPersonalNotification(EntityInterface $entity) {
$recipient = $entity
->getRecipient();
$value = FALSE;
if (!empty($recipient) && $recipient['0']['target_type'] === 'user') {
$destinations = $entity
->getDestinations();
$is_notification = in_array('notifications', $destinations, TRUE);
if ($is_notification === TRUE && count($destinations) <= 1) {
$value = TRUE;
}
}
return $value;
}
}