Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
* Contains \Drupal\edit\Access\EditEntityAccessCheck.
*/
namespace Drupal\edit\Access;
Angie Byron
committed
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Session\AccountInterface;
Dries Buytaert
committed
use Symfony\Component\Routing\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Drupal\Core\Entity\EntityInterface;
/**
* Access check for editing entities.
*/
class EditEntityAccessCheck implements AccessInterface {
Dries Buytaert
committed
Angie Byron
committed
/**
* The entity manager.
*
Angie Byron
committed
* @var \Drupal\Core\Entity\EntityManagerInterface
Angie Byron
committed
*/
protected $entityManager;
/**
* Constructs a EditEntityAccessCheck object.
*
Angie Byron
committed
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
Angie Byron
committed
* The entity manager.
*/
Angie Byron
committed
public function __construct(EntityManagerInterface $entity_manager) {
Angie Byron
committed
$this->entityManager = $entity_manager;
}
Dries Buytaert
committed
/**
* {@inheritdoc}
*/
public function access(Route $route, Request $request, AccountInterface $account) {
Dries Buytaert
committed
// @todo Request argument validation and object loading should happen
// elsewhere in the request processing pipeline:
// http://drupal.org/node/1798214.
if (!$this->validateAndUpcastRequestAttributes($request)) {
return static::KILL;
}
Dries Buytaert
committed
return $this->accessEditEntity($request->attributes->get('entity'), $account) ? static::ALLOW : static::DENY;
Dries Buytaert
committed
}
/**
* {@inheritdoc}
*/
protected function accessEditEntity(EntityInterface $entity, $account) {
return $entity->access('update', $account);
Dries Buytaert
committed
}
/**
* Validates and upcasts request attributes.
*/
protected function validateAndUpcastRequestAttributes(Request $request) {
// Load the entity.
if (!is_object($entity = $request->attributes->get('entity'))) {
$entity_id = $entity;
$entity_type = $request->attributes->get('entity_type');
Angie Byron
committed
if (!$entity_type || !$this->entityManager->getDefinition($entity_type)) {
return FALSE;
Dries Buytaert
committed
}
catch
committed
$entity = $this->entityManager->getStorage($entity_type)->load($entity_id);
Dries Buytaert
committed
if (!$entity) {
return FALSE;
Dries Buytaert
committed
}
$request->attributes->set('entity', $entity);
}
return TRUE;