Commit d5e99ddb authored by arpad.rozsa's avatar arpad.rozsa Committed by anon

Issue #2903220 by keithdoyle9, marcoscano, arpad.rozsa, Berdir, anon: Unable...

Issue #2903220 by keithdoyle9, marcoscano, arpad.rozsa, Berdir, anon: Unable to link to unpublished nodes
parent 3b5b286c
......@@ -3,6 +3,7 @@
namespace Drupal\linkit\Plugin\Linkit\Matcher;
use Drupal\Core\Form\FormStateInterface;
use Drupal\node\NodeInterface;
/**
* Provides specific linkit matchers for the node entity type.
......@@ -84,9 +85,27 @@ class NodeMatcher extends EntityMatcher {
protected function buildEntityQuery($search_string) {
$query = parent::buildEntityQuery($search_string);
$no_access = !$this->currentUser->hasPermission('bypass node access') && !count($this->moduleHandler->getImplementations('node_grants'));
if ($this->configuration['include_unpublished'] !== TRUE || $no_access) {
$query->condition('status', NODE_PUBLISHED);
if ($this->configuration['include_unpublished'] == FALSE) {
$query->condition('status', NodeInterface::PUBLISHED);
}
elseif (count($this->moduleHandler->getImplementations('node_grants')) === 0) {
if (($this->currentUser->hasPermission('bypass node access') || $this->currentUser->hasPermission('view any unpublished content'))) {
// User can see all content, no check necessary.
}
elseif ($this->currentUser->hasPermission('view own unpublished content')) {
// Users with "view own unpublished content" can see only their own.
if ($this->configuration['include_unpublished'] == TRUE) {
$or_condition = $query
->orConditionGroup()
->condition('status', NodeInterface::PUBLISHED)
->condition('uid', $this->currentUser->id());
$query->condition($or_condition);
}
}
}
else {
// All other users should only get published results.
$query->condition('status', NodeInterface::PUBLISHED);
}
return $query;
......
......@@ -18,7 +18,7 @@ class NodeMatcherTest extends LinkitKernelTestBase {
*
* @var array
*/
public static $modules = ['field', 'node'];
public static $modules = ['field', 'node', 'content_moderation', 'workflows'];
/**
* The matcher manager.
......@@ -34,6 +34,7 @@ class NodeMatcherTest extends LinkitKernelTestBase {
parent::setUp();
$this->installEntitySchema('node');
$this->installSchema('node', ['node_access']);
$this->installConfig(['field', 'node']);
$this->manager = $this->container->get('plugin.manager.linkit.matcher');
......@@ -74,7 +75,7 @@ class NodeMatcherTest extends LinkitKernelTestBase {
]);
$node->save();
// Unpublished node.
// Unpublished nodes.
$node = Node::create([
'title' => 'Lorem unpublishd',
'type' => $type1->id(),
......@@ -82,6 +83,13 @@ class NodeMatcherTest extends LinkitKernelTestBase {
]);
$node->save();
$node = Node::create([
'title' => 'Lorem unpublishd 2',
'type' => $type2->id(),
'status' => FALSE,
]);
$node->save();
// Set the current user to someone that is not the node owner.
\Drupal::currentUser()->setAccount($this->createUser([], ['access content']));
}
......@@ -133,7 +141,22 @@ class NodeMatcherTest extends LinkitKernelTestBase {
// Test with permissions to see unpublished nodes.
$suggestions = $plugin->execute('Lorem');
$this->assertEquals(5, count($suggestions->getSuggestions()), 'Correct number of suggestions');
// Test with permissions to see own unpublished nodes.
\Drupal::currentUser()->setAccount($this->createUser([], ['access content', 'view own unpublished content']));
$nodes = $this->container->get('entity_type.manager')->getStorage('node')->loadByProperties(['title' => 'Lorem unpublishd']);
$node4 = reset($nodes);
/** @var \Drupal\node\NodeInterface $node4 */
$node4->setOwnerId(\Drupal::currentUser()->id());
$node4->save();
$suggestions = $plugin->execute('Lorem');
$this->assertEquals(4, count($suggestions->getSuggestions()), 'Correct number of suggestions');
// Test with permissions to see any unpublished nodes.
\Drupal::currentUser()->setAccount($this->createUser([], ['access content', 'view any unpublished content']));
$suggestions = $plugin->execute('Lorem');
$this->assertEquals(5, count($suggestions->getSuggestions()), 'Correct number of suggestions');
}
/**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment