Skip to content
Snippets Groups Projects
Commit dc536b43 authored by Alvaro Hurtado's avatar Alvaro Hurtado Committed by Kristiaan Van den Eynde
Browse files

Issue #3236626 by alvar0hurtad0, Rastra Bhushan Khadka, kristiaanvandeneynde:...

Issue #3236626 by alvar0hurtad0, Rastra Bhushan Khadka, kristiaanvandeneynde: Group Node Permission Administration of Asian Language
parent bb4bf5f9
Branches
Tags
No related merge requests found
...@@ -115,46 +115,50 @@ class GroupPermissionHandler implements GroupPermissionHandlerInterface { ...@@ -115,46 +115,50 @@ class GroupPermissionHandler implements GroupPermissionHandlerInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPermissions($include_plugins = FALSE) { public function getPermissions($include_plugins = FALSE) {
$all_permissions = $this->buildPermissionsYaml();
// Add the plugin defined permissions to the whole. We query all defined // Add the plugin defined permissions to the whole. We query all defined
// plugins to avoid scenarios where modules want to ship with default // plugins to avoid scenarios where modules want to ship with default
// configuration but can't because their plugins may not be installed along // configuration but can't because their plugins may not be installed along
// with the module itself (i.e.: non-enforced plugins). // with the module itself (i.e.: non-enforced plugins).
if ($include_plugins) { $plugins = $include_plugins ? $this->pluginManager->getDefinitions() : [];
foreach ($this->pluginManager->getDefinitions() as $group_relation_type_id => $group_relation_type) { return $this->getPermissionsIncludingPlugins($plugins);
assert($group_relation_type instanceof GroupRelationTypeInterface);
$provider = $group_relation_type->getProvider();
$section = $group_relation_type->getLabel()->__toString();
$permissions = $this->pluginManager->getPermissionProvider($group_relation_type_id)->buildPermissions();
foreach ($permissions as $permission_name => $permission) {
$permission += ['provider' => $provider, 'section' => $section];
$all_permissions[$permission_name] = $this->completePermission($permission);
}
}
}
return $this->sortPermissions($all_permissions);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getPermissionsByGroupType(GroupTypeInterface $group_type) { public function getPermissionsByGroupType(GroupTypeInterface $group_type) {
$all_permissions = $this->buildPermissionsYaml(); $group_relation_types = [];
// Add the plugin defined permissions to the whole.
foreach ($group_type->getInstalledPlugins() as $plugin) { foreach ($group_type->getInstalledPlugins() as $plugin) {
assert($plugin instanceof GroupRelationInterface); assert($plugin instanceof GroupRelationInterface);
$group_relation_type = $plugin->getRelationType(); $group_relation_types[$plugin->getRelationTypeId()] = $plugin->getRelationType();
$provider = $group_relation_type->getProvider(); }
$section = $group_relation_type->getLabel()->__toString(); return $this->getPermissionsIncludingPlugins($group_relation_types);
}
$permissions = $this->pluginManager->getPermissionProvider($plugin->getRelationTypeId())->buildPermissions(); /**
* Gets all defined group permissions along with those from certain plugins.
*
* @param \Drupal\group\Plugin\Group\Relation\GroupRelationTypeInterface[] $group_relation_types
* The plugin definitions to get permissions from, keyed by plugin ID.
*
* @return array
* The permission list, structured as specified by ::getPermissions().
*/
protected function getPermissionsIncludingPlugins(array $group_relation_types) {
$all_permissions = $this->buildPermissionsYaml();
foreach ($group_relation_types as $group_relation_type_id => $group_relation_type) {
assert($group_relation_type instanceof GroupRelationTypeInterface);
$extras = [
'provider' => $group_relation_type->getProvider(),
'section' => $group_relation_type->getLabel()->getUntranslatedString(),
'section_args' => $group_relation_type->getLabel()->getArguments(),
'section_id' => $group_relation_type_id,
];
$permissions = $this->pluginManager->getPermissionProvider($group_relation_type_id)->buildPermissions();
foreach ($permissions as $permission_name => $permission) { foreach ($permissions as $permission_name => $permission) {
$permission += ['provider' => $provider, 'section' => $section]; $all_permissions[$permission_name] = $this->completePermission($permission + $extras);
$all_permissions[$permission_name] = $this->completePermission($permission);
} }
} }
...@@ -178,11 +182,17 @@ class GroupPermissionHandler implements GroupPermissionHandlerInterface { ...@@ -178,11 +182,17 @@ class GroupPermissionHandler implements GroupPermissionHandlerInterface {
'restrict access' => FALSE, 'restrict access' => FALSE,
'warning' => !empty($permission['restrict access']) ? 'Warning: Give to trusted roles only; this permission has security implications.' : '', 'warning' => !empty($permission['restrict access']) ? 'Warning: Give to trusted roles only; this permission has security implications.' : '',
'warning_args' => [], 'warning_args' => [],
'section' => 'General',
'section_args' => [],
'section_id' => 'general',
'allowed for' => ['anonymous', 'outsider', 'member'], 'allowed for' => ['anonymous', 'outsider', 'member'],
]; ];
// Translate the title and optionally the description and warning. // Translate the title and section.
$permission['title'] = $this->t($permission['title'], $permission['title_args']); $permission['title'] = $this->t($permission['title'], $permission['title_args']);
$permission['section'] = $this->t($permission['section'], $permission['section_args']);
// Optionally translate the description and warning.
if (!empty($permission['description'])) { if (!empty($permission['description'])) {
$permission['description'] = $this->t($permission['description'], $permission['description_args']); $permission['description'] = $this->t($permission['description'], $permission['description_args']);
} }
...@@ -202,39 +212,25 @@ class GroupPermissionHandler implements GroupPermissionHandlerInterface { ...@@ -202,39 +212,25 @@ class GroupPermissionHandler implements GroupPermissionHandlerInterface {
* @see \Drupal\group\Access\PermissionHandlerInterface::getPermissions() * @see \Drupal\group\Access\PermissionHandlerInterface::getPermissions()
*/ */
protected function buildPermissionsYaml() { protected function buildPermissionsYaml() {
$all_permissions = []; $full_permissions = [];
$all_callback_permissions = [];
foreach ($this->getYamlDiscovery()->findAll() as $provider => $permissions) { foreach ($this->getYamlDiscovery()->findAll() as $provider => $permissions) {
$permission_sets = [$permissions];
// The top-level 'permissions_callback' is a list of methods in controller // The top-level 'permissions_callback' is a list of methods in controller
// syntax, see \Drupal\Core\Controller\ControllerResolver. These methods // syntax, see \Drupal\Core\Controller\ControllerResolver. These methods
// should return an array of permissions in the same structure. // should return an array of permissions in the same structure.
if (isset($permissions['permission_callbacks'])) { if (isset($permissions['permission_callbacks'])) {
foreach ($permissions['permission_callbacks'] as $permission_callback) { foreach ($permissions['permission_callbacks'] as $permission_callback) {
$callback = $this->controllerResolver->getControllerFromDefinition($permission_callback); $callback = $this->controllerResolver->getControllerFromDefinition($permission_callback);
if ($callback_permissions = call_user_func($callback)) { $callback_permissions = call_user_func($callback);
// Add any callback permissions to the array of permissions. In case assert(is_array($callback_permissions));
// of any conflict, the YAML ones will take precedence. $permission_sets[] = $callback_permissions;
foreach ($callback_permissions as $name => $callback_permission) {
if (!is_array($callback_permission)) {
$callback_permission = ['title' => $callback_permission];
}
// Set the provider if none was specified.
$callback_permission += ['provider' => $provider];
// Set the section if none was specified.
$callback_permission += ['section' => 'General'];
$all_callback_permissions[$name] = $callback_permission;
}
}
} }
unset($permissions['permission_callbacks']); unset($permissions['permission_callbacks']);
} }
foreach ($permissions as $permission_name => $permission) { foreach (array_merge(...$permission_sets) as $permission_name => $permission) {
if (!is_array($permission)) { if (!is_array($permission)) {
$permission = ['title' => $permission]; $permission = ['title' => $permission];
} }
...@@ -242,19 +238,9 @@ class GroupPermissionHandler implements GroupPermissionHandlerInterface { ...@@ -242,19 +238,9 @@ class GroupPermissionHandler implements GroupPermissionHandlerInterface {
// Set the provider if none was specified. // Set the provider if none was specified.
$permission += ['provider' => $provider]; $permission += ['provider' => $provider];
// Set the section if none was specified. // Set the rest of the defaults.
$permission += ['section' => 'General']; $full_permissions[$permission_name] = $this->completePermission($permission);
$permissions[$permission_name] = $permission;
} }
$all_permissions += $permissions;
}
// Combine all defined permissions and set the rest of the defaults.
$full_permissions = $all_permissions + $all_callback_permissions;
foreach ($full_permissions as $permission_name => $permission) {
$full_permissions[$permission_name] = $this->completePermission($permission);
} }
return $full_permissions; return $full_permissions;
......
...@@ -49,10 +49,17 @@ interface GroupPermissionHandlerInterface { ...@@ -49,10 +49,17 @@ interface GroupPermissionHandlerInterface {
* the module providing the permission. You may set this to another * the module providing the permission. You may set this to another
* module's name to make it appear as if the permission was provided by * module's name to make it appear as if the permission was provided by
* that module. * that module.
* - section: (optional) The section name of the permission. This is used to * - section: (optional) The untranslated section name of the permission.
* maintain a clear overview on the permissions form. Defaults to the * This is used to maintain a clear overview on the permissions form.
* plugin name for plugin provided permissions and to "General" for all * Defaults to the plugin name for plugin provided permissions and to
* other permissions. * "General" for all other permissions.
* - section_args: (optional) The placeholder values for the section name.
* - section_id: (optional) The machine name to identify the section by,
* defaults to the plugin ID for plugin provided permissions and to
* "general" for all other permissions. This is not a great solution and
* should be refactored in 2.0.0.
*
* @todo Refactor before full release.
*/ */
public function getPermissions($include_plugins = FALSE); public function getPermissions($include_plugins = FALSE);
......
...@@ -107,13 +107,13 @@ abstract class GroupPermissionsForm extends FormBase { ...@@ -107,13 +107,13 @@ abstract class GroupPermissionsForm extends FormBase {
// Create a list of group permissions ordered by their provider and section. // Create a list of group permissions ordered by their provider and section.
foreach ($this->groupPermissionHandler->getPermissionsByGroupType($this->getGroupType()) as $permission_name => $permission) { foreach ($this->groupPermissionHandler->getPermissionsByGroupType($this->getGroupType()) as $permission_name => $permission) {
$by_provider_and_section[$permission['provider']][$permission['section']][$permission_name] = $permission; $by_provider_and_section[$permission['provider']][$permission['section_id']][$permission_name] = $permission;
} }
// Always put the 'General' section at the top if provided. // Always put the 'general' section at the top if provided.
foreach ($by_provider_and_section as $provider => $sections) { foreach ($by_provider_and_section as $provider => $sections) {
if (isset($sections['General'])) { if (isset($sections['general'])) {
$by_provider_and_section[$provider] = ['General' => $sections['General']] + $sections; $by_provider_and_section[$provider] = ['general' => $sections['general']] + $sections;
} }
} }
...@@ -201,10 +201,7 @@ abstract class GroupPermissionsForm extends FormBase { ...@@ -201,10 +201,7 @@ abstract class GroupPermissionsForm extends FormBase {
] ]
]; ];
foreach ($sections as $section => $permissions) { foreach ($sections as $section_id => $permissions) {
// Create a clean section ID.
$section_id = $provider . '-' . preg_replace('/[^a-z0-9_]+/', '_', strtolower($section));
// Start each section with a full width row containing the section name. // Start each section with a full width row containing the section name.
$form['permissions'][$section_id] = [ $form['permissions'][$section_id] = [
[ [
...@@ -213,7 +210,7 @@ abstract class GroupPermissionsForm extends FormBase { ...@@ -213,7 +210,7 @@ abstract class GroupPermissionsForm extends FormBase {
'class' => ['section'], 'class' => ['section'],
'id' => 'section-' . $section_id, 'id' => 'section-' . $section_id,
], ],
'#markup' => $section, '#markup' => reset($permissions)['section'],
] ]
]; ];
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment