Skip to content

Commit ad91cfa

Browse files
committed
Update to dynamically create permissions
1 parent 6949752 commit ad91cfa

File tree

4 files changed

+100
-23
lines changed

4 files changed

+100
-23
lines changed

ubc_media_entities/src/MediaAccessControlHandler.php

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,16 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
2020
{
2121
// Check if the operation is 'view'.
2222
if ($operation === 'view') {
23-
// Example: Restrict 'view' based on media type and a custom permission.
23+
// Dynamically check permission based on media type.
2424
$media_type = $entity->bundle();
25-
if ($account->hasPermission("view $media_type media")) {
26-
return AccessResult::allowed();
27-
}
28-
else {
29-
return AccessResult::forbidden();
30-
}
25+
$permission = "view $media_type media";
26+
27+
// Grant access if the user has the permission.
28+
return AccessResult::allowedIfHasPermission($account, $permission);
3129
}
3230

33-
// For other operations, fall back to the parent handler.
31+
// Fallback to parent handler for other operations.
3432
return parent::checkAccess($entity, $operation, $account);
3533
}
3634

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-
}
4535
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Drupal\ubc_media_entities;
4+
5+
use Drupal\Core\StringTranslation\StringTranslationTrait;
6+
use Drupal\media\Entity\MediaType;
7+
8+
/**
9+
* Provides dynamic permissions for media types.
10+
*/
11+
class PermissionProvider
12+
{
13+
use StringTranslationTrait;
14+
15+
/**
16+
* Generates dynamic permissions for all media types.
17+
*
18+
* @return array
19+
* An array of permissions.
20+
*/
21+
public static function generateMediaPermissions()
22+
{
23+
$permissions = [];
24+
$media_types = MediaType::loadMultiple();
25+
26+
foreach ($media_types as $media_type) {
27+
$type_id = $media_type->id();
28+
$type_label = $media_type->label();
29+
30+
// Add 'view' permission for the media type.
31+
$permissions["view $type_id media"] = [
32+
'title' => t('View @type media', ['@type' => $type_label]),
33+
'description' => t('Allows users to view media of type @type.', ['@type' => $type_label]),
34+
'restrict access' => true,
35+
];
36+
37+
}
38+
39+
return $permissions;
40+
}
41+
}

ubc_media_entities/ubc_media_entities.module

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,29 @@ function ubc_media_entities_file_download($uri) {
2525
return NULL;
2626
}
2727

28+
/**
29+
* Implements hook_permission().
30+
*/
31+
function ubc_media_entities_permission() {
32+
$permissions = [];
33+
$media_types = \Drupal::entityTypeManager()->getStorage('media_type')->loadMultiple();
34+
35+
foreach ($media_types as $media_type) {
36+
$type_id = $media_type->id();
37+
$type_label = $media_type->label();
38+
39+
// Create 'view' permission for the media type.
40+
$permissions["view $type_id media"] = [
41+
'title' => t('View @type media', ['@type' => $type_label]),
42+
'description' => t('Allows users to view media of type @type.', ['@type' => $type_label]),
43+
'restrict access' => TRUE,
44+
];
45+
46+
}
47+
48+
return $permissions;
49+
}
50+
2851
/**
2952
* Implements hook_entity_type_alter().
3053
*/
@@ -44,13 +67,37 @@ function ubc_media_entities_post_update_grant_private_file_permission() {
4467
$role_object->save();
4568
}
4669

70+
/**
71+
* Implements hook_post_update_()
72+
* Add anonymous permissions to media
73+
*/
74+
/* function ubc_media_entities_post_update_grant_anonymous_permissions() {
75+
$role_object = Role::load('authenticated');
76+
$role_object->grantPermission('view audio media');
77+
$role_object->grantPermission('view document media');
78+
$role_object->grantPermission('view file media');
79+
//$role_object->grantPermission('view ics_file media');
80+
$role_object->grantPermission('view image media');
81+
$role_object->grantPermission('view remote_video media');
82+
$role_object->grantPermission('view svg_icon media');
83+
$role_object->grantPermission('view video media');
84+
$role_object->save();
85+
} */
4786

4887
/**
4988
* Implements hook_post_update_()
50-
* Add permission to view private_file media
89+
* Add authenticated permissions to media
5190
*/
52-
function ubc_media_entities_post_update_grant_private_media_permission() {
91+
/* function ubc_media_entities_post_update_grant_authenticated_permissions() {
5392
$role_object = Role::load('authenticated');
93+
$role_object->grantPermission('view audio media');
94+
$role_object->grantPermission('view document media');
95+
$role_object->grantPermission('view file media');
96+
//$role_object->grantPermission('view ics_file media');
97+
$role_object->grantPermission('view image media');
5498
$role_object->grantPermission('view private_file media');
99+
$role_object->grantPermission('view remote_video media');
100+
$role_object->grantPermission('view svg_icon media');
101+
$role_object->grantPermission('view video media');
55102
$role_object->save();
56-
}
103+
} */

ubc_media_entities/ubc_media_entities.permissions.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ access private files:
33
description: 'View privately stored files from their direct URL path'
44
restrict access: TRUE
55

6-
view private_file media:
7-
title: 'View private file media'
8-
description: 'View private file media items'
9-
restrict access: TRUE
6+
7+
permission_callbacks:
8+
- Drupal\ubc_media_entities\PermissionProvider::generateMediaPermissions

0 commit comments

Comments
 (0)