Pull merge.
[yaffs-website] / web / core / modules / taxonomy / src / TermAccessControlHandler.php
1 <?php
2
3 namespace Drupal\taxonomy;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityAccessControlHandler;
7 use Drupal\Core\Entity\EntityInterface;
8 use Drupal\Core\Session\AccountInterface;
9
10 /**
11  * Defines the access control handler for the taxonomy term entity type.
12  *
13  * @see \Drupal\taxonomy\Entity\Term
14  */
15 class TermAccessControlHandler extends EntityAccessControlHandler {
16
17   /**
18    * {@inheritdoc}
19    */
20   protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
21     if ($account->hasPermission('administer taxonomy')) {
22       return AccessResult::allowed()->cachePerPermissions();
23     }
24
25     switch ($operation) {
26       case 'view':
27         $access_result = AccessResult::allowedIf($account->hasPermission('access content') && $entity->isPublished())
28           ->cachePerPermissions()
29           ->addCacheableDependency($entity);
30         if (!$access_result->isAllowed()) {
31           $access_result->setReason("The 'access content' permission is required and the taxonomy term must be published.");
32         }
33         return $access_result;
34
35       case 'update':
36         if ($account->hasPermission("edit terms in {$entity->bundle()}")) {
37           return AccessResult::allowed()->cachePerPermissions();
38         }
39
40         return AccessResult::neutral()->setReason("The following permissions are required: 'edit terms in {$entity->bundle()}' OR 'administer taxonomy'.");
41
42       case 'delete':
43         if ($account->hasPermission("delete terms in {$entity->bundle()}")) {
44           return AccessResult::allowed()->cachePerPermissions();
45         }
46
47         return AccessResult::neutral()->setReason("The following permissions are required: 'delete terms in {$entity->bundle()}' OR 'administer taxonomy'.");
48
49       default:
50         // No opinion.
51         return AccessResult::neutral()->cachePerPermissions();
52     }
53   }
54
55   /**
56    * {@inheritdoc}
57    */
58   protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
59     return AccessResult::allowedIfHasPermissions($account, ["create terms in $entity_bundle", 'administer taxonomy'], 'OR');
60   }
61
62 }