Skip to content
Snippets Groups Projects
Commit ce46bdd5 authored by Pablo López's avatar Pablo López Committed by Christian López Espínola
Browse files

Issue #3375471 by plopesc, penyaskito, zaporylie: DashboardLocalAction results...

parent 7cf70d1e
No related branches found
No related tags found
1 merge request!2Issue #3375471 by plopesc, penyaskito, zaporylie: DashboardLocalAction results...
Pipeline #147821 passed
......@@ -4,7 +4,7 @@ dashboard:
_controller: '\Drupal\dashboard\Controller\DashboardController::build'
_title: 'Dashboard'
requirements:
_permission: 'access administration pages'
_custom_access: '\Drupal\dashboard\Controller\DashboardController::access'
entity.dashboard.canonical:
path: '/admin/dashboard/{dashboard}'
......
......@@ -3,6 +3,7 @@
namespace Drupal\dashboard\Controller;
use Drupal\Component\Utility\Html;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Plugin\Context\EntityContext;
use Drupal\dashboard\DashboardManager;
......@@ -42,6 +43,18 @@ class DashboardController extends ControllerBase {
);
}
/**
* Access callback for the Dashboard page.
*
* @return \Drupal\Core\Access\AccessResultInterface
* Whether the user is allowed to access or not.
*/
public function access() {
$dashboard_exists = $this->dashboardManager->getDefaultDashboard() !== NULL;
return AccessResult::allowedIf($dashboard_exists)
->cachePerUser();
}
/**
* Builds the response.
*/
......
......@@ -5,16 +5,28 @@ namespace Drupal\dashboard\Plugin\Menu\LocalAction;
use Drupal\Core\Menu\LocalActionDefault;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\dashboard\DashboardInterface;
use Drupal\dashboard\DashboardManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Dashboard specific local action class.
*/
class DashboardLocalAction extends LocalActionDefault {
use StringTranslationTrait;
/**
* Constructs a BookVariationLocalAction object.
* The default dashboard for the current user, if available.
*
* @var \Drupal\dashboard\DashboardInterface|null
*/
protected ?DashboardInterface $defaultDashboard;
/**
* Constructs a DashboardLocalAction object.
*
* @param array $configuration
* A configuration array containing information about the plugin instance.
......@@ -50,6 +62,20 @@ class DashboardLocalAction extends LocalActionDefault {
);
}
/**
* Gets the default dashboard.
*
* @return \Drupal\dashboard\DashboardInterface|null
* The default dashboard for the current user, if available.
*/
protected function defaultDashboard(): ?DashboardInterface {
if (!isset($this->defaultDashboard)) {
$this->defaultDashboard = $this->dashboardManager->getDefaultDashboard();
}
return $this->defaultDashboard;
}
/**
* {@inheritdoc}
*/
......@@ -58,11 +84,36 @@ class DashboardLocalAction extends LocalActionDefault {
// Retrieve the default dashboard for the main dashboard route.
if ($route_match->getRouteName() === 'dashboard') {
$dashboard = $this->dashboardManager->getDefaultDashboard();
$parameters['dashboard'] = $dashboard->id();
if ($this->defaultDashboard()) {
$parameters['dashboard'] = $this->defaultDashboard()->id();
}
}
return $parameters;
}
/**
* {@inheritdoc}
*/
public function getRouteName() {
if ($this->defaultDashboard()) {
return $this->pluginDefinition['route_name'];
}
else {
return 'entity.dashboard.collection';
}
}
/**
* {@inheritdoc}
*/
public function getTitle(Request $request = NULL) {
if ($this->defaultDashboard()) {
return $this->pluginDefinition['title'];
}
else {
return $this->t('Manage dashboards');
}
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\Dashboard\Functional;
use Drupal\dashboard\Entity\Dashboard;
use Drupal\Tests\BrowserTestBase;
/**
* Test for access to dashboard controller.
*
* @group dashboard
*/
class DashboardAccessTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Modules to enable.
*
* @var string[]
*/
protected static $modules = ['dashboard'];
/**
* A user with permission to view dashboards.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
/**
* A role id with permissions to view dashboards.
*
* @var string
*/
protected $role;
/**
* A Dashboard to check access to.
*
* @var \Drupal\dashboard\DashboardInterface
*/
protected $dashboard;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->dashboard = Dashboard::create([
'id' => 'existing_dashboard',
'status' => TRUE,
]);
$this->dashboard->save();
$this->role = $this->drupalCreateRole([
'access administration pages',
'view the administration theme',
'view existing_dashboard dashboard',
]);
$this->adminUser = $this->drupalCreateUser();
$this->adminUser->addRole($this->role);
$this->adminUser->save();
$this->drupalPlaceBlock('local_tasks_block');
}
/**
* Checks that user can access if at least one dashboard is available.
*/
public function testAccessAvailableDashboard() {
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/dashboard');
$this->assertSession()->statusCodeEquals(200);
}
/**
* Checks that user cannot access if no dashboard is available.
*/
public function testAccessUnavailableDashboard() {
$this->dashboard->setStatus(FALSE)->save();
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/dashboard');
$this->assertSession()->statusCodeEquals(403);
$this->dashboard->delete();
$this->assertSession()->statusCodeEquals(403);
}
}
......@@ -134,7 +134,7 @@ class DashboardLinksTest extends BrowserTestBase {
$this->drupalLogin($this->adminUser);
$this->drupalGet('/admin/dashboard');
$this->assertSession()->pageTextContains('There is no dashboard to show.');
$this->assertSession()->statusCodeEquals(403);
}
/**
......
......@@ -2,8 +2,10 @@
namespace Drupal\Tests\dashboard\Functional;
use Drupal\dashboard\DashboardInterface;
use Drupal\dashboard\Entity\Dashboard;
use Drupal\Tests\BrowserTestBase;
use Drupal\user\UserInterface;
/**
* Test for dashboard navigation.
......@@ -24,26 +26,33 @@ class DashboardNavigationTest extends BrowserTestBase {
*/
protected static $modules = ['dashboard', 'toolbar'];
/**
* A Dashboard to check access to.
*
* @var \Drupal\dashboard\DashboardInterface
*/
protected DashboardInterface $dashboard;
/**
* A user with permission to administer dashboards.
*
* @var \Drupal\user\UserInterface
*/
protected $adminUser;
protected UserInterface $adminUser;
/**
* A role id with permissions to administer dashboards.
*
* @var string
*/
protected $adminRole;
protected string $adminRole;
/**
* A role id with permissions to administer dashboards.
*
* @var string
*/
protected $toolbarRole;
protected string $toolbarRole;
/**
* {@inheritdoc}
......@@ -51,8 +60,15 @@ class DashboardNavigationTest extends BrowserTestBase {
protected function setUp(): void {
parent::setUp();
$this->dashboard = Dashboard::create([
'id' => 'existing_dashboard',
'status' => TRUE,
'weight' => 0,
]);
$this->dashboard->save();
$this->adminRole = $this->drupalCreateRole([
'access administration pages',
'view existing_dashboard dashboard',
]);
$this->toolbarRole = $this->drupalCreateRole([
......@@ -70,13 +86,6 @@ class DashboardNavigationTest extends BrowserTestBase {
* Tests the existence of the dashboard navigation item.
*/
public function testDashboardToolbarItem() {
$dashboard = Dashboard::create([
'id' => 'existing_dashboard',
'status' => TRUE,
'weight' => 0,
]);
$dashboard->save();
$this->drupalLogin($this->adminUser);
// Assert that the dashboard navigation item is present in the HTML.
......@@ -88,7 +97,6 @@ class DashboardNavigationTest extends BrowserTestBase {
$this->drupalGet('<front>');
// Assert that the dashboard navigation item is not present in the HTML.
$this->assertSession()->elementNotExists('css', '#toolbar-administration #toolbar-link-dashboard');
}
}
......@@ -87,25 +87,25 @@ class DashboardSelectionTest extends BrowserTestBase {
// User has no specific permissions to access any dashboard.
$this->drupalGet('/admin/dashboard');
$this->assertSession()->pageTextContains('There is no dashboard to show.');
$this->assertSession()->statusCodeEquals(403);
// User can access only to dashboard1.
$role->grantPermission('view dashboard1 dashboard')->save();
$this->drupalGet('/admin/dashboard');
$this->assertSession()->pageTextNotContains('There is no dashboard to show.');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->elementExists('css', '.dashboard--dashboard1');
// User can access to both dashboards.
$role->grantPermission('view dashboard2 dashboard')->save();
$this->drupalGet('/admin/dashboard');
$this->assertSession()->pageTextNotContains('There is no dashboard to show.');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->elementExists('css', '.dashboard--dashboard2');
// Dashboard1 is disabled and inaccessible.
$dashboard1->disable();
$dashboard1->save();
$this->drupalGet('/admin/dashboard');
$this->assertSession()->pageTextNotContains('There is no dashboard to show.');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->elementExists('css', '.dashboard--dashboard2');
// Dashboard2 is disabled and inaccessible.
......@@ -114,14 +114,14 @@ class DashboardSelectionTest extends BrowserTestBase {
$dashboard2->disable();
$dashboard2->save();
$this->drupalGet('/admin/dashboard');
$this->assertSession()->pageTextNotContains('There is no dashboard to show.');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->elementExists('css', '.dashboard--dashboard1');
// Both dashboards are disabled and inaccessible.
$dashboard1->disable();
$dashboard1->save();
$this->drupalGet('/admin/dashboard');
$this->assertSession()->pageTextContains('There is no dashboard to show.');
$this->assertSession()->statusCodeEquals(403);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment