HeartbeatAccessControlHandler.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace Drupal\heartbeat;
  3. use Drupal\Core\Entity\EntityAccessControlHandler;
  4. use Drupal\Core\Entity\EntityInterface;
  5. use Drupal\Core\Session\AccountInterface;
  6. use Drupal\Core\Access\AccessResult;
  7. /**
  8. * Access controller for the Heartbeat entity.
  9. *
  10. * @see \Drupal\heartbeat\Entity\Heartbeat.
  11. */
  12. class HeartbeatAccessControlHandler extends EntityAccessControlHandler {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  17. /** @var \Drupal\heartbeat\Entity\HeartbeatInterface $entity */
  18. switch ($operation) {
  19. case 'view':
  20. if (!$entity->isPublished()) {
  21. return AccessResult::allowedIfHasPermission($account, 'view unpublished heartbeat entities');
  22. }
  23. return AccessResult::allowedIfHasPermission($account, 'view published heartbeat entities');
  24. case 'update':
  25. return AccessResult::allowedIfHasPermission($account, 'edit heartbeat entities');
  26. case 'delete':
  27. return AccessResult::allowedIfHasPermission($account, 'delete heartbeat entities');
  28. }
  29. // Unknown operation, no opinion.
  30. return AccessResult::neutral();
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
  36. return AccessResult::allowedIfHasPermission($account, 'add heartbeat entities');
  37. }
  38. }