From 0b5541674252d89b3641565e5f48b932ac18b622 Mon Sep 17 00:00:00 2001
From: Guo Xiang Tan <tgx_world@hotmail.com>
Date: Mon, 28 May 2018 11:32:55 +0800
Subject: [PATCH] UX: Don't display `all groups` option if group directory is
 disabled.a

https://meta.discourse.org/t/all-groups-option-leads-to-access-denied-page/88464
---
 .../discourse/templates/groups/index.hbs      |  2 +-
 .../components/group-dropdown.js.es6          | 14 ++--
 config/locales/client.en.yml                  |  1 -
 test/javascripts/acceptance/group-test.js.es6 | 77 ++++++++++---------
 4 files changed, 52 insertions(+), 42 deletions(-)

diff --git a/app/assets/javascripts/discourse/templates/groups/index.hbs b/app/assets/javascripts/discourse/templates/groups/index.hbs
index 4562d82d43d..c9a96ebe046 100644
--- a/app/assets/javascripts/discourse/templates/groups/index.hbs
+++ b/app/assets/javascripts/discourse/templates/groups/index.hbs
@@ -9,7 +9,7 @@
 
     <div class="groups-header-filters">
       {{text-field value=filterInput
-          placeholderKey="groups.index.all_groups"
+          placeholderKey="groups.index.all"
           class="groups-header-filters-name no-blur"}}
 
       {{combo-box value=type
diff --git a/app/assets/javascripts/select-kit/components/group-dropdown.js.es6 b/app/assets/javascripts/select-kit/components/group-dropdown.js.es6
index b2939b02788..7657a28e317 100644
--- a/app/assets/javascripts/select-kit/components/group-dropdown.js.es6
+++ b/app/assets/javascripts/select-kit/components/group-dropdown.js.es6
@@ -29,11 +29,15 @@ export default ComboBoxComponent.extend({
 
   @computed
   collectionHeader() {
-    return `
-      <a href="${Discourse.getURL("/groups")}" class="group-dropdown-filter">
-        ${I18n.t("groups.index.all").toLowerCase()}
-      </a>
-    `.htmlSafe();
+    if (this.siteSettings.enable_group_directory ||
+        (this.currentUser && this.currentUser.get('staff'))) {
+
+      return `
+        <a href="${Discourse.getURL("/groups")}" class="group-dropdown-filter">
+          ${I18n.t("groups.index.all").toLowerCase()}
+        </a>
+      `.htmlSafe();
+    }
   },
 
   actions: {
diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml
index 7e88bdbade5..53520a97d0c 100644
--- a/config/locales/client.en.yml
+++ b/config/locales/client.en.yml
@@ -464,7 +464,6 @@ en:
         all: "All Groups"
         empty: "There are no visible groups."
         filter: "Filter by group type"
-        all_groups: "All Groups"
         owner_groups: "Groups I am an owner of"
         close_groups: "Closed Groups"
         automatic_groups: "Automatic Groups"
diff --git a/test/javascripts/acceptance/group-test.js.es6 b/test/javascripts/acceptance/group-test.js.es6
index aeca037becf..a119b5aa4cb 100644
--- a/test/javascripts/acceptance/group-test.js.es6
+++ b/test/javascripts/acceptance/group-test.js.es6
@@ -1,6 +1,10 @@
 import { acceptance, logIn } from "helpers/qunit-helpers";
 
-acceptance("Group");
+acceptance("Group", {
+  settings: {
+    enable_group_directory: true
+  }
+});
 
 const response = object => {
   return [
@@ -10,50 +14,53 @@ const response = object => {
   ];
 };
 
-QUnit.test("Anonymous Viewing Group", assert => {
-  visit("/groups/discourse");
+QUnit.test("Anonymous Viewing Group", async assert => {
+  await visit("/groups/discourse");
 
-  andThen(() => {
-    assert.equal(
-      count(".nav-pills li a[title='Messages']"),
-      0,
-      'it deos not show group messages navigation link'
-    );
-  });
+  assert.equal(
+    count(".nav-pills li a[title='Messages']"),
+    0,
+    'it deos not show group messages navigation link'
+  );
 
-  click(".nav-pills li a[title='Activity']");
+  await click(".nav-pills li a[title='Activity']");
 
-  andThen(() => {
-    assert.ok(count('.user-stream-item') > 0, "it lists stream items");
-  });
+  assert.ok(count('.user-stream-item') > 0, "it lists stream items");
 
-  click(".activity-nav li a[href='/groups/discourse/activity/topics']");
+  await click(".activity-nav li a[href='/groups/discourse/activity/topics']");
 
-  andThen(() => {
-    assert.ok(find('.topic-list'), "it shows the topic list");
-    assert.equal(count('.topic-list-item'), 2, "it lists stream items");
-  });
+  assert.ok(find('.topic-list'), "it shows the topic list");
+  assert.equal(count('.topic-list-item'), 2, "it lists stream items");
 
-  click(".activity-nav li a[href='/groups/discourse/activity/mentions']");
+  await click(".activity-nav li a[href='/groups/discourse/activity/mentions']");
 
-  andThen(() => {
-    assert.ok(count('.user-stream-item') > 0, "it lists stream items");
-  });
+  assert.ok(count('.user-stream-item') > 0, "it lists stream items");
+  assert.ok(find(".nav-pills li a[title='Edit Group']").length === 0, 'it should not show messages tab if user is not admin');
+  assert.ok(find(".nav-pills li a[title='Logs']").length === 0, 'it should not show Logs tab if user is not admin');
+  assert.ok(count('.user-stream-item') > 0, "it lists stream items");
 
-  andThen(() => {
-    assert.ok(find(".nav-pills li a[title='Edit Group']").length === 0, 'it should not show messages tab if user is not admin');
-    assert.ok(find(".nav-pills li a[title='Logs']").length === 0, 'it should not show Logs tab if user is not admin');
-    assert.ok(count('.user-stream-item') > 0, "it lists stream items");
-  });
+  await expandSelectKit('.group-dropdown');
 
-  selectKit('.group-dropdown').expand();
+  assert.equal(
+    find('.select-kit-row').text().trim(), 'discourse',
+    'it displays the right row'
+  );
 
-  andThen(() => {
-    assert.equal(
-      find('.select-kit-row').text().trim(), 'discourse',
-      'it displays the right row'
-    );
-  });
+  assert.equal(
+    find('.group-dropdown-filter').text().trim(), I18n.t("groups.index.all").toLowerCase(),
+    'it displays the right header'
+  );
+
+  Discourse.SiteSettings.enable_group_directory = false;
+
+  await visit("/groups");
+  await visit("/groups/discourse");
+  await expandSelectKit('.group-dropdown');
+
+  assert.equal(
+    find('.group-dropdown-filter').length, 0,
+    'it should not display the default header'
+  );
 });
 
 QUnit.test("Anonymous Viewing Automatic Group", assert => {