PrivateMessageAccessControlHandler.php in Private Message 8
File
src/Entity/Access/PrivateMessageAccessControlHandler.php
View source
<?php
namespace Drupal\private_message\Entity\Access;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Entity\EntityAccessControlHandler;
use Drupal\Core\Entity\EntityHandlerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Plugin\Context\ContextHandlerInterface;
use Drupal\Core\Plugin\Context\ContextRepositoryInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\private_message\Service\PrivateMessageServiceInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PrivateMessageAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
protected $privateMessageService;
public function __construct(EntityTypeInterface $entity_type, ContextHandlerInterface $context_handler, ContextRepositoryInterface $context_repository, PrivateMessageServiceInterface $privateMessageService) {
parent::__construct($entity_type, $context_handler, $context_repository);
$this->privateMessageService = $privateMessageService;
}
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static($entity_type, $container
->get('context.handler'), $container
->get('context.repository'), $container
->get('private_message.service'));
}
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if ($account
->hasPermission('use private messaging system')) {
switch ($operation) {
case 'view':
if ($entity
->getOwner()
->id() == $account
->id()) {
return AccessResult::allowed();
}
$private_message_thread = $this->privateMessageService
->getThreadFromMessage($entity);
if ($private_message_thread
->isMember($account
->id())) {
return AccessResult::allowed();
}
break;
case 'delete':
if ($entity
->getOwner()
->id() == $account
->id()) {
return AccessResult::allowed();
}
$private_message_thread = $this->privateMessageService
->getThreadFromMessage($entity);
if ($private_message_thread
->isMember($account
->id())) {
return AccessResult::allowed();
}
break;
}
}
return AccessResult::forbidden();
}
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'use private messaging system');
}
}