Skip to content

Commit

Permalink
Merge pull request #16 from donquixote/issue-52-epic-group-integration
Browse files Browse the repository at this point in the history
Issue 52 epic group integration
  • Loading branch information
AaronGilMartinez committed Nov 25, 2024
2 parents 340b3b4 + a108e45 commit 5f23218
Show file tree
Hide file tree
Showing 23 changed files with 1,534 additions and 37 deletions.
8 changes: 7 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ jobs:
- name: Composer install
run: |
docker-compose exec -T web composer install --no-interaction
docker-compose exec -T web composer config --merge --json "extra.patches" '{"drupal/group": {"Using a translatable string as a category for field type is deprecated - https://www.drupal.org/project/group/issues/3458530": "https://www.drupal.org/files/issues/2024-07-02/group-translate-string-as-category-is-deprecated_0.patch"}}'
docker-compose exec -T web composer require --dev cweagans/composer-patches
- name: PhpCS
run: |
Expand All @@ -66,3 +67,8 @@ jobs:
- name: PhpUnit
run: |
docker-compose exec -T web ./vendor/bin/phpunit -vvv --debug
- name: PhpUnit - groupmedia 3
run: |
docker-compose exec -T web composer require --dev drupal/groupmedia:^3 -W
docker-compose exec -T web ./vendor/bin/phpunit -vvv --debug --testsuite "Collabora Online Group"
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ Minimal steps to see the editor in action:

Advanced usage:
- Configure roles and permissions as in "User permissions" section below.
- Create a non-admin user with sufficient roles, login,
- Create a non-admin user with sufficient roles, login,

### Running the tests

Expand Down Expand Up @@ -198,19 +198,19 @@ not needed, if the Collabora instance is configured from outside of
Drupal.

For each media type, the module introduces four permissions:
- "(media type): Edit any media file in Collabora"
- "(media type): Edit any media file in Collabora"
Users with this permission are allowed to edit documents attached
to a media entity of the given type, using the Collabora Online
editor.
- "(media type): Edit own media file in Collabora"
- "(media type): Edit own media file in Collabora"
Users with this permission are allowed to edit documents attached
to a media entity of the given type, using the Collabora Online
editor, if they are the owner/author of that media entity.
- "(media type): Preview published media file in Collabora"
- "(media type): Preview published media file in Collabora"
Users with this permission are allowed to preview documents attached
to a published media entity of the given type, using the Collabora
Online editor in preview/readonly mode.
- "(media type): Preview own unpublished media file in Collabora"
- "(media type): Preview own unpublished media file in Collabora"
Users with this permission are allowed to preview documents attached
to an unpublished media entity of the given type, using the Collabora Online
editor in preview/readonly mode.
Expand All @@ -223,6 +223,14 @@ Developers can use entity access hooks to alter which users may edit
or preview media files in Collabora. This would allow to grant access
based on e.g. membership in a group.

### Views

The module integrates with Views by providing links as view fields, allowing
users to perform specific operations on documents directly from the view display.

These operations include actions such as "preview" and "edit," which can be
easily accessed through the generated links.

### Other configuration

If you need to change the accepted extensions to upload, go to
Expand All @@ -246,6 +254,13 @@ upload_max_filesize = 30M

These set the limits to a maximum of 30M. You can change as appropriate.

Sub-modules
-------------

### Collabora Online Group

Integration of Collabora Online with Group module. Check out the [README](/modules/collabora_online_group/README.md) of the module.

License
-------

Expand Down
2 changes: 1 addition & 1 deletion collabora_online.info.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Collabora Online
description: Integrate Collabora Online for document viewing and editing.
package: Custom
package: Collabora Online
configure: collabora-online.settings
dependencies:
- drupal:media
Expand Down
28 changes: 28 additions & 0 deletions collabora_online.views.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

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

declare(strict_types=1);

/**
* Implements hook_views_data_alter().
*/
function collabora_online_views_data_alter(array &$data): void {
$data['media']['collabora_preview'] = [
'title' => t('Link to view in Collabora Online'),
'group' => t('Media'),
'field' => [
'id' => 'media_collabora_preview',
],
];
$data['media']['collabora_edit'] = [
'title' => t('Link to edit in Collabora Online'),
'group' => t('Media'),
'field' => [
'id' => 'media_collabora_edit',
],
];
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"drupal/core-composer-scaffold": "^10.3.6",
"drupal/core-dev": "^10.3.6",
"drupal/core-recommended": "^10.3.6",
"drupal/groupmedia": "^3 || ^4",
"drush/drush": "^12.4",
"openeuropa/task-runner": "^2@dev",
"openeuropa/task-runner-drupal-project-symlink": "^1.0.0-beta6",
Expand Down
12 changes: 12 additions & 0 deletions config/schema/collabora_online.schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,15 @@ collabora_online.settings:
allowfullscreen:
type: boolean
label: 'Allow full-screen.'

views.field.media_collabora_preview:
type: views_field
label: 'Collabora view link'
mapping:
text:
type: label
label: 'Text to display'

views.field.media_collabora_edit:
type: views.field.media_collabora_preview
label: 'Collabora edit link'
35 changes: 35 additions & 0 deletions modules/collabora_online_group/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
Collabora Online Group
=====================================

This submodule integrates the Group module (https://www.drupal.org/project/group)
by managing group-related permissions. It allows fine-grained control over user
access within groups, enabling specific permissions for Collabora content and
actions based on group membership.

### Requirements

- Groupmedia module: https://www.drupal.org/project/groupmedia
- Compatible with versions 3.x and 4.x.

### User permissions

The module maps existing Collabora media operation permissions to the group type
instances.

#### Permissions:
- "(media type): Edit any media file in Collabora"
- "(media type): Edit own media file in Collabora"
- "(media type): Preview published media file in Collabora"
- "(media type): Preview own unpublished media file in Collabora"

Check [Collabora Online README](/README.md#user-permissions) for more information about permissions.

### Views

Additionally, the submodule modifies Groupmedia view configuration to add links
for Collabora operations.

License
-------

This module is published under the MPL-2.0 license.
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name: Collabora Online Group
description: Integrates Collabora Online with Group module
package: Collabora Online
dependencies:
- collabora_online:collabora_online
- groupmedia:groupmedia

type: module
core_version_requirement: ^10
62 changes: 62 additions & 0 deletions modules/collabora_online_group/collabora_online_group.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

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

use Drupal\views\Entity\View;

/**
* Implements hook_install().
*/
function collabora_online_group_install(bool $is_syncing): void {
if (
$is_syncing ||
!\Drupal::moduleHandler()->moduleExists('views') ||
!($view = View::load('group_media'))
) {
return;
}

// Load display and apply changes.
$display = &$view->getDisplay('default');
if ($display === NULL) {
return;
}

// Add new fields to the display.
$display['display_options']['fields'] += [
'collabora_preview' => [
'id' => 'collabora_preview',
'table' => 'media',
'field' => 'collabora_preview',
'plugin_id' => 'media_collabora_preview',
'label' => '',
'exclude' => TRUE,
'text' => t('View in Collabora Online'),
],
];
$display['display_options']['fields'] += [
'collabora_edit' => [
'id' => 'collabora_edit',
'table' => 'media',
'field' => 'collabora_edit',
'plugin_id' => 'media_collabora_edit',
'label' => '',
'exclude' => TRUE,
'text' => t('Edit in Collabora Online'),
],
];
// 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',
'collabora_edit' => 'collabora_edit',
];
unset($display['display_options']['fields']['dropbutton']);
$display['display_options']['fields']['dropbutton'] = $dropbutton;

$view->save();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
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"]

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
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Drupal\collabora_online_group\Plugin\Group\RelationHandler;

use Drupal\groupmedia\Plugin\Group\RelationHandler\GroupMediaPermissionProvider;

/**
* Provides Collabora permissions for group.
*/
class CollaboraPermissionProvider extends GroupMediaPermissionProvider {

/**
* {@inheritdoc}
*/
public function buildPermissions(): array {
$permissions = $this->parent->buildPermissions();

/* @see \Drupal\group\Plugin\Group\RelationHandlerDefault\PermissionProvider::buildPermissions() */
$provider_chain = $this->groupRelationTypeManager()->getPermissionProvider($this->pluginId);

// Add Collabora permissions.
$prefix = 'Entity:';
if ($name = $provider_chain->getPermission('preview in collabora', 'entity')) {
$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");
}
if ($name = $provider_chain->getPermission('edit in collabora', 'entity', 'own')) {
$permissions[$name] = $this->buildPermission("$prefix Edit own %entity_type in collabora");
}

return $permissions;
}

/**
* {@inheritdoc}
*/
public function getPermission($operation, $target, $scope = 'any'): bool|string {
if (
$target === 'entity' &&
$this->definesEntityPermissions &&
($this->implementsOwnerInterface || $scope === 'any')
) {
switch ($operation) {
case 'preview in collabora':
if ($scope === 'any') {
return "preview $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";
}
}

return $this->parent->getPermission($operation, $target, $scope);
}

}
Loading

0 comments on commit 5f23218

Please sign in to comment.