annotate core/modules/node/src/Access/NodeAddAccessCheck.php @ 19:fa3358dc1485 tip

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