DateFormatAccessControlHandler.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. <?php
  2. namespace Drupal\system;
  3. use Drupal\Core\Access\AccessResult;
  4. use Drupal\Core\Entity\EntityAccessControlHandler;
  5. use Drupal\Core\Entity\EntityInterface;
  6. use Drupal\Core\Session\AccountInterface;
  7. /**
  8. * Defines the access control handler for the date format entity type.
  9. *
  10. * @see \Drupal\system\Entity\DateFormat
  11. */
  12. class DateFormatAccessControlHandler extends EntityAccessControlHandler {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. protected $viewLabelOperation = TRUE;
  17. /**
  18. * {@inheritdoc}
  19. */
  20. protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  21. // There are no restrictions on viewing the label of a date format.
  22. if ($operation === 'view label') {
  23. return AccessResult::allowed();
  24. }
  25. // Locked date formats cannot be updated or deleted.
  26. elseif (in_array($operation, ['update', 'delete'])) {
  27. if ($entity->isLocked()) {
  28. return AccessResult::forbidden('The DateFormat config entity is locked.')->addCacheableDependency($entity);
  29. }
  30. else {
  31. return parent::checkAccess($entity, $operation, $account)->addCacheableDependency($entity);
  32. }
  33. }
  34. return parent::checkAccess($entity, $operation, $account);
  35. }
  36. }