StatusAccessControlHandler.php 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace Drupal\statusmessage;
  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 Status entity.
  9. *
  10. * @see \Drupal\statusmessage\Entity\Status.
  11. */
  12. class StatusAccessControlHandler extends EntityAccessControlHandler {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  17. /** @var \Drupal\statusmessage\StatusInterface $entity */
  18. switch ($operation) {
  19. case 'view':
  20. if (!$entity->isPublished()) {
  21. return AccessResult::allowedIfHasPermission($account, 'view unpublished status entities');
  22. }
  23. return AccessResult::allowedIfHasPermission($account, 'view published status entities');
  24. case 'update':
  25. return AccessResult::allowedIfHasPermission($account, 'edit status entities');
  26. case 'delete':
  27. return AccessResult::allowedIfHasPermission($account, 'delete status 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 status entities');
  37. }
  38. }