Commit 56091999 authored by bucefal91's avatar bucefal91

Issue #2996549 Migrating existing access rules onto the hook implementation.

parent 6676f7a1
......@@ -44,27 +44,20 @@ class WebformEntitySettingsAccessForm extends WebformEntitySettingsBaseForm {
/** @var \Drupal\webform\WebformInterface $webform */
$webform = $this->entity;
$access = $webform->getAccessRules() + $this->accessRulesManager->getDefaultAccessRules();
$form['access']['#tree'] = TRUE;
foreach ($this->accessRulesManager->getAccessRulesInfo() as $access_rule => $info) {
$access = $webform->getAccessRules() + $this->accessRulesManager->getDefaultAccessRules();
$access_rules = $this->accessRulesManager->getAccessRulesInfo();
foreach ($access_rules as $access_rule => $info) {
$form['access'][$access_rule] = [
'#type' => ($access_rule === 'create') ? 'fieldset' : 'details',
'#title' => $info['title'],
'#open' => ($access[$access_rule]['roles'] || $access[$access_rule]['users']) ? TRUE : FALSE,
'#description' => $info['description'],
// Never convert description to help.
// @see _webform_preprocess_description_help()
'#help' => FALSE,
];
if ($info['description']) {
// If it's a renderable array, place as it as a child. Otherwise as
// #description property of the fieldset.
if (is_array($info['description'])) {
$form['access'][$access_rule]['description'] = $info['description'];
}
else {
$form['access'][$access_rule]['#description'] = $info['description'];
}
}
$form['access'][$access_rule]['roles'] = [
'#type' => 'webform_roles',
'#title' => $this->t('Roles'),
......
......@@ -2,6 +2,7 @@
namespace Drupal\webform;
use Drupal\Component\Utility\SortArray;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Extension\ModuleHandlerInterface;
......@@ -97,58 +98,22 @@ class WebformAccessRulesManager implements WebformAccessRulesManagerInterface {
*/
public function getAccessRulesInfo() {
$access_rules = $this->moduleHandler->invokeAll('webform_access_rules');
$access_rules += [
'create' => [
'title' => $this->t('Create submissions'),
'roles' => [
'anonymous',
'authenticated',
],
],
'view_any' => [
'title' => $this->t('View any submissions'),
],
'update_any' => [
'title' => $this->t('Update any submissions'),
],
'delete_any' => [
'title' => $this->t('Delete any submissions'),
],
'purge_any' => [
'title' => $this->t('Purge any submissions'),
],
'view_own' => [
'title' => $this->t('View own submissions'),
],
'update_own' => [
'title' => $this->t('Update own submissions'),
],
'delete_own' => [
'title' => $this->t('Delete own submissions'),
],
'administer' => [
'title' => $this->t('Administer webform & submissions'),
'description' => ['message' => [
'#type' => 'webform_message',
'#message_type' => 'warning',
'#message_message' => $this->t('<strong>Warning</strong>: The below settings give users, permissions, and roles full access to this webform and its submissions.'),
]],
],
'test' => [
'title' => $this->t('Test webform'),
],
];
$this->moduleHandler->alter('webform_access_rules', $access_rules);
// Set access rule default values.
foreach ($access_rules as $access_rule => $info) {
$access_rules[$access_rule] += [
'title' => NULL,
'description' => NULL,
'weight' => 0,
'roles' => [],
'users' => [],
'permissions' => [],
'description' => [],
];
}
uasort($access_rules, [SortArray::class, 'sortByWeightElement']);
return $access_rules;
}
......@@ -172,7 +137,7 @@ class WebformAccessRulesManager implements WebformAccessRulesManagerInterface {
}
/****************************************************************************/
// Get access rules methods.
// Check access rules methods.
/****************************************************************************/
/**
......
......@@ -375,6 +375,7 @@ function hook_webform_help_info_alter(array &$help) {
* - title: (string) Human friendly title of the rule.
* - description: (array) Renderable array that explains what this access rule
* stands for. Defaults to an empty array.
* - weight: (int) Sorting order of this access rule. Defaults to 0.
* - roles: (string[]) Array of role IDs that should be granted this access
* rule by default. Defaults to an empty array.
* - permissions: (string[]) Array of permissions that should be granted this
......@@ -396,6 +397,7 @@ function hook_webform_access_rules() {
],
'do_yet_another_operation' => [
'title' => t('Do yet another operation'),
'weight' => -100,
'permissions' => ['permission that enables "yet another" operation'],
],
];
......
......@@ -701,6 +701,53 @@ function webform_contextual_links_view_alter(&$element, $items) {
}
}
/**
* Implements hook_webform_access_rules().
*/
function webform_webform_access_rules() {
return [
'create' => [
'title' => t('Create submissions'),
'roles' => [
'anonymous',
'authenticated',
],
],
'view_any' => [
'title' => t('View any submissions'),
],
'update_any' => [
'title' => t('Update any submissions'),
],
'delete_any' => [
'title' => t('Delete any submissions'),
],
'purge_any' => [
'title' => t('Purge any submissions'),
],
'view_own' => [
'title' => t('View own submissions'),
],
'update_own' => [
'title' => t('Update own submissions'),
],
'delete_own' => [
'title' => t('Delete own submissions'),
],
'administer' => [
'title' => t('Administer webform & submissions'),
'description' => [
'#type' => 'webform_message',
'#message_type' => 'warning',
'#message_message' => t('<strong>Warning</strong>: The below settings give users, permissions, and roles full access to this webform and its submissions.'),
],
],
'test' => [
'title' => t('Test webform'),
],
];
}
/**
* Adds JavaScript to change the state of an element based on another element.
*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment