-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #27: Replace global permissions with per-bundle perms and 'edit…
… own'.
- Loading branch information
1 parent
4ef31f8
commit 8a1d00b
Showing
4 changed files
with
192 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
preview media in collabora: | ||
title: 'Preview media in Collabora' | ||
edit any media in collabora: | ||
title: 'Edit any media in Collabora' | ||
administer collabora instance: | ||
title: 'Administer the Collabora instance' | ||
restrict access: true | ||
|
||
permission_callbacks: | ||
- \Drupal\collabora_online\CollaboraMediaPermissions::mediaTypePermissions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
<?php | ||
|
||
namespace Drupal\collabora_online; | ||
|
||
use Drupal\Core\DependencyInjection\AutowireTrait; | ||
use Drupal\Core\DependencyInjection\ContainerInjectionInterface; | ||
use Drupal\Core\Entity\BundlePermissionHandlerTrait; | ||
use Drupal\Core\Entity\EntityTypeManagerInterface; | ||
use Drupal\Core\StringTranslation\StringTranslationTrait; | ||
use Drupal\media\MediaTypeInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
|
||
/** | ||
* Provides permissions for Collabora per media type. | ||
* | ||
* @see \Drupal\media\MediaPermissions | ||
*/ | ||
class CollaboraMediaPermissions implements ContainerInjectionInterface { | ||
|
||
use AutowireTrait; | ||
use BundlePermissionHandlerTrait; | ||
use StringTranslationTrait; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager | ||
* The entity type manager service. | ||
*/ | ||
public function __construct( | ||
protected readonly EntityTypeManagerInterface $entityTypeManager, | ||
) {} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public static function create(ContainerInterface $container) { | ||
return new static($container->get('entity_type.manager')); | ||
} | ||
|
||
/** | ||
* Returns an array of media type permissions. | ||
* | ||
* @return array | ||
* The media type permissions. | ||
* | ||
* @see \Drupal\user\PermissionHandlerInterface::getPermissions() | ||
*/ | ||
public function mediaTypePermissions(): array { | ||
// Generate media permissions for all media types. | ||
$media_types = $this->entityTypeManager->getStorage('media_type')->loadMultiple(); | ||
return $this->generatePermissions($media_types, [$this, 'buildPermissions']); | ||
} | ||
|
||
/** | ||
* Returns a list of permissions for a given media type. | ||
* | ||
* @param \Drupal\media\MediaTypeInterface $type | ||
* The media type. | ||
* | ||
* @return array | ||
* An associative array of permission names and descriptions. | ||
*/ | ||
protected function buildPermissions(MediaTypeInterface $type) { | ||
$type_id = $type->id(); | ||
$type_params = ['%type_name' => $type->label()]; | ||
|
||
return [ | ||
"preview $type_id in collabora" => [ | ||
'title' => $this->t('%type_name: Preview media file in Collabora', $type_params), | ||
], | ||
"edit own $type_id in collabora" => [ | ||
'title' => $this->t('%type_name: Edit own media file in Collabora', $type_params), | ||
], | ||
"edit any $type_id in collabora" => [ | ||
'title' => $this->t('%type_name: Edit any media file in Collabora', $type_params), | ||
], | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drupal\Tests\collabora_online\Functional; | ||
|
||
use Drupal\file\Entity\File; | ||
use Drupal\media\Entity\Media; | ||
use Drupal\media\MediaInterface; | ||
use Drupal\Tests\BrowserTestBase; | ||
use Drupal\Tests\media\Traits\MediaTypeCreationTrait; | ||
use Drupal\Tests\TestFileCreationTrait; | ||
use Drupal\user\Entity\Role; | ||
use Drupal\user\PermissionHandlerInterface; | ||
|
||
/** | ||
* Tests dynamically created permissions. | ||
*/ | ||
class PermissionTest extends BrowserTestBase { | ||
|
||
use MediaTypeCreationTrait; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected static $modules = [ | ||
'media', | ||
'collabora_online', | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected $defaultTheme = 'stark'; | ||
|
||
/** | ||
* Tests that dynamic permissions are properly created. | ||
*/ | ||
public function testDynamicPermissions(): void { | ||
$this->createMediaType('file', [ | ||
'id' => 'public_wiki', | ||
'label' => 'Public wiki', | ||
]); | ||
/** @var \Drupal\user\PermissionHandlerInterface $permission_handler */ | ||
$permission_handler = \Drupal::service(PermissionHandlerInterface::class); | ||
$permissions = $permission_handler->getPermissions(); | ||
$permissions = array_filter( | ||
$permissions, | ||
fn (array $permission) => $permission['provider'] === 'collabora_online', | ||
); | ||
// Remove noise that is hard to diff. | ||
$permissions = array_map( | ||
static function (array $permission) { | ||
$permission['title'] = (string) $permission['title']; | ||
if ($permission['description'] === NULL) { | ||
unset($permission['description']); | ||
} | ||
if ($permission['provider'] === 'collabora_online') { | ||
unset($permission['provider']); | ||
} | ||
return $permission; | ||
}, | ||
$permissions, | ||
); | ||
ksort($permissions); | ||
$this->assertSame([ | ||
'administer collabora instance' => [ | ||
'title' => 'Administer the Collabora instance', | ||
'restrict access' => TRUE, | ||
], | ||
'edit any public_wiki in collabora' => [ | ||
'title' => '<em class="placeholder">Public wiki</em>: Edit any media file in Collabora', | ||
'dependencies' => ['config' => ['media.type.public_wiki']], | ||
], | ||
'edit own public_wiki in collabora' => [ | ||
'title' => '<em class="placeholder">Public wiki</em>: Edit own media file in Collabora', | ||
'dependencies' => ['config' => ['media.type.public_wiki']], | ||
], | ||
'preview public_wiki in collabora' => [ | ||
'title' => '<em class="placeholder">Public wiki</em>: Preview media file in Collabora', | ||
'dependencies' => ['config' => ['media.type.public_wiki']], | ||
], | ||
], $permissions); | ||
} | ||
|
||
} |