Skip to content

Commit 6949752

Browse files
authored
Merge pull request #85 from ubc-web-services/private_file
Add private_file access permissions
2 parents f2095a5 + da5f89c commit 6949752

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Drupal\ubc_media_entities;
4+
5+
use Drupal\media\MediaAccessControlHandler as CoreMediaAccessControlHandler;
6+
use Drupal\Core\Entity\EntityInterface;
7+
use Drupal\Core\Session\AccountInterface;
8+
use Drupal\Core\Access\AccessResult;
9+
10+
/**
11+
* Access control handler for media entities.
12+
*/
13+
class MediaAccessControlHandler extends CoreMediaAccessControlHandler
14+
{
15+
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account)
20+
{
21+
// Check if the operation is 'view'.
22+
if ($operation === 'view') {
23+
// Example: Restrict 'view' based on media type and a custom permission.
24+
$media_type = $entity->bundle();
25+
if ($account->hasPermission("view $media_type media")) {
26+
return AccessResult::allowed();
27+
}
28+
else {
29+
return AccessResult::forbidden();
30+
}
31+
}
32+
33+
// For other operations, fall back to the parent handler.
34+
return parent::checkAccess($entity, $operation, $account);
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = null)
41+
{
42+
// Allow creation of media if the user has a specific permission.
43+
return AccessResult::allowedIfHasPermission($account, "create $entity_bundle media");
44+
}
45+
}

ubc_media_entities/ubc_media_entities.module

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Drupal\Core\Entity\EntityTypeInterface;
34
use Drupal\Core\StreamWrapper\StreamWrapperManager;
45
use Drupal\user\Entity\Role;
56

@@ -24,6 +25,15 @@ function ubc_media_entities_file_download($uri) {
2425
return NULL;
2526
}
2627

28+
/**
29+
* Implements hook_entity_type_alter().
30+
*/
31+
function ubc_media_entities_entity_type_alter(array &$entity_types) {
32+
if (isset($entity_types['media'])) {
33+
$entity_types['media']->setHandlerClass('access', 'Drupal\ubc_media_entities\MediaAccessControlHandler');
34+
}
35+
}
36+
2737
/**
2838
* Implements hook_post_update_()
2939
* Add permission to view private files
@@ -33,3 +43,14 @@ function ubc_media_entities_post_update_grant_private_file_permission() {
3343
$role_object->grantPermission('access private files');
3444
$role_object->save();
3545
}
46+
47+
48+
/**
49+
* Implements hook_post_update_()
50+
* Add permission to view private_file media
51+
*/
52+
function ubc_media_entities_post_update_grant_private_media_permission() {
53+
$role_object = Role::load('authenticated');
54+
$role_object->grantPermission('view private_file media');
55+
$role_object->save();
56+
}

ubc_media_entities/ubc_media_entities.permissions.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ access private files:
22
title: 'Access private files'
33
description: 'View privately stored files from their direct URL path'
44
restrict access: TRUE
5+
6+
view private_file media:
7+
title: 'View private file media'
8+
description: 'View private file media items'
9+
restrict access: TRUE

0 commit comments

Comments
 (0)