8d9d11911c87c166fa14a666d50f61bcc9fb67b3
[yaffs-website] / web / core / modules / node / src / Access / NodeAddAccessCheck.php
1 <?php
2
3 namespace Drupal\node\Access;
4
5 use Drupal\Core\Access\AccessResult;
6 use Drupal\Core\Entity\EntityManagerInterface;
7 use Drupal\Core\Routing\Access\AccessInterface;
8 use Drupal\Core\Session\AccountInterface;
9 use Drupal\node\NodeTypeInterface;
10
11 /**
12  * Determines access to for node add pages.
13  *
14  * @ingroup node_access
15  */
16 class NodeAddAccessCheck implements AccessInterface {
17
18   /**
19    * The entity manager.
20    *
21    * @var \Drupal\Core\Entity\EntityManagerInterface
22    */
23   protected $entityManager;
24
25   /**
26    * Constructs a EntityCreateAccessCheck object.
27    *
28    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
29    *   The entity manager.
30    */
31   public function __construct(EntityManagerInterface $entity_manager) {
32     $this->entityManager = $entity_manager;
33   }
34
35   /**
36    * Checks access to the node add page for the node type.
37    *
38    * @param \Drupal\Core\Session\AccountInterface $account
39    *   The currently logged in account.
40    * @param \Drupal\node\NodeTypeInterface $node_type
41    *   (optional) The node type. If not specified, access is allowed if there
42    *   exists at least one node type for which the user may create a node.
43    *
44    * @return string
45    *   A \Drupal\Core\Access\AccessInterface constant value.
46    */
47   public function access(AccountInterface $account, NodeTypeInterface $node_type = NULL) {
48     $access_control_handler = $this->entityManager->getAccessControlHandler('node');
49     // If checking whether a node of a particular type may be created.
50     if ($account->hasPermission('administer content types')) {
51       return AccessResult::allowed()->cachePerPermissions();
52     }
53     if ($node_type) {
54       return $access_control_handler->createAccess($node_type->id(), $account, [], TRUE);
55     }
56     // If checking whether a node of any type may be created.
57     foreach ($this->entityManager->getStorage('node_type')->loadMultiple() as $node_type) {
58       if (($access = $access_control_handler->createAccess($node_type->id(), $account, [], TRUE)) && $access->isAllowed()) {
59         return $access;
60       }
61     }
62
63     // No opinion.
64     return AccessResult::neutral();
65   }
66
67 }