FIX: Hide Unassigned if user doesn't have access (#179)

FIX: Hide Unassigned if user doesn’t have access (#179)

There’s no need to display the “Unassigned” button for regular users when assigns_public is false.

diff --git a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6 b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6
index 206d902..393031f 100644
--- a/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6
+++ b/assets/javascripts/discourse-assign/initializers/extend-for-assigns.js.es6
@@ -102,28 +102,33 @@ function registerTopicFooterButtons(api) {
 }
 
 function initialize(api) {
-  api.addNavigationBarItem({
-    name: "unassigned",
-    customFilter: (category) => {
-      return category && category.enable_unassigned_filter;
-    },
-    customHref: (category) => {
-      if (category) {
-        return getURL(category.url) + "/l/latest?status=open&assigned=nobody";
-      }
-    },
-    forceActive: (category, args, router) => {
-      const queryParams = router.currentRoute.queryParams;
-
-      return (
-        queryParams &&
-        Object.keys(queryParams).length === 2 &&
-        queryParams["assigned"] === "nobody" &&
-        queryParams["status"] === "open"
-      );
-    },
-    before: "top",
-  });
+  const siteSettings = api.container.lookup("site-settings:main");
+  const currentUser = api.getCurrentUser();
+
+  if (siteSettings.assigns_public || currentUser?.can_assign) {
+    api.addNavigationBarItem({
+      name: "unassigned",
+      customFilter: (category) => {
+        return category?.custom_fields?.enable_unassigned_filter === "true";
+      },
+      customHref: (category) => {
+        if (category) {
+          return getURL(category.url) + "/l/latest?status=open&assigned=nobody";
+        }
+      },
+      forceActive: (category, args) => {
+        const queryParams = args.currentRouteQueryParams;
+
+        return (
+          queryParams &&
+          Object.keys(queryParams).length === 2 &&
+          queryParams["assigned"] === "nobody" &&
+          queryParams["status"] === "open"
+        );
+      },
+      before: "top",
+    });
+  }
 
   api.addAdvancedSearchOptions(
     api.getCurrentUser() && api.getCurrentUser().can_assign
@@ -145,7 +150,6 @@ function initialize(api) {
   api.modifyClass("model:topic", {
     @computed("assigned_to_user")
     assignedToUserPath(assignedToUser) {
-      const siteSettings = api.container.lookup("site-settings:main");
       return getURL(
         siteSettings.assigns_user_url_path.replace(
           "{username}",
diff --git a/config/settings.yml b/config/settings.yml
index 68ae55d..9f521ae 100644
--- a/config/settings.yml
+++ b/config/settings.yml
@@ -4,7 +4,9 @@ plugins:
     client: true
   assigns_by_staff_mention: false
   unassign_creates_tracking_post: true
-  assigns_public: false
+  assigns_public:
+    default: false
+    client: true
   assign_self_regex: ""
   assign_other_regex: ""
   unassign_on_close: false
@@ -27,6 +29,6 @@ plugins:
     client: true
     type: group_list
     list_type: compact
-    default: ''
+    default: ""
     allow_any: false
     refresh: true
diff --git a/test/javascripts/acceptance/category-test.js.es6 b/test/javascripts/acceptance/category-test.js.es6
new file mode 100644
index 0000000..3e92cee
--- /dev/null
+++ b/test/javascripts/acceptance/category-test.js.es6
@@ -0,0 +1,114 @@
+import {
+  acceptance,
+  exists,
+  updateCurrentUser,
+} from "discourse/tests/helpers/qunit-helpers";
+import { visit } from "@ember/test-helpers";
+import { test } from "qunit";
+import I18n from "I18n";
+import DiscoveryFixtures from "discourse/tests/fixtures/discovery-fixtures";
+
+function stubCategory(needs, customFields) {
+  needs.site({
+    categories: [
+      {
+        id: 6,
+        name: "test",
+        slug: "test",
+        custom_fields: customFields,
+      },
+    ],
+  });
+
+  needs.pretender((server, helper) => {
+    server.get("/c/test/6/l/latest.json", () => {
+      return helper.response(
+        DiscoveryFixtures["/latest_can_create_topic.json"]
+      );
+    });
+  });
+}
+
+acceptance(
+  "Discourse Assign | Categories for users that can assign",
+  function (needs) {
+    needs.user();
+    needs.settings({
+      assign_enabled: true,
+      assigns_user_url_path: "/",
+      assigns_public: false,
+    });
+    stubCategory(needs, { enable_unassigned_filter: "true" });
+
+    test("can see Unassigned button", async (assert) => {
+      updateCurrentUser({ can_assign: true });
+      await visit("/c/test");
+
+      const title = I18n.t("filters.unassigned.help");
+      assert.ok(exists(`#navigation-bar li[title='${title}']`));
+    });
+  }
+);
+
+acceptance(
+  "Discourse Assign | Categories without enable_unassigned_filter",
+  function (needs) {
+    needs.user();
+    needs.settings({
+      assign_enabled: true,
+      assigns_user_url_path: "/",
+      assigns_public: false,
+    });
+    stubCategory(needs, { enable_unassigned_filter: "false" });
+
+    test("cannot see Unassigned button", async (assert) => {
+      updateCurrentUser({ can_assign: true });
+      await visit("/c/test");
+
+      const title = I18n.t("filters.unassigned.help");
+      assert.ok(!exists(`#navigation-bar li[title='${title}']`));
+    });
+  }
+);
+
+acceptance(
+  "Discourse Assign | Categories when assigns are public",
+  function (needs) {
+    needs.user();
+    needs.settings({
+      assign_enabled: true,
+      assigns_user_url_path: "/",
+      assigns_public: true,
+    });
+    stubCategory(needs, { enable_unassigned_filter: "true" });
+
+    test("can see Unassigned button", async (assert) => {
+      updateCurrentUser({ can_assign: false });
+      await visit("/c/test");
+
+      const title = I18n.t("filters.unassigned.help");
+      assert.ok(exists(`#navigation-bar li[title='${title}']`));
+    });
+  }
+);
+
+acceptance(
+  "Discourse Assign | Categories when assigns are private",
+  function (needs) {
+    needs.user();
+    needs.settings({
+      assign_enabled: true,
+      assigns_user_url_path: "/",
+      assigns_public: false,
+    });
+    stubCategory(needs, { enable_unassigned_filter: "true" });
+
+    test("cannot see Unassigned button", async (assert) => {
+      updateCurrentUser({ can_assign: false });
+      await visit("/c/test");
+
+      const title = I18n.t("filters.unassigned.help");
+      assert.ok(!exists(`#navigation-bar li[title='${title}']`));
+    });
+  }
+);

GitHub sha: ad957259872bdef2cde12bd70cf9b6c2127b9bad

This commit appears in #179 which was merged by CvX.