You are here

protected function PrivateMessageAccessControlHandler::checkAccess in Private Message 8

Same name and namespace in other branches
  1. 8.2 src/Entity/Access/PrivateMessageAccessControlHandler.php \Drupal\private_message\Entity\Access\PrivateMessageAccessControlHandler::checkAccess()

Link the activities to the permissions. checkAccess is called with the $operation as defined in the routing.yml file.

Overrides EntityAccessControlHandler::checkAccess

File

src/Entity/Access/PrivateMessageAccessControlHandler.php, line 64

Class

PrivateMessageAccessControlHandler
Access control handler for private message entities.

Namespace

Drupal\private_message\Entity\Access

Code

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();
}