Repository: hashicorp/vault
File: ui/app/services/namespace.js (JavaScript)

Code before:

  // the top-level namespace is "admin" for HVD managed clusters accessing the UI
  // (similar to "root" for self-managed clusters)
  // this getter checks if the user is specifically at the administrative namespace level
  get inHvdAdminNamespace() {
    return this.flags.isHvdManaged && this.path === 'admin';
  }

  get currentNamespace() {
    if (this.inRootNamespace) return 'root';

    const parts = this.path?.split('/');
    return parts[parts.length - 1];
  }

  get relativeNamespace() {
    // relative namespace is the current namespace minus the user's root.
    // so if we're in app/staging/group1 but the user's root is app, the
    // relative namespace is staging/group
    return getRelativePath(this.path, this.userRootNamespace);
  }

  setNamespace(path) {
    // If a user explicitly logs in to the 'root' namespace, the path is set to 'root'.
    // The root namespace doesn't have a set path, so when verifying the selected namespace, it returns null.
    // Adding a check here, so if the namespace is 'root', it'll be set it to an empty string to match the root namespace.
    if (!path || sanitizePath(path) === 'root') {
      this.path = '';
      return;
    }
    this.path = path;
  }

  @task({ drop: true })
  *findNamespacesForUser() {
    const waiterToken = waiter.beginAsync();
    // uses the adapter and the raw response here since
    // models get wiped when switching namespaces and we
    // want to keep track of these separately
    const store = this.store;
    const adapter = store.adapterFor('namespace');
    const userRoot = this.auth.authData.userRootNamespace;
    try {
      const ns = yield adapter.findAll(store, 'namespace', null, {
        adapterOptions: {
          forUser: true,
          namespace: userRoot,
        },
      });
      const keys = ns.data.keys || [];
      this.accessibleNamespaces = keys.map((n) => {

Review comment (suggestion):
```suggestion
    // Adding a check here, so if the namespace is 'root', it'll be set to an empty string to match the root namespace.
```

Code after:

  // the top-level namespace is "admin" for HVD managed clusters accessing the UI
  // (similar to "root" for self-managed clusters)
  // this getter checks if the user is specifically at the administrative namespace level
  get inHvdAdminNamespace() {
    return this.flags.isHvdManaged && this.path === 'admin';
  }

  get currentNamespace() {
    if (this.inRootNamespace) return 'root';

    const parts = this.path?.split('/');
    return parts[parts.length - 1];
  }

  get relativeNamespace() {
    // relative namespace is the current namespace minus the user's root.
    // so if we're in app/staging/group1 but the user's root is app, the
    // relative namespace is staging/group
    return getRelativePath(this.path, this.userRootNamespace);
  }

  setNamespace(path) {
    // If a user explicitly logs in to the 'root' namespace, the path is set to 'root'.
    // The root namespace doesn't have a set path, so when verifying the selected namespace, it returns null.
    // Adding a check here, so if the namespace is 'root', it'll be set to an empty string to match the root namespace.
    if (!path || sanitizePath(path) === 'root') {
      this.path = '';
      return;
    }
    this.path = path;
  }

  @task({ drop: true })
  *findNamespacesForUser() {
    const waiterToken = waiter.beginAsync();
    // uses the adapter and the raw response here since
    // models get wiped when switching namespaces and we
    // want to keep track of these separately
    const store = this.store;
    const adapter = store.adapterFor('namespace');
    const userRoot = this.auth.authData.userRootNamespace;
    try {
      const ns = yield adapter.findAll(store, 'namespace', null, {
        adapterOptions: {
          forUser: true,
          namespace: userRoot,
        },
      });
      const keys = ns.data.keys || [];
      this.accessibleNamespaces = keys.map((n) => {