diff --git a/metatag_extended_perms/README.txt b/metatag_extended_perms/README.txt new file mode 100644 index 0000000..b56ecaf --- /dev/null +++ b/metatag_extended_perms/README.txt @@ -0,0 +1,35 @@ +Metatag Extended Permissions +---------------------------- +This add-on for Metatag creates a new permission for each individual meta tag, +allowing for very fine controlled access over meta tag customization. + + +Usage +-------------------------------------------------------------------------------- +* Enable the Metatag Extended Permissions module. +* Assign the appropriate permissions via the admin/people/permisisons page. + + +Known issues +-------------------------------------------------------------------------------- +This module introduces a possibility for dramatically increasing the number of +checkboxes on the permissions page. This can lead to the following problems: +* The permissions admin page or node forms taking a long time to load. +* PHP timeout errors on the permissions admin or node forms pages. +* Out-of-memory errors loading the above. +* The web server not being able to process the permissions form due to hitting + PHP's max_input_vars limit. + +Because of these, it is advised to full test this module on a copy of a site +before enabling it on production, to help ensure these problems are not +encountered. + + +Credits / contact +-------------------------------------------------------------------------------- +Originally written by Michael Petri [1]. + + +References +-------------------------------------------------------------------------------- +1: https://www.drupal.org/u/michaelpetri diff --git a/metatag_extended_perms/metatag_extended_perms.info.yml b/metatag_extended_perms/metatag_extended_perms.info.yml new file mode 100644 index 0000000..01cf8be --- /dev/null +++ b/metatag_extended_perms/metatag_extended_perms.info.yml @@ -0,0 +1,7 @@ +name: Metatag Extended Permissions +type: module +description: "Adds individual permissions for each meta tag, allowing for fine-grained access to the meta tags. Note: this may nead to performance issues on the permissions admin page, please see the included README.txt file for details." +core: 8.x +package: SEO +dependencies: + - metatag diff --git a/metatag_extended_perms/metatag_extended_perms.module b/metatag_extended_perms/metatag_extended_perms.module new file mode 100644 index 0000000..fb0ff57 --- /dev/null +++ b/metatag_extended_perms/metatag_extended_perms.module @@ -0,0 +1,43 @@ +get('plugin.manager.metatag.group'); + + foreach (Element::children($element) as $group_id) { + $group = $group_manager->getDefinition($group_id, FALSE); + if ($group === NULL) { + continue; + } + + // By default restrict access to group and regain access when user has + // access to at least one tag in group; this prevents displaying empty + // groups. + $element[$group_id]['#access'] = FALSE; + + // Check through each meta tag field on the field widget. + foreach (Element::children($element[$group_id]) as $tag_id) { + // Check tag permission. + $element[$group_id][$tag_id]['#access'] = \Drupal::currentUser() + ->hasPermission('access metatag ' . $group_id . '__' . $tag_id); + + // Make the group accessible if user has access to the tag. + if ($element[$group_id][$tag_id]['#access']) { + $element[$group_id]['#access'] = TRUE; + } + } + } + } +} diff --git a/metatag_extended_perms/metatag_extended_perms.permissions.yml b/metatag_extended_perms/metatag_extended_perms.permissions.yml new file mode 100644 index 0000000..1ebde3d --- /dev/null +++ b/metatag_extended_perms/metatag_extended_perms.permissions.yml @@ -0,0 +1,6 @@ +# @file +# Provide individual permissions for each meta tag available on the site. + +# All of the actual permissions are provided by a callback. +permission_callbacks: + - Drupal\metatag_extended_perms\MetatagPermissions::permissions diff --git a/metatag_extended_perms/src/MetatagPermissions.php b/metatag_extended_perms/src/MetatagPermissions.php new file mode 100644 index 0000000..cc93705 --- /dev/null +++ b/metatag_extended_perms/src/MetatagPermissions.php @@ -0,0 +1,83 @@ +tagManager = $tag_manager; + $this->groupManager = $group_manager; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container) { + return new static( + $container->get('plugin.manager.metatag.tag'), + $container->get('plugin.manager.metatag.group') + ); + } + + /** + * Get each permission. + * + * @return array + * Permissions array. + */ + public function permissions() { + $permissions = []; + + // Build permissions for each tag in each group. + foreach ($this->tagManager->getDefinitions() as $key => $metatag) { + $group = $this->groupManager->getDefinition($metatag['group']); + + $permissions += [ + 'access metatag ' . $metatag['group'] . '__' . $key => [ + 'title' => $this->t('Access %tag in %group', [ + '%tag' => $metatag['label'], + '%group' => $group['label'], + ]), + ], + ]; + } + + return $permissions; + } + +} diff --git a/metatag_extended_perms/tests/src/Functional/SiteStillWorks.php b/metatag_extended_perms/tests/src/Functional/SiteStillWorks.php new file mode 100644 index 0000000..aa11850 --- /dev/null +++ b/metatag_extended_perms/tests/src/Functional/SiteStillWorks.php @@ -0,0 +1,54 @@ +getEditable('system.site') + ->set('page.front', '/node') + ->save(TRUE); + } + + /** + * Make sure the site still works. For now just check the front page. + */ + public function testTheSiteStillWorks() { + // Load the front page. + $this->drupalGet(''); + + // Confirm that the site didn't throw a server error or something else. + $this->assertSession()->statusCodeEquals(200); + + // Confirm that the front page contains the standard text. + $this->assertText($this->t('Welcome to Drupal')); + } + +}