Dynamic permissions!

This commit is contained in:
Naomi 2018-10-25 19:47:12 +01:00
parent 570d442d41
commit 142c22f8e2
2 changed files with 148 additions and 51 deletions

View File

@ -1,3 +1,7 @@
permission_callbacks:
- Drupal\opencase_entities\OpenCaseEntityPermissions::permissions
administer opencase entity bundles:
title: 'Administer OpenCase Actor/Case/Activity Types'
description: 'Create, configure and delete types of actor (such as "Client" or "Legal Advisor"), types of case (such as "Housing" or "Asylum") and types of activity (such as "Case Note" or "Interview")'
@ -7,57 +11,57 @@ administer actor entities:
description: 'Allow to access the administration form to configure Actor entities.'
restrict access: true
add client entities:
title: 'Create new Client entities'
delete client entities:
title: 'Delete Client entities'
edit client entities:
title: 'Edit Client entities'
view published client entities:
title: 'View published Client entities'
view unpublished client entities:
title: 'View unpublished Client entities'
view all client revisions:
title: 'View all Client revisions'
revert all client revisions:
title: 'Revert all Client revisions'
description: 'Role requires permission <em>view Client revisions</em> and <em>edit rights</em> for client entities in question or <em>administer client entities</em>.'
delete all client revisions:
title: 'Delete all Client revisions'
description: 'Role requires permission to <em>view Client revisions</em> and <em>delete rights</em> for client entities in question or <em>administer client entities</em>.'
add volunteer entities:
title: 'Create new Volunteer entities'
delete volunteer entities:
title: 'Delete Volunteer entities'
edit volunteer entities:
title: 'Edit Volunteer entities'
view published volunteer entities:
title: 'View published Volunteer entities'
view unpublished volunteer entities:
title: 'View unpublished Volunteer entities'
view all volunteer revisions:
title: 'View all Volunteer revisions'
revert all volunteer revisions:
title: 'Revert all Volunteer revisions'
description: 'Role requires permission <em>view Volunteer revisions</em> and <em>edit rights</em> for volunteer entities in question or <em>administer volunteer entities</em>.'
delete all volunteer revisions:
title: 'Delete all Volunteer revisions'
description: 'Role requires permission to <em>view Volunteer revisions</em> and <em>delete rights</em> for volunteer entities in question or <em>administer volunteer entities</em>.'
#add client entities:
# title: 'Create new Client entities'
#
#delete client entities:
# title: 'Delete Client entities'
#
#edit client entities:
# title: 'Edit Client entities'
#
#view published client entities:
# title: 'View published Client entities'
#
#view unpublished client entities:
# title: 'View unpublished Client entities'
#
#view all client revisions:
# title: 'View all Client revisions'
#
#revert all client revisions:
# title: 'Revert all Client revisions'
# description: 'Role requires permission <em>view Client revisions</em> and <em>edit rights</em> for client entities in question or <em>administer client entities</em>.'
#
#delete all client revisions:
# title: 'Delete all Client revisions'
# description: 'Role requires permission to <em>view Client revisions</em> and <em>delete rights</em> for client entities in question or <em>administer client entities</em>.'
#
#add volunteer entities:
# title: 'Create new Volunteer entities'
#
#delete volunteer entities:
# title: 'Delete Volunteer entities'
#
#edit volunteer entities:
# title: 'Edit Volunteer entities'
#
#view published volunteer entities:
# title: 'View published Volunteer entities'
#
#view unpublished volunteer entities:
# title: 'View unpublished Volunteer entities'
#
#view all volunteer revisions:
# title: 'View all Volunteer revisions'
#
#revert all volunteer revisions:
# title: 'Revert all Volunteer revisions'
# description: 'Role requires permission <em>view Volunteer revisions</em> and <em>edit rights</em> for volunteer entities in question or <em>administer volunteer entities</em>.'
#
#delete all volunteer revisions:
# title: 'Delete all Volunteer revisions'
# description: 'Role requires permission to <em>view Volunteer revisions</em> and <em>delete rights</em> for volunteer entities in question or <em>administer volunteer entities</em>.'
add case entities:
title: 'Create new Case entities'

View File

@ -0,0 +1,93 @@
<?php
namespace Drupal\opencase_entities;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
class OpenCaseEntityPermissions implements ContainerInjectionInterface {
use StringTranslationTrait;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructor for MyModulePermissions.
*
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct(EntityTypeManagerInterface $entity_type_manager) {
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static($container->get('entity_type.manager'));
}
/**
* Get permissions for MyModule.
*
* @return array
* Permissions array.
*/
public function permissions() {
$permissions = [];
foreach ($this->entityTypeManager->getStorage('oc_actor_type')->loadMultiple() as $id => $type) {
$permissions += [
"add $id entities" => [
'title' => $this->t('Create new %type entities', array('%type' => $type->label())),
]
];
$permissions += [
"edit $id entities" => [
'title' => $this->t('Edit %type entities', array('%type' => $type->label())),
]
];
$permissions += [
"delete $id entities" => [
'title' => $this->t('Delete %type entities', array('%type' => $type->label())),
]
];
$permissions += [
"view published $id entities" => [
'title' => $this->t('View published %type entities', array('%type' => $type->label())),
]
];
$permissions += [
"view all $id revisions" => [
'title' => $this->t('View %type revisions', array('%type' => $type->label())),
]
];
$permissions += [
"revert all $id revisions" => [
'title' => $this->t('Revert %type revisions', array('%type' => $type->label())),
]
];
$permissions += [
"delete all $id revisions" => [
'title' => $this->t('Delete %type revisions', array('%type' => $type->label())),
]
];
}
return $permissions;
}
}