Commit 9731a112 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 5275106a
Loading
Loading
Loading
Loading
+40 −61
Original line number Diff line number Diff line
@@ -113,43 +113,40 @@ class GroupPermissionHandler implements GroupPermissionHandlerInterface {
   * {@inheritdoc}
   */
  public function getPermissions($include_plugins = FALSE) {
    $all_permissions = $this->buildPermissionsYaml();

    // Add the plugin defined permissions to the whole. We query all defined
    // plugins to avoid scenarios where modules want to ship with default
    // configuration but can't because their plugins may not be installed along
    // with the module itself (i.e.: non-enforced plugins).
    if ($include_plugins) {
      /** @var \Drupal\group\Plugin\GroupContentEnablerInterface $plugin */
      foreach ($this->pluginManager->getAll() as $plugin) {
        $provider = $plugin->getProvider();
        $section = $plugin->getLabel()->__toString();

        foreach ($plugin->getPermissions() as $permission_name => $permission) {
          $permission += ['provider' => $provider, 'section' => $section];
          $all_permissions[$permission_name] = $this->completePermission($permission);
        }
      }
    }

    return $this->sortPermissions($all_permissions);
    $plugins = $include_plugins ? iterator_to_array($this->pluginManager->getAll()) : [];
    return $this->getPermissionsWithPlugins($plugins);
  }

  /**
   * {@inheritdoc}
   */
  public function getPermissionsByGroupType(GroupTypeInterface $group_type) {
    $all_permissions = $this->buildPermissionsYaml();
    return $this->getPermissionsWithPlugins(iterator_to_array($group_type->getInstalledContentPlugins()));
  }

    // Add the plugin defined permissions to the whole.
    foreach ($group_type->getInstalledContentPlugins() as $plugin) {
      $provider = $plugin->getProvider();
      $section = $plugin->getLabel()->__toString();
  /**
   * Gets all defined group permissions along with those from certain plugins.
   *
   * @param \Drupal\group\Plugin\GroupContentEnablerInterface[]
   *   The list of plugins to get permissions from, keyed by plugin ID.
   *
   * @return array
   *   The permission list, structured as specified by ::getPermissions().
   */
  protected function getPermissionsWithPlugins(array $plugins) {
    $all_permissions = $this->buildPermissionsYaml();

    /** @var \Drupal\group\Plugin\GroupContentEnablerInterface $plugin */
    foreach ($plugins as $plugin) {
      $extras = [
        'provider' => $plugin->getProvider(),
        'section' => $plugin->getLabel()->getUntranslatedString(),
        'section_args' => $plugin->getLabel()->getArguments(),
        'section_id' => $plugin->getPluginId(),
      ];

      foreach ($plugin->getPermissions() as $permission_name => $permission) {
        $permission += ['provider' => $provider, 'section' => $section];
        $all_permissions[$permission_name] = $this->completePermission($permission);
        $all_permissions[$permission_name] = $this->completePermission($permission + $extras);
      }
    }

@@ -173,11 +170,17 @@ class GroupPermissionHandler implements GroupPermissionHandlerInterface {
      'restrict access' => FALSE,
      'warning' => !empty($permission['restrict access']) ? 'Warning: Give to trusted roles only; this permission has security implications.' : '',
      'warning_args' => [],
      'section' => 'General',
      'section_args' => [],
      'section_id' => 'general',
      '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['section'] = $this->t($permission['section'], $permission['section_args']);

    // Optionally translate the description and warning.
    if (!empty($permission['description'])) {
      $permission['description'] = $this->t($permission['description'], $permission['description_args']);
    }
@@ -197,39 +200,25 @@ class GroupPermissionHandler implements GroupPermissionHandlerInterface {
   * @see \Drupal\group\Access\PermissionHandlerInterface::getPermissions()
   */
  protected function buildPermissionsYaml() {
    $all_permissions = [];
    $all_callback_permissions = [];
    $full_permissions = [];

    foreach ($this->getYamlDiscovery()->findAll() as $provider => $permissions) {
      $permission_sets = [$permissions];

      // The top-level 'permissions_callback' is a list of methods in controller
      // syntax, see \Drupal\Core\Controller\ControllerResolver. These methods
      // should return an array of permissions in the same structure.
      if (isset($permissions['permission_callbacks'])) {
        foreach ($permissions['permission_callbacks'] as $permission_callback) {
          $callback = $this->controllerResolver->getControllerFromDefinition($permission_callback);
          if ($callback_permissions = call_user_func($callback)) {
            // Add any callback permissions to the array of permissions. In case
            // of any conflict, the YAML ones will take precedence.
            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;
          $callback_permissions = call_user_func($callback);
          assert(is_array($callback_permissions));
          $permission_sets[] = $callback_permissions;
        }
          }
        }

        unset($permissions['permission_callbacks']);
      }

      foreach ($permissions as $permission_name => $permission) {
      foreach (array_merge(...$permission_sets) as $permission_name => $permission) {
        if (!is_array($permission)) {
          $permission = ['title' => $permission];
        }
@@ -237,20 +226,10 @@ class GroupPermissionHandler implements GroupPermissionHandlerInterface {
        // Set the provider if none was specified.
        $permission += ['provider' => $provider];

        // Set the section if none was specified.
        $permission += ['section' => 'General'];

        $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) {
        // Set the rest of the defaults.
        $full_permissions[$permission_name] = $this->completePermission($permission);
      }
    }

    return $full_permissions;
  }
+9 −4
Original line number Diff line number Diff line
@@ -49,10 +49,15 @@ interface GroupPermissionHandlerInterface {
   *     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
   *     that module.
   *   - section: (optional) The section name of the permission. This is used to
   *     maintain a clear overview on the permissions form. Defaults to the
   *     plugin name for plugin provided permissions and to "General" for all
   *     other permissions.
   *   - section: (optional) The untranslated section name of the permission.
   *     This is used to maintain a clear overview on the permissions form.
   *     Defaults to the plugin name for plugin provided permissions and to
   *     "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.
   */
  public function getPermissions($include_plugins = FALSE);

+6 −9
Original line number Diff line number Diff line
@@ -103,13 +103,13 @@ abstract class GroupPermissionsForm extends FormBase {

    // Create a list of group permissions ordered by their provider and section.
    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) {
      if (isset($sections['General'])) {
        $by_provider_and_section[$provider] = ['General' => $sections['General']] + $sections;
      if (isset($sections['general'])) {
        $by_provider_and_section[$provider] = ['general' => $sections['general']] + $sections;
      }
    }

@@ -182,10 +182,7 @@ abstract class GroupPermissionsForm extends FormBase {
        ]
      ];

      foreach ($sections as $section => $permissions) {
        // Create a clean section ID.
        $section_id = $provider . '-' . preg_replace('/[^a-z0-9_]+/', '_', strtolower($section));

      foreach ($sections as $section_id => $permissions) {
        // Start each section with a full width row containing the section name.
        $form['permissions'][$section_id] = [
          [
@@ -194,7 +191,7 @@ abstract class GroupPermissionsForm extends FormBase {
              'class' => ['section'],
              'id' => 'section-' . $section_id,
            ],
            '#markup' => $section,
            '#markup' => reset($permissions)['section'],
          ]
        ];