Skip to content

Commit

Permalink
Merge pull request #15 from donquixote/group-unpublished-permissions
Browse files Browse the repository at this point in the history
Update group mapping with unpublished permissions
  • Loading branch information
AaronGilMartinez committed Nov 25, 2024
2 parents 8a46be6 + 23a00ae commit bfd8dbe
Show file tree
Hide file tree
Showing 14 changed files with 370 additions and 137 deletions.
5 changes: 5 additions & 0 deletions collabora_online.views.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

/**
* @file
* Provide views data for collabora_online.module.
*/

declare(strict_types=1);

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
<?php

/**
* @file
* Install, update and uninstall functions for collabora_online_group.
*/

use Drupal\views\Entity\View;

/**
Expand Down Expand Up @@ -43,7 +48,8 @@ function collabora_online_group_install(bool $is_syncing): void {
'text' => t('Edit in Collabora Online'),
],
];
// Add new fields as options for the dropbutton and move the element to the end.
// Add new fields as options for the dropbutton, and move the dropbutton to
// the end of the array.
$dropbutton = $display['display_options']['fields']['dropbutton'];
$dropbutton['fields'] += [
'collabora_preview' => 'collabora_preview',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ services:
group.relation_handler.permission_provider.collabora_group_media:
class: 'Drupal\collabora_online_group\Plugin\Group\RelationHandler\CollaboraPermissionProvider'
decorates: group.relation_handler.permission_provider.group_media
arguments: [ '@group.relation_handler.permission_provider.collabora_group_media.inner' ]
arguments: ["@group.relation_handler.permission_provider.collabora_group_media.inner"]

group.relation_handler.access_control.group_media:
class: 'Drupal\collabora_online_group\Plugin\Group\RelationHandler\CollaboraAccessControl'
arguments: ["@group.relation_handler.access_control"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

declare(strict_types=1);

namespace Drupal\collabora_online_group\Plugin\Group\RelationHandler;

use Drupal\Core\Access\AccessResultInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\group\Plugin\Group\RelationHandler\AccessControlInterface;
use Drupal\group\Plugin\Group\RelationHandler\AccessControlTrait;
use Drupal\group\Plugin\Group\RelationHandlerDefault\AccessControl;

/**
* Provides access control for collabora group.
*/
class CollaboraAccessControl extends AccessControl {

use AccessControlTrait;

/**
* Constructs a new CollaboraAccessControl.
*
* @param \Drupal\group\Plugin\Group\RelationHandler\AccessControlInterface $parent
* The default access control.
*/
public function __construct(AccessControlInterface $parent) {
$this->parent = $parent;
}

/**
* {@inheritdoc}
*/
public function entityAccess(EntityInterface $entity, $operation, AccountInterface $account, $return_as_object = FALSE): AccessResultInterface|bool {
// Add support for unpublished operation: preview in collabora.
$check_published = $operation === 'preview in collabora' && $this->implementsPublishedInterface;

if ($check_published && !$entity->isPublished()) {
$operation .= ' unpublished';
}

return $this->parent->entityAccess($entity, $operation, $account, $return_as_object);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ public function buildPermissions(): array {
// Add Collabora permissions.
$prefix = 'Entity:';
if ($name = $provider_chain->getPermission('preview in collabora', 'entity')) {
$permissions[$name] = $this->buildPermission("$prefix Preview %entity_type in collabora");
$permissions[$name] = $this->buildPermission("$prefix Preview published %entity_type in collabora");
}
if ($name = $provider_chain->getPermission('preview in collabora unpublished', 'entity', 'own')) {
$permissions[$name] = $this->buildPermission("$prefix Preview own unpublished %entity_type in collabora");
}
if ($name = $provider_chain->getPermission('edit in collabora', 'entity')) {
$permissions[$name] = $this->buildPermission("$prefix Edit any %entity_type in collabora");
Expand All @@ -39,20 +42,28 @@ public function buildPermissions(): array {
* {@inheritdoc}
*/
public function getPermission($operation, $target, $scope = 'any'): bool|string {
if ($target === 'entity') {
if (
$target === 'entity' &&
$this->definesEntityPermissions &&
($this->implementsOwnerInterface || $scope === 'any')
) {
switch ($operation) {
case 'preview in collabora':
return "preview $this->pluginId in collabora";
if ($scope === 'any') {
return "preview $this->pluginId in collabora";
}

case 'edit in collabora':
if (
$this->definesEntityPermissions &&
($this->implementsOwnerInterface || $scope === 'any')
) {
return "edit $scope $this->pluginId in collabora";
return FALSE;

case 'preview in collabora unpublished':
if ($this->implementsPublishedInterface && $scope === 'own') {
return "preview $scope unpublished $this->pluginId in collabora";
}

return FALSE;

case 'edit in collabora':
return "edit $scope $this->pluginId in collabora";
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ public function testViewLinks(): void {
Url::fromRoute(
'collabora-online.view',
[
'media' => $media->id()
'media' => $media->id(),
],
[
'query' => [
'destination' => "/group/{$group->id()}/media",
]
],
]
)->toString(),
$operation_links[0]->getAttribute('href')
Expand All @@ -129,12 +129,12 @@ public function testViewLinks(): void {
Url::fromRoute(
'collabora-online.edit',
[
'media' => $media->id()
'media' => $media->id(),
],
[
'query' => [
'destination' => "/group/{$group->id()}/media",
]
],
]
)->toString(),
$operation_links[1]->getAttribute('href')
Expand Down
Loading

0 comments on commit bfd8dbe

Please sign in to comment.