TermAccessControlHandler.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Drupal\taxonomy;
  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 taxonomy term entity type.
  9. *
  10. * @see \Drupal\taxonomy\Entity\Term
  11. */
  12. class TermAccessControlHandler extends EntityAccessControlHandler {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
  17. if ($account->hasPermission('administer taxonomy')) {
  18. return AccessResult::allowed()->cachePerPermissions();
  19. }
  20. switch ($operation) {
  21. case 'view':
  22. $access_result = AccessResult::allowedIf($account->hasPermission('access content') && $entity->isPublished())
  23. ->cachePerPermissions()
  24. ->addCacheableDependency($entity);
  25. if (!$access_result->isAllowed()) {
  26. $access_result->setReason("The 'access content' permission is required and the taxonomy term must be published.");
  27. }
  28. return $access_result;
  29. case 'update':
  30. if ($account->hasPermission("edit terms in {$entity->bundle()}")) {
  31. return AccessResult::allowed()->cachePerPermissions();
  32. }
  33. return AccessResult::neutral()->setReason("The following permissions are required: 'edit terms in {$entity->bundle()}' OR 'administer taxonomy'.");
  34. case 'delete':
  35. if ($account->hasPermission("delete terms in {$entity->bundle()}")) {
  36. return AccessResult::allowed()->cachePerPermissions();
  37. }
  38. return AccessResult::neutral()->setReason("The following permissions are required: 'delete terms in {$entity->bundle()}' OR 'administer taxonomy'.");
  39. default:
  40. // No opinion.
  41. return AccessResult::neutral()->cachePerPermissions();
  42. }
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
  48. return AccessResult::allowedIfHasPermissions($account, ["create terms in $entity_bundle", 'administer taxonomy'], 'OR');
  49. }
  50. }