Commit b0a36e3b authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3274382 by Xiaohua Guan, yas, shota niioka: Fix the permission of K8s...

Issue #3274382 by Xiaohua Guan, yas, shota niioka: Fix the permission of K8s namespace when adding K8s deployment
parent e7ae474c
Loading
Loading
Loading
Loading
+4 −2
Original line number Diff line number Diff line
@@ -463,9 +463,11 @@ abstract class K8sContentForm extends CloudContentForm implements K8sContentForm

    $options = [];
    foreach ($namespaces ?: [] as $namespace) {
      if ($this->currentUser->hasPermission('view k8s namespace ' . $namespace->getName())) {
        $name = $namespace->getName();
        $options[$name] = $name;
      }
    }

    natcasesort($options);
    return $options;
+37 −9
Original line number Diff line number Diff line
@@ -6,7 +6,10 @@ use Drupal\Component\Serialization\Yaml;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Drupal\k8s\Service\K8sServiceException;
use Symfony\Component\HttpFoundation\RedirectResponse;

/**
 * Form controller for the entity create form.
@@ -34,6 +37,8 @@ class K8sCreateForm extends K8sContentForm {
      '#weight' => $weight++,
    ];

    if (method_exists($this->entity, 'getNamespace')) {

      $options = [];
      try {
        $options = $this->getNamespaceOptions();
@@ -42,8 +47,31 @@ class K8sCreateForm extends K8sContentForm {
        $this->k8sService->handleError($e, $cloud_context, $this->entity);
      }

    if (!empty($options)
    && method_exists($this->entity, 'getNamespace')) {
      // Redirect to list view if there isn't a namespace which can be used.
      if (empty($options)) {
        $this->messenger->addWarning(
          $this->t('You will not be able to create a K8s @type since you have no accessible K8s namespace.', [
            '@type' => $this->entity->getEntityType()->getSingularLabel(),
          ])
        );

        if ($this->currentUser->hasPermission('add k8s namespace')) {
          $add_k8s_namespace = Link::createFromRoute($this->t('create a namespace'), 'entity.k8s_namespace.add_form', [
            'cloud_context' => $cloud_context,
          ])->toString();

          $this->messenger->addWarning($this->t('Try to @add_k8s_namespace or contact your site administrator.', [
            '@add_k8s_namespace' => $add_k8s_namespace,
          ]));
        }

        $redirect_url = Url::fromRoute("view.k8s_{$this->getShortEntityTypeNameUnderScore($this->entity)}.list", [
          'cloud_context' => $cloud_context,
        ]);
        $redirect = new RedirectResponse($redirect_url->toString());
        $redirect->send();
        return $form;
      }

      $form[$name_underscore]['namespace'] = [
        '#type'          => 'select',