diff --git a/modules/access_unpublished_group/README.txt b/modules/access_unpublished_group/README.txt index 4489da9..9db9ea6 100644 --- a/modules/access_unpublished_group/README.txt +++ b/modules/access_unpublished_group/README.txt @@ -3,7 +3,7 @@ ACCESS UNPUBLISHED GROUP Description: ------------ Extends Access Unpublished module to support content belonging to -Groups. +Groups 2.x. Usage: ------ diff --git a/modules/access_unpublished_group/access_unpublished_group.install b/modules/access_unpublished_group/access_unpublished_group.install new file mode 100644 index 0000000..82e7d45 --- /dev/null +++ b/modules/access_unpublished_group/access_unpublished_group.install @@ -0,0 +1,15 @@ +getStorage('group_role')->loadMultiple(); - foreach ($group_roles as $role) { - $permissions = $role->getPermissions(); - foreach ($permissions as $permission) { - if (strpos($permission, 'access_unpublished_group_') === 0) { - $groupContentInfo[str_replace('access_unpublished_group_', '', $permission)]['handlers']['access'] = 'Drupal\access_unpublished_group\Plugin\AccessUnpublishedGroupContentAccessControlHandler'; - } - } - } -} diff --git a/modules/access_unpublished_group/src/Access/AccessUnpublishedGroupPermissions.php b/modules/access_unpublished_group/src/Access/AccessUnpublishedGroupPermissions.php index 76bcb54..5480814 100644 --- a/modules/access_unpublished_group/src/Access/AccessUnpublishedGroupPermissions.php +++ b/modules/access_unpublished_group/src/Access/AccessUnpublishedGroupPermissions.php @@ -2,11 +2,11 @@ namespace Drupal\access_unpublished_group\Access; -use Drupal\Group\Plugin\GroupContentEnablerInterface; +use Drupal\group\Plugin\Group\Relation\GroupRelationInterface; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Drupal\Core\Routing\RouteMatchInterface; -use Drupal\group\Plugin\GroupContentEnablerManagerInterface; +use Drupal\group\Plugin\Group\Relation\GroupRelationTypeManagerInterface; /** * Provides dynamic permissions for groups of different types. @@ -23,16 +23,16 @@ class AccessUnpublishedGroupPermissions implements ContainerInjectionInterface { /** * The group content enabler plugin manager. * - * @var \Drupal\group\Plugin\GroupContentEnablerManagerInterface + * @var \Drupal\group\Plugin\Group\Relation\GroupRelationTypeManagerInterface */ - protected $groupContentEnablerManager; + protected $groupRelationTypeManager; /** * Constructs the AccessUnpublishedGroupPermissions object. */ - public function __construct(RouteMatchInterface $route_match, GroupContentEnablerManagerInterface $group_content_enabler) { + public function __construct(RouteMatchInterface $route_match, GroupRelationTypeManagerInterface $group_relation_type_manager) { $this->routeMatch = $route_match; - $this->groupContentEnablerManager = $group_content_enabler; + $this->groupRelationTypeManager = $group_relation_type_manager; } /** @@ -41,7 +41,7 @@ class AccessUnpublishedGroupPermissions implements ContainerInjectionInterface { public static function create(ContainerInterface $container) { return new static( $container->get('current_route_match'), - $container->get('plugin.manager.group_content_enabler') + $container->get('group_relation_type.manager') ); } @@ -54,11 +54,11 @@ class AccessUnpublishedGroupPermissions implements ContainerInjectionInterface { */ public function groupPermissions() { $perms = []; - if (is_null($this->groupContentEnablerManager)) { + if (is_null($this->groupRelationTypeManager)) { return []; } if ($group_type = $this->routeMatch->getParameter('group_type')) { - $plugins = $this->groupContentEnablerManager->getInstalled($group_type); + $plugins = $this->groupRelationTypeManager->getInstalled($group_type); foreach ($plugins as $plugin) { $perms += $this->buildPermissions($plugin); } @@ -69,16 +69,16 @@ class AccessUnpublishedGroupPermissions implements ContainerInjectionInterface { /** * Returns list of group permissions for a given group content enabler plugin. * - * @param \Drupal\Group\Plugin\GroupContentEnablerInterface $plugin + * @param \Drupal\group\Plugin\Group\Relation\GroupRelationInterface $plugin * The group content enabler plugin. * * @return array * An associative array of permission names and descriptions. */ - protected function buildPermissions(GroupContentEnablerInterface $plugin) { + protected function buildPermissions(GroupRelationInterface $plugin) { $permissions = []; $permissions['access_unpublished_group_' . $plugin->getPluginId()] = [ - 'title' => 'Access unpublished ' . $plugin->getLabel(), + 'title' => 'Access unpublished ' . $plugin->getPluginDefinition()->get('label') ?? '', ]; return $permissions; } diff --git a/modules/access_unpublished_group/src/AccessUnpublishedGroupServiceProvider.php b/modules/access_unpublished_group/src/AccessUnpublishedGroupServiceProvider.php new file mode 100644 index 0000000..8d39323 --- /dev/null +++ b/modules/access_unpublished_group/src/AccessUnpublishedGroupServiceProvider.php @@ -0,0 +1,56 @@ +getParameter('container.modules'); + $discovery = new AnnotatedClassDiscovery( + 'Plugin/Group/Relation', + $container->get('container.namespaces'), + 'Drupal\group\Annotation\GroupRelationType', + [] + ); + + // Iterate through all group relations and decorate their access_control + // handler. + foreach ($discovery->getDefinitions() as $group_relation_type_id => $group_relation_type) { + assert($group_relation_type instanceof GroupRelationTypeInterface); + // Skip plugins whose provider is not installed. + if (!isset($modules[$group_relation_type->getProvider()])) { + continue; + } + + $existing_service_name = "group.relation_handler.access_control.$group_relation_type_id"; + $new_service_name = "access_unpublished_group.relation_handler.access_control.$group_relation_type_id"; + + // Decorate the service if it exists. + if ($container->hasDefinition($existing_service_name)) { + $container->register($new_service_name, AccessUnpublishedGroupAccessControl::class) + ->setDecoratedService($existing_service_name) + ->addArgument(new Reference("$new_service_name.inner")) + ->setPublic(TRUE) + ->setShared(FALSE); + } + } + } + +} diff --git a/modules/access_unpublished_group/src/Plugin/AccessUnpublishedGroupContentAccessControlHandler.php b/modules/access_unpublished_group/src/Plugin/Group/RelationHandler/AccessUnpublishedGroupAccessControl.php similarity index 54% rename from modules/access_unpublished_group/src/Plugin/AccessUnpublishedGroupContentAccessControlHandler.php rename to modules/access_unpublished_group/src/Plugin/Group/RelationHandler/AccessUnpublishedGroupAccessControl.php index 52aa0a5..29d32b3 100644 --- a/modules/access_unpublished_group/src/Plugin/AccessUnpublishedGroupContentAccessControlHandler.php +++ b/modules/access_unpublished_group/src/Plugin/Group/RelationHandler/AccessUnpublishedGroupAccessControl.php @@ -1,33 +1,47 @@ parent = $parent; + } /** * {@inheritdoc} */ public function entityAccess(EntityInterface $entity, $operation, AccountInterface $account, $return_as_object = FALSE) { - $result = parent::entityAccess($entity, $operation, $account, TRUE); + $result = $this->parent->entityAccess($entity, $operation, $account, $return_as_object); + if ($operation == 'view' && $result->isForbidden()) { - /** @var \Drupal\group\Entity\Storage\GroupContentStorageInterface $storage */ + /** @var \Drupal\group\Entity\Storage\GroupRelationshipStorageInterface $storage */ $storage = $this->entityTypeManager->getStorage('group_content'); - $group_contents = $storage->loadByEntity($entity); + $group_relationships = $storage->loadByEntity($entity); // Find content that uses this plugin and has the group permission. $has_group_permission = FALSE; - foreach ($group_contents as $group_content) { - $plugin_id = $group_content->getContentPlugin()->getPluginId(); + foreach ($group_relationships as $group_relationship) { + $plugin_id = $group_relationship->getPlugin()->getRelationTypeId(); if ($plugin_id === $this->pluginId) { - $group = $group_content->getGroup(); - $permission_id = 'access_unpublished_group_' . $group_content->getContentPlugin()->getPluginId(); + $group = $group_relationship->getGroup(); + $permission_id = 'access_unpublished_group_' . $plugin_id; if ($group->hasPermission($permission_id, $account)) { $has_group_permission = TRUE; break; @@ -51,6 +65,7 @@ class AccessUnpublishedGroupContentAccessControlHandler extends GroupContentAcce } } } + return $return_as_object ? $result : $result->isAllowed(); }