access_policy-1.0.x-dev/src/Plugin/access_policy/AccessRule/IsOwn.php
src/Plugin/access_policy/AccessRule/IsOwn.php
<?php namespace Drupal\access_policy\Plugin\access_policy\AccessRule; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Session\AccountInterface; use Drupal\user\EntityOwnerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** * Is authored by the current user. * * Checks to see if the entity was created by the current user. * * @AccessRule( * id = "is_own", * handlers = { * "query_alter" = "\Drupal\access_policy\AccessRuleQueryHandler\IsOwn" * } * ) */ class IsOwn extends AccessRuleBase implements ContainerFactoryPluginInterface { /** * The entity type manager. * * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ protected $entityTypeManager; /** * Constructs a new IsOwn access rule. * * @param array $configuration * A configuration array containing information about the plugin instance. * @param string $plugin_id * The plugin_id for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager * The entity type manager. */ public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager) { parent::__construct($configuration, $plugin_id, $plugin_definition); $this->entityTypeManager = $entity_type_manager; } /** * {@inheritdoc} */ public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { return new static( $configuration, $plugin_id, $plugin_definition, $container->get('entity_type.manager'), ); } /** * {@inheritdoc} */ public function isApplicable(EntityInterface $entity) { if ($entity instanceof EntityOwnerInterface) { return TRUE; } return FALSE; } /** * {@inheritdoc} */ public function accessRuleForm(array $form, FormStateInterface $form_state) { return $form; } /** * {@inheritdoc} */ public function validate(EntityInterface $entity, AccountInterface $account) { if ($entity->getOwnerId() == $account->id()) { return TRUE; } return FALSE; } /** * {@inheritdoc} */ public function getCacheContexts() { return ['user']; } }