Skip to content

Commit

Permalink
8.2.1
Browse files Browse the repository at this point in the history
- Added access check to hide the "Outline" tab from book module.
  • Loading branch information
pookmish authored Oct 19, 2022
2 parents fb6df6b + 412fc18 commit 472df34
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 21 deletions.
17 changes: 0 additions & 17 deletions .github/workflows/back-to-dev.yml

This file was deleted.

10 changes: 10 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,21 @@ on:
jobs:
build:
runs-on: ubuntu-latest
if: github.event.pull_request.merged
steps:
- name: Tag
id: tag
uses: K-Phoen/semver-release-action@master
with:
release_branch: main
tag_format: "%major%.%minor%.%patch%"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- uses: actions/checkout@v3
with:
ref: 'main'
- name: Back to Dev
if: ${{ steps.tag.outputs.tag }}
run: |
composer global require su-sws/stanford-caravan:dev-8.x-2.x
~/.composer/vendor/bin/sws-caravan back-to-dev $GITHUB_REF $GITHUB_WORKSPACE main
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Stanford Fields

8.2.1
--------------------------------------------------------------------------------
_Release Date: 2022-10-19_

- Added access check to hide the "Outline" tab from book module.

8.2.0
--------------------------------------------------------------------------------
_Release Date: 2022-10-13_
Expand Down
4 changes: 4 additions & 0 deletions src/Routing/StanfordFieldsRouteSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ protected function alterRoutes(RouteCollection $collection) {
if ($route = $collection->get('book.admin_edit')) {
$route->setDefault('_form', '\Drupal\stanford_fields\Form\StanfordFieldBookAdminEditForm');
}

if ($route = $collection->get('entity.node.book_outline_form')) {
$route->setRequirement('_custom_access', 'book.manager:checkBookOutlineAccess');
}
}

}
26 changes: 25 additions & 1 deletion src/Service/StanfordFieldsBookManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use Drupal\book\BookManagerInterface;
use Drupal\Component\Utility\NestedArray;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
Expand All @@ -29,8 +32,10 @@ class StanfordFieldsBookManager implements BookManagerInterface {
* Config factory service.
* @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
* Event dispatcher service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* Entity type manager service.
*/
public function __construct(protected BookManagerInterface $bookManager, protected ConfigFactoryInterface $configFactory, protected EventDispatcherInterface $eventDispatcher) {
public function __construct(protected BookManagerInterface $bookManager, protected ConfigFactoryInterface $configFactory, protected EventDispatcherInterface $eventDispatcher, protected EntityTypeManagerInterface $entityTypeManager) {
}

/**
Expand Down Expand Up @@ -468,4 +473,23 @@ public function checkNodeIsRemovable(NodeInterface $node) {
return $this->bookManager->checkNodeIsRemovable($node);
}

/**
* Check for access on the "Outline" book route.
*
* @param \Drupal\Core\Session\AccountInterface $account
* Current account.
* @param int $node
* Node entity id.
*
* @return \Drupal\Core\Access\AccessResultReasonInterface
* Resulting access.
*/
public function checkBookOutlineAccess(AccountInterface $account, int $node): AccessResultInterface {
$node = $this->entityTypeManager->getStorage('node')->load($node);
if ($node && $this->nodeAllowedInBook($node)) {
return AccessResult::allowedIfHasPermission($account, 'administer book outlines');
}
return AccessResult::forbidden();
}

}
1 change: 1 addition & 0 deletions src/StanfordFieldsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function register(ContainerBuilder $container) {
->addArgument(new Reference('stanford_fields.book_manager.inner'))
->addArgument(new Reference('config.factory'))
->addArgument(new Reference('event_dispatcher'))
->addArgument(new Reference('entity_type.manager'))
->setPublic(FALSE);
}
}
Expand Down
2 changes: 1 addition & 1 deletion stanford_fields.info.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ type: module
description: 'Field types, widgets and formatters to enhance Drupal and Contrib.'
core_version_requirement: ^9 || ^10
package: Stanford
version: 8.2.0
version: 8.2.1
dependencies:
- drupal:field
36 changes: 36 additions & 0 deletions tests/src/Kernel/Service/StanfordFieldBookManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use Drupal\Core\Form\FormState;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountProxyInterface;
use Drupal\Core\Url;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
use Drupal\Tests\stanford_fields\Kernel\StanfordFieldKernelTestBase;
use Drupal\user\Entity\User;
use Drupal\user\RoleInterface;

/**
* Decorated book manager service tests.
Expand Down Expand Up @@ -142,4 +145,37 @@ public function testUpdateOutline() {
$this->assertEquals(24, $node->book['weight']);
}

public function testOutlineAccess() {
// Create user 1 first.
User::create(['name' => $this->randomMachineName()])->save();

$account = User::create(['name' => $this->randomMachineName()]);

$account->save();
$account = User::load($account->id());
$this->container->get('current_user')->setAccount($account);

$access = Url::fromRoute('entity.node.book_outline_form', ['node' => 999])
->access($account);
$this->assertFalse($access);

$access = Url::fromRoute('entity.node.book_outline_form', ['node' => $this->book->id()])
->access($account);
$this->assertFalse($access);

user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['administer book outlines']);
$access = Url::fromRoute('entity.node.book_outline_form', ['node' => $this->book->id()])
->access($account);
$this->assertTrue($access);

\Drupal::configFactory()->getEditable('book.settings')
->set('allowed_types', ['foobar_page'])
->set('child_type', 'foobar_page')
->save();

$access = Url::fromRoute('entity.node.book_outline_form', ['node' => $this->book->id()])
->access($account);
$this->assertFalse($access);
}

}
5 changes: 3 additions & 2 deletions tests/src/Kernel/StanfordFieldKernelTestBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Drupal\Tests\stanford_fields\Kernel;

use Drupal\Core\Session\AccountInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\NodeType;
use Drupal\user\Entity\Role;
Expand Down Expand Up @@ -38,8 +37,10 @@ protected function setUp(): void {
$this->installSchema('node', ['node_access']);

NodeType::create(['type' => 'page'])->save();
Role::create(['id' => AccountInterface::ANONYMOUS_ROLE])->save();
Role::create(['id' => RoleInterface::ANONYMOUS_ID])->save();
Role::create(['id' => RoleInterface::AUTHENTICATED_ID])->save();
user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, ['access content']);
user_role_grant_permissions(RoleInterface::AUTHENTICATED_ID, ['access content']);
}

}

0 comments on commit 472df34

Please sign in to comment.