3
0
Fork 0
mirror of https://github.com/farmOS/farmOS.git synced 2024-02-23 11:37:38 +01:00

Only show "Members" tab on group assets.

This commit is contained in:
Michael Stenta 2021-06-04 15:33:37 -04:00
parent ef2481d544
commit f88b58e0d0
3 changed files with 92 additions and 0 deletions

View file

@ -3,3 +3,12 @@ services:
class: Drupal\farm_group\GroupMembership
arguments:
[ '@farm.log_query', '@entity_type.manager', '@datetime.time', '@database' ]
farm_group.route_subscriber:
class: Drupal\farm_group\Routing\RouteSubscriber
tags:
- { name: event_subscriber }
farm_group.group_members_access:
class: Drupal\farm_group\Access\FarmGroupMembersViewsAccessCheck
arguments: [ '@entity_type.manager' ]
tags:
- { name: access_check, applies_to: _group_members_access }

View file

@ -0,0 +1,56 @@
<?php
namespace Drupal\farm_group\Access;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Routing\Access\AccessInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Access\AccessResult;
/**
* Checks access for displaying Views of group members.
*/
class FarmGroupMembersViewsAccessCheck implements AccessInterface {
/**
* The asset storage.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $assetStorage;
/**
* FarmGroupMembersViewsAccessCheck constructor.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager service.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->assetStorage = $entity_type_manager->getStorage('asset');
}
/**
* A custom access check.
*
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The route match.
*/
public function access(RouteMatchInterface $route_match) {
// If there is no "asset" parameter, bail.
$asset_id = $route_match->getParameter('asset');
if (empty($asset_id)) {
return AccessResult::allowed();
}
// Allow access if the asset is a group.
/** @var \Drupal\asset\Entity\AssetInterface $asset */
$asset = $this->assetStorage->load($asset_id);
$access = AccessResult::allowedIf($asset->bundle() == 'group');
// Invalidate the access result when assets are changed.
$access->addCacheTags(["asset_list"]);
return $access;
}
}

View file

@ -0,0 +1,27 @@
<?php
namespace Drupal\farm_group\Routing;
use Drupal\Core\Routing\RouteSubscriberBase;
use Symfony\Component\Routing\RouteCollection;
/**
* Alter routes for the farm_group module.
*
* @ingroup farm
*/
class RouteSubscriber extends RouteSubscriberBase {
/**
* {@inheritdoc}
*/
public function alterRoutes(RouteCollection $collection) {
// Add our _group_members_access requirement to
// view.farm_group_members.page.
if ($route = $collection->get('view.farm_group_members.page')) {
$route->setRequirement('_group_members_access', 'Drupal\farm_group\Access\FarmGroupMembersViewsAccessCheck::access');
}
}
}