annotate core/modules/node/src/Access/NodeAddAccessCheck.php @ 17:129ea1e6d783

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