diff --git a/node_authlink.permissions.yml b/node_authlink.permissions.yml index d81234e6cf1c5740bddc424c38f34edcf716f25e..768583829d8f9324def1efbf5ed64877ca285cd1 100644 --- a/node_authlink.permissions.yml +++ b/node_authlink.permissions.yml @@ -1,7 +1,11 @@ - configure node_authlink module: title: 'Configure node_authlink module' description: '' + create and delete node authlinks: title: 'Create and delete node authlinks' - description: '' + description: 'Granting this permission will allow to the selected roles to create and delete node authlinks for every content type.' + restrict access: TRUE + +permission_callbacks: + - \Drupal\node_authlink\NodeAuthlinkPermissions::permissions diff --git a/node_authlink.routing.yml b/node_authlink.routing.yml index 67a991f1dfbe114e3c6a8690c363d77367b8d515..cff4ce2486a9740a71bfac5abe3cafd10dade0f1 100644 --- a/node_authlink.routing.yml +++ b/node_authlink.routing.yml @@ -4,7 +4,6 @@ node_authlink.node_authlink_node_form: _form: '\Drupal\node_authlink\Form\NodeAuthlinkNodeForm' _title: 'Authlink' requirements: - _permission: 'create and delete node authlinks' _custom_access: '\Drupal\node_authlink\Form\NodeAuthlinkNodeForm::access' options: _admin_route: TRUE diff --git a/src/Form/NodeAuthlinkNodeForm.php b/src/Form/NodeAuthlinkNodeForm.php index a4d4a4f0f97983464f7307ff7803e704ac728de3..6fb841cee1297431af749c467f8612ead535ba7b 100644 --- a/src/Form/NodeAuthlinkNodeForm.php +++ b/src/Form/NodeAuthlinkNodeForm.php @@ -230,10 +230,13 @@ class NodeAuthlinkNodeForm extends FormBase { if (is_numeric($node)) { $node = Node::load($node); $enable = $this->config('node_authlink.settings')->get('enable'); - if (isset($enable[$node->bundle()]) && $enable[$node->bundle()]) { + if (isset($enable[$node->bundle()]) && $enable[$node->bundle()] + && ($account->hasPermission('create and delete node authlinks') + || $account->hasPermission(sprintf('create and delete node %s authlinks', $node->bundle())))) { return AccessResult::allowed(); } } return AccessResult::forbidden(); } + } diff --git a/src/NodeAuthlinkPermissions.php b/src/NodeAuthlinkPermissions.php new file mode 100644 index 0000000000000000000000000000000000000000..505b7773fd66cd19173de4e05b2e09cd2a868044 --- /dev/null +++ b/src/NodeAuthlinkPermissions.php @@ -0,0 +1,56 @@ +<?php + +namespace Drupal\node_authlink; + +use Drupal\Core\DependencyInjection\ContainerInjectionInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\StringTranslation\StringTranslationTrait; +use Symfony\Component\DependencyInjection\ContainerInterface; + +/** + * Node authlink permissions generator. + */ +class NodeAuthlinkPermissions implements ContainerInjectionInterface +{ + + use StringTranslationTrait; + + /** + * Used to get all the node types. + * + * @var \Drupal\Core\Entity\EntityTypeManagerInterface + */ + protected $entityTypeManager; + + /** + * NodeAuthlinkPermissions constructor. + * + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager + * Entity type manager. + */ + public function __construct(EntityTypeManagerInterface $entityTypeManager) { + $this->entityTypeManager = $entityTypeManager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static($container->get('entity_type.manager')); + } + + /** + * Permissions callback to granularize node authlink. + */ + public function permissions() { + $permissions = []; + foreach ($this->entityTypeManager->getStorage('node_type')->loadMultiple() as $nodeType) { + $permission_name = sprintf('create and delete node %s authlinks', $nodeType->id()); + $permissions[$permission_name] = [ + 'title' => $this->t('Create and delete node "@node_type" authlinks', ['@node_type' => $nodeType->label()]) + ]; + } + return $permissions; + } + +}